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.
17 echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
21 # Fail on any error; Treat undefined variables as errors
24 #####################################################################
26 #####################################################################
31 # DESTINATION DIRECTORY (must exists)
34 # NOTE: --timeout needs to be large enough: if a large dir tree don't change a lot of time can pass without I/O
35 # NOTE: --inplace will clobber linked files in older snapshots. DON'T USE IT!
36 RSYNC_OPTIONS="-HAXa --stats --timeout 1800 --numeric-ids --delete --delete-excluded --ignore-errors $@"
37 # NUMBER OF SAVED ARCHIVES
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_PWD="--password-file=/etc/wizbackup.pwd --contimeout 10"
49 export RSYNC_RSH="ssh -i /root/.ssh/id_backup -c arcfour -x -o VerifyHostKeyDNS=yes"
63 echo "$(date): Starting rsync: rsync $RSYNC_PWD $RSYNC_OPTIONS $SRC $DEST/$DATE/"
64 rsync $RSYNC_PWD $RSYNC_OPTIONS "$SRC" "$DEST/tmp/" 2>&1 | tgrep -v -E 'vanished|some files'
68 RESULT=0 # Ignore error 24 (Partial transfer due to vanished source files)
69 # Cleanup old incomplete backups
70 if [ -d "$DEST/$DATE" ]; then
71 echo "$(date): Removing existing backup $DEST/$DATE"
74 mv "$DEST/tmp" "$DEST/$DATE"
77 echo "$(date): rsync failed: $RESULT"
78 echo "$(date): Leaving incomplete backup in $DEST/tmp"
87 # Safety net (4 slashes just in case)
89 /|//|///|////) exit 666 ;;
92 if [ ! -d "$DEST" ]; then
93 echo "$(date): Creating missing destination directory $DEST."
94 mkdir -p "$DEST" || exit 667
97 cd "$DEST" || exit 668
102 local old="`ls | head -n -$N_SNAPSHOT`"
103 if [ ! -z "$old" ]; then
104 echo "$(date): Removing oldest snapshot(s): $old..."
105 rm -rf "$old" || exit 669
111 local newest=`ls | tail -n 1`
112 if [ -z "$newest" ]; then
113 echo "$(date): No previous snapshot found, performing a full backup!"
114 elif [ -d "$DEST/tmp" ]; then
115 echo "$(date): Continuing with preexisting snapshot $DEST/tmp"
117 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
118 # TODO: Creating the hardlinks takes a lot of time.
119 # Perhaps we could save time by recycling the oldest snapshot
120 cp -lR "$DEST/$newest" "$DEST/tmp" || exit 670
126 # Avoid clobbering the latest snapshot if the remote host does
127 # not allow us to connect
128 # --contimeout: sometimes hangs on connection...
129 rsync $RSYNC_PWD $RSYNC_OPTIONS --dry-run --no-recursive "$SRC" >/dev/null
131 if [ $RESULT -ne 0 ]; then
132 echo "$(date): rsync test failed: $RESULT. Aborting."
137 ######################
139 ######################
141 # make sure to be root
142 if (( `id -u` != 0 )); then { echo "Sorry, must be root. Exiting..."; exit; } fi
144 echo "$(date): START backup: $SRC -> $DEST"
150 echo "$(date): END backup: $SRC -> $DEST"