]> git.cworth.org Git - tar/blob - scripts/backup.in
Imported Upstream version 1.20
[tar] / scripts / backup.in
1 #! /bin/sh
2 # This program is part of GNU tar
3 # Copyright (C) 2004, 2005, 2006 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    -V, --version           Display program version.
42
43    Optional argument WHEN is for backward compatibility only. It has been
44    superseded by --time option.
45    TIME argument can be one of:
46
47         now          --     do backup immediately.
48         HH           --     do backup at HH hours.
49         HH:MM        --     do backup at HH:MM.
50
51 Send bug reports to @PACKAGE_BUGREPORT@.
52 EOF
53 }
54
55 # For compatibility with previous versions, deduce the backup level
56 # from the command name
57 case "$PROGNAME" in
58 level-[0-9]) DUMP_LEVEL=`expr $PROGNAME : 'level-\([0-9][0-9]*\)'`;;
59 esac
60
61 for opt
62 do
63   if [ -z "$prev" ]; then
64       option=$opt
65       optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
66   else
67       option="${prev}=$opt"
68       prev=""
69       optarg=$opt
70   fi
71   case $option in
72   --l=*|--le=*|--lev=*|--leve=*|--level=*)
73          DUMP_LEVEL=$optarg
74          ;;
75   -l|--l|--le|--lev|--leve|--level)
76          prev=$option
77          ;;
78   --verb=*|--verbo=*|--verbos=*|--verbose=*)
79          VERBOSE=$optarg
80          ;;
81   -v|--verb|--verbo|--verbos|--verbose)
82          VERBOSE=100
83          ;;
84   -v*)   VERBOSE=`expr $option : "-v\(.*\)"`;;
85   --t=*|--ti=*|--tim=*|--time=*)
86          TIME=$optarg
87          ;;
88   -t)    prev=--t;;
89   -t*)   TIME=`expr $option : "-t\(.*\)"`;;
90   --t|--ti|--tim|--time)
91          prev=$option
92          ;;
93   -V|--v|--ve|--ver|--vers|--versi|--versio|--version)
94          echo "backup (@PACKAGE_NAME@) @VERSION@"
95          license
96          exit;;
97   -h|--h|--he|--hel|--help)
98          usage
99          exit;;
100   -f|--f|--fo|--for|--forc|--force)
101          FORCE=yes
102          ;;
103   *)     if [ "x$TIME" != "x" ]; then
104             bailout "Extra argument. Try $PROGNAME --help for more info."
105          else
106             TIME=$option
107          fi;;
108   esac
109 done
110
111 if [ "x$TIME" = x ]; then
112     bailout "No backup time specified. Try $PROGNAME --help for more info."
113     exit 1
114 fi
115
116 init_backup
117
118 # Maybe sleep until around specified or default hour.
119 wait_time $TIME
120
121 if [ $DUMP_LEVEL -ne 0 ]; then
122     PREV_LEVEL=`expr $DUMP_LEVEL - 1`
123     PREV_DATE=`ls -t ${LOGPATH}/log-*-level-$PREV_LEVEL|
124                 head -n 1|
125                 sed "s,${LOGPATH}/log-\(.*\)-level.*,\1,"`
126     if [ "x$PREV_DATE" = x ]; then
127         bailout "Can't determine date of the previous backup"
128     fi
129     message 0 "Backup from $PREV_DATE to $NOW"
130 fi
131
132 # start doing things
133
134 # Make sure the log file did not already exist.  Create it.
135
136 if [ "x$FORCE" = "xyes" ]; then
137     rm ${LOGFILE}
138 fi
139
140 if [ -f "${LOGFILE}" ] ; then
141    bailout "Log file ${LOGFILE} already exists."
142 else
143    touch "${LOGFILE}"
144 fi
145 message 1 "Ready for backup."
146 message 10 "TAR invocation: $TAR_PART1"
147 message 20 "Variables:"
148 message 20 "BACKUP_DIRS=$BACKUP_DIRS"
149 message 20 "BACKUP_FILES=$BACKUP_FILES"
150
151 # The buch of commands below is run in a subshell for which all output is
152 # piped through `tee' to the logfile.  Doing this, instead of having
153 # multiple pipelines all over the place, is cleaner and allows access to
154 # the exit value from various commands more easily.
155 (
156  message 1 "preparing tapes"
157  $MT_BEGIN "${TAPE_FILE}"
158  rm -f "${VOLNO_FILE}"
159
160  message 1 "processing backup directories"
161
162  set - ${BACKUP_DIRS}
163  while [ $# -ne 0 ] ; do
164      date="`date`"
165      fs="`echo \"${1}\" | sed -e 's/^.*://'`"
166      fs=`root_fs $fs`
167      fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
168      remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
169      if [ -z "$remotehost" ]; then
170          remotehost=$localhost
171      fi
172
173      echo "Backing up ${1} at ${date}"
174      message 10 "fs=$fs"
175      message 10 "fsname=$fsname"
176      message 10 "remotehost=$remotehost"
177      if [ $DUMP_LEVEL -eq 0 ]; then
178          make_level_log ${remotehost}
179      else
180          echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
181          remote_run "${remotehost}" cp "`level_log_name ${fsname} $PREV_LEVEL`" "`level_log_name temp`"
182      fi
183
184      ${DUMP_BEGIN-:} $DUMP_LEVEL $remotehost $fs $fsname
185      backup_host ${remotehost} \
186                 "--listed=`level_log_name temp`" \
187                 "--label='`print_level` backup of ${fs} on ${remotehost} at ${NOW}'" \
188                 -C ${fs} .
189
190      # `rsh' doesn't exit with the exit status of the remote command.  What
191      # stupid lossage.  TODO: think of a reliable workaround.
192      if [ $? -ne 0 ] ; then
193          echo "Backup of ${1} failed." 1>&2
194          # I'm assuming that the tar will have written an empty
195          # file to the tape, otherwise I should do a cat here.
196      else
197          flush_level_log ${remotehost} ${fsname}
198      fi
199      ${MT_STATUS} "$TAPE_FILE"
200      ${DUMP_END-:} $DUMP_LEVEL $remotehost $fs $fsname
201      echo "sleeping ${SLEEP_TIME} seconds"
202      sleep ${SLEEP_TIME}
203      shift
204  done
205
206  # Dump any individual files requested.
207
208  if [ "x${BACKUP_FILES}" != "x" ] ; then
209      message 1 "processing individual files"
210
211      date="`date`"
212
213      if [ $DUMP_LEVEL -eq 0 ]; then
214          make_level_log $localhost
215      else
216          echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
217          remote_run "${localhost}" cp "`level_log_name MISC $PREV_LEVEL`" "`level_log_name temp`"
218      fi
219
220      echo "Backing up miscellaneous files at ${date}"
221
222      ${DUMP_BEGIN-:} $DUMP_LEVEL $localhost MISC MISC
223      backup_host $localhost \
224                "--listed=`level_log_name temp`"\
225                "--label='`print_level` backup of miscellaneous files at ${NOW}'" \
226                ${BACKUP_FILES}
227
228      if [ $? -ne 0 ] ; then
229          echo "Backup of miscellaneous files failed."
230          # I'm assuming that the tar will have written an empty
231          # file to the tape, otherwise I should do a cat here.
232      else
233          flush_level_log $localhost MISC
234      fi
235      ${MT_STATUS} "$TAPE_FILE"
236      ${DUMP_END-:} $DUMP_LEVEL $localhost MISC MISC
237  else
238      echo "No miscellaneous files specified"
239  fi
240
241  message 1 "final cleanup"
242
243  $MT_REWIND "${TAPE_FILE}"
244  $MT_OFFLINE "${TAPE_FILE}"
245  echo "."
246 ) 2>&1 | tee -a "${LOGFILE}"
247
248 if test "${ADMINISTRATOR}" != NONE; then
249      echo "Sending the dump log to ${ADMINISTRATOR}"
250      mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"
251 fi
252
253 # EOF