#!/usr/local/bin/bash # incremental backup script, written by Travis Morgan Aug 17, 2005 # Note that this was written for FreeBSD 5.4 and so may not work with # other unix versions because of the differences in how they do time formats # released under the GPL version 2 src="/path/to/source" dest="/path/to/destination" curtime=`date "+%s"` name="incremental" # check the source dir exists if [ ! -d "$src" ] ; then echo "Source directory $src does not exist. Exiting.." exit fi # check the dest dir exists if [ ! -d "$dest" ] ; then echo "Creating destination directory $dest.." mkdir -p "$dest" || { echo "Could not create destination directory $dest. Exiting.." exit } fi # if the day is sunday delete the timestamp.done to force a full backup if [ "`date "+%a"`" = "Sun" ] ; then rm -f "$dest/timestamp.done" fi # find out when we last made a backup if [ -f "$dest/timestamp.done" ] ; then lasttime=`stat -f "%m"` else lasttime=0; name="full" touch -m -t `date -j -f "%s" "$lasttime" "+%Y%m%d%H%M.%S"` "$dest/timestamp.done" fi # create a new timestmap file for use with find rm -f "$dest/timestamp.new" touch -m -t `date -j -f "%s" "$curtime" "+%Y%m%d%H%M.%S"` "$dest/timestamp.new" echo -e "$dest/timestamp.done\n$dest/timestamp.new" > "$dest/filelist.txt" # find all files that have modification times between last backup and this one cd "$src" || { echo "Cannot change directory to $src.. Exiting." exit } find "." -newer "$dest/timestamp.done" -and -not -newer "$dest/timestamp.new" >> "$dest/filelist.txt" 2>/dev/null tar -zcnvf "$dest/$name-`date -j -f "%s" "$curtime" "+%Y%m%d-%H:%m:%S"`.tgz" -T "$dest/filelist.txt" rm "$dest/filelist.txt" # update the old timestamp to the new time touch -m -t `date -j -f "%s" "$curtime" "+%Y%m%d%H%M.%S"` "$dest/timestamp.done" now=`date "+%s"` echo "The backup took $(($now - $curtime)) seconds to complete."