Improvements to cron_dump_to_disk
[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."
63else
64 echo "failure!"
a405c61d 65 echo " There were problems remounting $BACKUPPART in read-write mode!"
9d6d171f
SP
66 echo "Aborting ${BACKUP} System Dump Backup."
67 echo "Aborted."
1733496b
SP
68 echo "-------------------------------------------------------------------------------"
69 exit 1
70fi
71
a405c61d 72echo -n "Checking backup partition for correct dir structure ... "
1733496b
SP
73if [ -d $BACKUPPART/$BACKUPDIR -a -w $BACKUPPART/$BACKUPDIR ]; then
74 echo "done."
75
a405c61d 76 echo -n "Checking backup partition for available space ... "
1733496b
SP
77 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}'`
78 SAVAILFREE=`df --block-size=1048576 |grep -Ew $BACKUPPART|awk '{printf "%6.0f\n", $4}'`
79 SAVAILDEL=`du -s --block-size=1048576 $BACKUPPART/$BACKUPDIR/. |awk '{printf "%6.0f\n", $1}'`
80 SAVAIL=`expr $SAVAILFREE + $SAVAILDEL`
81
82 if [ `expr $SAVAIL - $SREQ` -gt "0" ]; then
83 echo "done."
b35abb32 84 echo "Available: $SAVAIL MB Required: $SREQ MB."
1733496b
SP
85 else
86 echo "no enough space!"
b35abb32
SP
87 echo "There is not enough space left in $BACKUPPART for the backup!"
88 echo "Available: $SAVAIL MB Required: $SREQ MB."
a405c61d 89 echo -n "Remounting backup partition read-only ... "
1733496b
SP
90 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
91 echo "done."
92 else
93 echo "failure!"
b35abb32 94 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
95 echo "Aborting ${BACKUP} System Dump Backup."
96 echo "Aborted."
1733496b
SP
97 echo "-------------------------------------------------------------------------------"
98 exit 1
99 fi
9d6d171f
SP
100 echo "Aborting ${BACKUP} System Dump Backup."
101 echo "Aborted."
1733496b
SP
102 echo "-------------------------------------------------------------------------------"
103 exit 1
104 fi
105
a405c61d 106 echo -n "Deleting old files ... "
1733496b
SP
107 if [ `ls -la $BACKUPPART/$BACKUPDIR/|wc -l` -gt "3" ]; then
108 rm -f $BACKUPPART/$BACKUPDIR/* &> /dev/null
109 echo "done."
110 else
111 echo "no old files to delete."
112 fi
113
a405c61d 114 echo "Dumping filesystems ... "
1733496b
SP
115 for FS in $FSTODUMP
116 do
117 if [ "$FS" = "/" ]; then
118 FSNAME="root"
119 else
120 FSNAME=`echo $FS|tr / _|cut -b 2-`
121 fi
122 sync
b35abb32 123 echo -n "Starting dump of $FSNAME ( $FS ) ... "
1733496b
SP
124 if ( dump -$DLEVEL $UPDATEDDATE -z -M -s 27306 -f $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE. $FS &> $BACKUPPART/$BACKUPDIR/$FSNAME.log ) then
125 echo "done."
126 else
127 echo "problems!"
b35abb32
SP
128 echo "There where problems with the dump of $FSNAME ( $FS )."
129 echo "Check logfile $BACKUPPART/$BACKUPDIR/$FSNAME.log for more info"
130 echo "Also check log archive file $DUMPLOGARCH."
1733496b
SP
131 fi
132 cat $BACKUPPART/$BACKUPDIR/$FSNAME.log |gzip >> $DUMPLOGARCH
133 echo "-------------------------------------------------------------------------------" |gzip >> $DUMPLOGARCH
134 done
135
a405c61d 136 echo -n "Setting ownership and permissions of dump files ... "
1733496b
SP
137 chmod $DUMPFILESMODE $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
138 chown $DUMPFILESOWN $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
139 echo "done."
140
a405c61d 141 echo -n "Compressing dump log files ... "
1733496b
SP
142 gzip $BACKUPPART/$BACKUPDIR/*.log &> /dev/null
143 echo "done."
144 sync
145
146else
147 echo "problems!"
b35abb32
SP
148 echo "There are problems with the directory structure."
149 echo "Check dirs: $BACKUPPART/$BACKUPDIR"
a405c61d 150 echo -n "Remounting backup partition read-only ... "
1733496b
SP
151 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
152 echo "done."
153 else
154 echo "failure!"
b35abb32 155 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
156 echo "Aborting ${BACKUP} System Dump Backup."
157 echo "Aborted."
1733496b
SP
158 echo "-------------------------------------------------------------------------------"
159 exit 1
160 fi
9d6d171f
SP
161 echo "Aborting ${BACKUP} System Dump Backup."
162 echo "Aborted."
1733496b
SP
163 echo "-------------------------------------------------------------------------------"
164 exit 1
165fi
166
a405c61d 167echo -n "Remounting backup partition read-only ... "
1733496b
SP
168if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
169 echo "done."
170else
171 echo "failure!"
b35abb32 172 echo "There were problems remounting $BACKUPPART in read-only mode!"
9d6d171f
SP
173 echo "Aborting ${BACKUP} System Dump Backup."
174 echo "Aborted."
1733496b
SP
175 echo "-------------------------------------------------------------------------------"
176 exit 1
177fi
178
b35abb32 179echo "### End of ${BACKUP} System Dump Backup ###"
a405c61d 180echo "Done."
1733496b 181echo "-------------------------------------------------------------------------------"