backup.in 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. #! /bin/sh
  2. # This program is part of GNU tar
  3. # Copyright 2004, 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., 59 Temple Place - Suite 330, Boston, MA
  18. # 02111-1307, 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 -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. fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
  149. remotehost="`expr \"${1}\" : '\([^/][^/]*\):.*'`"
  150. if [ -z "$remotehost" ]; then
  151. remotehost=$localhost
  152. fi
  153. echo "Backing up ${1} at ${date}"
  154. message 10 "fs=$fs"
  155. message 10 "fsname=$fsname"
  156. message 10 "remotehost=$remotehost"
  157. if [ $DUMP_LEVEL -eq 0 ]; then
  158. make_level_log ${remotehost}
  159. else
  160. echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
  161. remote_run "${remotehost}" cp "`level_log_name ${fsname} $PREV_LEVEL`" "`level_log_name temp`"
  162. fi
  163. ${DUMP_BEGIN-:} $DUMP_LEVEL $remotehost $fs $fsname
  164. backup_host ${remotehost} \
  165. "--listed=`level_log_name temp`" \
  166. "--label='`print_level` backup of ${fs} on ${remotehost} at ${NOW}'" \
  167. -C ${ROOT_FS}${fs} .
  168. # `rsh' doesn't exit with the exit status of the remote command. What
  169. # stupid lossage. TODO: think of a reliable workaround.
  170. if [ $? -ne 0 ] ; then
  171. echo "Backup of ${1} failed." 1>&2
  172. # I'm assuming that the tar will have written an empty
  173. # file to the tape, otherwise I should do a cat here.
  174. else
  175. flush_level_log ${remotehost} ${fsname}
  176. fi
  177. ${MT_STATUS}
  178. ${DUMP_END-:} $DUMP_LEVEL $remotehost $fs $fsname
  179. echo "sleeping ${SLEEP_TIME} seconds"
  180. sleep ${SLEEP_TIME}
  181. shift
  182. done
  183. # Dump any individual files requested.
  184. if [ "x${BACKUP_FILES}" != "x" ] ; then
  185. message 1 "processing individual files"
  186. date="`date`"
  187. if [ $DUMP_LEVEL -eq 0 ]; then
  188. make_level_log $localhost
  189. else
  190. echo "Last `prev_level` dump on this filesystem was on $PREV_DATE"
  191. remote_run "${localhost}" cp "`level_log_name MISC $PREV_LEVEL`" "`level_log_name temp`"
  192. fi
  193. echo "Backing up miscellaneous files at ${date}"
  194. ${DUMP_BEGIN-:} $DUMP_LEVEL $localhost MISC MISC
  195. backup_host $localhost \
  196. "--listed=`level_log_name temp`"\
  197. "--label='`print_level` backup of miscellaneous files at ${NOW}'" \
  198. ${BACKUP_FILES}
  199. if [ $? -ne 0 ] ; then
  200. echo "Backup of miscellaneous files failed."
  201. # I'm assuming that the tar will have written an empty
  202. # file to the tape, otherwise I should do a cat here.
  203. else
  204. flush_level_log $localhost MISC
  205. fi
  206. ${MT_STATUS}
  207. ${DUMP_END-:} $DUMP_LEVEL $localhost MISC MISC
  208. else
  209. echo "No miscellaneous files specified"
  210. fi
  211. message 1 "final cleanup"
  212. $MT_REWIND "${TAPE_FILE}"
  213. $MT_OFFLINE "${TAPE_FILE}"
  214. echo "."
  215. ) 2>&1 | tee -a "${LOGFILE}"
  216. if test "${ADMINISTRATOR}" != NONE; then
  217. echo "Sending the dump log to ${ADMINISTRATOR}"
  218. mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"
  219. fi
  220. # EOF