Yet more improvements to cron_dump_to_disk.
[dump.git] / examples / cron_dump_to_disk / backup
CommitLineData
1733496b 1#!/bin/bash
6a33575a
SP
2
3###
4 # Copyright (C) 2001 Eugenio Diaz <getnito@yahoo.com>
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # 3. Neither the name of the University nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS
24 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # SUCH DAMAGE.
32 #
33 ##
34 #
35 # This script will make a simple backup of the most critical
36 # partitions, using the "dump" facility, into the backup partition.
37 #
38 # Run the following to make a complete backup:
39 #
40 # $ su -c "backup full"
41 #
42 # Run the following to make an incremental backup:
43 #
44 # $ su -c "backup inc"
45 #
46 # Add "nodumpdate" to run a backup but not effect the backup state
47 # recorded in the file /var/lib/dumpdates and reported by dump -W:
48 #
49 # $ su -c "backup full nodumpdate"
50 #
51 ##
1733496b
SP
52
53if [ "$2" = "nodumpdate" ]; then
54 UPDATEDDATE=""
55else
56 UPDATEDDATE="-u"
57fi
58
59if [ "$1" = "full" ]; then
60 DLEVEL="0"
61 BTYPE="full"
9d6d171f 62 BACKUP="Full"
1733496b
SP
63elif [ "$1" = "inc" ]; then
64 DLEVEL="1"
65 BTYPE="inc"
9d6d171f 66 BACKUP="Incremental"
1733496b 67else
6a33575a 68 echo "Usage: $0 full|inc [nodumpdate]"
1733496b
SP
69 exit 1
70fi
71
72
73#
74# Configuration Parameters
75#
76
b35abb32
SP
77BACKUPPART=${BACKUPPART:-"/backup"}
78BACKUPDIR=${BACKUPDIR:-"current"}
1733496b 79DUMPLOGARCH="$BACKUPPART/backup.dump.log.gz"
b35abb32 80FSTODUMP=${FSTODUMP:-/ /var /home /mnt/hdb1 /usr}
1733496b
SP
81DUMPFILESMODE="0644"
82DUMPFILESOWN="root.root"
83
84#
85# Start
6a33575a 86#
1733496b
SP
87
88echo
6a33575a
SP
89echo "############################################################"
90echo "Starting ${BACKUP} backup"
91echo "############################################################"
1733496b
SP
92echo
93
94
95#
9d6d171f 96# Make system backup
1733496b
SP
97#
98
9d6d171f 99echo "### ${BACKUP} System Dump Backup ###"
a405c61d
SP
100echo "Using backup partition: $BACKUPPART"
101echo "Filesystems to dump: $FSTODUMP"
1733496b 102
a405c61d 103echo -n "Remounting backup partition read-write ... "
1733496b
SP
104if ( mount $BACKUPPART -o remount,rw &> /dev/null ) then
105 echo "done."
2e0e9e2e
SP
106elif ( mount $BACKUPPART -o rw &> /dev/null ) then
107 echo "done."
1733496b
SP
108else
109 echo "failure!"
6a33575a 110 echo "There were problems remounting $BACKUPPART in read-write mode!"
9d6d171f
SP
111 echo "Aborting ${BACKUP} System Dump Backup."
112 echo "Aborted."
6a33575a 113 echo "------------------------------------------------------------"
1733496b
SP
114 exit 1
115fi
116
6a33575a 117echo -n "Checking directory structure on backup partition ... "
1733496b
SP
118if [ -d $BACKUPPART/$BACKUPDIR -a -w $BACKUPPART/$BACKUPDIR ]; then
119 echo "done."
120
a405c61d 121 echo -n "Checking backup partition for available space ... "
6a33575a
SP
122 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}')
123 SAVAILFREE=$(df --block-size=1048576 | grep -Fe $BACKUPPART | awk '{printf "%6.0f\n", $4}')
124 SAVAILDEL=$(du -s --block-size=1048576 $BACKUPPART/$BACKUPDIR/. | awk '{printf "%6.0f\n", $1}')
125 SAVAIL=$(( $SAVAILFREE + $SAVAILDEL ))
1733496b 126
6a33575a 127 if [ "$(( $SAVAIL - $SREQ ))" -gt "0" ]; then
1733496b 128 echo "done."
b35abb32 129 echo "Available: $SAVAIL MB Required: $SREQ MB."
1733496b 130 else
2e0e9e2e 131 echo "not enough space!"
b35abb32
SP
132 echo "There is not enough space left in $BACKUPPART for the backup!"
133 echo "Available: $SAVAIL MB Required: $SREQ MB."
a405c61d 134 echo -n "Remounting backup partition read-only ... "
1733496b
SP
135 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
136 echo "done."
2e0e9e2e
SP
137 elif ( mount $BACKUPPART -o ro &> /dev/null ) then
138 echo "done."
1733496b
SP
139 else
140 echo "failure!"
b35abb32 141 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
142 echo "Aborting ${BACKUP} System Dump Backup."
143 echo "Aborted."
6a33575a 144 echo "------------------------------------------------------------"
1733496b
SP
145 exit 1
146 fi
9d6d171f
SP
147 echo "Aborting ${BACKUP} System Dump Backup."
148 echo "Aborted."
6a33575a 149 echo "------------------------------------------------------------"
1733496b
SP
150 exit 1
151 fi
152
a405c61d 153 echo -n "Deleting old files ... "
6a33575a 154 if [ "$(ls -la $BACKUPPART/$BACKUPDIR/ | wc -l)" -gt "3" ]; then
1733496b
SP
155 rm -f $BACKUPPART/$BACKUPDIR/* &> /dev/null
156 echo "done."
157 else
158 echo "no old files to delete."
159 fi
160
a405c61d 161 echo "Dumping filesystems ... "
1733496b
SP
162 for FS in $FSTODUMP
163 do
164 if [ "$FS" = "/" ]; then
165 FSNAME="root"
166 else
6a33575a 167 FSNAME="$(echo $FS | tr / _ | cut -b 2-)"
1733496b
SP
168 fi
169 sync
6a33575a 170 TODAY="$(date +%a%Y%m%d)"
b35abb32 171 echo -n "Starting dump of $FSNAME ( $FS ) ... "
ad80e1e1 172 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
173 echo "done."
174 else
175 echo "problems!"
2e0e9e2e
SP
176 echo "There were problems with the dump of $FSNAME ( $FS )."
177 echo "Check log file at $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz"
b35abb32 178 echo "Also check log archive file $DUMPLOGARCH."
1733496b 179 fi
ad80e1e1
SP
180
181 echo -n "Verifying dump of $FSNAME ( $FS ) ... "
6a33575a 182 echo "------------------------------------------------------------" >> $BACKUPPART/$BACKUPDIR/$FSNAME.log
ad80e1e1
SP
183 echo "Result of dump for $FSNAME ( $FS ):" >> $BACKUPPART/$BACKUPDIR/$FSNAME.log
184 if ( restore -C -M -f $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE. >> $BACKUPPART/$BACKUPDIR/$FSNAME.log 2>&1 ) then
185 echo "done."
186 else
187 echo "problems!"
188 echo "There were problems verifying the dump of $FSNAME ( $FS )."
189 echo "Check log file $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz for more info"
190 fi
6a33575a
SP
191 gzip < $BACKUPPART/$BACKUPDIR/$FSNAME.log >> $DUMPLOGARCH
192 echo "------------------------------------------------------------" | gzip >> $DUMPLOGARCH
1733496b
SP
193 done
194
a405c61d 195 echo -n "Setting ownership and permissions of dump files ... "
1733496b
SP
196 chmod $DUMPFILESMODE $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
197 chown $DUMPFILESOWN $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
198 echo "done."
199
a405c61d 200 echo -n "Compressing dump log files ... "
1733496b
SP
201 gzip $BACKUPPART/$BACKUPDIR/*.log &> /dev/null
202 echo "done."
203 sync
204
205else
206 echo "problems!"
b35abb32 207 echo "There are problems with the directory structure."
2e0e9e2e 208 echo "Check directory: $BACKUPPART/$BACKUPDIR"
a405c61d 209 echo -n "Remounting backup partition read-only ... "
1733496b
SP
210 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
211 echo "done."
212 else
213 echo "failure!"
b35abb32 214 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
215 echo "Aborting ${BACKUP} System Dump Backup."
216 echo "Aborted."
6a33575a 217 echo "------------------------------------------------------------"
1733496b
SP
218 exit 1
219 fi
9d6d171f
SP
220 echo "Aborting ${BACKUP} System Dump Backup."
221 echo "Aborted."
6a33575a 222 echo "------------------------------------------------------------"
1733496b
SP
223 exit 1
224fi
225
a405c61d 226echo -n "Remounting backup partition read-only ... "
6a33575a 227if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
1733496b
SP
228 echo "done."
229else
230 echo "failure!"
b35abb32 231 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
232 echo "Aborting ${BACKUP} System Dump Backup."
233 echo "Aborted."
6a33575a 234 echo "------------------------------------------------------------"
1733496b
SP
235 exit 1
236fi
237
b35abb32 238echo "### End of ${BACKUP} System Dump Backup ###"
a405c61d 239echo "Done."
6a33575a 240echo "------------------------------------------------------------"
ad80e1e1
SP
241
242## end of script