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