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