Backup using tar

Gnu tar has the nice feature of creating incremental backups. When you use incremental backups, tar creates the normal tar-file and at the same time it will also generate a special file containing a list of files, modification date, etc. If you make now a second incremental backup only files that changed regarding to the list tar made will be saved. To recover a file you need all the incremental backups and the complete first backup, since you don't know where the file will be.

Here is a sample script I use:

# backup some important files
# save them on the second hard-drive
#
# written by Arun Persaud
#    2001-10-10

MAILTO=""
export MAILTO

KEEP_DAYS=10    #days
KEEP_WEEKS=33   #days
KEEP_MONTHS=100 #days

# find and delete files that are older than xyz
find /backup/log/     -type f -mtime +$KEEP_DAYS   -exec rm "{}" \;
find /backup/daily/   -type f -mtime +$KEEP_DAYS   -exec rm "{}" \;
find /backup/weekly/  -type f -mtime +$KEEP_WEEKS  -exec rm "{}" \;
find /backup/monthly/ -type f -mtime +$KEEP_MONTHS -exec rm "{}" \;


# get the Date
DATE=`date +%Y-%m-%d` # complete date
WEEKDAY=`date +%w`    # day of the week, sunday=0
DAY=`date +%d`        # day of month, 01-31
WEEK=`date +%U`       # week 00-53
MONTH=`date +%m`      # mont 01..12

if [ "$DAY" = "01" ] ; then
   DIR=monthly
   WEEK=m`date +%U`    # dump level-0
elif [ "$WEEKDAY" = "0" ] ; then
   DIR=weekly
else
   DIR=daily
fi

# do backup
tar --create \
    --file=/backup/$DIR/backup_$DATE.tgz \
    --listed-incremental=/backup/log/backup_$WEEK.snar \
    --verbose \
    --exclude=dirs-that-should-not-be-included \
    --gzip /dirs/that/should/be/included
chmod 600 /backup/$DIR/backup_$DATE.tgz

The above script can be called directly from crontab.