Cleanup tmp directory if cp -lR fails
[wizbackup.git] / wizbackup
1 #!/bin/bash
2 #
3 # WizBackup 2.0 - Simple rsync backup
4 # Based on incremental-backup 0.1 by Matteo Mattei
5 #
6 # Copyright 2006 Matteo Mattei <matteo.mattei@gmail.com>
7 # Copyright 2007, 2008, 2009, 2010, 2011 Bernie Innocenti <bernie@codewiz.org>
8 #
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.
13 #
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.
18 #
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/>.
21 #
22
23
24 if [ $# -lt 2 ]; then
25         echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
26         exit 1
27 fi
28
29 # Treat undefined variables as errors
30 set -u
31
32 #####################################################################
33 # CONFIGURATION
34 #####################################################################
35
36 # Source path
37 SRC=$1; shift
38
39 # DESTINATION DIRECTORY (must exists)
40 DEST=$1; shift
41
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 $@"
45
46 # Number of saved snapshots
47 SNAPSHOTS=45
48
49 RESULT=500
50 DATE=`date +"%Y%m%d"`
51 DEST="`echo $DEST | sed -e 's/\/$//'`"
52
53
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"
57 else
58         export RSYNC_RSH="ssh -i /etc/wizbackup/ssh_id -c arcfour -x -o VerifyHostKeyDNS=yes -o StrictHostKeyChecking=no"
59 fi
60
61 # Error tolerant grep
62 tgrep()
63 {
64         grep "$@"
65         return 0
66 }
67
68 do_backup()
69 {
70         set -o pipefail
71         echo "$(date): rsync $RSYNC_OPTS $SRC $DEST/tmp/"
72         rsync $RSYNC_OPTS "$SRC" "$DEST/tmp/" 2>&1 | tgrep -v -E 'vanished|some files'
73         RESULT=$?
74         case "$RESULT" in
75         0|24)
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"
80                         rm -rf "$DEST/$DATE"
81                 fi
82                 mv "$DEST/tmp" "$DEST/$DATE"
83                 ;;
84         *)
85                 echo "$(date): rsync failed: $RESULT"
86                 echo "$(date): Leaving incomplete backup in $DEST/tmp"
87                 ;;
88         esac
89         set +o pipefail
90 }
91
92
93 do_init()
94 {
95         # Safety net (4 slashes just in case)
96         case "$DEST" in
97                 /|//|///|////) exit 666 ;;
98         esac
99
100         if [ ! -d "$DEST" ]; then
101                 echo "$(date): Creating missing destination directory $DEST."
102                 mkdir -p "$DEST" || exit 667
103         fi
104
105         cd "$DEST" || exit 668
106 }
107
108 do_prune()
109 {
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
114         fi
115 }
116
117 do_link()
118 {
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!"
124         else
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"
129                 RESULT=$?
130                 if [ $RESULT -ne 0 ]; then
131                         echo "$(date): Failed to setup tmp snapshot: $RESULT. Cleaning up."
132                         rm -rf "$DEST/tmp"
133                         exit $RESULT
134                 fi
135         fi
136 }
137
138 do_test()
139 {
140         # TODO: test for free space and free inodes in the $DEST filesystem
141
142         # Avoid clobbering the latest snapshot if the remote host does
143         # not allow us to connect
144         # --contimeout: sometimes hangs on connection...
145         rsync $RSYNC_OPTS --dry-run --no-recursive "$SRC" >/dev/null
146         RESULT=$?
147         if [ $RESULT -ne 0 ]; then
148                 echo "$(date): rsync test failed: $RESULT.  Aborting."
149                 exit $RESULT
150         fi
151 }
152
153 ######################
154 # MAIN
155 ######################
156
157 # make sure to be root
158 if (( `id -u` != 0 )); then { echo "Sorry, must be root.  Exiting..."; exit; } fi
159
160 echo "$(date): START backup: $SRC -> $DEST"
161 do_init
162 do_prune
163 do_test
164 do_link
165 do_backup
166 echo "$(date): END backup: $SRC -> $DEST"
167
168 exit $RESULT