restore.in 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. # 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. fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
  102. remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
  103. if [ -z "$remotehost" ]; then
  104. remotehost=$localhost
  105. fi
  106. message 10 "fs=$fs"
  107. message 10 "fsname=$fsname"
  108. message 10 "remotehost=$remotehost"
  109. LOGPAT="`level_log_name ${fsname} '[0-9]'`"
  110. PREFIX="`level_log_name ${fsname} ''`"
  111. message 10 LOGPAT=$LOGPAT
  112. message 10 PREFIX=$PREFIX
  113. LEVELS=`remote_run "${remotehost}" ls $LOGPAT |
  114. sed "s,$PREFIX,," | sort -n`
  115. message 10 "LEVELS=$LEVELS"
  116. echo "Starting restore of ${1} at level $DUMP_LEVEL."
  117. for level in $LEVELS
  118. do
  119. if [ $level -lt $DUMP_LEVEL ]; then
  120. message 10 "Skipping level $level"
  121. continue;
  122. fi
  123. message 10 "Restoring level $level"
  124. DATE=`get_dump_time $level`
  125. FILE="`level_log_name ${fsname} ${level}`"
  126. message 10 "FILE=$FILE"
  127. LABEL="`print_level $level` backup of ${fs} on ${remotehost} at ${DATE}"
  128. ${RESTORE_BEGIN-:} $level $remotehost $fs $fsname
  129. backup_host ${remotehost} \
  130. "--listed=\"$FILE\"" \
  131. "--label=\"$LABEL\"" \
  132. -C ${ROOT_FS-/}$fs
  133. ${RESTORE_END-:} $level $remotehost $fs $fsname
  134. done
  135. }
  136. restore_files()
  137. {
  138. LOGPAT="`level_log_name MISC '[0-9]'`"
  139. PREFIX="`level_log_name MISC ''`"
  140. message 10 LOGPAT=$LOGPAT
  141. message 10 PREFIX=$PREFIX
  142. LEVELS=`remote_run "${localhost}" ls $LOGPAT | sed "s,$PREFIX,," | sort -n`
  143. message 10 "LEVELS=$LEVELS"
  144. echo "Starting restore of miscellaneous files at level $DUMP_LEVEL."
  145. for level in $LEVELS
  146. do
  147. if [ $level -lt $DUMP_LEVEL ]; then
  148. message 10 "Skipping level $level"
  149. continue;
  150. fi
  151. message 10 "Restoring level $level"
  152. DATE=`get_dump_time $level`
  153. FILE="`level_log_name MISC ${level}`"
  154. message 10 "FILE=$FILE"
  155. LABEL="`print_level $level` backup of miscellaneous files at ${DATE}"
  156. ${RESTORE_BEGIN-:} $level $localhost MISC MISC
  157. backup_host ${localhost} \
  158. "--listed=\"$FILE\"" \
  159. "--label=\"$LABEL\"" \
  160. -C ${ROOT_FS-/} $@
  161. ${RESTORE_END-:} $level $localhost MISC MISC
  162. done
  163. }
  164. # Operation Overwiew:
  165. #
  166. # 1. Determine the time of the last backup
  167. # 2. Create list of incremental listings to process
  168. # 3. For each filesystem:
  169. # 3.1. Start at the requested dump level (default 0) and proceed up to
  170. # the last available level:
  171. # 3.1.1 Deduce the volume label
  172. # 3.1.2. Invoke [rsh] tar --listed=FILE --label=LABEL [opts] -xf $TAPE_FILE
  173. # 4. End
  174. (message 1 "Preparing for restore"
  175. message 1 "processing backup directories"
  176. for dir in ${BACKUP_DIRS}
  177. do
  178. message 1 "Processing $dir"
  179. case $dir in
  180. ${PATTERN-*}) restore_fs $dir;;
  181. esac
  182. done
  183. if [ "x${BACKUP_FILES}" != "x" ] ; then
  184. message 1 "processing miscellaneous files"
  185. if [ -z "$PATTERN" ]; then
  186. restore_files
  187. else
  188. RESTORE_FILES=""
  189. for file in ${BACKUP_FILES}
  190. do
  191. rel_file=`expr $file : '/\(.*\)'`
  192. case $file in
  193. $PATTERN) if [ -z "$RESTORE_FILES" ]; then
  194. RESTORE_FILES="$rel_file"
  195. else
  196. RESTORE_FILES="$RESTORE_FILES $rel_file"
  197. fi;;
  198. esac
  199. done
  200. [ -z "$RESTORE_FILES" ] || restore_files $RESTORE_FILES
  201. fi
  202. fi) 2>&1 | tee -a "${LOGFILE}"
  203. # EOF