LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating a date-based filename for a tarball? (https://www.linuxquestions.org/questions/linux-newbie-8/creating-a-date-based-filename-for-a-tarball-127581/)

HomeBrewer 12-20-2003 12:07 PM

Creating a date-based filename for a tarball?
 
I am using tar to backup portions of my Linux server to another disk on a WinXP machine via a mounted share. I run the backup as a cron job each night.

The command I'm using now (tar -czvf /mnt/HelixBackups/Helix_backup.tar.gz /var/www /home /var/lib/mysql /etc/my.cnf) works well, backing up the files I want archived, and storing the tar file on the windows machine. It has an obvious drawback, however, in that each time it is run, it destroys the previous night's backup in favor of the current one, due to the filename being specified on the command line.

Is there a way to automatically genterate a unique filename each night, perhaps based on the date? That way I could keep a week or so of backups on the drive...

Thanks for any assistance!

green_dragon37 12-20-2003 12:17 PM

You bet:
Using the "date" command (note, the ( ` ) is a backtick, next to the 1 key):
"tar -czvf /mnt/HelixBackups/Helix_backup.`date +%d%m%y`.tar.gz ...."

Ian

HomeBrewer 12-20-2003 01:16 PM

Thank you for your quick response, green_dragon37!

Now, so that I don't have weeks and weeks of these backup files accumulating on my disk, I'd like to delete those that are over a certain age old. So, if I scheduled another cron job to run after the completion of the backup job, and decided I wanted to keep three days of backup tars but delete anything older than that, would the following command work?

find /mnt/HelixBackups -mtime +3 -exec rm -f Helix_backup.*.tar.gz

I put this command together by piecing together various pieces of data found with Google. It looks (to my inexperienced eye) like it would do the job, what do you think? On some of the reference sites I saw, the line was terminated with a semi-colon (;), is that required here?

green_dragon37 12-20-2003 01:32 PM

I think the command needs to be:

" find /mnt/HelixBackups -name Helix_backup.*.tar.gz -mtime +3 -exec rm -f {} \; "

the {} says to work on the matching filename(s) and the \; tells find that that is the end of the command to execute.

Also, might I suggest using bzip2 compression on those backups, it is a more efficient algorithm. "tar -cvjf filename.tar.bz2 files"

Ian

HomeBrewer 12-20-2003 02:16 PM

I knew I was close on the "find" command string, but I was really unsure of what the line ended with...

Quote:

Originally posted by green_dragon37
Also, might I suggest using bzip2 compression on those backups, it is a more efficient algorithm.
You certainly can -- I'm not proud! If there's a better way to do it, I'm all for it.

Thanks again, green_dragon37, you've been a big help!


All times are GMT -5. The time now is 12:03 AM.