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.
16 echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
20 # Fail on any error; Treat undefined variables as errors
23 #####################################################################
25 #####################################################################
30 # DESTINATION DIRECTORY (must exists)
33 # NOTE: --timeout needs to be large enough: if a large dir tree don't change a lot of time can pass without I/O
34 # NOTE: --inplace will clobber linked files in older snapshots. DON'T USE IT!
35 RSYNC_OPTS="-HAXa --stats --timeout 1800 --numeric-ids --delete --delete-excluded --ignore-errors $@"
37 # Number of saved snapshots
42 DEST="`echo $DEST | sed -e 's/\/$//'`"
45 # Use "backup" ssh key with ssh protocol, or password file for rsync protocol
46 if [ "${SRC%:*}" == "rsync" ]; then
47 RSYNC_OPTS="$RSYNC_OPTS --password-file=/etc/wizbackup/rsync_password --contimeout 10"
49 export RSYNC_RSH="ssh -i /etc/wizbackup/ssh_id -c arcfour -x -o VerifyHostKeyDNS=yes -o StrictHostKeyChecking=no"
62 echo "$(date): rsync $RSYNC_OPTS $SRC $DEST/tmp/"
63 rsync $RSYNC_OPTS "$SRC" "$DEST/tmp/" 2>&1 | tgrep -v -E 'vanished|some files'
67 RESULT=0 # Ignore error 24 (Partial transfer due to vanished source files)
68 # Cleanup old incomplete backups
69 if [ -d "$DEST/$DATE" ]; then
70 echo "$(date): Removing existing backup $DEST/$DATE"
73 mv "$DEST/tmp" "$DEST/$DATE"
76 echo "$(date): rsync failed: $RESULT"
77 echo "$(date): Leaving incomplete backup in $DEST/tmp"
86 # Safety net (4 slashes just in case)
88 /|//|///|////) exit 666 ;;
91 if [ ! -d "$DEST" ]; then
92 echo "$(date): Creating missing destination directory $DEST."
93 mkdir -p "$DEST" || exit 667
96 cd "$DEST" || exit 668
101 local old="`ls | grep -v tmp | head -n -$SNAPSHOTS`"
102 if [ ! -z "$old" ]; then
103 echo "$(date): Removing oldest snapshot(s): $old..."
104 rm -rf "$old" || exit 669
110 local newest=`ls | grep -v tmp | tail -n 1`
111 if [ -d "$DEST/tmp" ]; then
112 echo "$(date): Continuing with pre-existing snapshot $DEST/tmp"
113 elif [ -z "$newest" ]; then
114 echo "$(date): No previous snapshot found, performing a full backup!"
116 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
117 # TODO: Creating the hardlinks takes a lot of time.
118 # Perhaps we could save time by recycling the oldest snapshot
119 cp -lR "$DEST/$newest" "$DEST/tmp" || exit 670
125 # Avoid clobbering the latest snapshot if the remote host does
126 # not allow us to connect
127 # --contimeout: sometimes hangs on connection...
128 rsync $RSYNC_OPTS --dry-run --no-recursive "$SRC" >/dev/null
130 if [ $RESULT -ne 0 ]; then
131 echo "$(date): rsync test failed: $RESULT. Aborting."
136 ######################
138 ######################
140 # make sure to be root
141 if (( `id -u` != 0 )); then { echo "Sorry, must be root. Exiting..."; exit; } fi
143 echo "$(date): START backup: $SRC -> $DEST"
149 echo "$(date): END backup: $SRC -> $DEST"