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"
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 pushd "$DEST" >/dev/null || exit 668
99 local old="`ls | head -n -$N_SNAPSHOT`"
100 if [ ! -z "$old" ]; then
101 echo "$(date): Removing oldest snapshot(s): $old..."
102 rm -rf "$old" || exit 669
105 local newest=`ls | tail -n 1`
106 if [ -z "$newest" ]; then
107 echo "$(date): No previous snapshot found, performing a full backup!"
108 elif [ -d "$DEST/tmp" ]; then
109 echo "$(date): Continuing with preexisting snapshot $DEST/tmp"
111 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
112 # TODO: Creating the hardlinks takes a lot of time.
113 # Perhaps we could save time by recycling the oldest snapshot
114 cp -lR "$DEST/$newest" "$DEST/tmp" || exit 670
122 # Avoid clobbering the latest snapshot if the remote host does
123 # not allow us to connect
124 # --contimeout: sometimes hangs on connection...
125 rsync $RSYNC_PWD $RSYNC_OPTIONS --dry-run --no-recursive "$SRC" >/dev/null
127 if [ $RESULT -ne 0 ]; then
128 echo "$(date): rsync test failed: $RESULT. Aborting."
133 ######################
135 ######################
137 # make sure to be root
138 if (( `id -u` != 0 )); then { echo "Sorry, must be root. Exiting..."; exit; } fi
140 echo "$(date): START backup: $SRC -> $DEST"
144 echo "$(date): END backup: $SRC -> $DEST"