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