4f9c8287c1dc009e8e269dbf77bd45ebcfc93cf0
[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   err "Unable to create the following directories:
28   $datadir $imagedir $isomountdir $tmpdir"
29 fi
30
31 echo "-> Creating the iso image in $isoimage"
32 dd of=$isoimage bs=1M count=0 seek=4440
33
34 [ $? != 0 ] && err "dd failed to create $isoimage"
35
36 mke2fs -b 2048 -F $isoimage
37
38 if [ $? != 0 ]
39 then
40   rm -rf $isomountdir $isoimage
41   err "Unable to create the iso image: $isoimage"
42 fi
43
44 echo "-> Mounting the iso image"
45 mount -t ext2 -o loop $isoimage $isomountdir
46 if [ $? != 0 ]
47 then
48   rm -rf $isomountdir $isoimage
49   err "Unable to mount the iso image: $isoimage -> $isomountdir"
50 fi
51
52 echo "-> Directories being backedup and excluded:"
53 echo "backupdirs: $backupdirs"
54 echo "excludedirs: $excludedirs"
55
56 echo "-> Running find to get the status of files"
57 {
58   for type in d f l; do
59     findformat="$type %#m %u %g %s %CY%Cm%Cd%CH%CM%CS 0 %p\0"
60     if [ -z $excludedirs ]
61     then 
62       find $backupdirs -type $type -printf "$findformat"
63     else
64       echo $excludedirs | sed -e 's/ /\n/g' > $tmpdir/excluded
65       find $backupdirs -type $type -printf "$findformat" | grep -z -v -f $tmpdir/excluded
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 $backupdb.tmp
84   err "Unable to rsync to pack the image"
85 fi
86
87 echo "-> Copying over database and status file"
88 mv $backupdb.tmp $backupdb
89 cp $statusfile $backupdb $restorescript $isomountdir
90
91 [ $? != 0 ] && err "Unable to copy the database and status file"
92
93 echo "-> Unmounting image"
94 umount $isomountdir
95
96 [ $? != 0 ] && err "Unable to unmount the iso dir: $isomountdir"
97
98 echo "-> Calculating md5sum for image"
99 md5sum $isoimage | awk '{print$1}' > $isoimage.md5sum
100
101 [ $? != 0 ] && err "Unable to calculate the md5sum for this image: $isoimage"
102
103 cat $statusfile | mailx -s "DVD image available to burn - $today" $mailto
104
105 echo $today > $lastbackupfile
106
107