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