39534ac7203b67f96070e7b8666f8fa75183fd55
[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, 2012, 2015 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 CONF_FILE="/etc/wizbackup/wizbackup.conf"
42
43 # NOTE: --timeout needs to be large enough: if a large dir tree don't change a lot of time can pass without I/O
44 # NOTE: --inplace will clobber linked files in older snapshots. DON'T USE IT!
45 RSYNC_OPTS="-HAXa --stats --timeout 1800 --numeric-ids --delete --delete-excluded --ignore-errors $@"
46
47 # Number of months to keep
48 MONTHS=3
49
50 # Abort backup if the destination volume has less than these GBs free
51 MIN_FREE_GB=10
52
53 RESULT=500
54 DATE=$(date +"%Y%m%d")
55 if [ $(date +"%d") = 1 ]; then
56         DATE="$DATE-monthly"
57 elif [ $(date +"%w") = 0 ]; then
58         DATE="$DATE-weekly"
59 fi
60 DEST="`echo $DEST | sed -e 's/\/$//'`"
61
62 if [ -f "$CONF_FILE" ]; then
63         source /etc/wizbackup/wizbackup.conf
64 fi
65
66 # Use "backup" ssh key with ssh protocol, or password file for rsync protocol
67 if [ "${SRC%:*}" == "rsync" ]; then
68         RSYNC_OPTS="$RSYNC_OPTS --password-file=/etc/wizbackup/rsync_password --contimeout 10"
69 else
70         export RSYNC_RSH="ssh -i /etc/wizbackup/ssh_id -c arcfour -x -o VerifyHostKeyDNS=yes -o StrictHostKeyChecking=no"
71 fi
72
73 # Error tolerant grep
74 tgrep() {
75         grep "$@"
76         return 0
77 }
78
79 do_backup() {
80         set -o pipefail
81         echo "$(date): rsync $RSYNC_OPTS $SRC $DEST/tmp/"
82         rsync $RSYNC_OPTS "$SRC" "$DEST/tmp/" 2>&1 | tgrep -v -E 'vanished|some files'
83         RESULT=$?
84         case "$RESULT" in
85         0|24)
86                 RESULT=0 # Ignore error 24 (Partial transfer due to vanished source files)
87                 # Cleanup old incomplete backups
88                 if [ -d "$DEST/$DATE" ]; then
89                         echo "$(date): Removing existing backup $DEST/$DATE"
90                         rm -rf "$DEST/$DATE"
91                 fi
92                 mv "$DEST/tmp" "$DEST/$DATE"
93                 ;;
94         *)
95                 echo "$(date): rsync failed: $RESULT"
96                 echo "$(date): Leaving incomplete backup in $DEST/tmp"
97                 ;;
98         esac
99         set +o pipefail
100 }
101
102 do_init() {
103         # Safety net (4 slashes just in case)
104         case "$DEST" in
105                 /|//|///|////) exit 666 ;;
106         esac
107
108         if [ ! -d "$DEST" ]; then
109                 echo "$(date): Creating missing destination directory $DEST."
110                 mkdir -p "$DEST" || exit 667
111         fi
112
113         cd "$DEST" || exit 668
114 }
115
116 do_prune() {
117         local num_snapshots="$1"
118         local suffix="$2"
119         local oldest="$(ls -d [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$suffix | head -n -$num_snapshots)"
120         for old in $oldest; do
121                 echo "$(date): Removing oldest snapshot(s): $old..."
122                 rm -rf "$old" || exit 669
123         done
124 }
125
126 do_link() {
127         local newest=`ls -d [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]* | tail -n 1`
128         if [ -d "$DEST/tmp" ]; then
129                 echo "$(date): Continuing with pre-existing snapshot $DEST/tmp"
130         elif [ -z "$newest" ]; then
131                 echo "$(date): No previous snapshot found, performing a full backup!"
132         else
133                 echo "$(date): Linking snapshot $DEST/$newest to $DEST/tmp"
134                 # TODO: Creating the hardlinks takes a lot of time.
135                 # Perhaps we could save time by recycling the oldest snapshot
136                 cp -la "$DEST/$newest" "$DEST/tmp"
137                 RESULT=$?
138                 if [ $RESULT -ne 0 ]; then
139                         echo "$(date): Failed to setup tmp snapshot: $RESULT. Cleaning up."
140                         rm -rf "$DEST/tmp"
141                         exit $RESULT
142                 fi
143         fi
144 }
145
146 do_test() {
147         # TODO: test for free space and free inodes in the $DEST filesystem
148         block_size=`stat --file-system --format "%S" "$DEST"`
149         free_blocks=`stat --file-system --format "%f" "$DEST"`
150         free_inodes=`stat --file-system --format "%d" "$DEST"`
151         free_gb=$((block_size * free_blocks / 1024 / 1024 / 1024))
152
153         if [ "$free_gb" -lt "$MIN_FREE_GB" ]; then
154                 echo "$(date): Aborting due to insufficient free space ${free_gb}GB."
155                 exit 670
156         fi
157
158         # Avoid clobbering the latest snapshot if the remote host does
159         # not allow us to connect
160         # --contimeout: sometimes hangs on connection...
161         rsync $RSYNC_OPTS --dry-run --no-recursive "$SRC" >/dev/null
162         RESULT=$?
163         if [ $RESULT -ne 0 ]; then
164                 echo "$(date): rsync test failed: $RESULT.  Aborting."
165                 exit $RESULT
166         fi
167 }
168
169 ######################################
170 # Main
171 ######################################
172
173 # make sure to be root
174 if (( `id -u` != 0 )); then { echo "Sorry, must be root.  Exiting..."; exit; } fi
175
176 echo "$(date): BEGIN backup: $0 $@"
177 echo "$(date): $0 $SRC $DEST $@"
178 do_init
179 do_prune 6 ""
180 do_prune 4 "-weekly"
181 do_prune $MONTHS "-monthly"
182 do_test
183 do_link
184 do_backup
185 echo "$(date): END backup: $SRC -> $DEST"
186
187 exit $RESULT