Merge branch 'master' of home:dvl/backup
[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/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 isomountdir="/backup/iso-mount"
18 isoimage="/backup/ISOs/$today.img"
19 restorescript="$scriptsdir/restore.sh"
20
21 echo "Creating the iso image in $isoimage"
22 touch $isoimage
23 # chmod 600 $isoimage
24
25 # Create filesystem on iso image here
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 iso image here
31 mount -t ext2 -o loop $isoimage $isomountdir
32
33 mkdir -p $tmpdir
34 chmod 700 $tmpdir
35
36 touch $currentfiles
37 chmod 600 $currentfiles
38 echo "Running find to get the status of files"
39 {
40   for type in d f l; do
41     findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
42     find $backupdirs -type $type -printf "$findformat"
43   done
44 } > $currentfiles
45
46 echo "Determining list of files to backup with lsbackups"
47 {
48   # lsbackups expects the current date followed by a null before the list of files
49   printf "$today\0"
50   cat $currentfiles
51 } | lsbackups > $backups 2>$statusfile
52
53 echo "Running rsync to pack the image"
54 rsyncopts="-l -p -t -g -o -0 --files-from=- --stats --progress"
55 cat $backups | rsync $rsyncopts / $isomountdir
56
57 echo "Copying over database and status file"
58 cp $statusfile $backupdb $restorescript $isomountdir
59
60 echo "Unmounting image"
61 umount $isomountdir
62
63 echo "Calculating md5sum for image"
64 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
65
66 # Burn the iso image
67 echo "Burn the image to a DVD"
68 if cdrecord-wrapper.sh -dao speed=8 dev=ATA:1,0,0 $isoimage; then
69   md5sum_file=$(tempfile)
70   dd if=/dev/hdc bs=2048k count=2220 | md5sum | awk '{print$1}' > $md5sum_file
71   if ! cmp $md5sum_file $isoimage.md5sum; then
72     echo "Failed" | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
73     exit 1
74   fi
75 else
76   echo "Failed" | mailx -s "DVD burning failed!!!" $mailto
77   exit 1
78 fi
79
80 # Email me
81 cat $statusfile | mailx -s "DVD burned.  File it as $today" $mailto