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