f33ea99251242a5a1affdc700c0df307f995d8b7
[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 if [ -f /etc/lsbackups.conf ]
12 then
13   . /etc/lsbackups.conf
14 fi 
15
16 # If the noburn file is there then don't burn.
17 [ -f "$noburnfile" ] && exit 0
18
19 # Discover disk images by looking for .img.md5sum files in $imagedir
20 imgmd5=$(ls $imagedir/*.img.md5sum 2>/dev/null | head -n 1)
21
22 # If we didn't find an non-empty file then exit gracefully
23 [ -z "$imgmd5" ] && exit 0
24 [ -s "$imgmd5" ] || exit 0
25
26 logfile=$(tempfile)
27
28 # Get the name of the disk image by stripping off the tailing '.md5sum'
29 img=${imgmd5%.md5sum}
30
31 touch $noburnfile
32
33 # A little paranoia.  Make sure the disk image file is there.
34 [ -z "$img" ] && exit 1
35 [ -s "$img" ] || exit 1
36
37 # To avoid buffer underruns I'm going to stop cron and renice myself
38 $cronstopstart stop
39 renice -10 $$
40
41 # Burn the image to a disk.
42 cdrecord-wrapper.sh -dao dev=$sdev $img > $logfile 2>&1
43
44 if [ "0" != "$?" ]; then
45   echo >&2 "cdrecord failed!"
46   exit 1
47 fi
48
49 # We don't need to be high-priority anymore.
50 renice 0 $$
51 $cronstopstart start
52
53 # I don't know if this *really* helps but give cdrecord a chance to clean up.
54 sleep 60
55
56 # Now verify the disk by running md5sum on the entire contents of the disk
57 md5sum=$(tempfile)
58 dd if=$dev bs=1M count=4440 2>>$logfile | md5sum | awk '{print$1}' > $md5sum
59
60 # Check that the md5sums match
61 if ! cmp $md5sum $img.md5sum; then
62   echo "Failed" | 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 } | 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}')" == "2" ]; 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