Added 2nd try to rsync when the 1st attempt fails to cache the burn image properly.
[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 # Now check to see if we need to cache the image to a local drive before burning the image
29 if [[ $cacheburnimg == 1 && -d $cacheburnimgdir ]]; then
30   rsync -av --exclude '*.mnt' $img* $cacheburnimgdir >> $logfile 2>&1
31   if [ $? != 0 ]; then
32     sleep 60
33     rsync -av --exclude '*.mnt' $img* $cacheburnimgdir >> $logfile 2>&1
34     if [ $? != 0 ]; then
35       cat $logfile | mailx -s "backups: failed to cache $img to local dir $cacheburnimgdir !!!" $mailto
36       exit 1
37     fi
38   fi
39   img=$cacheburnimgdir/$(basename "$img")
40 fi
41
42 # A little paranoia.  Make sure the disk image file is there.
43 [ -z "$img" ] && exit 1
44 [ -s "$img" ] || exit 1
45
46 # To avoid buffer underruns I'm going to stop cron and renice myself
47 $cronstopstart stop
48 renice -10 $$
49
50 # Burn the image to a disk.
51 cdrecord-wrapper.sh $cdrecordopts dev=$sdev $img >> $logfile 2>&1
52
53 if [ "0" != "$?" ]; then
54   echo >&2 "cdrecord failed!"
55   $cronstopstart start
56   cat $logfile | grep -v -e '[\b\r]' | mailx -s "backups: failed to burn $img!!!" $mailto
57   exit 1
58 fi
59
60 # We don't need to be high-priority anymore.
61 renice 0 $$
62 $cronstopstart start
63
64 # I don't know if this *really* helps but give cdrecord a chance to clean up.
65 sleep 60
66
67 # Now verify the disk by running md5sum on the entire contents of the disk
68 md5sum=$(tempfile)
69 dd if=$dev bs=1M count=$imagesizemb 2>>$logfile | md5sum | awk '{print$1}' > $md5sum
70
71 # Check that the md5sums match
72 if ! cmp $md5sum $img.md5sum; then
73   echo "md5sum check FAILED" >> $logfile
74   echo "dd if=$dev bs=1M count=$imagesizemb 2>>$logfile | md5sum | awk '{print\$1}' > $md5sum" >> $logfile
75   e2label $dev >> $logfile 2>&1
76   cat $logfile | grep -v -e '[\b\r]' | mailx -s "DVD md5sum doesn't match image file!!!" $mailto
77   exit 1
78 fi
79
80 # Record the date in the stat file to indicate that this burn was a success
81 date >> $img.stat
82
83 # Update the source file stat file if we burned a cached copy of the image
84 [[ $cacheburnimg == 1 && -d $cacheburnimgdir ]] && date >> ${imgmd5%.md5sum}.stat
85
86 { # Send e-mail
87   echo "Image md5sum:"
88   cat $img.md5sum
89   echo
90   echo "md5sum of burned disks:"
91   cat $md5sum
92   echo
93   cat $img.stat
94   echo
95   cat $logfile
96 } | grep -v -e '[\b\r]' | mailx -s "DVD burned.  File as $(basename ${img%.*})" $mailto >> $logfile 2>&1
97
98 # Two lines in the stat file indicate two successful burns.  Clean-up the image.
99 if [ "$(wc -l $img.stat 2>/dev/null | awk '{print$1}')" == "$makeXcopies" ]; then
100   # Burned the image $makeXcopies number of times.  Removing it and associated files! 
101   if [[ $cacheburnimg == 1 && -d $cacheburnimgdir ]]; then
102     # Remove our cached copy of the image, its md5sum, and stat file
103     rm -f $img{,.md5sum,.stat}
104     # Now restore our $img variable to point back to the original source file
105     img=${imgmd5%.md5sum}
106   fi
107   rm -f $img{,.md5sum,.stat}
108   [ -d $img.mnt ] && rmdir $img.mnt
109 fi
110