Log the complete command line
[wizbackup.git] / wizbackup
1 #!/bin/bash
2 #
3 # WizBackup 2.0 - Simple rsync backup with snapshots
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 if [ $# -lt 2 ]; then
24         echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
25         exit 1
26 fi
27
28 # Treat undefined variables as errors
29 set -u
30
31 #####################################################################
32 # CONFIGURATION
33 #####################################################################
34
35 # Source rsync URL
36 SRC=$1; shift
37
38 # Destination directory (will be created if it doesn't exist)
39 DEST=$1; shift
40
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 $@"
44
45 # Number of saved snapshots
46 SNAPSHOTS=45
47
48 # Abort backup if the destination volume has less than these GBs free
49 MIN_FREE_GB=10
50
51 RESULT=500
52 DATE=`date +"%Y%m%d"`
53 DEST="`echo $DEST | sed -e 's/\/$//'`"
54
55
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"
59 else
60         export RSYNC_RSH="ssh -i /etc/wizbackup/ssh_id -c arcfour -x -o VerifyHostKeyDNS=yes -o StrictHostKeyChecking=no"
61 fi
62
63 # Error tolerant grep
64 tgrep()
65 {
66         grep "$@"
67         return 0
68 }
69
70 do_backup()
71 {
72         set -o pipefail
73         echo "$(date): rsync $RSYNC_OPTS $SRC $DEST/tmp/"
74         rsync $RSYNC_OPTS "$SRC" "$DEST/tmp/" 2>&1 | tgrep -v -E 'vanished|some files'
75         RESULT=$?
76         case "$RESULT" in
77         0|24)
78                 RESULT=0 # Ignore error 24 (Partial transfer due to vanished source files)
79                 # Cleanup old incomplete backups
80                 if [ -d "$DEST/$DATE" ]; then
81                         echo "$(date): Removing existing backup $DEST/$DATE"
82                         rm -rf "$DEST/$DATE"
83                 fi
84                 mv "$DEST/tmp" "$DEST/$DATE"
85                 ;;
86         *)
87                 echo "$(date): rsync failed: $RESULT"
88                 echo "$(date): Leaving incomplete backup in $DEST/tmp"
89                 ;;
90         esac
91         set +o pipefail
92 }
93
94
95 do_init()
96 {
97         # Safety net (4 slashes just in case)
98         case "$DEST" in
99                 /|//|///|////) exit 666 ;;
100         esac
101
102         if [ ! -d "$DEST" ]; then
103                 echo "$(date): Creating missing destination directory $DEST."
104                 mkdir -p "$DEST" || exit 667
105         fi
106
107         cd "$DEST" || exit 668
108 }
109
110 do_prune()
111 {
112         local old="`ls | grep -v tmp | head -n -$SNAPSHOTS`"
113         if [ ! -z "$old" ]; then
114                 echo "$(date): Removing oldest snapshot(s): $old..."
115                 rm -rf "$old" || exit 669
116         fi
117 }
118
119 do_link()
120 {
121         local newest=`ls | grep  -v tmp | tail -n 1`
122         if [ -d "$DEST/tmp" ]; then
123                 echo "$(date): Continuing with pre-existing snapshot $DEST/tmp"
124         elif [ -z "$newest" ]; then
125                 echo "$(date): No previous snapshot found, performing a full backup!"
126         else
127                 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
128                 # TODO: Creating the hardlinks takes a lot of time.
129                 # Perhaps we could save time by recycling the oldest snapshot
130                 cp -la "$DEST/$newest" "$DEST/tmp"
131                 RESULT=$?
132                 if [ $RESULT -ne 0 ]; then
133                         echo "$(date): Failed to setup tmp snapshot: $RESULT. Cleaning up."
134                         rm -rf "$DEST/tmp"
135                         exit $RESULT
136                 fi
137         fi
138 }
139
140 do_test()
141 {
142         # TODO: test for free space and free inodes in the $DEST filesystem
143         block_size=`stat --file-system --format "%S" "$DEST"`
144         free_blocks=`stat --file-system --format "%f" "$DEST"`
145         free_inodes=`stat --file-system --format "%d" "$DEST"`
146         free_gb=$((block_size * free_blocks / 1024 / 1024 / 1024))
147
148         if [ "$free_gb" -lt "$MIN_FREE_GB" ]; then
149                 echo "$(date): Aborting due to insufficient free space ${free_gb}GB."
150                 exit 670
151         fi
152
153         # Avoid clobbering the latest snapshot if the remote host does
154         # not allow us to connect
155         # --contimeout: sometimes hangs on connection...
156         rsync $RSYNC_OPTS --dry-run --no-recursive "$SRC" >/dev/null
157         RESULT=$?
158         if [ $RESULT -ne 0 ]; then
159                 echo "$(date): rsync test failed: $RESULT.  Aborting."
160                 exit $RESULT
161         fi
162 }
163
164 ######################################
165 # Main
166 ######################################
167
168 # make sure to be root
169 if (( `id -u` != 0 )); then { echo "Sorry, must be root.  Exiting..."; exit; } fi
170
171 echo "$(date): BEGIN backup: $SRC -> $DEST"
172 echo "$(date): $0 $SRC $DEST $@"
173 do_init
174 do_prune
175 do_test
176 do_link
177 do_backup
178 echo "$(date): END backup: $SRC -> $DEST"
179
180 exit $RESULT