]> Dogcows Code - chaz/tar/blob - scripts/restore.in
scripts: fix option parsing
[chaz/tar] / scripts / restore.in
1 #! /bin/sh
2 # This program is part of GNU tar
3 # Copyright (C) 2004, 2006 Free Software Foundation, Inc.
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 usage() {
25 cat - <<EOF
26 usage: $PROGNAME [OPTIONS] [PATTERN [PATTERN...]]
27 Options are:
28
29 -a, --all Restore all filesystems.
30 -l, --level=LEVEL Start restoring from the given backup LEVEL
31 (default $DUMP_LEVEL).
32 -v, --verbose[=LEVEL] Set verbosity level. Default 100.
33
34 Informational options:
35 -h, --help Display this help message.
36 -V, --version Display program version.
37
38 Send bug reports to @PACKAGE_BUGREPORT@.
39 EOF
40 }
41
42 unset PATTERN
43 DUMP_LEVEL=0
44 CMDLINE="$0 $@"
45
46 for opt
47 do
48 if [ -z "$prev" ]; then
49 option=$opt
50 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
51 else
52 option="${prev}=$opt"
53 prev=""
54 optarg=$opt
55 fi
56 case $option in
57 -a|--a|--al|--all)
58 RESTORE_ALL=1
59 ;;
60 --l=*|--le=*|--lev=*|--leve=*|--level=*)
61 DUMP_LEVEL=$optarg
62 ;;
63 -l?*) DUMP_LEVEL=`expr $option : '-l\(.*\)'`;;
64 -l|--l|--le|--lev|--leve|--level)
65 prev=--level
66 ;;
67 --verb=*|--verbo=*|--verbos=*|--verbose=*)
68 VERBOSE=$optarg
69 ;;
70 -v|--verb|--verbo|--verbos|--verbose)
71 VERBOSE=100
72 ;;
73 -v*) VERBOSE=`expr $option : '-v\(.*\)'`;;
74 -V|--v|--ve|--ver|--vers|--versi|--versio|--version)
75 echo "restore (@PACKAGE_NAME@) @VERSION@"
76 license
77 exit;;
78 -h|--h|--he|--hel|--help)
79 usage
80 exit;;
81 -*) bailout "Unknown option $opt. Try $PROGNAME --help for more info.";;
82 *) if [ -z "$PATTERN" ]; then
83 PATTERN=$opt
84 else
85 PATTERN="$PATTERN|$opt"
86 fi
87 ;;
88 esac
89 done
90
91 if [ -z "$RESTORE_ALL" ]; then
92 if [ -z "$PATTERN" ]; then
93 usage
94 exit;
95 fi
96 fi
97
98 init_restore
99 cat > $LOGFILE <<EOF
100 This file contains any messages produced by $PROGNAME.
101
102 It was created by GNU $PROGNAME, from @PACKAGE@ (@VERSION@).
103 Invocation command line was
104
105 \$ $CMDLINE
106
107 EOF
108
109 restore_fs()
110 {
111 fs="`echo \"${1}\" | sed -e 's/^.*://'`"
112 fs=`root_fs $fs`
113 fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
114 remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
115 if [ -z "$remotehost" ]; then
116 remotehost=$localhost
117 fi
118 message 10 "fs=$fs"
119 message 10 "fsname=$fsname"
120 message 10 "remotehost=$remotehost"
121
122 LOGPAT="`level_log_name ${fsname} '[0-9]'`"
123 PREFIX="`level_log_name ${fsname} ''`"
124 message 10 LOGPAT=$LOGPAT
125 message 10 PREFIX=$PREFIX
126 LEVELS=`remote_run "${remotehost}" ls $LOGPAT |
127 sed "s,$PREFIX,," | sort -n`
128 message 10 "LEVELS=$LEVELS"
129
130 echo "Starting restore of ${1} at level $DUMP_LEVEL."
131 for level in $LEVELS
132 do
133 if [ $level -lt $DUMP_LEVEL ]; then
134 message 10 "Skipping level $level"
135 continue;
136 fi
137 message 10 "Restoring level $level"
138
139 DATE=`get_dump_time $level`
140 FILE="`level_log_name ${fsname} ${level}`"
141 message 10 "FILE=$FILE"
142
143 LABEL="`print_level $level` backup of ${fs} on ${remotehost} at ${DATE}"
144 ${RESTORE_BEGIN-:} $level $remotehost $fs $fsname
145 backup_host ${remotehost} \
146 "--listed=\"$FILE\"" \
147 "--label=\"$LABEL\"" \
148 -C $fs
149 ${RESTORE_END-:} $level $remotehost $fs $fsname
150 done
151 }
152
153 restore_files()
154 {
155 LOGPAT="`level_log_name MISC '[0-9]'`"
156 PREFIX="`level_log_name MISC ''`"
157 message 10 LOGPAT=$LOGPAT
158 message 10 PREFIX=$PREFIX
159 LEVELS=`remote_run "${localhost}" ls $LOGPAT | sed "s,$PREFIX,," | sort -n`
160 message 10 "LEVELS=$LEVELS"
161
162 echo "Starting restore of miscellaneous files at level $DUMP_LEVEL."
163 for level in $LEVELS
164 do
165 if [ $level -lt $DUMP_LEVEL ]; then
166 message 10 "Skipping level $level"
167 continue;
168 fi
169 message 10 "Restoring level $level"
170
171 DATE=`get_dump_time $level`
172 FILE="`level_log_name MISC ${level}`"
173 message 10 "FILE=$FILE"
174
175 LABEL="`print_level $level` backup of miscellaneous files at ${DATE}"
176 ${RESTORE_BEGIN-:} $level $localhost MISC MISC
177 backup_host ${localhost} \
178 "--listed=\"$FILE\"" \
179 "--label=\"$LABEL\"" \
180 -C ${ROOT_FS-/} $@
181 ${RESTORE_END-:} $level $localhost MISC MISC
182 done
183 }
184
185 # Operation Overwiew:
186 #
187 # 1. Determine the time of the last backup
188 # 2. Create list of incremental listings to process
189 # 3. For each filesystem:
190 # 3.1. Start at the requested dump level (default 0) and proceed up to
191 # the last available level:
192 # 3.1.1 Deduce the volume label
193 # 3.1.2. Invoke [rsh] tar --listed=FILE --label=LABEL [opts] -xf $TAPE_FILE
194 # 4. End
195
196 (message 1 "Preparing for restore"
197
198 message 1 "processing backup directories"
199
200 for dir in ${BACKUP_DIRS}
201 do
202 message 1 "Processing $dir"
203 case $dir in
204 ${PATTERN-*}) restore_fs $dir;;
205 esac
206 done
207
208 if [ "x${BACKUP_FILES}" != "x" ] ; then
209 message 1 "processing miscellaneous files"
210 if [ -z "$PATTERN" ]; then
211 restore_files
212 else
213 RESTORE_FILES=""
214 for file in ${BACKUP_FILES}
215 do
216 rel_file=`expr $file : '/\(.*\)'`
217 case $file in
218 $PATTERN) if [ -z "$RESTORE_FILES" ]; then
219 RESTORE_FILES="$rel_file"
220 else
221 RESTORE_FILES="$RESTORE_FILES $rel_file"
222 fi;;
223 esac
224 done
225 [ -z "$RESTORE_FILES" ] || restore_files $RESTORE_FILES
226 fi
227
228 fi) 2>&1 | tee -a "${LOGFILE}"
229
230 # EOF
This page took 0.05253 seconds and 5 git commands to generate.