Added checks for failed md5sum calculation.
[backups/.git] / scripts / pack-image.sh
1 #!/bin/bash
2
3 scriptsdir=$(dirname $0)
4
5 export PATH=$scriptsdir:/bin:/usr/bin:/sbin:/usr/sbin
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 simulate=
14 # process command line arguments
15 case "$1" in
16   -s) simulate=1;;
17 esac
18
19 # file locations and other values
20 today=$(date +%Y%m%d%H%M%S)
21 isoimage="$imagedir/$today.iso"
22 isomountdir="$isoimage.mnt"
23 restorescript="$scriptsdir/restore.sh"
24
25 echo "-> Checking for available disk space on $imagedir"
26 available=$(df $imagedir | awk '{print$4}' | grep -E "[0-9]+")
27 required=$((imagesizemb*1024))
28 [ $required -gt $available ] && err "Not enough space for the backup image on $imagedir"
29
30 echo "-> Creating directories"
31 mkdir --mode=700 -p $datadir $imagedir $isomountdir $tmpdir
32 if [ $? != 0 ]; then
33   test -d $isomountdir && rm -rf $isomountdir
34   err "Unable to create the following directories:
35   $datadir $imagedir $isomountdir $tmpdir"
36 fi
37
38 echo "-> Creating the iso image in $isoimage"
39 dd of=$isoimage bs=1M count=0 seek=$imagesizemb
40
41 [ $? != 0 ] && err "dd failed to create $isoimage"
42
43 mke2fs -b $blocksize -F $isoimage
44 if [ $? != 0 ]; then
45   rm -rf $isomountdir $isoimage
46   err "Unable to create the iso image: $isoimage"
47 fi
48
49 e2label $isoimage $today
50 if [ $? != 0 ]; then
51   rm -rf $isomountdir $isoimage
52   err "Unable to label the iso image: $isoimage"
53 fi
54
55 echo "-> Mounting the iso image"
56 mount -t ext2 -o loop $isoimage $isomountdir
57 if [ $? != 0 ]; then
58   rm -rf $isomountdir $isoimage
59   err "Unable to mount the iso image: $isoimage -> $isomountdir"
60 fi
61
62 echo "-> Directories being backedup and excluded:"
63 echo "backupdirs: $backupdirs"
64 echo "excludedirs: $excludedirs"
65
66 echo "-> Running find to get the status of files"
67 {
68   for type in d f l; do
69     findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
70     if [ -z "$excludedirs" ]; then
71       find $backupdirs -type $type -printf "$findformat"
72     else
73       echo $excludedirs | sed -e 's/ /\n/g' > $tmpdir/excluded
74       find $backupdirs -type $type -printf "$findformat" | grep -z -v -f $tmpdir/excluded
75     fi
76   done
77 } > $currentfiles
78
79 if [ -n "$simulate" ]; then
80   backupdbin=$backupdbout
81 fi
82
83 echo "-> Determining list of files to backup with lsbackups"
84 {
85   # lsbackups expects the current date followed by a null before the list of files
86   printf "$today\0"
87   cat $currentfiles
88 } | lsbackups > $backups 2>$statusfile
89
90 if [ -n "$simulate" ]; then
91    umount $isomountdir
92    rm -rf $isomountdir $isoimage
93    cat $statusfile
94    echo "-> Simulated backup complete! Backup database: $backupdbout"
95    exit;
96 fi
97
98 echo "-> Running rsync to pack the image"
99 rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
100 cat $backups | rsync $rsyncopts / $isomountdir
101 if [ $? != 0 ]; then
102   umount $isomountdir
103   rm -rf $isomountdir $isoimage $backupdbout
104   err "Unable to rsync to pack the image"
105 fi
106
107 echo "-> Copying over database and status file"
108 cp $backupdbout $isomountdir/$(basename $backupdbin)
109 [ $? != 0 ] && err "Unable to copy the database to $isomountdir"
110
111 cp $statusfile $restorescript $isomountdir
112 [ $? != 0 ] && err "Unable to copy the status file to $isomountdir"
113
114 echo "-> Unmounting image"
115 umount $isomountdir
116
117 [ $? != 0 ] && err "Unable to unmount the iso dir: $isomountdir"
118
119 echo "-> Calculating md5sum for image"
120 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
121
122 [ $? != 0 ] && err "Unable to calculate the md5sum for this image: $isoimage"
123 [ -z "$isoimage.md5sum" ] && err "Unable to calculate the md5sum for this image: $isoimage"
124 [ -s "$isoimage.md5sum" ] || err "Unable to calculate the md5sum for this image: $isoimage"
125
126 cat $statusfile | mailx -s "DVD image available to burn - $today" $mailto
127
128 echo $today > $lastbackupfile
129
130 echo "-> Moving new database to $backupdbin"
131 mv $backupdbout $backupdbin
132 [ $? != 0 ] && err "Unable to move backupdb: $backupdbout to $backupdbin"
133