92afa19539e1d0c456de66d0d9cb1b2c9b3bc0c0
[backups/.git] / scripts / drive.sh
1 #!/bin/bash
2
3 scriptsdir=$(dirname $0)
4
5 export PATH=$scriptsdir:$PATH
6
7 # file locations and other values
8 today=$(date +%Y%m%d%H%M%S)
9 mailto='carl@ecbaldwin.net'
10 datadir="/var/lib/backups"
11 backupdirs="/home /etc /var/backups /var/lib/debfoster /var/lib/mailman /var/lib/iptables /var/lib/ip6tables /var/lib/dpkg"
12 tmpdir="/dev/shm/backups"
13 currentfiles="$tmpdir/files.db"
14 backupdb="$datadir/backups.db"
15 backups="$tmpdir/backup-list.db"
16 statusfile="$tmpdir/backup-status.txt"
17 imagedir="/backup/imgs"
18 isoimage="$imagedir/$today.img"
19 isomountdir="$isoimage.mnt"
20 restorescript="$scriptsdir/restore.sh"
21
22 echo "Creating directories"
23 mkdir --mode=700 -p $datadir $imagedir $isomountdir $tmpdir
24
25 echo "Creating the iso image in $isoimage"
26 dd if=/dev/null of=$isoimage bs=1M count=0 seek=4440
27 mke2fs -b 2048 -F $isoimage
28
29 echo "Mounting the iso image"
30 mount -t ext2 -o loop $isoimage $isomountdir
31
32 echo "Running find to get the status of files"
33 {
34   for type in d f l; do
35     findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
36     find $backupdirs -type $type -printf "$findformat"
37   done
38 } > $currentfiles
39
40 echo "Determining list of files to backup with lsbackups"
41 {
42   # lsbackups expects the current date followed by a null before the list of files
43   printf "$today\0"
44   cat $currentfiles
45 } | lsbackups > $backups 2>$statusfile
46
47 echo "Running rsync to pack the image"
48 rsyncopts="-l -p -t -g -o -0 --files-from=- --stats --progress"
49 cat $backups | rsync $rsyncopts / $isomountdir
50
51 echo "Copying over database and status file"
52 cp $statusfile $backupdb $restorescript $isomountdir
53
54 echo "Unmounting image"
55 umount $isomountdir
56
57 echo "Calculating md5sum for image"
58 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
59
60 exit 0
61
62 # Burn the iso image
63 echo "Burn the image to a DVD"
64 if cdrecord-wrapper.sh -dao speed=8 dev=ATA:1,0,0 $isoimage; then
65   md5sum_file=$(tempfile)
66   dd if=/dev/hdc bs=1M count=4440 | md5sum | awk '{print$1}' > $md5sum_file
67   if ! cmp $md5sum_file $isoimage.md5sum; then
68     echo "Failed" | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
69     exit 1
70   fi
71 else
72   echo "Failed" | mailx -s "DVD burning failed!!!" $mailto
73   exit 1
74 fi
75
76 # Email me
77 cat $statusfile | mailx -s "DVD burned.  File it as $today" $mailto