backup.in 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #! /bin/sh
  2. # Make backups.
  3. # Copyright 2004-2006, 2013, 2019 Free Software Foundation
  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. DUMP_LEVEL=0
  19. TIME=
  20. NOW=`now`
  21. usage() {
  22. cat - <<EOF
  23. usage: $PROGNAME [OPTIONS] [WHEN]
  24. Options are:
  25. -l, --level=LEVEL Do backup level LEVEL (default $DUMP_LEVEL).
  26. -f, --force Force backup even if today's log file already
  27. exists.
  28. -v, --verbose[=LEVEL] Set verbosity level. Default 100.
  29. -t, --time=TIME Wait till TIME, then do backup.
  30. Informational options:
  31. -h, --help Display this help message.
  32. -V, --version Display program version.
  33. Optional argument WHEN is for backward compatibility only. It has been
  34. superseded by --time option.
  35. TIME argument can be one of:
  36. now -- do backup immediately.
  37. HH -- do backup at HH hours.
  38. HH:MM -- do backup at HH:MM.
  39. Send bug reports to @PACKAGE_BUGREPORT@.
  40. EOF
  41. }
  42. # For compatibility with previous versions, deduce the backup level
  43. # from the command name
  44. case "$PROGNAME" in
  45. level-[0-9]) DUMP_LEVEL=`expr $PROGNAME : 'level-\([0-9][0-9]*\)'`;;
  46. esac
  47. for opt
  48. do
  49. if [ -z "$prev" ]; then
  50. option=$opt
  51. optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
  52. else
  53. option="${prev}=$opt"
  54. prev=""
  55. optarg=$opt
  56. fi
  57. case $option in
  58. --l=*|--le=*|--lev=*|--leve=*|--level=*)
  59. DUMP_LEVEL=$optarg
  60. ;;
  61. -l?*) DUMP_LEVEL=`expr $option : '-l\(.*\)'`;;
  62. -l|--l|--le|--lev|--leve|--level)
  63. prev=--level
  64. ;;
  65. --verb=*|--verbo=*|--verbos=*|--verbose=*)
  66. VERBOSE=$optarg
  67. ;;
  68. -v|--verb|--verbo|--verbos|--verbose)
  69. VERBOSE=100
  70. ;;
  71. -v*) VERBOSE=`expr $option : '-v\(.*\)'`;;
  72. --t=*|--ti=*|--tim=*|--time=*)
  73. TIME=$optarg
  74. ;;
  75. -t?*) TIME=`expr $option : '-t\(.*\)'`;;
  76. -t|--t|--ti|--tim|--time)
  77. prev=--time
  78. ;;
  79. -V|--v|--ve|--ver|--vers|--versi|--versio|--version)
  80. echo "backup (@PACKAGE_NAME@) @VERSION@"
  81. license
  82. exit;;
  83. -h|--h|--he|--hel|--help)
  84. usage
  85. exit;;
  86. -f|--f|--fo|--for|--forc|--force)
  87. FORCE=yes
  88. ;;
  89. *) if [ "x$TIME" != "x" ]; then
  90. bailout "Extra argument. Try $PROGNAME --help for more info."
  91. else
  92. TIME=$option
  93. fi;;
  94. esac
  95. done
  96. if [ "x$TIME" = x ]; then
  97. bailout "No backup time specified. Try $PROGNAME --help for more info."
  98. exit 1
  99. fi
  100. init_backup
  101. # Maybe sleep until around specified or default hour.
  102. wait_time $TIME
  103. if [ $DUMP_LEVEL -ne 0 ]; then
  104. PREV_LEVEL=`expr $DUMP_LEVEL - 1`
  105. PREV_DATE=`ls -t ${LOGPATH}/log-*-level-$PREV_LEVEL|
  106. head -n 1|
  107. sed "s,${LOGPATH}/log-\(.*\)-level.*,\1,"`
  108. if [ "x$PREV_DATE" = x ]; then
  109. bailout "Can't determine date of the previous backup"
  110. fi
  111. message 0 "Backup from $PREV_DATE to $NOW"
  112. fi
  113. # start doing things
  114. # Make sure the log file did not already exist. Create it.
  115. if [ "x$FORCE" = "xyes" ]; then
  116. rm ${LOGFILE}
  117. fi
  118. if [ -f "${LOGFILE}" ] ; then
  119. bailout "Log file ${LOGFILE} already exists."
  120. else
  121. touch "${LOGFILE}"
  122. fi
  123. message 1 "Ready for backup."
  124. message 10 "TAR invocation: $TAR_PART1"
  125. message 20 "Variables:"
  126. message 20 "BACKUP_DIRS=$BACKUP_DIRS"
  127. message 20 "BACKUP_FILES=$BACKUP_FILES"
  128. # The bunch of commands below is run in a subshell for which all output is
  129. # piped through 'tee' to the logfile. Doing this, instead of having
  130. # multiple pipelines all over the place, is cleaner and allows access to
  131. # the exit value from various commands more easily.
  132. (
  133. message 1 "preparing tapes"
  134. if ! $MT_BEGIN "${TAPE_FILE}"; then
  135. echo >&2 "$0: tape initialization failed"
  136. exit 1
  137. fi
  138. rm -f "${VOLNO_FILE}"
  139. message 1 "processing backup directories"
  140. set - ${BACKUP_DIRS}
  141. while [ $# -ne 0 ] ; do
  142. date="`date`"
  143. fs="`echo \"${1}\" | sed -e 's/^.*://'`"
  144. fs=`root_fs $fs`
  145. fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
  146. remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
  147. if [ -z "$remotehost" ]; then
  148. remotehost=$localhost
  149. fi
  150. echo "Backing up ${1} at ${date}"
  151. message 10 "fs=$fs"
  152. message 10 "fsname=$fsname"
  153. message 10 "remotehost=$remotehost"
  154. if [ $DUMP_LEVEL -eq 0 ]; then
  155. make_level_log ${remotehost}
  156. else
  157. echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
  158. remote_run "${remotehost}" cp "`level_log_name ${fsname} $PREV_LEVEL`" "`level_log_name temp`"
  159. fi
  160. ${DUMP_BEGIN-:} $DUMP_LEVEL $remotehost $fs $fsname
  161. backup_host ${remotehost} \
  162. "--listed=`level_log_name temp`" \
  163. "--label='`print_level` backup of ${fs} on ${remotehost} at ${NOW}'" \
  164. -C ${fs} .
  165. # 'rsh' doesn't exit with the exit status of the remote command. What
  166. # stupid lossage. TODO: think of a reliable workaround.
  167. if [ $? -ne 0 ] ; then
  168. echo "$0: backup of ${1} failed." 1>&2
  169. # I'm assuming that the tar will have written an empty
  170. # file to the tape, otherwise I should do a cat here.
  171. else
  172. flush_level_log ${remotehost} ${fsname}
  173. fi
  174. ${MT_STATUS} "$TAPE_FILE"
  175. ${DUMP_END-:} $DUMP_LEVEL $remotehost $fs $fsname
  176. echo "sleeping ${SLEEP_TIME} seconds"
  177. sleep ${SLEEP_TIME}
  178. shift
  179. done
  180. # Dump any individual files requested.
  181. if [ "x${BACKUP_FILES}" != "x" ] ; then
  182. message 1 "processing individual files"
  183. date="`date`"
  184. if [ $DUMP_LEVEL -eq 0 ]; then
  185. make_level_log $localhost
  186. else
  187. echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
  188. remote_run "${localhost}" cp "`level_log_name MISC $PREV_LEVEL`" "`level_log_name temp`"
  189. fi
  190. echo "Backing up miscellaneous files at ${date}"
  191. ${DUMP_BEGIN-:} $DUMP_LEVEL $localhost MISC MISC
  192. backup_host $localhost \
  193. "--listed=`level_log_name temp`"\
  194. "--label='`print_level` backup of miscellaneous files at ${NOW}'" \
  195. ${BACKUP_FILES}
  196. if [ $? -ne 0 ] ; then
  197. echo "Backup of miscellaneous files failed."
  198. # I'm assuming that the tar will have written an empty
  199. # file to the tape, otherwise I should do a cat here.
  200. else
  201. flush_level_log $localhost MISC
  202. fi
  203. ${MT_STATUS} "$TAPE_FILE"
  204. ${DUMP_END-:} $DUMP_LEVEL $localhost MISC MISC
  205. else
  206. echo "No miscellaneous files specified"
  207. fi
  208. message 1 "final cleanup"
  209. $MT_REWIND "${TAPE_FILE}"
  210. $MT_OFFLINE "${TAPE_FILE}"
  211. echo "."
  212. ) 2>&1 | tee -a "${LOGFILE}"
  213. RC=$?
  214. if test "${ADMINISTRATOR}" != NONE; then
  215. echo "Sending the dump log to ${ADMINISTRATOR}"
  216. mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"
  217. fi
  218. exit $RC
  219. # EOF