Backup Script Using tar

July 18, 2004

A simple backup script that performs monthly, weekly, and daily incremental backups. This is a handy little script that backs up to a single tgz file no matter how many different filesystems you give it to backup.

#!/bin/sh
#
# Backup Script by digitalpeer
# Original:  www.linuxgazette.com/issue47/misc/pollman/run-backup
#
# each_dir_under_here Space delimited list of directories to backup.  If 
#     multiple directories are listed use quotes around the argument.
# dump_to The directory to save files to.
# [key] An optional parameter (hostname is used if not specified) that is 
#     used to name the backup files.  Particularly necessary if you have 
#     multiple computers backing up to the same dump_to path.
 
if [ $# -lt 3 ]
then
   echo "usage: backup.sh each_dir_under_here dump_to [key]"
   exit;
fi
 
COMPUTER=`hostname`
 
if [ $# -gt 3 ]
then
   COMPUTER=$3
fi
 
DIRECTORIES=$1
BACKUPDIR=$2
TAR=/bin/tar		    # name and locaction of tar
 
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.
 
 
if [ $DOM = "01" ]; then  # monthly full backup
	NEWER=""
	$TAR $NEWER -z -c -f $BACKUPDIR/$COMPUTER-$DM.tgz $DIRECTORIES
fi
 
if [ $DOW = "Sun" ]; then # weekly full backup
        NEWER=""
	NOW=`date +%d-%b`
	echo $NOW > $BACKUPDIR/$COMPUTER-full-date #update full backup date
	$TAR $NEWER -z -c -f $BACKUPDIR/$COMPUTER-$DOW.tgz $DIRECTORIES --ignore-failed-read
 
else    # make incremental backup - overwrite last weeks
	NEWER="--newer `cat $BACKUPDIR/$COMPUTER-full-date`" #get date of last full backup
	$TAR $NEWER -z -c -f $BACKUPDIR/$COMPUTER-$DOW.tgz $DIRECTORIES --ignore-failed-read
fi

Related Posts

1 Comment

Comment August 10, 2011 by krushna
Thanks for sharing ,Really helpful