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