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