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