restore.in 5.9 KB

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