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