4
0

restore.in 5.9 KB

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