Used Carl's super-cool shell script shorthand to source system config file.
[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 # file locations and other values
14 today=$(date +%Y%m%d%H%M%S)
15 isoimage="$imagedir/$today.img"
16 isomountdir="$isoimage.mnt"
17 restorescript="$scriptsdir/restore.sh"
18
19 echo "-> Creating directories"
20 mkdir --mode=700 -p $datadir $imagedir $isomountdir $tmpdir
21 if [ $? != 0 ] 
22 then
23   if test -d $isomountdir
24   then
25     rm -rf $isomountdir
26   fi
27   echo "-E- Unable to create the following directories: "
28   echo "    $datadir $imagedir $isomountdir $tmpdir"
29   exit -1
30 fi
31
32 echo "-> Creating the iso image in $isoimage"
33 dd if=/dev/null of=$isoimage bs=1M count=0 seek=4440
34 mke2fs -b 2048 -F $isoimage
35 if [ $? != 0 ]
36 then
37   rm -rf $isomountdir $isoimage
38   echo "-E- Unable to create the iso image: $isoimage"
39   exit -1
40 fi
41
42 echo "-> Mounting the iso image"
43 mount -t ext2 -o loop $isoimage $isomountdir
44 if [ $? != 0 ]
45 then
46   rm -rf $isomountdir $isoimage
47   echo "-E- Unable to mount the iso image: $isoimage -> $isomountdir"
48   exit -1
49 fi
50
51 echo "-> Directories being backedup and excluded:"
52 echo "backupdirs: $backupdirs"
53 echo "excludedirs: $excludedirs"
54
55 echo "-> Running find to get the status of files"
56 {
57   for type in d f l; do
58     findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
59     if [ -z $excludeddirs ]
60     then 
61       find $backupdirs -type $type -printf "$findformat"
62     else
63       regex=`echo $excludedirs | sed -e 's/ /.*\\\\|/g'`
64       regex=`echo "'\($regex.*\)'"`
65       find $backupdirs -type $type -o -regex $regex -prune -o -printf "$findformat"
66     fi
67   done
68 } > $currentfiles
69
70 echo "-> Determining list of files to backup with lsbackups"
71 {
72   # lsbackups expects the current date followed by a null before the list of files
73   printf "$today\0"
74   cat $currentfiles
75 } | lsbackups > $backups 2>$statusfile
76
77 echo "-> Running rsync to pack the image"
78 rsyncopts="-W -H -S -l -p -t -g -o -0 --files-from=- --stats --progress"
79 cat $backups | rsync $rsyncopts / $isomountdir
80 if [ $? != 0 ]
81 then
82   umount $isomountdir
83   rm -rf $isomountdir $isoimage
84   echo "-E- Unable to rsync to pack the image"
85   exit -1
86 fi
87
88 echo "-> Copying over database and status file"
89 cp $statusfile $backupdb $restorescript $isomountdir
90 if [ $? != 0 ]
91 then
92   echo "-E- Unable to copy the database and status file"
93   exit -1
94 fi
95
96 echo "-> Unmounting image"
97 umount $isomountdir
98 if [ $? != 0 ]
99 then
100   echo "-E- Unable to unmount the iso dir: $isomountdir"
101   exit -1
102 fi
103
104 echo "-> Calculating md5sum for image"
105 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
106 if [ $? != 0 ]
107 then
108   echo "-E- Unable to calculatethe md5sum for this image: $isoimage"
109   exit -1
110 fi
111
112 cat $statusfile | mailx -s "DVD image available to burn - $today" $mailto
113
114 echo $today > $lastbackupfile
115
116