backup.in 7.3 KB

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