Skip to content

Instantly share code, notes, and snippets.

@YourFriendCaspian
Created September 6, 2017 04:48
Show Gist options
  • Save YourFriendCaspian/7b43de8c8eebfea1d083663fa3c83376 to your computer and use it in GitHub Desktop.
Save YourFriendCaspian/7b43de8c8eebfea1d083663fa3c83376 to your computer and use it in GitHub Desktop.
Full and Incremental Backup Script
#!/bin/sh
# full and incremental backup script
# created 07 February 2000
# Based on a script by Daniel O'Callaghan <[email protected]>
# and modified by Gerhard Mourani <[email protected]>
# and modified by Shawn Anderson <[email protected]> on 2016-08-14
#Change the 5 variables below to fit your computer/backup
#Create the backup script <backup.cron> file,
#touch /etc/cron.daily/backup.cron
#and add the following lines to this backup file
COMPUTER=$(hostname) # name of this computer
BACKUPSET=HOMEDIR # name of the backup set
DIRECTORIES="/home" # directories 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 location 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
#Set various things up
# Is PV installed?
type pv >/dev/null 2>&1 || sudo apt-get install pv
# Do the required paths exist
if [ ! -d $BACKUPDIR ]; then
mkdir $BACKUPDIR
fi
if [ ! -d $TIMEDIR ]; then
mkdir $TIMEDIR
fi
# On the 1st of the month a permanent 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 - -C $DIRECTORIES/* | pv -s $(du -sb $DIRECTORIES | awk '{print $1}') | gzip > $BACKUPDIR/$BACKUPSET-$COMPUTER-$DM.tgz
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 - -C $DIRECTORIES/* | pv -s $(du -sb $DIRECTORIES | awk '{print $1}') | gzip > $BACKUPDIR/$BACKUPSET-$COMPUTER-$DOW.tgz
# Make incremental backup - overwrite last weeks
else
# Get date of last full backup
NEWER="--newer `cat $TIMEDIR/$COMPUTER-full-date`"
$TAR $NEWER cf - -C $DIRECTORIES/* | pv -s $(du -sb $DIRECTORIES | awk '{print $1}') | gzip > $BACKUPDIR/$BACKUPSET-$COMPUTER-$DOW.tgz
fi
# Remove backup files older than 90 days (this really shouldn't be necessary unless something
# isn't right with the auto-rotation. I have it in just for good measures
find $BACKUPDIR/$BACKUPSET-$COMPUTER* -mtime +90 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment