restore.in 5.9 KB

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