More improvements from Aaron S. Hawley <aaron.s.hawley@gmail.com>.
[dump.git] / examples / cron_dump_to_disk / backup
CommitLineData
1733496b
SP
1#!/bin/bash
2#
3# This script will make a simple backup of the most critical partitions,
ad80e1e1
SP
4# using the "dump" facility, into the backup partition.
5#
6# Run the following to make a complete backup:
7#
8# $ su -c "backup full"
9#
10# Run the following to make an incremental backup:
11#
12# $ su -c "backup inc"
13#
14# Add "nodumpdate" to run a backup but not effect the backup state
15# recorded in the file /var/lib/dumpdates and reported by dump -W:
16#
17# $ su -c "backup full nodumpdate"
1733496b
SP
18#
19
20if [ "$2" = "nodumpdate" ]; then
21 UPDATEDDATE=""
22else
23 UPDATEDDATE="-u"
24fi
25
26if [ "$1" = "full" ]; then
27 DLEVEL="0"
28 BTYPE="full"
9d6d171f 29 BACKUP="Full"
1733496b
SP
30elif [ "$1" = "inc" ]; then
31 DLEVEL="1"
32 BTYPE="inc"
9d6d171f 33 BACKUP="Incremental"
1733496b
SP
34else
35 echo "Usage: $0 full|inc [nodumpdate]"
36 exit 1
37fi
38
39
40#
41# Configuration Parameters
42#
43
b35abb32
SP
44BACKUPPART=${BACKUPPART:-"/backup"}
45BACKUPDIR=${BACKUPDIR:-"current"}
1733496b 46DUMPLOGARCH="$BACKUPPART/backup.dump.log.gz"
b35abb32 47FSTODUMP=${FSTODUMP:-/ /var /home /mnt/hdb1 /usr}
1733496b
SP
48DUMPFILESMODE="0644"
49DUMPFILESOWN="root.root"
50
51#
52# Start
53#
54
55echo
56echo "#####################################################################"
9d6d171f 57echo "Starting ${BACKUP} backup."
1733496b
SP
58echo "#####################################################################"
59echo
60
61
62#
9d6d171f 63# Make system backup
1733496b
SP
64#
65
9d6d171f 66echo "### ${BACKUP} System Dump Backup ###"
a405c61d
SP
67echo "Using backup partition: $BACKUPPART"
68echo "Filesystems to dump: $FSTODUMP"
1733496b 69
a405c61d 70echo -n "Remounting backup partition read-write ... "
1733496b
SP
71if ( mount $BACKUPPART -o remount,rw &> /dev/null ) then
72 echo "done."
2e0e9e2e
SP
73elif ( mount $BACKUPPART -o rw &> /dev/null ) then
74 echo "done."
1733496b
SP
75else
76 echo "failure!"
a405c61d 77 echo " There were problems remounting $BACKUPPART in read-write mode!"
9d6d171f
SP
78 echo "Aborting ${BACKUP} System Dump Backup."
79 echo "Aborted."
1733496b
SP
80 echo "-------------------------------------------------------------------------------"
81 exit 1
82fi
83
a405c61d 84echo -n "Checking backup partition for correct dir structure ... "
1733496b
SP
85if [ -d $BACKUPPART/$BACKUPDIR -a -w $BACKUPPART/$BACKUPDIR ]; then
86 echo "done."
87
a405c61d 88 echo -n "Checking backup partition for available space ... "
2e0e9e2e
SP
89 SREQ=`for i in $FSTODUMP; do dump -$DLEVEL -S $i 2> /dev/null; done | awk '{x=x+$1/1048576} END {printf "%6.0f\n", x}'`
90 SAVAILFREE=`df --block-size=1048576 | grep -Fe $BACKUPPART | awk '{printf "%6.0f\n", $4}'`
91 SAVAILDEL=`du -s --block-size=1048576 $BACKUPPART/$BACKUPDIR/. | awk '{printf "%6.0f\n", $1}'`
1733496b
SP
92 SAVAIL=`expr $SAVAILFREE + $SAVAILDEL`
93
94 if [ `expr $SAVAIL - $SREQ` -gt "0" ]; then
95 echo "done."
b35abb32 96 echo "Available: $SAVAIL MB Required: $SREQ MB."
1733496b 97 else
2e0e9e2e 98 echo "not enough space!"
b35abb32
SP
99 echo "There is not enough space left in $BACKUPPART for the backup!"
100 echo "Available: $SAVAIL MB Required: $SREQ MB."
a405c61d 101 echo -n "Remounting backup partition read-only ... "
1733496b
SP
102 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
103 echo "done."
2e0e9e2e
SP
104 elif ( mount $BACKUPPART -o ro &> /dev/null ) then
105 echo "done."
1733496b
SP
106 else
107 echo "failure!"
b35abb32 108 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
109 echo "Aborting ${BACKUP} System Dump Backup."
110 echo "Aborted."
1733496b
SP
111 echo "-------------------------------------------------------------------------------"
112 exit 1
113 fi
9d6d171f
SP
114 echo "Aborting ${BACKUP} System Dump Backup."
115 echo "Aborted."
1733496b
SP
116 echo "-------------------------------------------------------------------------------"
117 exit 1
118 fi
119
a405c61d 120 echo -n "Deleting old files ... "
2e0e9e2e 121 if [ `ls -la $BACKUPPART/$BACKUPDIR/ | wc -l` -gt "3" ]; then
1733496b
SP
122 rm -f $BACKUPPART/$BACKUPDIR/* &> /dev/null
123 echo "done."
124 else
125 echo "no old files to delete."
126 fi
127
a405c61d 128 echo "Dumping filesystems ... "
1733496b
SP
129 for FS in $FSTODUMP
130 do
131 if [ "$FS" = "/" ]; then
132 FSNAME="root"
133 else
2e0e9e2e 134 FSNAME=`echo $FS | tr / _ | cut -b 2-`
1733496b
SP
135 fi
136 sync
ad80e1e1 137 TODAY="`date +%a%Y%m%d`"
b35abb32 138 echo -n "Starting dump of $FSNAME ( $FS ) ... "
ad80e1e1 139 if ( dump -$DLEVEL $UPDATEDDATE -z -M -s 27306 -f $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE. -Q $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE.qfa -L ${TODAY}file $FS &> $BACKUPPART/$BACKUPDIR/$FSNAME.log ) then
1733496b
SP
140 echo "done."
141 else
142 echo "problems!"
2e0e9e2e
SP
143 echo "There were problems with the dump of $FSNAME ( $FS )."
144 echo "Check log file at $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz"
b35abb32 145 echo "Also check log archive file $DUMPLOGARCH."
1733496b 146 fi
ad80e1e1
SP
147
148 echo -n "Verifying dump of $FSNAME ( $FS ) ... "
149 echo "-------------------------------------------------------------------------------" >> $BACKUPPART/$BACKUPDIR/$FSNAME.log
150 echo "Result of dump for $FSNAME ( $FS ):" >> $BACKUPPART/$BACKUPDIR/$FSNAME.log
151 if ( restore -C -M -f $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE. >> $BACKUPPART/$BACKUPDIR/$FSNAME.log 2>&1 ) then
152 echo "done."
153 else
154 echo "problems!"
155 echo "There were problems verifying the dump of $FSNAME ( $FS )."
156 echo "Check log file $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz for more info"
157 fi
1733496b
SP
158 cat $BACKUPPART/$BACKUPDIR/$FSNAME.log |gzip >> $DUMPLOGARCH
159 echo "-------------------------------------------------------------------------------" |gzip >> $DUMPLOGARCH
160 done
161
a405c61d 162 echo -n "Setting ownership and permissions of dump files ... "
1733496b
SP
163 chmod $DUMPFILESMODE $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
164 chown $DUMPFILESOWN $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
165 echo "done."
166
a405c61d 167 echo -n "Compressing dump log files ... "
1733496b
SP
168 gzip $BACKUPPART/$BACKUPDIR/*.log &> /dev/null
169 echo "done."
170 sync
171
172else
173 echo "problems!"
b35abb32 174 echo "There are problems with the directory structure."
2e0e9e2e 175 echo "Check directory: $BACKUPPART/$BACKUPDIR"
a405c61d 176 echo -n "Remounting backup partition read-only ... "
1733496b
SP
177 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
178 echo "done."
179 else
180 echo "failure!"
b35abb32 181 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
182 echo "Aborting ${BACKUP} System Dump Backup."
183 echo "Aborted."
1733496b
SP
184 echo "-------------------------------------------------------------------------------"
185 exit 1
186 fi
9d6d171f
SP
187 echo "Aborting ${BACKUP} System Dump Backup."
188 echo "Aborted."
1733496b
SP
189 echo "-------------------------------------------------------------------------------"
190 exit 1
191fi
192
a405c61d 193echo -n "Remounting backup partition read-only ... "
1733496b
SP
194if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
195 echo "done."
196else
197 echo "failure!"
b35abb32 198 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
199 echo "Aborting ${BACKUP} System Dump Backup."
200 echo "Aborted."
1733496b
SP
201 echo "-------------------------------------------------------------------------------"
202 exit 1
203fi
204
b35abb32 205echo "### End of ${BACKUP} System Dump Backup ###"
a405c61d 206echo "Done."
1733496b 207echo "-------------------------------------------------------------------------------"
ad80e1e1
SP
208
209## end of script