Initial commit
[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
15 if [ $# -lt 2 ]
16 then
17         echo "Usage: $0 SOURCE DEST [RSYNC_OPTS]"
18         exit 1
19 fi
20
21 # Fail on any error; Treat undefined variables as errors
22 set -e -u
23
24 #####################################################################
25 # CONFIGURATION
26 #####################################################################
27
28 # Source path
29 SRC=$1; shift
30
31 # DESTINATION DIRECTORY (must exists)
32 DEST=$1; shift
33
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
38 N_SNAPSHOT=45
39 RESULT=500
40
41 DATE=`date +"%Y%m%d"`
42 DEST="`echo $DEST | sed -e 's/\/$//'`"
43
44
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"
48 else
49         export RSYNC_RSH="ssh -i /root/.ssh/id_backup -c arcfour -x -o VerifyHostKeyDNS=yes"
50         RSYNC_PWD=""
51 fi
52
53 # Error tolerant grep
54 tgrep()
55 {
56         grep "$@"
57         return 0
58 }
59
60 do_backup()
61 {
62         set -o pipefail
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'
65         RESULT=$?
66         case "$RESULT" in
67         0|24)
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"
72                         rm -rf "$DEST/$DATE"
73                 fi
74                 mv "$DEST/tmp" "$DEST/$DATE"
75                 ;;
76         *)
77                 echo "$(date): rsync failed: $RESULT"
78                 echo "$(date): Leaving incomplete backup in $DEST/tmp"
79                 ;;
80         esac
81         set +o pipefail
82 }
83
84 do_link()
85 {
86         # Safety net (4 slashes just in case)
87         case "$DEST" in
88                 /|//|///|////) exit 666 ;;
89         esac
90
91         if [ ! -d "$DEST" ]; then
92                 echo "$(date): Creating missing destination directory $DEST."
93                 mkdir -p "$DEST" || exit 667
94         fi
95
96         pushd "$DEST" >/dev/null || exit 668
97
98         # Remove old backups
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
103         fi
104
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"
110         else
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
115         fi
116
117         popd >/dev/null
118 }
119
120 do_test()
121 {
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
126         RESULT=$?
127         if [ $RESULT -ne 0 ]; then
128                 echo "$(date): rsync test failed: $RESULT.  Aborting."
129                 exit $RESULT
130         fi
131 }
132
133 ######################
134 # MAIN
135 ######################
136
137 # make sure to be root
138 if (( `id -u` != 0 )); then { echo "Sorry, must be root.  Exiting..."; exit; } fi
139
140 echo "$(date): START backup: $SRC -> $DEST"
141 do_test
142 do_link
143 do_backup
144 echo "$(date): END backup: $SRC -> $DEST"
145
146 exit $RESULT