3 # WizBackup 2.0 - Simple rsync backup with snapshots
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/>.
24 echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
28 # Treat undefined variables as errors
31 #####################################################################
33 #####################################################################
38 # Destination directory (will be created if it doesn't exist)
41 # NOTE: --timeout needs to be large enough: if a large dir tree don't change a lot of time can pass without I/O
42 # NOTE: --inplace will clobber linked files in older snapshots. DON'T USE IT!
43 RSYNC_OPTS="-HAXa --stats --timeout 1800 --numeric-ids --delete --delete-excluded --ignore-errors $@"
45 # Number of saved snapshots
48 # Abort backup if the destination volume has less than these GBs free
53 DEST="`echo $DEST | sed -e 's/\/$//'`"
56 # Use "backup" ssh key with ssh protocol, or password file for rsync protocol
57 if [ "${SRC%:*}" == "rsync" ]; then
58 RSYNC_OPTS="$RSYNC_OPTS --password-file=/etc/wizbackup/rsync_password --contimeout 10"
60 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"
93 # Safety net (4 slashes just in case)
95 /|//|///|////) exit 666 ;;
98 if [ ! -d "$DEST" ]; then
99 echo "$(date): Creating missing destination directory $DEST."
100 mkdir -p "$DEST" || exit 667
103 cd "$DEST" || exit 668
107 local old="`ls | grep -v tmp | head -n -$SNAPSHOTS`"
108 if [ ! -z "$old" ]; then
109 echo "$(date): Removing oldest snapshot(s): $old..."
110 rm -rf "$old" || exit 669
115 local newest=`ls | grep -v tmp | tail -n 1`
116 if [ -d "$DEST/tmp" ]; then
117 echo "$(date): Continuing with pre-existing snapshot $DEST/tmp"
118 elif [ -z "$newest" ]; then
119 echo "$(date): No previous snapshot found, performing a full backup!"
121 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
122 # TODO: Creating the hardlinks takes a lot of time.
123 # Perhaps we could save time by recycling the oldest snapshot
124 cp -la "$DEST/$newest" "$DEST/tmp"
126 if [ $RESULT -ne 0 ]; then
127 echo "$(date): Failed to setup tmp snapshot: $RESULT. Cleaning up."
135 # TODO: test for free space and free inodes in the $DEST filesystem
136 block_size=`stat --file-system --format "%S" "$DEST"`
137 free_blocks=`stat --file-system --format "%f" "$DEST"`
138 free_inodes=`stat --file-system --format "%d" "$DEST"`
139 free_gb=$((block_size * free_blocks / 1024 / 1024 / 1024))
141 if [ "$free_gb" -lt "$MIN_FREE_GB" ]; then
142 echo "$(date): Aborting due to insufficient free space ${free_gb}GB."
146 # Avoid clobbering the latest snapshot if the remote host does
147 # not allow us to connect
148 # --contimeout: sometimes hangs on connection...
149 rsync $RSYNC_OPTS --dry-run --no-recursive "$SRC" >/dev/null
151 if [ $RESULT -ne 0 ]; then
152 echo "$(date): rsync test failed: $RESULT. Aborting."
157 ######################################
159 ######################################
161 # make sure to be root
162 if (( `id -u` != 0 )); then { echo "Sorry, must be root. Exiting..."; exit; } fi
164 echo "$(date): BEGIN backup: $SRC -> $DEST"
165 echo "$(date): $0 $SRC $DEST $@"
171 echo "$(date): END backup: $SRC -> $DEST"