linux cron: want to backup a folder

A good thing to do would be to create a new compressed archive in your home.

Create this script named for exmaple */home/sh/c2duo_mms_backup.sh*:

#!/bin/bash

cd /usr/local/src/djcode/
tar zcf /home/sh/c2duo_mms-`date +%Y%m%d`.tar.gz c2duo_mms

Be sure to add the executable permission to the script:

chmod +x /home/sh/c2duo_mms_backup.sh

Then add the relevant crontab entry with the crontab -e command:

0 13 * * 2 /home/sh/c2duo_mms_backup.sh

The script will create a new compressed archive every Tuesday with the date in the filename, so that you can keep older backups if you want. File name will look like this:

c2duo_mms_20110719.tar.gz 

$ crontab -e
0 13 * * 2 cp -b /usr/local/src/djcode/c2duo_mms /home/sh/

The crontab -e command should pull up the crontab file for editing in your preferred editor (Set by the EDITOR or VISUAL environment variabels). The crontab line says to run the command on the 0th minute, 13th hour, 2nd day of the week, any day of the month any year. The command itself is a simple single file copy, except that I added the -b argument so that cp makes a backup file. This should leave you with TWO backups at all times, the current one and the previous one (with a .bk extension).

Edit: For a folder instead of a file, try rsync:

0 13 * * 2 rsync -av /usr/local/src/djcode/c2duo_mms/ /home/sh/c2duo_mms/

Use command crontab -e and add this line to your crontab:

0 13 * * 2 cp -pra /usr/local/src/djcode/c2duo_mms /home/sh

Tags:

Backup

Cron