3 # WizBackup 2.0 - Simple rsync backup
4 # Based on incremental-backup 0.1 by Matteo Mattei
6 # Copyright 2006 Matteo Mattei <matteo.mattei@gmail.com>
7 # Copyright 2007, 2008, 2009, 2010, 2011 Bernie Innocenti <bernie@codewiz.org>
9 # This program is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License,
12 # or (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
29 # Fail on any error; Treat undefined variables as errors
32 #####################################################################
34 #####################################################################
39 # DESTINATION DIRECTORY (must exists)
42 # NOTE: --timeout needs to be large enough: if a large dir tree don't change a lot of time can pass without I/O
43 # NOTE: --inplace will clobber linked files in older snapshots. DON'T USE IT!
44 RSYNC_OPTS="-HAXa --stats --timeout 1800 --numeric-ids --delete --delete-excluded --ignore-errors $@"
46 # Number of saved snapshots
51 DEST="`echo $DEST | sed -e 's/\/$//'`"
54 # Use "backup" ssh key with ssh protocol, or password file for rsync protocol
55 if [ "${SRC%:*}" == "rsync" ]; then
56 RSYNC_OPTS="$RSYNC_OPTS --password-file=/etc/wizbackup/rsync_password --contimeout 10"
58 export RSYNC_RSH="ssh -i /etc/wizbackup/ssh_id -c arcfour -x -o VerifyHostKeyDNS=yes -o StrictHostKeyChecking=no"
71 echo "$(date): rsync $RSYNC_OPTS $SRC $DEST/tmp/"
72 rsync $RSYNC_OPTS "$SRC" "$DEST/tmp/" 2>&1 | tgrep -v -E 'vanished|some files'
76 RESULT=0 # Ignore error 24 (Partial transfer due to vanished source files)
77 # Cleanup old incomplete backups
78 if [ -d "$DEST/$DATE" ]; then
79 echo "$(date): Removing existing backup $DEST/$DATE"
82 mv "$DEST/tmp" "$DEST/$DATE"
85 echo "$(date): rsync failed: $RESULT"
86 echo "$(date): Leaving incomplete backup in $DEST/tmp"
95 # Safety net (4 slashes just in case)
97 /|//|///|////) exit 666 ;;
100 if [ ! -d "$DEST" ]; then
101 echo "$(date): Creating missing destination directory $DEST."
102 mkdir -p "$DEST" || exit 667
105 cd "$DEST" || exit 668
110 local old="`ls | grep -v tmp | head -n -$SNAPSHOTS`"
111 if [ ! -z "$old" ]; then
112 echo "$(date): Removing oldest snapshot(s): $old..."
113 rm -rf "$old" || exit 669
119 local newest=`ls | grep -v tmp | tail -n 1`
120 if [ -d "$DEST/tmp" ]; then
121 echo "$(date): Continuing with pre-existing snapshot $DEST/tmp"
122 elif [ -z "$newest" ]; then
123 echo "$(date): No previous snapshot found, performing a full backup!"
125 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
126 # TODO: Creating the hardlinks takes a lot of time.
127 # Perhaps we could save time by recycling the oldest snapshot
128 cp -lR "$DEST/$newest" "$DEST/tmp" || exit 670
134 # Avoid clobbering the latest snapshot if the remote host does
135 # not allow us to connect
136 # --contimeout: sometimes hangs on connection...
137 rsync $RSYNC_OPTS --dry-run --no-recursive "$SRC" >/dev/null
139 if [ $RESULT -ne 0 ]; then
140 echo "$(date): rsync test failed: $RESULT. Aborting."
145 ######################
147 ######################
149 # make sure to be root
150 if (( `id -u` != 0 )); then { echo "Sorry, must be root. Exiting..."; exit; } fi
152 echo "$(date): START backup: $SRC -> $DEST"
158 echo "$(date): END backup: $SRC -> $DEST"