A TAR backup script

Here is a backup script.
I use it for 2 months and it works fine.

Maybe it could help some of you. But you use it at your Own RISK

DESC :
On the 1st of the month a permanet full backup is made
Every Sunday a full backup is made - overwriting last Sundays backup
The rest of the time an incremental backup is made. Each incremental
backup overwrites last weeks incremental backup of the same name.

You have to change these 4 variables :
COMPUTER
DIRECTORIES
BACKUPDIR
TIMEDIR

The SCRIPT

#!/bin/sh

full and incremental backup script

created 02 September 2004

Based on a script by Daniel O’Callaghan

#Change the 5 variables below to fit your computer/backup

COMPUTER=deep # name of this computer
DIRECTORIES="/home /etc /var /usr/local" # directoris to backup
BACKUPDIR=/backups # where to store the backups
TIMEDIR=/backups/last-full # where to store time of full backup
TAR=/bin/tar # name and locaction of tar

#You should not have to change anything below here

PATH=/usr/local/bin:/usr/bin:/bin
DOW=date +%a # Day of the week e.g. Mon
DOM=date +%d # Date of the Month e.g. 27
DM=date +%d%b # Date and Month e.g. 27Sep

On the 1st of the month a permanet full backup is made

Every Sunday a full backup is made - overwriting last Sundays backup

The rest of the time an incremental backup is made. Each incremental

backup overwrites last weeks incremental backup of the same name.

if NEWER = “”, then tar backs up all files in the directories

otherwise it backs up files newer than the NEWER date. NEWER

gets it date from the file written every Sunday.

Monthly full backup

if [ $DOM = “01” ]; then
NEWER=""
$TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DM.tar $DIRECTORIES
fi

Weekly full backup

if [ $DOW = “Sun” ]; then
NEWER=""
NOW=date +%d-%b

    # Update full backup date
    echo $NOW > $TIMEDIR/$COMPUTER-full-date
    $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES

Make incremental backup - overwrite last weeks

else

    # Get date of last full backup
    NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`"
    $TAR $NEWER -cf $BACKUPDIR/$COMPUTER-$DOW.tar $DIRECTORIES

fi

If you are not running this backup script from the beginning of the month 01-month-year, the incremental backups will need the time of the Sunday backup to be able to work properly.

If you start in the middle of the week, you will need to create the time file in the TIMEDIR.

To create the time file in the TIMEDIR directory do :

Touch /backups/last-full/myserver-full-date

Where myserver is the name of your server e.g. server1.
The time file consists of a single line with the present date i.e. 27-Nov.

edit full date and write on a single line : dd-month (eg : 27-Nov)
then do :

date +%d%b < /backups/last-full/myserver-full-date

Where /backups/last-full is the variable TIMEDIR wherein we want to store the time of the full backup,

Make this script executable and change its default permissions to be writable only by the super-user root 755.

chmod 755 /etc/cron.daily/backup.cron

Here is an abbreviated look of the backup directory after one week:

/# ls -l /backups/

total 22217
-rw-r–r-- 1 root root 10731288 Feb 27 11:24 pad-01Nov.tar
-rw-r–r-- 1 root root 6879 Feb 27 11:24 pad-Fri.tar
-rw-r–r-- 1 root root 2831 Feb 27 11:24 pad-Mon.tar
-rw-r–r-- 1 root root 7924 Feb 27 11:25 pad-Sat.tar
-rw-r–r-- 1 root root 11923013 Feb 27 11:24 pad-Sun.tar
-rw-r–r-- 1 root root 5643 Feb 27 11:25 pad-Thu.tar
-rw-r–r-- 1 root root 3152 Feb 27 11:25 pad-Tue.tar
-rw-r–r-- 1 root root 4567 Feb 27 11:25 pad-Wed.tar
drwxr-xr-x 2 root root 1024 Feb 27 11:20 last-full

To restore a specific file you may do :

cd
tar xpf /your/backups/dir/your_tar_backup_file.tar
home/toto/contents/document.doc home/quota.user

(p : preserve owner and access authority)

Pascal