Find the answer to your Linux question:
Results 1 to 4 of 4
Hi. I was wondering how to run a script to backup a folder nightly. My question is, If I were backing up say "website.tar.bz2"; How would I add a line ...
  1. #1
    Just Joined!
    Join Date
    Jun 2004
    Posts
    78

    creating dated filenames in a script

    Hi. I was wondering how to run a script to backup a folder nightly. My question is, If I were backing up say "website.tar.bz2"; How would I add a line before the GTAR command to rename the previous file?


    mv /backup/website.tar.bz2 TO "website01-01-2005.tar.bz2"
    gtar -cvf /www/html/* TO /backup/website.tar.bz2

    I tried mv website.tar.bz2 TO website%DATE.tar.bz2 but it didn't work.

    Any help would be graciously accepted...

  2. #2
    Just Joined!
    Join Date
    Aug 2004
    Posts
    2
    Dat=`date +%m%d%y`
    mv website.tar.bz2 TO website$Dat.tar.bz2


    not sure if that will work but somthing to try I guess

  3. #3
    Just Joined!
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    53
    Code:
    #!/bin/bash
    DAT=`date +%m-%d-%Y`
    
    if [ -f /backup/website.tar.bz2 ]; then
     mv -f /backup/website.tar.bz2 /backup/website.${DAT}.tar.bz2
    fi
    tar cjf /backup/website.tar.bz2 /www/html
    The above date statement returns "08-26-2004" (today) I hope that is what you are after. Just swap %m and %d if you prefer the other way around.
    I also added a check function to see if there actually is a file to rename. Using gtar is not very portable, plain tar with the j option for bz2 will work on all recent linux systems.

    A better way to do this may be to use ${DAT} in the file name specified for tar. You should also need some sort of "clean up" of old files so your disks don't fill up. Something like this to delete files older than 14 days.
    Code:
    find /backup -name website*.tar.bz2 -ctime +14 -exec rm -f {} \;
    Good luck with your scripting

  4. #4
    Just Joined!
    Join Date
    Jun 2004
    Posts
    78
    Thanks, I'm sorry I didnt reply to you two

    I thought I did, but anyway WORKS GREAT!!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...