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