]> Dogcows Code - chaz/tar/blob - scripts/backup.in
Update
[chaz/tar] / scripts / backup.in
1 #! /bin/sh
2 # This program is part of GNU tar
3 # Copyright 2004, 2005, Free Software Foundation
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 1, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 # 02110-1301, USA.
19
20 # Load library routines
21 SYSCONFDIR=${SYSCONFDIR-@sysconfdir@}
22 . ${LIBDIR-@libexecdir@}/backup.sh
23
24 DUMP_LEVEL=0
25 TIME=
26 NOW=`now`
27
28 usage() {
29 cat - <<EOF
30 usage: $PROGNAME [OPTIONS] [WHEN]
31 Options are:
32
33 -l, --level=LEVEL Do backup level LEVEL (default $DUMP_LEVEL).
34 -f, --force Force backup even if today's log file already
35 exists.
36 -v, --verbose[=LEVEL] Set verbosity level. Default 100.
37 -t, --time=TIME Wait till TIME, then do backup.
38
39 Informational options:
40 -h, --help Display this help message.
41 -L, --license Display program license.
42 -V, --version Display program version.
43
44 Optional argument WHEN is for backward compatibility only. It has been
45 superseded by --time option.
46 TIME argument can be one of:
47
48 now -- do backup immediately.
49 HH -- do backup at HH hours.
50 HH:MM -- do backup at HH:MM.
51
52 Send bug reports to @PACKAGE_BUGREPORT@.
53 EOF
54 }
55
56 # For compatibility with previous versions, deduce the backup level
57 # from the command name
58 case "$PROGNAME" in
59 level-[0-9]) DUMP_LEVEL=`expr $PROGNAME : 'level-\([0-9][0-9]*\)'`;;
60 esac
61
62 for opt
63 do
64 if [ -z "$prev" ]; then
65 option=$opt
66 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
67 else
68 option="${prev}=$opt"
69 prev=""
70 optarg=$opt
71 fi
72 case $option in
73 --l=*|--le=*|--lev=*|--leve=*|--level=*)
74 DUMP_LEVEL=$optarg
75 ;;
76 -l|--l|--le|--lev|--leve|--level)
77 prev=$option
78 ;;
79 --verb=*|--verbo=*|--verbos=*|--verbose=*)
80 VERBOSE=$optarg
81 ;;
82 -v|--verb|--verbo|--verbos|--verbose)
83 VERBOSE=100
84 ;;
85 -v*) VERBOSE=`expr $option : "-v\(.*\)"`;;
86 --t=*|--ti=*|--tim=*|--time=*)
87 TIME=$optarg
88 ;;
89 -t) prev=--t;;
90 -t*) TIME=`expr $option : "-t\(.*\)"`;;
91 --t|--ti|--tim|--time)
92 prev=$option
93 ;;
94 -V|--v|--ve|--ver|--vers|--versi|--versio|--version)
95 echo "backup (@PACKAGE@ @VERSION@)"
96 exit 0;;
97 -L|--li|--lic|--lice|--licen|--licens|--license)
98 license
99 exit;;
100 -h|--h|--he|--hel|--help)
101 usage
102 exit;;
103 -f|--f|--fo|--for|--forc|--force)
104 FORCE=yes
105 ;;
106 *) if [ "x$TIME" != "x" ]; then
107 bailout "Extra argument. Try $PROGNAME --help for more info."
108 else
109 TIME=$option
110 fi;;
111 esac
112 done
113
114 if [ "x$TIME" = x ]; then
115 bailout "No backup time specified. Try $PROGNAME --help for more info."
116 exit 1
117 fi
118
119 init_backup
120
121 # Maybe sleep until around specified or default hour.
122 wait_time $TIME
123
124 if [ $DUMP_LEVEL -ne 0 ]; then
125 PREV_LEVEL=`expr $DUMP_LEVEL - 1`
126 PREV_DATE=`ls -t ${LOGPATH}/log-*-level-$PREV_LEVEL|
127 head -n 1|
128 sed "s,${LOGPATH}/log-\(.*\)-level.*,\1,"`
129 if [ "x$PREV_DATE" = x ]; then
130 bailout "Can't determine date of the previous backup"
131 fi
132 message 0 "Backup from $PREV_DATE to $NOW"
133 fi
134
135 # start doing things
136
137 # Make sure the log file did not already exist. Create it.
138
139 if [ "x$FORCE" = "xyes" ]; then
140 rm ${LOGFILE}
141 fi
142
143 if [ -f "${LOGFILE}" ] ; then
144 bailout "Log file ${LOGFILE} already exists."
145 else
146 touch "${LOGFILE}"
147 fi
148 message 1 "Ready for backup."
149 message 10 "TAR invocation: $TAR_PART1"
150 message 20 "Variables:"
151 message 20 "BACKUP_DIRS=$BACKUP_DIRS"
152 message 20 "BACKUP_FILES=$BACKUP_FILES"
153
154 # The buch of commands below is run in a subshell for which all output is
155 # piped through `tee' to the logfile. Doing this, instead of having
156 # multiple pipelines all over the place, is cleaner and allows access to
157 # the exit value from various commands more easily.
158 (
159 message 1 "preparing tapes"
160 $MT_BEGIN "${TAPE_FILE}"
161 rm -f "${VOLNO_FILE}"
162
163 message 1 "processing backup directories"
164
165 set - ${BACKUP_DIRS}
166 while [ $# -ne 0 ] ; do
167 date="`date`"
168 fs="`echo \"${1}\" | sed -e 's/^.*://'`"
169 fs=`root_fs $fs`
170 fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
171 remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
172 if [ -z "$remotehost" ]; then
173 remotehost=$localhost
174 fi
175
176 echo "Backing up ${1} at ${date}"
177 message 10 "fs=$fs"
178 message 10 "fsname=$fsname"
179 message 10 "remotehost=$remotehost"
180 if [ $DUMP_LEVEL -eq 0 ]; then
181 make_level_log ${remotehost}
182 else
183 echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
184 remote_run "${remotehost}" cp "`level_log_name ${fsname} $PREV_LEVEL`" "`level_log_name temp`"
185 fi
186
187 ${DUMP_BEGIN-:} $DUMP_LEVEL $remotehost $fs $fsname
188 backup_host ${remotehost} \
189 "--listed=`level_log_name temp`" \
190 "--label='`print_level` backup of ${fs} on ${remotehost} at ${NOW}'" \
191 -C ${fs} .
192
193 # `rsh' doesn't exit with the exit status of the remote command. What
194 # stupid lossage. TODO: think of a reliable workaround.
195 if [ $? -ne 0 ] ; then
196 echo "Backup of ${1} failed." 1>&2
197 # I'm assuming that the tar will have written an empty
198 # file to the tape, otherwise I should do a cat here.
199 else
200 flush_level_log ${remotehost} ${fsname}
201 fi
202 ${MT_STATUS} "$TAPE_FILE"
203 ${DUMP_END-:} $DUMP_LEVEL $remotehost $fs $fsname
204 echo "sleeping ${SLEEP_TIME} seconds"
205 sleep ${SLEEP_TIME}
206 shift
207 done
208
209 # Dump any individual files requested.
210
211 if [ "x${BACKUP_FILES}" != "x" ] ; then
212 message 1 "processing individual files"
213
214 date="`date`"
215
216 if [ $DUMP_LEVEL -eq 0 ]; then
217 make_level_log $localhost
218 else
219 echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
220 remote_run "${localhost}" cp "`level_log_name MISC $PREV_LEVEL`" "`level_log_name temp`"
221 fi
222
223 echo "Backing up miscellaneous files at ${date}"
224
225 ${DUMP_BEGIN-:} $DUMP_LEVEL $localhost MISC MISC
226 backup_host $localhost \
227 "--listed=`level_log_name temp`"\
228 "--label='`print_level` backup of miscellaneous files at ${NOW}'" \
229 ${BACKUP_FILES}
230
231 if [ $? -ne 0 ] ; then
232 echo "Backup of miscellaneous files failed."
233 # I'm assuming that the tar will have written an empty
234 # file to the tape, otherwise I should do a cat here.
235 else
236 flush_level_log $localhost MISC
237 fi
238 ${MT_STATUS} "$TAPE_FILE"
239 ${DUMP_END-:} $DUMP_LEVEL $localhost MISC MISC
240 else
241 echo "No miscellaneous files specified"
242 fi
243
244 message 1 "final cleanup"
245
246 $MT_REWIND "${TAPE_FILE}"
247 $MT_OFFLINE "${TAPE_FILE}"
248 echo "."
249 ) 2>&1 | tee -a "${LOGFILE}"
250
251 if test "${ADMINISTRATOR}" != NONE; then
252 echo "Sending the dump log to ${ADMINISTRATOR}"
253 mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"
254 fi
255
256 # EOF
This page took 0.046078 seconds and 4 git commands to generate.