Extra debug information added to md5sum check failure. Changed dd count= option to...
[backups/.git] / scripts / burn-imgs.sh
1 #!/bin/sh
2
3 export PATH=$(dirname $0):/bin:/usr/bin:/sbin
4
5 # source the default configuration
6 . config.sh
7
8 # source the system specific configuration
9 [ -f /etc/lsbackups.conf ] && . /etc/lsbackups.conf
10
11 # If the noburn file is there then don't burn.
12 [ -f "$noburnfile" ] && exit 0
13
14 # Discover disk images by looking for .iso.md5sum files in $imagedir
15 imgmd5=$(ls $imagedir/*.iso.md5sum 2>/dev/null | head -n 1)
16
17 # If we didn't find an non-empty file then exit gracefully
18 [ -z "$imgmd5" ] && exit 0
19 [ -s "$imgmd5" ] || exit 0
20
21 logfile=$(tempfile)
22
23 # Get the name of the disk image by stripping off the tailing '.md5sum'
24 img=${imgmd5%.md5sum}
25
26 touch $noburnfile
27
28 # A little paranoia.  Make sure the disk image file is there.
29 [ -z "$img" ] && exit 1
30 [ -s "$img" ] || exit 1
31
32 # To avoid buffer underruns I'm going to stop cron and renice myself
33 $cronstopstart stop
34 renice -10 $$
35
36 # Burn the image to a disk.
37 cdrecord-wrapper.sh $cdrecordopts dev=$sdev $img > $logfile 2>&1
38
39 if [ "0" != "$?" ]; then
40   echo >&2 "cdrecord failed!"
41   $cronstopstart start
42   cat $logfile | grep -v "MB written" | grep -v "to quit" | mailx -s "backups: failed to burn $img!!!" $mailto
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=$imagesizemb 2>>$logfile | md5sum | awk '{print$1}' > $md5sum
56
57 # Check that the md5sums match
58 if ! cmp $md5sum $img.md5sum; then
59   echo "md5sum check FAILED" >> $logfile
60   echo "dd if=$dev bs=1M count=$imagesizemb 2>>$logfile | md5sum | awk '{print\$1}' > $md5sum" >> $logfile
61   e2label $dev >> $logfile
62   cat $logfile | grep -v "MB written" | grep -v "to quit" | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
63   exit 1
64 fi
65
66 # Record the date in the stat file to indicate that this burn was a success
67 date >> $img.stat
68
69 { # Send e-mail
70   echo "Image md5sum:"
71   cat $img.md5sum
72   echo
73   echo "md5sum of burned disks:"
74   cat $md5sum
75   echo
76   cat $img.stat
77   echo
78   cat $logfile
79 } | grep -v "MB written" | grep -v "to quit" | mailx -s "DVD burned.  File as $(basename ${img%.*})" $mailto > $logfile 2>&1
80
81 # Two lines in the stat file indicate two successful burns.  Clean-up the image.
82 if [ "$(wc -l $img.stat 2>/dev/null | awk '{print$1}')" == "$makeXcopies" ]; then
83   # Burned the image twice.  Removing it and associated files!
84   rm -f $img{,.md5sum,.stat}
85   [ -d $img.mnt ] && rmdir $img.mnt
86 fi