b4d71bf807219ff3189e41894780f1a971d6a9d7
[backups/.git] / scripts / burn-imgs.sh
1 #!/bin/sh
2
3 export PATH=$(dirname $0):/bin:/usr/bin:/sbin
4
5 cronstopstart="/etc/init.d/cron"
6 . config.sh
7
8 # If the noburn file is there then don't burn.
9 [ -f "$noburnfile" ] && exit 0
10
11 # Discover disk images by looking for .img.md5sum files in $imagedir
12 imgmd5=$(ls $imagedir/*.img.md5sum 2>/dev/null | head -n 1)
13
14 # If we didn't find an non-empty file then exit gracefully
15 [ -z "$imgmd5" ] && exit 0
16 [ -s "$imgmd5" ] || exit 0
17
18 echo MD5 file: $imgmd5
19
20 logfile=$(tempfile)
21
22 echo Logfile: $logfile
23
24 # Get the name of the disk image by stripping off the tailing '.md5sum'
25 img=${imgmd5%.md5sum}
26
27 echo Image: $img
28
29 touch $noburnfile
30
31 # A little paranoia.  Make sure the disk image file is there.
32 [ -z "$img" ] && exit 1
33 [ -s "$img" ] || exit 1
34
35 # To avoid buffer underruns I'm going to stop cron and renice myself
36 $cronstopstart stop
37 renice -10 $$
38
39 # Burn the image to a disk.
40 cdrecord-wrapper.sh -dao dev=$sdev $img > $logfile 2>&1
41
42 if [ "0" != "$?" ]; then
43   echo >&2 "cdrecord failed!"
44   exit 1
45 fi
46
47 # We don't need to be high-priority anymore.
48 renice 0 $$
49 $cronstopstart start
50
51 # Now verify the disk by running md5sum on the entire contents of the disk
52 md5sum=$(tempfile)
53 echo Md5sum: $md5sum
54 dd if=$dev bs=1M count=4440 2>$logfile | md5sum | awk '{print$1}' > $md5sum
55
56 # Check that the md5sums match
57 if ! cmp $md5sum $img.md5sum; then
58   echo "Failed" | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
59   exit 1
60 fi
61
62 # Record the date in the stat file to indicate that this burn was a success
63 date >> $img.stat
64
65 { # Send e-mail
66   echo "Image md5sum:"
67   cat $img.md5sum
68   echo
69   echo "md5sum of burned disks:"
70   cat $md5sum
71   echo
72   cat $img.stat
73   echo
74   cat $logfile
75 } | mailx -s "DVD burned.  File as ${img%.*}" $mailto > $logfile 2>&1
76
77 # Two lines in the stat file indicate two successful burns.  Clean-up the image.
78 if [ "$(wc -l $img.stat 2>/dev/null | awk '{print$1}')" == "2" ]; then
79   # Burned the image twice.  Removing it and associated files!
80   rm -f $img{,.md5sum,.stat}
81   [ -d $img.mnt ] && rmdir $img.mnt
82 fi