]> Dogcows Code - chaz/tar/blob - scripts/backup.sh.in
(test_root): Append / to ROOT_FS unless it already ends in it.
[chaz/tar] / scripts / backup.sh.in
1 #! /bin/sh
2 # This program is part of GNU tar
3 # Copyright 2004, 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., 59 Temple Place - Suite 330, Boston, MA
18 # 02111-1307, 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_offl
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 case "${ROOT_FS}" in
108 */) ;;
109 *) ROOT_FS="${ROOT_FS}/"
110 esac
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 [ "$RSH" = "" ]; then
176 RSH=rsh
177 fi
178 POSIXLY_CORRECT=1
179 export POSIXLY_CORRECT
180 }
181
182 init_backup() {
183 init_common
184 TAR_PART1="${TAR} -c --format=gnu --multi-volume --one-file-system --sparse --volno-file=${VOLNO_FILE}"
185 if [ "x$XLIST" != x ]; then
186 TAR_PART1="${TAR_PART1} \`test -r $REMOTEBACKUPDIR/$XLIST && echo \"--exclude-from $REMOTEBACKUPDIR/$XLIST\"\`"
187 fi
188 if [ "$RSH_COMMAND" != "" ]; then
189 TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
190 fi
191 if [ x$BLOCKING != x ]; then
192 TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
193 fi
194
195 # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
196 if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
197 TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
198 fi
199 # Set logfile name
200 # Logfile name should be in the form ``log-1993-03-18-level-0''
201 # They go in the directory `@sysconfdir@/log'.
202 # i.e. year-month-date. This format is useful for sorting by name, since
203 # logfiles are intentionally kept online for future reference.
204 LOGFILE="${LOGPATH}/log-`now`-level-${DUMP_LEVEL}"
205 }
206
207 init_restore() {
208 init_common
209 # FIXME: Replace --list with --extract
210 TAR_PART1="${TAR} --extract --multi-volume"
211 if [ "$RSH_COMMAND" != "" ]; then
212 TAR_PART1="${TAR_PART1} --rsh-command=$RSH_COMMAND"
213 fi
214 if [ x$BLOCKING != x ]; then
215 TAR_PART1="${TAR_PART1} --blocking=${BLOCKING}"
216 fi
217
218 # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
219 if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
220 TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
221 fi
222 LOGFILE="${LOGPATH}/restore-`now`"
223 }
224
225 wait_time() {
226 if [ "${1}" != "now" ]; then
227 if [ "${1}x" != "x" ]; then
228 spec="${1}"
229 else
230 spec="${BACKUP_HOUR}"
231 fi
232
233 pausetime="`date | awk -v spec=\"${spec}\" '
234 BEGIN {
235 split(spec, time, ":")
236 }
237 {
238 split($4, now, ":")
239 diff = 3600 * (time[1] - now[1]) + 60 * (time[2] - now[2]);
240 if (diff < 0)
241 diff += 3600 * 24
242 print diff
243 }'`"
244 clear
245 echo "${SLEEP_MESSAGE}"
246 sleep "${pausetime}"
247 fi
248 }
249
250 level_log_name() {
251 echo "$REMOTEBACKUPDIR/${1}.level-${2-$DUMP_LEVEL}"
252 }
253
254 # Prepare a temporary level logfile
255 # usage: make_level_log HOSTNAME
256 make_level_log() {
257 if [ "z${localhost}" != "z$1" ] ; then
258 $RSH "$1" mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
259 $RSH "$1" rm -f `level_log_name temp`
260 else
261 mkdir $REMOTEBACKUPDIR > /dev/null 2>&1
262 rm -f `level_log_name temp`
263 fi
264 }
265
266 # Rename temporary log
267 # usage: flush_level_log HOSTNAME FSNAME
268 flush_level_log() {
269 message 10 "RENAME: `level_log_name temp` --> `level_log_name $2`"
270 if [ "z${localhost}" != "z$1" ] ; then
271 $RSH "$1" mv -f `level_log_name temp` "`level_log_name $2`"
272 else
273 mv -f `level_log_name temp` "`level_log_name $2`"
274 fi
275 }
276
277 # Return the timestamp of the last backup.
278 # usage: get_dump_time LEVEL
279 get_dump_time() {
280 ls -r ${LOGPATH}/log-*-level-$1 \
281 | head -1 \
282 | sed "s,.*log-\(.*\)-level-$1,\1,"
283 }
284
285 # Do actual backup on a host
286 # usage: backup_host HOSTNAME [TAR_ARGUMENTS]
287 backup_host() {
288 message 10 "ARGS: $@"
289 rhost=$1
290 shift
291 if [ "z${localhost}" != "z$rhost" ] ; then
292 $RSH "$rhost" ${TAR_PART1} -f "${localhost}:${TAPE_FILE}" $@
293 else
294 # Using `sh -c exec' causes nested quoting and shell substitution
295 # to be handled here in the same way rsh handles it.
296 CMD="exec ${TAR_PART1} -f \"${TAPE_FILE}\" $@"
297 message 10 "CMD: $CMD"
298 sh -c "$CMD"
299 message 10 "RC: $?"
300 fi
301 }
302
303 print_level() {
304 if [ ${1-$DUMP_LEVEL} -eq 0 ]; then
305 echo "Full"
306 else
307 echo "Level ${1-$DUMP_LEVEL}"
308 fi
309 }
310
311 prev_level() {
312 print_level `expr $DUMP_LEVEL - 1` | tr A-Z a-z
313 }
314
315 remote_run() {
316 rhost=$1
317 shift
318 message 10 "REMOTE $rhost: $@"
319 if [ "x$rhost" != "x${localhost}" ] ; then
320 $RSH "${rhost}" "$@"
321 else
322 $*
323 fi
324 }
325
326 license() {
327 cat - <<EOF
328 This program is part of GNU tar
329 Copyright 2004, Free Software Foundation
330
331 This program is free software; you can redistribute it and/or modify
332 it under the terms of the GNU General Public License as published by
333 the Free Software Foundation; either version 1, or (at your option)
334 any later version.
335
336 This program is distributed in the hope that it will be useful,
337 but WITHOUT ANY WARRANTY; without even the implied warranty of
338 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
339 GNU General Public License for more details.
340
341 You should have received a copy of the GNU General Public License
342 along with this program; if not, write to the Free Software
343 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
344 02111-1307, USA.
345 EOF
346 }
This page took 0.051589 seconds and 5 git commands to generate.