3 scriptsdir=$(dirname $0)
5 export PATH=$scriptsdir:/bin:/usr/bin:/sbin:/usr/sbin
7 # source the default configuration
10 # source the system specific configuration
11 [ -f /etc/lsbackups.conf ] && . /etc/lsbackups.conf
14 # process command line arguments
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"
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"
30 echo "-> Creating directories"
31 mkdir --mode=700 -p $datadir $imagedir $isomountdir $tmpdir
33 test -d $isomountdir && rm -rf $isomountdir
34 err "Unable to create the following directories:
35 $datadir $imagedir $isomountdir $tmpdir"
38 echo "-> Creating the iso image in $isoimage"
39 dd of=$isoimage bs=1M count=0 seek=$imagesizemb
41 [ $? != 0 ] && err "dd failed to create $isoimage"
43 mke2fs -b $blocksize -F $isoimage
45 rm -rf $isomountdir $isoimage
46 err "Unable to create the iso image: $isoimage"
49 e2label $isoimage $today
51 rm -rf $isomountdir $isoimage
52 err "Unable to label the iso image: $isoimage"
55 echo "-> Mounting the iso image"
56 mount -t ext2 -o loop $isoimage $isomountdir
58 rm -rf $isomountdir $isoimage
59 err "Unable to mount the iso image: $isoimage -> $isomountdir"
62 echo "-> Directories being backedup and excluded:"
63 echo "backupdirs: $backupdirs"
64 echo "excludedirs: $excludedirs"
66 echo "-> Running find to get the status of files"
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"
73 echo $excludedirs | sed -e 's/ /\n/g' > $tmpdir/excluded
74 find $backupdirs -type $type -printf "$findformat" | grep -z -v -f $tmpdir/excluded
79 if [ -n $simulate ]; then
80 backupdbin=$backupdbout
83 echo "-> Determining list of files to backup with lsbackups"
85 # lsbackups expects the current date followed by a null before the list of files
88 } | lsbackups > $backups 2>$statusfile
90 if [ -n $simulate ]; then
92 rm -rf $isomountdir $isoimage
94 echo "-> Simulated backup complete! Backup database: $backupdbout"
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
103 rm -rf $isomountdir $isoimage $backupdbout
104 err "Unable to rsync to pack the image"
107 echo "-> Copying over database and status file"
108 cp $backupdbout $isomountdir/$(basename $backupdbin)
109 [ $? != 0 ] && err "Unable to copy the database to $isomountdir"
111 cp $statusfile $restorescript $isomountdir
112 [ $? != 0 ] && err "Unable to copy the status file to $isomountdir"
114 echo "-> Unmounting image"
117 [ $? != 0 ] && err "Unable to unmount the iso dir: $isomountdir"
119 echo "-> Calculating md5sum for image"
120 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
122 [ $? != 0 ] && err "Unable to calculate the md5sum for this image: $isoimage"
124 cat $statusfile | mailx -s "DVD image available to burn - $today" $mailto
126 echo $today > $lastbackupfile
128 echo "-> Moving new database to $backupdbin"
129 mv $backupdbout $backupdbin
130 [ $? != 0 ] && err "Unable to move backupdb: $backupdbout to $backupdbin"