More improvements from Aaron S. Hawley <ashawley@sourceforge.net>
[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,
4# using the "dump" facility, into the backup partition. It will stop the
5# webserver, and recurse the sites directories making a tar mini-backup of
6# the database dirs. It will the restart the webserver, and start the
7# "dump" backup.
8#
9
10if [ "$2" = "nodumpdate" ]; then
11 UPDATEDDATE=""
12else
13 UPDATEDDATE="-u"
14fi
15
16if [ "$1" = "full" ]; then
17 DLEVEL="0"
18 BTYPE="full"
9d6d171f 19 BACKUP="Full"
1733496b
SP
20elif [ "$1" = "inc" ]; then
21 DLEVEL="1"
22 BTYPE="inc"
9d6d171f 23 BACKUP="Incremental"
1733496b
SP
24else
25 echo "Usage: $0 full|inc [nodumpdate]"
26 exit 1
27fi
28
29
30#
31# Configuration Parameters
32#
33
b35abb32
SP
34BACKUPPART=${BACKUPPART:-"/backup"}
35BACKUPDIR=${BACKUPDIR:-"current"}
1733496b 36DUMPLOGARCH="$BACKUPPART/backup.dump.log.gz"
b35abb32 37FSTODUMP=${FSTODUMP:-/ /var /home /mnt/hdb1 /usr}
1733496b
SP
38DUMPFILESMODE="0644"
39DUMPFILESOWN="root.root"
40
41#
42# Start
43#
44
45echo
46echo "#####################################################################"
9d6d171f 47echo "Starting ${BACKUP} backup."
1733496b
SP
48echo "#####################################################################"
49echo
50
51
52#
9d6d171f 53# Make system backup
1733496b
SP
54#
55
9d6d171f 56echo "### ${BACKUP} System Dump Backup ###"
a405c61d
SP
57echo "Using backup partition: $BACKUPPART"
58echo "Filesystems to dump: $FSTODUMP"
1733496b 59
a405c61d 60echo -n "Remounting backup partition read-write ... "
1733496b
SP
61if ( mount $BACKUPPART -o remount,rw &> /dev/null ) then
62 echo "done."
2e0e9e2e
SP
63elif ( mount $BACKUPPART -o rw &> /dev/null ) then
64 echo "done."
1733496b
SP
65else
66 echo "failure!"
a405c61d 67 echo " There were problems remounting $BACKUPPART in read-write mode!"
9d6d171f
SP
68 echo "Aborting ${BACKUP} System Dump Backup."
69 echo "Aborted."
1733496b
SP
70 echo "-------------------------------------------------------------------------------"
71 exit 1
72fi
73
a405c61d 74echo -n "Checking backup partition for correct dir structure ... "
1733496b
SP
75if [ -d $BACKUPPART/$BACKUPDIR -a -w $BACKUPPART/$BACKUPDIR ]; then
76 echo "done."
77
a405c61d 78 echo -n "Checking backup partition for available space ... "
2e0e9e2e
SP
79 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}'`
80 SAVAILFREE=`df --block-size=1048576 | grep -Fe $BACKUPPART | awk '{printf "%6.0f\n", $4}'`
81 SAVAILDEL=`du -s --block-size=1048576 $BACKUPPART/$BACKUPDIR/. | awk '{printf "%6.0f\n", $1}'`
1733496b
SP
82 SAVAIL=`expr $SAVAILFREE + $SAVAILDEL`
83
84 if [ `expr $SAVAIL - $SREQ` -gt "0" ]; then
85 echo "done."
b35abb32 86 echo "Available: $SAVAIL MB Required: $SREQ MB."
1733496b 87 else
2e0e9e2e 88 echo "not enough space!"
b35abb32
SP
89 echo "There is not enough space left in $BACKUPPART for the backup!"
90 echo "Available: $SAVAIL MB Required: $SREQ MB."
a405c61d 91 echo -n "Remounting backup partition read-only ... "
1733496b
SP
92 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
93 echo "done."
2e0e9e2e
SP
94 elif ( mount $BACKUPPART -o ro &> /dev/null ) then
95 echo "done."
1733496b
SP
96 else
97 echo "failure!"
b35abb32 98 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
99 echo "Aborting ${BACKUP} System Dump Backup."
100 echo "Aborted."
1733496b
SP
101 echo "-------------------------------------------------------------------------------"
102 exit 1
103 fi
9d6d171f
SP
104 echo "Aborting ${BACKUP} System Dump Backup."
105 echo "Aborted."
1733496b
SP
106 echo "-------------------------------------------------------------------------------"
107 exit 1
108 fi
109
a405c61d 110 echo -n "Deleting old files ... "
2e0e9e2e 111 if [ `ls -la $BACKUPPART/$BACKUPDIR/ | wc -l` -gt "3" ]; then
1733496b
SP
112 rm -f $BACKUPPART/$BACKUPDIR/* &> /dev/null
113 echo "done."
114 else
115 echo "no old files to delete."
116 fi
117
a405c61d 118 echo "Dumping filesystems ... "
1733496b
SP
119 for FS in $FSTODUMP
120 do
121 if [ "$FS" = "/" ]; then
122 FSNAME="root"
123 else
2e0e9e2e 124 FSNAME=`echo $FS | tr / _ | cut -b 2-`
1733496b
SP
125 fi
126 sync
b35abb32 127 echo -n "Starting dump of $FSNAME ( $FS ) ... "
1733496b
SP
128 if ( dump -$DLEVEL $UPDATEDDATE -z -M -s 27306 -f $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE. $FS &> $BACKUPPART/$BACKUPDIR/$FSNAME.log ) then
129 echo "done."
130 else
131 echo "problems!"
2e0e9e2e
SP
132 echo "There were problems with the dump of $FSNAME ( $FS )."
133 echo "Check log file at $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz"
b35abb32 134 echo "Also check log archive file $DUMPLOGARCH."
1733496b
SP
135 fi
136 cat $BACKUPPART/$BACKUPDIR/$FSNAME.log |gzip >> $DUMPLOGARCH
137 echo "-------------------------------------------------------------------------------" |gzip >> $DUMPLOGARCH
138 done
139
a405c61d 140 echo -n "Setting ownership and permissions of dump files ... "
1733496b
SP
141 chmod $DUMPFILESMODE $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
142 chown $DUMPFILESOWN $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
143 echo "done."
144
a405c61d 145 echo -n "Compressing dump log files ... "
1733496b
SP
146 gzip $BACKUPPART/$BACKUPDIR/*.log &> /dev/null
147 echo "done."
148 sync
149
150else
151 echo "problems!"
b35abb32 152 echo "There are problems with the directory structure."
2e0e9e2e 153 echo "Check directory: $BACKUPPART/$BACKUPDIR"
a405c61d 154 echo -n "Remounting backup partition read-only ... "
1733496b
SP
155 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
156 echo "done."
157 else
158 echo "failure!"
b35abb32 159 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
160 echo "Aborting ${BACKUP} System Dump Backup."
161 echo "Aborted."
1733496b
SP
162 echo "-------------------------------------------------------------------------------"
163 exit 1
164 fi
9d6d171f
SP
165 echo "Aborting ${BACKUP} System Dump Backup."
166 echo "Aborted."
1733496b
SP
167 echo "-------------------------------------------------------------------------------"
168 exit 1
169fi
170
a405c61d 171echo -n "Remounting backup partition read-only ... "
1733496b
SP
172if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
173 echo "done."
174else
175 echo "failure!"
b35abb32 176 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
177 echo "Aborting ${BACKUP} System Dump Backup."
178 echo "Aborted."
1733496b
SP
179 echo "-------------------------------------------------------------------------------"
180 exit 1
181fi
182
b35abb32 183echo "### End of ${BACKUP} System Dump Backup ###"
a405c61d 184echo "Done."
1733496b 185echo "-------------------------------------------------------------------------------"