orphan
[debian/dump] / examples / cron_dump_to_disk / backup
1 #!/bin/bash
2
3 ###
4  # Copyright (C) 2001  Eugenio Diaz <getnito@yahoo.com>
5  #
6  # Redistribution and use in source and binary forms, with or without
7  # modification, are permitted provided that the following conditions
8  # are met:
9  #
10  # 1. Redistributions of source code must retain the above copyright
11  #    notice, this list of conditions and the following disclaimer.
12  # 2. Redistributions in binary form must reproduce the above
13  #    copyright notice, this list of conditions and the following
14  #    disclaimer in the documentation and/or other materials provided
15  #    with the distribution.
16  # 3. Neither the name of the University nor the names of its
17  #    contributors may be used to endorse or promote products derived
18  #    from this software without specific prior written permission.
19  #
20  # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS''
21  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23  # PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS
24  # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
27  # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28  # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30  # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  # SUCH DAMAGE.
32  #
33  ##
34  #
35  # This script will make a simple backup of the most critical
36  # partitions, using the "dump" facility, into the backup partition.
37  #
38  # Run the following to make a complete backup:
39  #
40  # $ su -c "backup full"
41  #
42  # Run the following to make an incremental backup:
43  #
44  # $ su -c "backup inc"
45  #
46  # Add "nodumpdate" to run a backup but not effect the backup state
47  # recorded in the file /var/lib/dumpdates and reported by dump -W:
48  #
49  # $ su -c "backup full nodumpdate"
50  #
51  ##
52
53 if [ "$2" = "nodumpdate" ]; then
54    UPDATEDDATE=""
55 else
56    UPDATEDDATE="-u"
57 fi
58
59 if [ "$1" = "full" ]; then
60    DLEVEL="0"
61    BTYPE="full"
62    BACKUP="Full"
63 elif [ "$1" = "inc" ]; then
64    DLEVEL="1"
65    BTYPE="inc"
66    BACKUP="Incremental"
67 else
68    echo "Usage: $0 full|inc [nodumpdate]"
69    exit 1
70 fi
71
72
73 #
74 # Configuration Parameters
75 #
76
77 BACKUPPART=${BACKUPPART:-"/backup"}
78 BACKUPDIR=${BACKUPDIR:-"current"}
79 DUMPLOGARCH="$BACKUPPART/backup.dump.log.gz"
80 FSTODUMP=${FSTODUMP:-/ /var /home /mnt/hdb1 /usr}
81 DUMPFILESMODE="0644"
82 DUMPFILESOWN="root.root"
83
84 #
85 # Start
86 #
87
88 echo
89 echo "############################################################"
90 echo "Starting ${BACKUP} backup"
91 echo "############################################################"
92 echo
93
94
95 #
96 #  Make system backup
97 #
98
99 echo "### ${BACKUP} System Dump Backup ###"
100 echo "Using backup partition: $BACKUPPART"
101 echo "Filesystems to dump: $FSTODUMP"
102
103 echo -n "Remounting backup partition read-write ... "
104 if ( mount $BACKUPPART -o remount,rw &> /dev/null ) then
105    echo "done."
106 elif ( mount $BACKUPPART -o rw &> /dev/null ) then
107    echo "done."
108 else
109    echo "failure!"
110    echo "There were problems remounting $BACKUPPART in read-write mode!"
111    echo "Aborting ${BACKUP} System Dump Backup."
112    echo "Aborted."
113    echo "------------------------------------------------------------"
114    exit 1
115 fi
116
117 echo -n "Checking directory structure on backup partition ... "
118 if [ -d $BACKUPPART/$BACKUPDIR -a -w $BACKUPPART/$BACKUPDIR ]; then
119    echo "done."
120
121    echo -n "Checking backup partition for available space ... "
122    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}')
123    SAVAILFREE=$(df --block-size=1048576 | grep -Fe $BACKUPPART | awk '{printf "%6.0f\n", $4}')
124    SAVAILDEL=$(du -s --block-size=1048576 $BACKUPPART/$BACKUPDIR/. | awk '{printf "%6.0f\n", $1}')
125    SAVAIL=$(( $SAVAILFREE + $SAVAILDEL ))
126
127    if [ "$(( $SAVAIL - $SREQ ))" -gt "0" ]; then
128       echo "done."
129       echo "Available: $SAVAIL MB Required: $SREQ MB."
130    else
131       echo "not enough space!"
132       echo "There is not enough space left in $BACKUPPART for the backup!"
133       echo "Available: $SAVAIL MB Required: $SREQ MB."
134       echo -n "Remounting backup partition read-only ... "
135       if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
136          echo "done."
137       elif ( mount $BACKUPPART -o ro &> /dev/null ) then
138          echo "done."
139       else
140          echo "failure!"
141          echo "There were problems remounting $BACKUPPART in read-only mode!"
142          echo "Aborting ${BACKUP} System Dump Backup."
143          echo "Aborted."
144          echo "------------------------------------------------------------"
145          exit 1
146       fi
147       echo "Aborting ${BACKUP} System Dump Backup."
148       echo "Aborted."
149       echo "------------------------------------------------------------"
150       exit 1
151    fi
152
153    echo -n "Deleting old files ... "
154    if [ "$(ls -la $BACKUPPART/$BACKUPDIR/ | wc -l)" -gt "3" ]; then
155       rm -f $BACKUPPART/$BACKUPDIR/* &> /dev/null
156       echo "done."
157    else
158       echo "no old files to delete."
159    fi
160
161    echo "Dumping filesystems ... "
162    for FS in $FSTODUMP
163    do
164       if [ "$FS" = "/" ]; then
165          FSNAME="root"
166       else
167          FSNAME="$(echo $FS | tr / _ | cut -b 2-)"
168       fi
169       sync
170       TODAY="$(date +%a%Y%m%d)"
171       echo -n "Starting dump of $FSNAME ( $FS ) ... "
172       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
173          echo "done."
174       else
175          echo "problems!"
176          echo "There were problems with the dump of $FSNAME ( $FS )."
177          echo "Check log file at $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz"
178          echo "Also check log archive file $DUMPLOGARCH."
179       fi
180
181       echo -n "Verifying dump of $FSNAME ( $FS ) ... "
182       echo "------------------------------------------------------------" >> $BACKUPPART/$BACKUPDIR/$FSNAME.log
183       echo "Result of dump for $FSNAME ( $FS ):" >> $BACKUPPART/$BACKUPDIR/$FSNAME.log
184       if ( restore -C -M -f $BACKUPPART/$BACKUPDIR/$FSNAME.$BTYPE. >> $BACKUPPART/$BACKUPDIR/$FSNAME.log 2>&1 ) then
185          echo "done."
186       else
187          echo "problems!"
188          echo "There were problems verifying the dump of $FSNAME ( $FS )."
189          echo "Check log file $BACKUPPART/$BACKUPDIR/$FSNAME.log.gz for more info"
190       fi
191       gzip < $BACKUPPART/$BACKUPDIR/$FSNAME.log >> $DUMPLOGARCH
192       echo "------------------------------------------------------------" | gzip >> $DUMPLOGARCH
193    done
194
195    echo -n "Setting ownership and permissions of dump files ... "
196    chmod $DUMPFILESMODE $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
197    chown $DUMPFILESOWN $BACKUPPART/$BACKUPDIR/* $DUMPLOGARCH &> /dev/null
198    echo "done."
199
200    echo -n "Compressing dump log files ... "
201    gzip $BACKUPPART/$BACKUPDIR/*.log &> /dev/null
202    echo "done."
203    sync
204
205 else
206    echo "problems!"
207    echo "There are problems with the directory structure."
208    echo "Check directory: $BACKUPPART/$BACKUPDIR"
209    echo -n "Remounting backup partition read-only ... "
210    if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
211       echo "done."
212    else
213       echo "failure!"
214       echo "There were problems remounting $BACKUPPART in read-only mode!"
215       echo "Aborting ${BACKUP} System Dump Backup."
216       echo "Aborted."
217       echo "------------------------------------------------------------"
218       exit 1
219    fi
220    echo "Aborting ${BACKUP} System Dump Backup."
221    echo "Aborted."
222    echo "------------------------------------------------------------"
223    exit 1
224 fi
225
226 echo -n "Remounting backup partition read-only ... "
227 if ( mount $BACKUPPART -o remount,ro &> /dev/null ) then
228    echo "done."
229 else
230    echo "failure!"
231    echo "There were problems remounting $BACKUPPART in read-only mode!"
232    echo "Aborting ${BACKUP} System Dump Backup."
233    echo "Aborted."
234    echo "------------------------------------------------------------"
235    exit 1
236 fi
237
238 echo "### End of ${BACKUP} System Dump Backup ###"
239 echo "Done."
240 echo "------------------------------------------------------------"
241
242 ## end of script