]> Dogcows Code - chaz/tar/blob - scripts/backup.sh.in
(mt_begin,mt_rewind,mt_offline,mt_status): Use $MT to invoke mt
[chaz/tar] / scripts / backup.sh.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 PROGNAME=`basename $0`
21 CONFIGPATH="$SYSCONFDIR/backup"
22 REMOTEBACKUPDIR="$SYSCONFDIR/tar-backup"
23 CONFIGFILE=${CONFIGPATH}/backup-specs
24 DIRLIST=${CONFIGPATH}/dirs
25 FILELIST=${CONFIGPATH}/files
26 LOGPATH=${CONFIGPATH}/log
27
28 # Default functions for running various magnetic tape commands
29 mt_begin() {
30 $MT -f "$1" retension
31 }
32
33 mt_rewind() {
34 $MT -f "$1" rewind
35 }
36
37 mt_offline() {
38 $MT -f "$1" offl
39 }
40
41 mt_status() {
42 $MT -f "$1" status
43 }
44
45 # The main configuration file may override any of these variables
46 MT_BEGIN=mt_begin
47 MT_REWIND=mt_rewind
48 MT_OFFLINE=mt_offline
49 MT_STATUS=mt_status
50
51 # Insure `mail' is in PATH.
52 PATH="/usr/ucb:${PATH}"
53 export PATH
54 # Put startdate in the subject line of mailed report, since if it happens
55 # to run longer than 24 hours (as may be the case if someone forgets to put
56 # in the next volume of the tape in adequate time), the backup date won't
57 # appear too misleading.
58 startdate="`date`"
59 here="`pwd`"
60 # Save local hostname
61 localhost="`hostname | sed -e 's/\..*//' | tr A-Z a-z`"
62
63 # Produce a diagnostic output
64 message() {
65 if [ "$VERBOSE" != "" ]; then
66 if [ $VERBOSE -ge $1 ]; then
67 shift
68 echo "$@" >&2
69 fi
70 fi
71 }
72
73 # Bail out and exit.
74 bailout() {
75 echo "$PROGNAME: $*" >&2
76 exit 1
77 }
78
79 # Return current date
80 now() {
81 #IF_DATE_FORMAT_OK
82 date +%Y-%m-%d
83 #ELSE_DATE_FORMAT_OK
84 LC_ALL=C date | \
85 sed 's/[^ ]* *\([^ ]*\) *\([^ ]*\).* \([^ ]*\)$/\3-\1-\2/
86 /-[0-9]$/s/\([0-9]\)$/0\1/
87 /Jan/{s/Jan/01/p;q;}
88 /Feb/{s/Feb/02/p;q;}
89 /Mar/{s/Mar/03/p;q;}
90 /Apr/{s/Apr/04/p;q;}
91 /May/{s/May/05/p;q;}
92 /Jun/{s/Jun/06/p;q;}
93 /Jul/{s/Jul/07/p;q;}
94 /Aug/{s/Aug/08/p;q;}
95 /Sep/{s/Sep/09/p;q;}
96 /Oct/{s/Oct/10/p;q;}
97 /Nov/{s/Nov/11/p;q;}
98 /Dec/{s/Dec/12/p;q;}'
99 #ENDIF_DATE_FORMAT_OK
100 }
101
102 # Bail out if we don't have root privileges.
103 test_root() {
104 if [ ! -w ${ROOT_FS-/} ]; then
105 bailout "The backup must be run as root or else some files will fail to be dumped."
106 fi
107 }
108
109 root_fs() {
110 echo "${ROOT_FS}$1" | tr -s /
111 }
112
113 advice() {
114 echo "Directory $1 is not found." >&2
115 cat >&2 <<EOF
116 The following directories and files are needed for the backup to function:
117
118 1. Directory with configuration files and file lists:
119 $CONFIGPATH
120 2. Directory for backup log files
121 $LOGPATH
122 3. Main configuration file
123 $CONFIGFILE
124
125 Please, create these and invoke the script again.
126 EOF
127 }
128
129 init_common() {
130 # Check if the necessary directories exist
131 if [ ! -d $CONFIGPATH ]; then
132 advice $CONFIGPATH
133 exit 1
134 fi
135 if [ ! -d $LOGPATH ]; then
136 if mkdir $LOGPATH; then
137 :
138 else
139 advice $LOGPATH
140 exit 1
141 fi
142 fi
143 # Get the values of BACKUP_DIRS, BACKUP_FILES, and other variables.
144 if [ ! -r $CONFIGFILE ]; then
145 echo "$PROGNAME: cannot read $CONFIGFILE. Stop." >&2
146 exit 1
147 fi
148 . $CONFIGFILE
149
150 # Environment sanity check
151
152 test_root
153
154 if [ x"${ADMINISTRATOR}" = x ]; then
155 bailout "ADMINISTRATOR not defined"
156 fi
157
158 [ x"$TAR" = x ] && TAR=tar
159 [ x"$SLEEP_TIME" = x ] && SLEEP_TIME=60
160
161 if [ x$VOLNO_FILE = x ]; then
162 bailout "VOLNO_FILE not specified"
163 fi
164
165 if [ -r $DIRLIST ]; then
166 BACKUP_DIRS="$BACKUP_DIRS `cat $DIRLIST`"
167 fi
168 if [ -r $FILELIST ]; then
169 BACKUP_FILES="$BACKUP_FILES `cat $FILELIST`"
170 fi
171
172 if [ \( x"$BACKUP_DIRS" = x \) -a \( x"$BACKUP_FILES" = x \) ]; then
173 bailout "Neither BACKUP_DIRS nor BACKUP_FILES specified"
174 fi
175 if [ -n "$RSH" ]; then
176 RSH=rsh
177 MT_RSH_OPTION=
178 else
179 MT_RSH_OPTION="--rsh-command=$RSH"
180 fi
181 if [ -n $TAPE_FILE ]; then
182 TAPE_FILE=/dev/tape
183 fi
184
185 # If TAPE_FILE is a remote device, update mt invocation accordingly
186 case $TAPE_FILE in
187 *:*) MT="$MT $MT_RSH_OPTION";;
188 *) ;;
189 esac
190
191 POSIXLY_CORRECT=1
192 export POSIXLY_CORRECT
193 }
194
195 init_backup() {
196 init_common
197 TAR_PART1="${TAR} -c --format=gnu --multi-volume --one-file-system --sparse --volno-file=${VOLNO_FILE}"
198 if [ "x$XLIST" != x ]; then
199 TAR_PART1="${TAR_PART1} \`test -r $REMOTEBACKUPDIR/$XLIST && echo \"--exclude-from $REMOTEBACKUPDIR/$XLIST\"\`"
200 fi
201 if [ "$RSH_COMMAND" != "" ]; then
202 TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
203 fi
204 if [ x$BLOCKING != x ]; then
205 TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
206 fi
207
208 # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
209 if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
210 TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
211 fi
212 # Set logfile name
213 # Logfile name should be in the form ``log-1993-03-18-level-0''
214 # They go in the directory `@sysconfdir@/log'.
215 # i.e. year-month-date. This format is useful for sorting by name, since
216 # logfiles are intentionally kept online for future reference.
217 LOGFILE="${LOGPATH}/log-`now`-level-${DUMP_LEVEL}"
218 }
219
220 init_restore() {
221 init_common
222 # FIXME: Replace --list with --extract
223 TAR_PART1="${TAR} --extract --multi-volume"
224 if [ "$RSH_COMMAND" != "" ]; then
225 TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
226 fi
227 if [ x$BLOCKING != x ]; then
228 TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
229 fi
230
231 # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
232 if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
233 TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
234 fi
235 LOGFILE="${LOGPATH}/restore-`now`"
236 }
237
238 wait_time() {
239 if [ "${1}" != "now" ]; then
240 if [ "${1}x" != "x" ]; then
241 spec="${1}"
242 else
243 spec="${BACKUP_HOUR}"
244 fi
245
246 pausetime="`date | awk -v spec=\"${spec}\" '
247 BEGIN {
248 split(spec, time, ":")
249 }
250 {
251 split($4, now, ":")
252 diff = 3600 * (time[1] - now[1]) + 60 * (time[2] - now[2]);
253 if (diff < 0)
254 diff += 3600 * 24
255 print diff
256 }'`"
257 clear
258 echo "${SLEEP_MESSAGE}"
259 sleep "${pausetime}"
260 fi
261 }
262
263 level_log_name() {
264 echo "$REMOTEBACKUPDIR/${1}.level-${2-$DUMP_LEVEL}"
265 }
266
267 # Prepare a temporary level logfile
268 # usage: make_level_log HOSTNAME
269 make_level_log() {
270 if [ "z${localhost}" != "z$1" ] ; then
271 $RSH "$1" mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
272 $RSH "$1" rm -f `level_log_name temp`
273 else
274 mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
275 rm -f `level_log_name temp`
276 fi
277 }
278
279 # Rename temporary log
280 # usage: flush_level_log HOSTNAME FSNAME
281 flush_level_log() {
282 message 10 "RENAME: `level_log_name temp` --> `level_log_name $2`"
283 if [ "z${localhost}" != "z$1" ] ; then
284 $RSH "$1" mv -f `level_log_name temp` "`level_log_name $2`"
285 else
286 mv -f `level_log_name temp` "`level_log_name $2`"
287 fi
288 }
289
290 # Return the timestamp of the last backup.
291 # usage: get_dump_time LEVEL
292 get_dump_time() {
293 ls -r ${LOGPATH}/log-*-level-$1 \
294 | head -n 1 \
295 | sed "s,.*log-\(.*\)-level-$1,\1,"
296 }
297
298 # Do actual backup on a host
299 # usage: backup_host HOSTNAME [TAR_ARGUMENTS]
300 backup_host() {
301 message 10 "ARGS: $@"
302 rhost=$1
303 shift
304 if [ "z${localhost}" != "z$rhost" ] ; then
305 $RSH "$rhost" ${TAR_PART1} -f "${localhost}:${TAPE_FILE}" $@
306 else
307 # Using `sh -c exec' causes nested quoting and shell substitution
308 # to be handled here in the same way rsh handles it.
309 CMD="exec ${TAR_PART1} -f \"${TAPE_FILE}\" $@"
310 message 10 "CMD: $CMD"
311 sh -c "$CMD"
312 message 10 "RC: $?"
313 fi
314 }
315
316 print_level() {
317 if [ ${1-$DUMP_LEVEL} -eq 0 ]; then
318 echo "Full"
319 else
320 echo "Level ${1-$DUMP_LEVEL}"
321 fi
322 }
323
324 prev_level() {
325 print_level `expr $DUMP_LEVEL - 1` | tr A-Z a-z
326 }
327
328 remote_run() {
329 rhost=$1
330 shift
331 message 10 "REMOTE $rhost: $@"
332 if [ "x$rhost" != "x${localhost}" ] ; then
333 $RSH "${rhost}" "$@"
334 else
335 $*
336 fi
337 }
338
339 license() {
340 cat - <<EOF
341 This program is part of GNU tar
342 Copyright 2004, Free Software Foundation
343
344 This program is free software; you can redistribute it and/or modify
345 it under the terms of the GNU General Public License as published by
346 the Free Software Foundation; either version 1, or (at your option)
347 any later version.
348
349 This program is distributed in the hope that it will be useful,
350 but WITHOUT ANY WARRANTY; without even the implied warranty of
351 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
352 GNU General Public License for more details.
353
354 You should have received a copy of the GNU General Public License
355 along with this program; if not, write to the Free Software
356 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
357 02110-1301, USA.
358 EOF
359 }
This page took 0.04977 seconds and 5 git commands to generate.