Added checks for failed md5sum calculation.
[backups/.git] / scripts / pack-image.sh
index 5bbfc9650594c82a70e1530a150e0f3f599ea0d0..ea702a5b767c48039455751d21177f402359999b 100755 (executable)
 
 scriptsdir=$(dirname $0)
 
-export PATH=$scriptsdir:$PATH
+export PATH=$scriptsdir:/bin:/usr/bin:/sbin:/usr/sbin
 
+# source the default configuration
 . config.sh
 
+# source the system specific configuration
+[ -f /etc/lsbackups.conf ] && . /etc/lsbackups.conf
+
+simulate=
+# process command line arguments
+case "$1" in
+  -s) simulate=1;;
+esac
+
 # file locations and other values
 today=$(date +%Y%m%d%H%M%S)
-isoimage="$imagedir/$today.img"
+isoimage="$imagedir/$today.iso"
 isomountdir="$isoimage.mnt"
 restorescript="$scriptsdir/restore.sh"
 
-echo "Creating directories"
+echo "-> Checking for available disk space on $imagedir"
+available=$(df $imagedir | awk '{print$4}' | grep -E "[0-9]+")
+required=$((imagesizemb*1024))
+[ $required -gt $available ] && err "Not enough space for the backup image on $imagedir"
+
+echo "-> Creating directories"
 mkdir --mode=700 -p $datadir $imagedir $isomountdir $tmpdir
-chgrp backup $imagedir
-chmod g+s    $imagedir
-chmod g+w    $imagedir
+if [ $? != 0 ]; then
+  test -d $isomountdir && rm -rf $isomountdir
+  err "Unable to create the following directories:
+  $datadir $imagedir $isomountdir $tmpdir"
+fi
+
+echo "-> Creating the iso image in $isoimage"
+dd of=$isoimage bs=1M count=0 seek=$imagesizemb
 
-echo "Creating the iso image in $isoimage"
-dd if=/dev/null of=$isoimage bs=1M count=0 seek=4440
-mke2fs -b 2048 -F $isoimage
+[ $? != 0 ] && err "dd failed to create $isoimage"
 
-echo "Mounting the iso image"
+mke2fs -b $blocksize -F $isoimage
+if [ $? != 0 ]; then
+  rm -rf $isomountdir $isoimage
+  err "Unable to create the iso image: $isoimage"
+fi
+
+e2label $isoimage $today
+if [ $? != 0 ]; then
+  rm -rf $isomountdir $isoimage
+  err "Unable to label the iso image: $isoimage"
+fi
+
+echo "-> Mounting the iso image"
 mount -t ext2 -o loop $isoimage $isomountdir
+if [ $? != 0 ]; then
+  rm -rf $isomountdir $isoimage
+  err "Unable to mount the iso image: $isoimage -> $isomountdir"
+fi
+
+echo "-> Directories being backedup and excluded:"
+echo "backupdirs: $backupdirs"
+echo "excludedirs: $excludedirs"
 
-echo "Running find to get the status of files"
+echo "-> Running find to get the status of files"
 {
   for type in d f l; do
     findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
-    find $backupdirs -type $type -printf "$findformat"
+    if [ -z "$excludedirs" ]; then
+      find $backupdirs -type $type -printf "$findformat"
+    else
+      echo $excludedirs | sed -e 's/ /\n/g' > $tmpdir/excluded
+      find $backupdirs -type $type -printf "$findformat" | grep -z -v -f $tmpdir/excluded
+    fi
   done
 } > $currentfiles
 
-echo "Determining list of files to backup with lsbackups"
+if [ -n "$simulate" ]; then
+  backupdbin=$backupdbout
+fi
+
+echo "-> Determining list of files to backup with lsbackups"
 {
   # lsbackups expects the current date followed by a null before the list of files
   printf "$today\0"
   cat $currentfiles
 } | lsbackups > $backups 2>$statusfile
 
-echo "Running rsync to pack the image"
-rsyncopts="-l -p -t -g -o -0 --files-from=- --stats --progress"
+if [ -n "$simulate" ]; then
+   umount $isomountdir
+   rm -rf $isomountdir $isoimage
+   cat $statusfile
+   echo "-> Simulated backup complete! Backup database: $backupdbout"
+   exit;
+fi
+
+echo "-> Running rsync to pack the image"
+rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
 cat $backups | rsync $rsyncopts / $isomountdir
+if [ $? != 0 ]; then
+  umount $isomountdir
+  rm -rf $isomountdir $isoimage $backupdbout
+  err "Unable to rsync to pack the image"
+fi
 
-echo "Copying over database and status file"
-cp $statusfile $backupdb $restorescript $isomountdir
+echo "-> Copying over database and status file"
+cp $backupdbout $isomountdir/$(basename $backupdbin)
+[ $? != 0 ] && err "Unable to copy the database to $isomountdir"
 
-echo "Unmounting image"
+cp $statusfile $restorescript $isomountdir
+[ $? != 0 ] && err "Unable to copy the status file to $isomountdir"
+
+echo "-> Unmounting image"
 umount $isomountdir
 
-echo "Calculating md5sum for image"
+[ $? != 0 ] && err "Unable to unmount the iso dir: $isomountdir"
+
+echo "-> Calculating md5sum for image"
 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
 
+[ $? != 0 ] && err "Unable to calculate the md5sum for this image: $isoimage"
+[ -z "$isoimage.md5sum" ] && err "Unable to calculate the md5sum for this image: $isoimage"
+[ -s "$isoimage.md5sum" ] || err "Unable to calculate the md5sum for this image: $isoimage"
+
 cat $statusfile | mailx -s "DVD image available to burn - $today" $mailto
 
 echo $today > $lastbackupfile
+
+echo "-> Moving new database to $backupdbin"
+mv $backupdbout $backupdbin
+[ $? != 0 ] && err "Unable to move backupdb: $backupdbout to $backupdbin"
+