restore.in 6.1 KB

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