restore.in 5.9 KB

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