d1de13687c41dd9fd5d2290ac3453c9d962dcc9d
[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. 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
10 if [ "$2" = "nodumpdate" ]; then
11 UPDATEDDATE=""
12 else
13 UPDATEDDATE="-u"
14 fi
15
16 if [ "$1" = "full" ]; then
17 DLEVEL="0"
18 BTYPE="full"
19 BACKUP="Full"
20 elif [ "$1" = "inc" ]; then
21 DLEVEL="1"
22 BTYPE="inc"
23 BACKUP="Incremental"
24 else
25 echo "Usage: $0 full|inc [nodumpdate]"
26 exit 1
27 fi
28
29
30 #
31 # Configuration Parameters
32 #
33
34 BACKUPPART=${BACKUPPART:-"/backup"}
35 BACKUPDIR=${BACKUPDIR:-"current"}
36 DUMPLOGARCH="$BACKUPPART/backup.dump.log.gz"
37 FSTODUMP=${FSTODUMP:-/ /var /home /mnt/hdb1 /usr}
38 DUMPFILESMODE="0644"
39 DUMPFILESOWN="root.root"
40
41 #
42 # Start
43 #
44
45 echo
46 echo "#####################################################################"
47 echo "Starting ${BACKUP} backup."
48 echo "#####################################################################"
49 echo
50
51
52 #
53 # Make system backup
54 #
55
56 echo "### ${BACKUP} System Dump Backup ###"
57 echo "Using backup partition: $BACKUPPART"
58 echo "Filesystems to dump: $FSTODUMP"
59
60 echo -n "Remounting backup partition read-write ... "
61 if ( mount $BACKUPPART -o remount,rw &> /dev/null ) then
62 echo "done."
63 else
64 echo "failure!"
65 echo " There were problems remounting $BACKUPPART in read-write mode!"
66 echo "Aborting ${BACKUP} System Dump Backup."
67 echo "Aborted."
68 echo "-------------------------------------------------------------------------------"
69 exit 1
70 fi
71
72 echo -n "Checking backup partition for correct dir structure ... "
73 if [ -d $BACKUPPART/$BACKUPDIR -a -w $BACKUPPART/$BACKUPDIR ]; then
74 echo "done."
75
76 echo -n "Checking backup partition for available space ... "
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."
84 echo "Available: $SAVAIL MB Required: $SREQ MB."
85 else
86 echo "no enough space!"
87 echo "There is not enough space left in $BACKUPPART for the backup!"
88 echo "Available: $SAVAIL MB Required: $SREQ MB."
89 echo -n "Remounting backup partition read-only ... "
90 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
91 echo "done."
92 else
93 echo "failure!"
94 echo "There were problems remounting $BACKUPPART in read-only mode!"
95 echo "Aborting ${BACKUP} System Dump Backup."
96 echo "Aborted."
97 echo "-------------------------------------------------------------------------------"
98 exit 1
99 fi
100 echo "Aborting ${BACKUP} System Dump Backup."
101 echo "Aborted."
102 echo "-------------------------------------------------------------------------------"
103 exit 1
104 fi
105
106 echo -n "Deleting old files ... "
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
114 echo "Dumping filesystems ... "
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
123 echo -n "Starting dump of $FSNAME ( $FS ) ... "
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!"
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."
131 fi
132 cat $BACKUPPART/$BACKUPDIR/$FSNAME.log |gzip >> $DUMPLOGARCH
133 echo "-------------------------------------------------------------------------------" |gzip >> $DUMPLOGARCH
134 done
135
136 echo -n "Setting ownership and permissions of dump files ... "
137 chmod $DUMPFILESMODE $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
138 chown $DUMPFILESOWN $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
139 echo "done."
140
141 echo -n "Compressing dump log files ... "
142 gzip $BACKUPPART/$BACKUPDIR/*.log &> /dev/null
143 echo "done."
144 sync
145
146 else
147 echo "problems!"
148 echo "There are problems with the directory structure."
149 echo "Check dirs: $BACKUPPART/$BACKUPDIR"
150 echo -n "Remounting backup partition read-only ... "
151 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
152 echo "done."
153 else
154 echo "failure!"
155 echo "There were problems remounting $BACKUPPART in read-only mode!"
156 echo "Aborting ${BACKUP} System Dump Backup."
157 echo "Aborted."
158 echo "-------------------------------------------------------------------------------"
159 exit 1
160 fi
161 echo "Aborting ${BACKUP} System Dump Backup."
162 echo "Aborted."
163 echo "-------------------------------------------------------------------------------"
164 exit 1
165 fi
166
167 echo -n "Remounting backup partition read-only ... "
168 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
169 echo "done."
170 else
171 echo "failure!"
172 echo "There were problems remounting $BACKUPPART in read-only mode!"
173 echo "Aborting ${BACKUP} System Dump Backup."
174 echo "Aborted."
175 echo "-------------------------------------------------------------------------------"
176 exit 1
177 fi
178
179 echo "### End of ${BACKUP} System Dump Backup ###"
180 echo "Done."
181 echo "-------------------------------------------------------------------------------"