Debian Codegraph @

nightly-backup.sh

raw symbols SCIP
1 #!/bin/bash
2 # nightly-backup.sh
3 # http://www.richardneill.org/source.php#nightly-backup-rsync
4 # Copyright (c) 2005 Richard Neill <[email protected]>.
5 # This is Free Software licensed under the GNU GPL.
6 # ==> Included in ABS Guide with script author's kind permission.
7 # ==> (Thanks!)
8
9 # This does a backup from the host computer to a locally connected
10 #+ firewire HDD using rsync and ssh.
11 # (Script should work with USB-connected device (see lines 40-43).
12 # It then rotates the backups.
13 # Run it via cron every night at 5am.
14 # This only backs up the home directory.
15 # If ownerships (other than the user's) should be preserved,
16 #+ then run the rsync process as root (and re-instate the -o).
17 # We save every day for 7 days, then every week for 4 weeks,
18 #+ then every month for 3 months.
19
20 # See: http://www.mikerubel.org/computers/rsync_snapshots/
21 #+ for more explanation of the theory.
22 # Save as: $HOME/bin/nightly-backup_firewire-hdd.sh
23
24 # Known bugs:
25 # ----------
26 # i) Ideally, we want to exclude ~/.tmp and the browser caches.
27
28 # ii) If the user is sitting at the computer at 5am,
29 #+ and files are modified while the rsync is occurring,
30 #+ then the BACKUP_JUSTINCASE branch gets triggered.
31 # To some extent, this is a
32 #+ feature, but it also causes a "disk-space leak".
33
34
35
36
37
38 ##### BEGIN CONFIGURATION SECTION ############################################
39 LOCAL_USER=rjn # User whose home directory should be backed up.
40 MOUNT_POINT=/backup # Mountpoint of backup drive.
41 # NO trailing slash!
42 # This must be unique (eg using a udev symlink)
43 # MOUNT_POINT=/media/disk # For USB-connected device.
44 SOURCE_DIR=/home/$LOCAL_USER # NO trailing slash - it DOES matter to rsync.
45 BACKUP_DEST_DIR=$MOUNT_POINT/backup/`hostname -s`.${LOCAL_USER}.nightly_backup
46 DRY_RUN=false #If true, invoke rsync with -n, to do a dry run.
47 # Comment out or set to false for normal use.
48 VERBOSE=false # If true, make rsync verbose.
49 # Comment out or set to false otherwise.
50 COMPRESS=false # If true, compress.
51 # Good for internet, bad on LAN.
52 # Comment out or set to false otherwise.
53
54 ### Exit Codes ###
55 E_VARS_NOT_SET=64
56 E_COMMANDLINE=65
57 E_MOUNT_FAIL=70
58 E_NOSOURCEDIR=71
59 E_UNMOUNTED=72
60 E_BACKUP=73
61 ##### END CONFIGURATION SECTION ##############################################
62
63
64 # Check that all the important variables have been set:
65 if [ -z "$LOCAL_USER" ] ||
66 [ -z "$SOURCE_DIR" ] ||
67 [ -z "$MOUNT_POINT" ] ||
68 [ -z "$BACKUP_DEST_DIR" ]
69 then
70 echo 'One of the variables is not set! Edit the file: $0. BACKUP FAILED.'
71 exit $E_VARS_NOT_SET
72 fi
73
74 if [ "$#" != 0 ] # If command-line param(s) . . .
75 then # Here document(ation).
76 cat <<-ENDOFTEXT
77 Automatic Nightly backup run from cron.
78 Read the source for more details: $0
79 The backup directory is $BACKUP_DEST_DIR .
80 It will be created if necessary; initialisation is no longer required.
81
82 WARNING: Contents of $BACKUP_DEST_DIR are rotated.
83 Directories named 'backup.\$i' will eventually be DELETED.
84 We keep backups from every day for 7 days (1-8),
85 then every week for 4 weeks (9-12),
86 then every month for 3 months (13-15).
87
88 You may wish to add this to your crontab using 'crontab -e'
89 # Back up files: $SOURCE_DIR to $BACKUP_DEST_DIR
90 #+ every night at 3:15 am
91 15 03 * * * /home/$LOCAL_USER/bin/nightly-backup_firewire-hdd.sh
92
93 Don't forget to verify the backups are working,
94 especially if you don't read cron's mail!"
95 ENDOFTEXT
96 exit $E_COMMANDLINE
97 fi
98
99
100 # Parse the options.
101 # ==================
102
103 if [ "$DRY_RUN" == "true" ]; then
104 DRY_RUN="-n"
105 echo "WARNING:"
106 echo "THIS IS A 'DRY RUN'!"
107 echo "No data will actually be transferred!"
108 else
109 DRY_RUN=""
110 fi
111
112 if [ "$VERBOSE" == "true" ]; then
113 VERBOSE="-v"
114 else
115 VERBOSE=""
116 fi
117
118 if [ "$COMPRESS" == "true" ]; then
119 COMPRESS="-z"
120 else
121 COMPRESS=""
122 fi
123
124
125 # Every week (actually of 8 days) and every month,
126 #+ extra backups are preserved.
127 DAY_OF_MONTH=`date +%d` # Day of month (01..31).
128 if [ $DAY_OF_MONTH = 01 ]; then # First of month.
129 MONTHSTART=true
130 elif [ $DAY_OF_MONTH = 08 \
131 -o $DAY_OF_MONTH = 16 \
132 -o $DAY_OF_MONTH = 24 ]; then
133 # Day 8,16,24 (use 8, not 7 to better handle 31-day months)
134 WEEKSTART=true
135 fi
136
137
138
139 # Check that the HDD is mounted.
140 # At least, check that *something* is mounted here!
141 # We can use something unique to the device, rather than just guessing
142 #+ the scsi-id by having an appropriate udev rule in
143 #+ /etc/udev/rules.d/10-rules.local
144 #+ and by putting a relevant entry in /etc/fstab.
145 # Eg: this udev rule:
146 # BUS="scsi", KERNEL="sd*", SYSFS{vendor}="WDC WD16",
147 # SYSFS{model}="00JB-00GVA0 ", NAME="%k", SYMLINK="lacie_1394d%n"
148
149 if mount | grep $MOUNT_POINT >/dev/null; then
150 echo "Mount point $MOUNT_POINT is indeed mounted. OK"
151 else
152 echo -n "Attempting to mount $MOUNT_POINT..."
153 # If it isn't mounted, try to mount it.
154 sudo mount $MOUNT_POINT 2>/dev/null
155
156 if mount | grep $MOUNT_POINT >/dev/null; then
157 UNMOUNT_LATER=TRUE
158 echo "OK"
159 # Note: Ensure that this is also unmounted
160 #+ if we exit prematurely with failure.
161 else
162 echo "FAILED"
163 echo -e "Nothing is mounted at $MOUNT_POINT. BACKUP FAILED!"
164 exit $E_MOUNT_FAIL
165 fi
166 fi
167
168
169 # Check that source dir exists and is readable.
170 if [ ! -r $SOURCE_DIR ] ; then
171 echo "$SOURCE_DIR does not exist, or cannot be read. BACKUP FAILED."
172 exit $E_NOSOURCEDIR
173 fi
174
175
176 # Check that the backup directory structure is as it should be.
177 # If not, create it.
178 # Create the subdirectories.
179 # Note that backup.0 will be created as needed by rsync.
180
181 for ((i=1;i<=15;i++)); do
182 if [ ! -d $BACKUP_DEST_DIR/backup.$i ]; then
183 if /bin/mkdir -p $BACKUP_DEST_DIR/backup.$i ; then
184 # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ No [ ] test brackets. Why?
185 echo "Warning: directory $BACKUP_DEST_DIR/backup.$i is missing,"
186 echo "or was not initialised. (Re-)creating it."
187 else
188 echo "ERROR: directory $BACKUP_DEST_DIR/backup.$i"
189 echo "is missing and could not be created."
190 if [ "$UNMOUNT_LATER" == "TRUE" ]; then
191 # Before we exit, unmount the mount point if necessary.
192 cd
193 sudo umount $MOUNT_POINT &&
194 echo "Unmounted $MOUNT_POINT again. Giving up."
195 fi
196 exit $E_UNMOUNTED
197 fi
198 fi
199 done
200
201
202 # Set the permission to 700 for security
203 #+ on an otherwise permissive multi-user system.
204 if ! /bin/chmod 700 $BACKUP_DEST_DIR ; then
205 echo "ERROR: Could not set permissions on $BACKUP_DEST_DIR to 700."
206
207 if [ "$UNMOUNT_LATER" == "TRUE" ]; then
208 # Before we exit, unmount the mount point if necessary.
209 cd ; sudo umount $MOUNT_POINT \
210 && echo "Unmounted $MOUNT_POINT again. Giving up."
211 fi
212
213 exit $E_UNMOUNTED
214 fi
215
216 # Create the symlink: current -> backup.1 if required.
217 # A failure here is not critical.
218 cd $BACKUP_DEST_DIR
219 if [ ! -h current ] ; then
220 if ! /bin/ln -s backup.1 current ; then
221 echo "WARNING: could not create symlink current -> backup.1"
222 fi
223 fi
224
225
226 # Now, do the rsync.
227 echo "Now doing backup with rsync..."
228 echo "Source dir: $SOURCE_DIR"
229 echo -e "Backup destination dir: $BACKUP_DEST_DIR\n"
230
231
232 /usr/bin/rsync $DRY_RUN $VERBOSE -a -S --delete --modify-window=60 \
233 --link-dest=../backup.1 $SOURCE_DIR $BACKUP_DEST_DIR/backup.0/
234
235 # Only warn, rather than exit if the rsync failed,
236 #+ since it may only be a minor problem.
237 # E.g., if one file is not readable, rsync will fail.
238 # This shouldn't prevent the rotation.
239 # Not using, e.g., `date +%a` since these directories
240 #+ are just full of links and don't consume *that much* space.
241
242 if [ $? != 0 ]; then
243 BACKUP_JUSTINCASE=backup.`date +%F_%T`.justincase
244 echo "WARNING: the rsync process did not entirely succeed."
245 echo "Something might be wrong."
246 echo "Saving an extra copy at: $BACKUP_JUSTINCASE"
247 echo "WARNING: if this occurs regularly, a LOT of space will be consumed,"
248 echo "even though these are just hard-links!"
249 fi
250
251 # Save a readme in the backup parent directory.
252 # Save another one in the recent subdirectory.
253 echo "Backup of $SOURCE_DIR on `hostname` was last run on \
254 `date`" > $BACKUP_DEST_DIR/README.txt
255 echo "This backup of $SOURCE_DIR on `hostname` was created on \
256 `date`" > $BACKUP_DEST_DIR/backup.0/README.txt
257
258 # If we are not in a dry run, rotate the backups.
259 [ -z "$DRY_RUN" ] &&
260
261 # Check how full the backup disk is.
262 # Warn if 90%. if 98% or more, we'll probably fail, so give up.
263 # (Note: df can output to more than one line.)
264 # We test this here, rather than before
265 #+ so that rsync may possibly have a chance.
266 DISK_FULL_PERCENT=`/bin/df $BACKUP_DEST_DIR |
267 tr "\n" ' ' | awk '{print $12}' | grep -oE [0-9]+ `
268 echo "Disk space check on backup partition \
269 $MOUNT_POINT $DISK_FULL_PERCENT% full."
270 if [ $DISK_FULL_PERCENT -gt 90 ]; then
271 echo "Warning: Disk is greater than 90% full."
272 fi
273 if [ $DISK_FULL_PERCENT -gt 98 ]; then
274 echo "Error: Disk is full! Giving up."
275 if [ "$UNMOUNT_LATER" == "TRUE" ]; then
276 # Before we exit, unmount the mount point if necessary.
277 cd; sudo umount $MOUNT_POINT &&
278 echo "Unmounted $MOUNT_POINT again. Giving up."
279 fi
280 exit $E_UNMOUNTED
281 fi
282
283
284 # Create an extra backup.
285 # If this copy fails, give up.
286 if [ -n "$BACKUP_JUSTINCASE" ]; then
287 if ! /bin/cp -al $BACKUP_DEST_DIR/backup.0 \
288 $BACKUP_DEST_DIR/$BACKUP_JUSTINCASE
289 then
290 echo "ERROR: Failed to create extra copy \
291 $BACKUP_DEST_DIR/$BACKUP_JUSTINCASE"
292 if [ "$UNMOUNT_LATER" == "TRUE" ]; then
293 # Before we exit, unmount the mount point if necessary.
294 cd ;sudo umount $MOUNT_POINT &&
295 echo "Unmounted $MOUNT_POINT again. Giving up."
296 fi
297 exit $E_UNMOUNTED
298 fi
299 fi
300
301
302 # At start of month, rotate the oldest 8.
303 if [ "$MONTHSTART" == "true" ]; then
304 echo -e "\nStart of month. \
305 Removing oldest backup: $BACKUP_DEST_DIR/backup.15" &&
306 /bin/rm -rf $BACKUP_DEST_DIR/backup.15 &&
307 echo "Rotating monthly,weekly backups: \
308 $BACKUP_DEST_DIR/backup.[8-14] -> $BACKUP_DEST_DIR/backup.[9-15]" &&
309 /bin/mv $BACKUP_DEST_DIR/backup.14 $BACKUP_DEST_DIR/backup.15 &&
310 /bin/mv $BACKUP_DEST_DIR/backup.13 $BACKUP_DEST_DIR/backup.14 &&
311 /bin/mv $BACKUP_DEST_DIR/backup.12 $BACKUP_DEST_DIR/backup.13 &&
312 /bin/mv $BACKUP_DEST_DIR/backup.11 $BACKUP_DEST_DIR/backup.12 &&
313 /bin/mv $BACKUP_DEST_DIR/backup.10 $BACKUP_DEST_DIR/backup.11 &&
314 /bin/mv $BACKUP_DEST_DIR/backup.9 $BACKUP_DEST_DIR/backup.10 &&
315 /bin/mv $BACKUP_DEST_DIR/backup.8 $BACKUP_DEST_DIR/backup.9
316
317 # At start of week, rotate the second-oldest 4.
318 elif [ "$WEEKSTART" == "true" ]; then
319 echo -e "\nStart of week. \
320 Removing oldest weekly backup: $BACKUP_DEST_DIR/backup.12" &&
321 /bin/rm -rf $BACKUP_DEST_DIR/backup.12 &&
322
323 echo "Rotating weekly backups: \
324 $BACKUP_DEST_DIR/backup.[8-11] -> $BACKUP_DEST_DIR/backup.[9-12]" &&
325 /bin/mv $BACKUP_DEST_DIR/backup.11 $BACKUP_DEST_DIR/backup.12 &&
326 /bin/mv $BACKUP_DEST_DIR/backup.10 $BACKUP_DEST_DIR/backup.11 &&
327 /bin/mv $BACKUP_DEST_DIR/backup.9 $BACKUP_DEST_DIR/backup.10 &&
328 /bin/mv $BACKUP_DEST_DIR/backup.8 $BACKUP_DEST_DIR/backup.9
329
330 else
331 echo -e "\nRemoving oldest daily backup: $BACKUP_DEST_DIR/backup.8" &&
332 /bin/rm -rf $BACKUP_DEST_DIR/backup.8
333
334 fi &&
335
336 # Every day, rotate the newest 8.
337 echo "Rotating daily backups: \
338 $BACKUP_DEST_DIR/backup.[1-7] -> $BACKUP_DEST_DIR/backup.[2-8]" &&
339 /bin/mv $BACKUP_DEST_DIR/backup.7 $BACKUP_DEST_DIR/backup.8 &&
340 /bin/mv $BACKUP_DEST_DIR/backup.6 $BACKUP_DEST_DIR/backup.7 &&
341 /bin/mv $BACKUP_DEST_DIR/backup.5 $BACKUP_DEST_DIR/backup.6 &&
342 /bin/mv $BACKUP_DEST_DIR/backup.4 $BACKUP_DEST_DIR/backup.5 &&
343 /bin/mv $BACKUP_DEST_DIR/backup.3 $BACKUP_DEST_DIR/backup.4 &&
344 /bin/mv $BACKUP_DEST_DIR/backup.2 $BACKUP_DEST_DIR/backup.3 &&
345 /bin/mv $BACKUP_DEST_DIR/backup.1 $BACKUP_DEST_DIR/backup.2 &&
346 /bin/mv $BACKUP_DEST_DIR/backup.0 $BACKUP_DEST_DIR/backup.1 &&
347
348 SUCCESS=true
349
350
351 if [ "$UNMOUNT_LATER" == "TRUE" ]; then
352 # Unmount the mount point if it wasn't mounted to begin with.
353 cd ; sudo umount $MOUNT_POINT && echo "Unmounted $MOUNT_POINT again."
354 fi
355
356
357 if [ "$SUCCESS" == "true" ]; then
358 echo 'SUCCESS!'
359 exit 0
360 fi
361
362 # Should have already exited if backup worked.
363 echo 'BACKUP FAILED! Is this just a dry run? Is the disk full?) '
364 exit $E_BACKUP