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