4
0

backup.in 7.1 KB

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