backup.in 6.9 KB

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