Backup script for incremental backups using tar
Do a yearly, quarterly, monthly, weekly, daily incremental backup.
#!/bin/sh BACKUPNAME="home" DIRBACKUPSTORAGE="/backup" DIRTOBACKUP="/home/username" DOW=`date +%a` # doy of the week, Sun DOM=`date +%d` # day of the month, 01 DM=`date +%d%b` # date and month 01Jan YMD=`date +20%y%m%d` # datestamp M=`date +%b` # month Sep # Yearly level 0 backup if [ $DM = "01Jan" -o ! -f $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap ]; then rm $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap tar -c -v -J -g $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap -f $DIRBACKUPSTORAGE/$BACKUPNAME-$YMD.0.tar.xz \ $DIRTOBACKUP cp $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap $DIRBACKUPSTORAGE/$BACKUPNAME.1.snap cp $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap $DIRBACKUPSTORAGE/$BACKUPNAME.2.snap cp $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap $DIRBACKUPSTORAGE/$BACKUPNAME.3.snap cp $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap $DIRBACKUPSTORAGE/$BACKUPNAME.4.snap fi # Quarterly level 1 backup if [ $DM = "01Apr" -o $DM = "01Jul" -o $DM = "01Oct" ]; then cp $DIRBACKUPSTORAGE/$BACKUPNAME.0.snap $DIRBACKUPSTORAGE/$BACKUPNAME.1.snap tar -c -v -J -g $DIRBACKUPSTORAGE/$BACKUPNAME.1.snap -f $DIRBACKUPSTORAGE/$BACKUPNAME-$YMD.1.tar.xz \ $DIRTOBACKUP fi # Monthly level 2 backup if [ $DOM = "15" ]; then cp $DIRBACKUPSTORAGE/$BACKUPNAME.1.snap $DIRBACKUPSTORAGE/$BACKUPNAME.2.snap tar -c -v -J -g $DIRBACKUPSTORAGE/$BACKUPNAME.2.snap -f $DIRBACKUPSTORAGE/$BACKUPNAME-$YMD.2.tar.xz \ $DIRTOBACKUP else # Weekly level 3 backup if [ $DOW = "Sun" ]; then cp $DIRBACKUPSTORAGE/$BACKUPNAME.2.snap $DIRBACKUPSTORAGE/$BACKUPNAME.3.snap tar -c -v -J -g $DIRBACKUPSTORAGE/$BACKUPNAME.3.snap -f $DIRBACKUPSTORAGE/$BACKUPNAME-$YMD.3.tar.xz \ $DIRTOBACKUP else # Daily level 4 backup cp $DIRBACKUPSTORAGE/$BACKUPNAME.3.snap $DIRBACKUPSTORAGE/$BACKUPNAME.4.snap tar -c -v -J -g $DIRBACKUPSTORAGE/$BACKUPNAME.4.snap -f $DIRBACKUPSTORAGE/$BACKUPNAME-$YMD.4.tar.xz \ $DIRTOBACKUP fi fi
Restoring
$ tar --extract \
--listed-incremental=/dev/null \
--file home.0.tar
$ tar --extract \
--listed-incremental=/dev/null \
--file home.1.tar
References