rolling directory backup using cron and tar

First lets create a gzip compressed tar file of the directory to be backed up:

tar --create --gzip --absolute-names --preserve-permissions --file=/backups/etc-`date +"%b_%d_%Y"`.tar.gz /etc

this will create a file called etc-Apr_15_2007.tar.gz in your /backups folder. To create a 7 day rolling backup you can use the following:

tar --create --gzip --absolute-names --preserve-permissions --file=/backups/etc-`date +"%A"`.tar.gz /etc

this will create a backup file called etc-Sunday.tar.gz.
To schedule the backup with cron add the following to your crontab file (crontab -e):

0 1 * * * tar --create --gzip --absolute-names --preserve-permissions --file=/backups/etc-`date +"\%A"`.tar.gz /etc

This will run the backup at 1 am every day. Note the use of \% to escape the % symbol in the crontab file.

 

Filed under  //

Comments [0]