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