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