re-mark 1.29b-2 as not yet uploaded (merge madness!)
[debian/tar] / scripts / backup.in
1 #! /bin/sh
2 # Make backups.
3
4 # Copyright 2004-2006, 2013 Free Software Foundation
5
6 # This file is part of GNU tar.
7
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.
12
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.
17
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/>.
20
21 # Load library routines
22 SYSCONFDIR=${SYSCONFDIR-@sysconfdir@}
23 . ${LIBDIR-@libexecdir@}/backup.sh
24
25 DUMP_LEVEL=0
26 TIME=
27 NOW=`now`
28
29 usage() {
30         cat - <<EOF
31 usage: $PROGNAME [OPTIONS] [WHEN]
32 Options are:
33
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.
39
40 Informational options:
41    -h, --help              Display this help message.
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?*)  DUMP_LEVEL=`expr $option : '-l\(.*\)'`;;
77   -l|--l|--le|--lev|--leve|--level)
78          prev=--level
79          ;;
80   --verb=*|--verbo=*|--verbos=*|--verbose=*)
81          VERBOSE=$optarg
82          ;;
83   -v|--verb|--verbo|--verbos|--verbose)
84          VERBOSE=100
85          ;;
86   -v*)   VERBOSE=`expr $option : '-v\(.*\)'`;;
87   --t=*|--ti=*|--tim=*|--time=*)
88          TIME=$optarg
89          ;;
90   -t?*)  TIME=`expr $option : '-t\(.*\)'`;;
91   -t|--t|--ti|--tim|--time)
92          prev=--time
93          ;;
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
103          ;;
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
111
112 if [ "x$TIME" = x ]; then
113     bailout "No backup time specified. Try $PROGNAME --help for more info."
114     exit 1
115 fi
116
117 init_backup
118
119 # Maybe sleep until around specified or default hour.
120 wait_time $TIME
121
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"
129     fi
130     message 0 "Backup from $PREV_DATE to $NOW"
131 fi
132
133 # start doing things
134
135 # Make sure the log file did not already exist.  Create it.
136
137 if [ "x$FORCE" = "xyes" ]; then
138     rm ${LOGFILE}
139 fi
140
141 if [ -f "${LOGFILE}" ] ; then
142    bailout "Log file ${LOGFILE} already exists."
143 else
144    touch "${LOGFILE}"
145 fi
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"
151
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.
156 (
157  message 1 "preparing tapes"
158  $MT_BEGIN "${TAPE_FILE}"
159  rm -f "${VOLNO_FILE}"
160
161  message 1 "processing backup directories"
162
163  set - ${BACKUP_DIRS}
164  while [ $# -ne 0 ] ; do
165      date="`date`"
166      fs="`echo \"${1}\" | sed -e 's/^.*://'`"
167      fs=`root_fs $fs`
168      fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
169      remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
170      if [ -z "$remotehost" ]; then
171          remotehost=$localhost
172      fi
173
174      echo "Backing up ${1} at ${date}"
175      message 10 "fs=$fs"
176      message 10 "fsname=$fsname"
177      message 10 "remotehost=$remotehost"
178      if [ $DUMP_LEVEL -eq 0 ]; then
179          make_level_log ${remotehost}
180      else
181          echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
182          remote_run "${remotehost}" cp "`level_log_name ${fsname} $PREV_LEVEL`" "`level_log_name temp`"
183      fi
184
185      ${DUMP_BEGIN-:} $DUMP_LEVEL $remotehost $fs $fsname
186      backup_host ${remotehost} \
187                 "--listed=`level_log_name temp`" \
188                 "--label='`print_level` backup of ${fs} on ${remotehost} at ${NOW}'" \
189                 -C ${fs} .
190
191      # 'rsh' doesn't exit with the exit status of the remote command.  What
192      # stupid lossage.  TODO: think of a reliable workaround.
193      if [ $? -ne 0 ] ; then
194          echo "Backup of ${1} failed." 1>&2
195          # I'm assuming that the tar will have written an empty
196          # file to the tape, otherwise I should do a cat here.
197      else
198          flush_level_log ${remotehost} ${fsname}
199      fi
200      ${MT_STATUS} "$TAPE_FILE"
201      ${DUMP_END-:} $DUMP_LEVEL $remotehost $fs $fsname
202      echo "sleeping ${SLEEP_TIME} seconds"
203      sleep ${SLEEP_TIME}
204      shift
205  done
206
207  # Dump any individual files requested.
208
209  if [ "x${BACKUP_FILES}" != "x" ] ; then
210      message 1 "processing individual files"
211
212      date="`date`"
213
214      if [ $DUMP_LEVEL -eq 0 ]; then
215          make_level_log $localhost
216      else
217          echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
218          remote_run "${localhost}" cp "`level_log_name MISC $PREV_LEVEL`" "`level_log_name temp`"
219      fi
220
221      echo "Backing up miscellaneous files at ${date}"
222
223      ${DUMP_BEGIN-:} $DUMP_LEVEL $localhost MISC MISC
224      backup_host $localhost \
225                "--listed=`level_log_name temp`"\
226                "--label='`print_level` backup of miscellaneous files at ${NOW}'" \
227                ${BACKUP_FILES}
228
229      if [ $? -ne 0 ] ; then
230          echo "Backup of miscellaneous files failed."
231          # I'm assuming that the tar will have written an empty
232          # file to the tape, otherwise I should do a cat here.
233      else
234          flush_level_log $localhost MISC
235      fi
236      ${MT_STATUS} "$TAPE_FILE"
237      ${DUMP_END-:} $DUMP_LEVEL $localhost MISC MISC
238  else
239      echo "No miscellaneous files specified"
240  fi
241
242  message 1 "final cleanup"
243
244  $MT_REWIND "${TAPE_FILE}"
245  $MT_OFFLINE "${TAPE_FILE}"
246  echo "."
247 ) 2>&1 | tee -a "${LOGFILE}"
248
249 if test "${ADMINISTRATOR}" != NONE; then
250      echo "Sending the dump log to ${ADMINISTRATOR}"
251      mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"
252 fi
253
254 # EOF