level-0 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #!/bin/sh
  2. #
  3. # Run this script as root on the machine that has the tape drive, to make a
  4. # full (level-0) dump.
  5. #
  6. # If you give `now' as an argument, the dump is done immediately.
  7. # Otherwise, it waits until 1am, or until the hour given as argument.
  8. # Specify the hour as a number from 0 to 23.
  9. #
  10. # You must edit the file `backup-specs' to set the parameters for your site.
  11. # Useful for backup-specs, in case things have to be done slightly
  12. # differently for different dump levels.
  13. DUMP_LEVEL=0
  14. # Insure `mail' is in PATH.
  15. PATH="/usr/ucb:${PATH}"
  16. export PATH
  17. # This is not the most reliable test in the world. The following might be
  18. # more predictable:
  19. #
  20. # whoami="`whoami`"
  21. # euid="`sed -ne '/^'\"${whoami}\"':/{s/^[^:]*:[^:]*://;s/:.*//p;q;}' /etc/passwd`"
  22. # if [ "${euid}" != 0 ]; then ...
  23. #
  24. if [ ! -w / ]; then
  25. echo "The backup must be run as root or else some files will fail to be dumped."
  26. exit 1
  27. fi
  28. # Get the values of BACKUP_DIRS, BACKUP_FILES, and other variables.
  29. . ./backup-specs
  30. # Maybe sleep until around specified or default hour.
  31. if [ "${1}" != "now" ]; then
  32. if [ "${1}x" != "x" ]; then
  33. spec="${1}"
  34. else
  35. spec="${BACKUP_HOUR}"
  36. fi
  37. pausetime="`date | awk '
  38. {
  39. hr = substr($4, 1, 2);
  40. mn = substr($4, 4, 2);
  41. if((hr + 0) < (spec + 0))
  42. print 3600 * (spec - hr) - 60 * mn;
  43. else
  44. print 3600 * (spec + (24 - hr)) - 60 * mn;
  45. }' spec=\"${spec}\"`"
  46. clear
  47. echo "${SLEEP_MESSAGE}"
  48. sleep "${pausetime}"
  49. fi
  50. # start doing things
  51. # Put startdate in the subject line of mailed report, since if it happens
  52. # to run longer than 24 hours (as may be the case if someone forgets to put
  53. # in the next volume of the tape in adequate time), the backup date won't
  54. # appear too misleading.
  55. startdate="`date`"
  56. here="`pwd`"
  57. # Logfile name should be in the form ``log-1993-03-18-level-0''
  58. # They go in the subdirectory `log' of the current directory.
  59. # i.e. year-month-date. This format is useful for sorting by name, since
  60. # logfiles are intentionally kept online for future reference.
  61. LOGFILE="log/log-`date | sed -ne '
  62. s/[^ ]* *\([^ ]*\) *\([^ ]*\).* \([^ ]*\)$/\3-\1-\2/
  63. /-[0-9]$/s/\([0-9]\)$/0\1/
  64. /Jan/{s/Jan/01/p;q;}
  65. /Feb/{s/Feb/02/p;q;}
  66. /Mar/{s/Mar/03/p;q;}
  67. /Apr/{s/Apr/04/p;q;}
  68. /May/{s/May/05/p;q;}
  69. /Jun/{s/Jun/06/p;q;}
  70. /Jul/{s/Jul/07/p;q;}
  71. /Aug/{s/Aug/08/p;q;}
  72. /Sep/{s/Sep/09/p;q;}
  73. /Oct/{s/Oct/10/p;q;}
  74. /Nov/{s/Nov/11/p;q;}
  75. /Dec/{s/Dec/12/p;q;}'`-level-${DUMP_LEVEL}"
  76. localhost="`hostname | sed -e 's/\..*//'`"
  77. TAR_PART1="${TAR} -c --multi-volume --one-file-system --blocking=${BLOCKING} --sparse --volno-file=${VOLNO_FILE}"
  78. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  79. if [ "x${DUMP_REMIND_SCRIPT}" != "x" ]; then
  80. TAR_PART1="${TAR_PART1} --info-script='${DUMP_REMIND_SCRIPT}'"
  81. fi
  82. # Make sure the log file did not already exist. Create it.
  83. if [ -f "${LOGFILE}" ] ; then
  84. echo "Log file ${LOGFILE} already exists." 1>&2
  85. exit 1
  86. else
  87. touch "${LOGFILE}"
  88. fi
  89. # Most everything below here is run in a subshell for which all output is
  90. # piped through `tee' to the logfile. Doing this, instead of having
  91. # multiple pipelines all over the place, is cleaner and allows access to
  92. # the exit value from various commands more easily.
  93. (
  94. # Caveat: Some version of `mt' require `-t', not `-f'.
  95. mt -f "${TAPE_FILE}" rewind
  96. rm -f "${VOLNO_FILE}"
  97. set - ${BACKUP_DIRS}
  98. while [ $# -ne 0 ] ; do
  99. date="`date`"
  100. remotehost="`echo \"${1}\" | sed -e 's/:.*$//'`"
  101. fs="`echo \"${1}\" | sed -e 's/^.*://'`"
  102. fsname="`echo \"${1}\" | sed -e 's/\//:/g'`"
  103. # This filename must be absolute; it is opened on the machine that runs tar.
  104. TAR_PART2="--listed=/etc/tar-backup/temp.level-0"
  105. TAR_PART3="--label='Full backup of ${fs} on ${remotehost} at ${date}' -C ${fs} ."
  106. echo "Backing up ${1} at ${date}"
  107. # Actually back things up.
  108. if [ "z${localhost}" != "z${remotehost}" ] ; then
  109. rsh "${remotehost}" mkdir /etc/tar-backup > /dev/null 2>&1
  110. rsh "${remotehost}" rm -f /etc/tar-backup/temp.level-0
  111. rsh "${remotehost}" ${TAR_PART1} -f "${localhost}:${TAPE_FILE}" ${TAR_PART2} ${TAR_PART3}
  112. else
  113. mkdir /etc/tar-backup > /dev/null 2>&1
  114. rm -f /etc/tar-backup/temp.level-0
  115. # Using `sh -c exec' causes nested quoting and shell substitution
  116. # to be handled here in the same way rsh handles it.
  117. sh -c "exec ${TAR_PART1} -f \"${TAPE_FILE}\" ${TAR_PART2} ${TAR_PART3}"
  118. fi
  119. # `rsh' doesn't exit with the exit status of the remote command. What
  120. # stupid lossage. TODO: think of a reliable workaround.
  121. if [ $? -ne 0 ] ; then
  122. echo "Backup of ${1} failed." 1>&2
  123. # I'm assuming that the tar will have written an empty
  124. # file to the tape, otherwise I should do a cat here.
  125. else
  126. if [ "z${localhost}" != "z${remotehost}" ] ; then
  127. rsh "${remotehost}" mv -f /etc/tar-backup/temp.level-0 "/etc/tar-backup/${fsname}.level-0"
  128. else
  129. mv -f /etc/tar-backup/temp.level-0 "/etc/tar-backup/${fsname}.level-0"
  130. fi
  131. fi
  132. ${TAPE_STATUS}
  133. sleep 60
  134. shift
  135. done
  136. # Dump any individual files requested.
  137. if [ "x${BACKUP_FILES}" != "x" ] ; then
  138. date="`date`"
  139. TAR_PART2="--listed=/etc/tar-backup/temp.level-0"
  140. TAR_PART3="--label='Full backup of miscellaneous files at ${date}'"
  141. mkdir /etc/tar-backup > /dev/null 2>&1
  142. rm -f /etc/tar-backup/temp.level-0
  143. echo "Backing up miscellaneous files at ${date}"
  144. # Using `sh -c exec' causes nested quoting and shell substitution
  145. # to be handled here in the same way rsh handles it.
  146. sh -c "exec ${TAR_PART1} -f \"${TAPE_FILE}\" ${TAR_PART2} ${TAR_PART3} ${BACKUP_FILES}"
  147. # `rsh' doesn't exit with the exit status of the remote command. What
  148. # lossage. TODO: think of a reliable workaround.
  149. if [ $? -ne 0 ] ; then
  150. echo "Backup of miscellaneous files failed."
  151. # I'm assuming that the tar will have written an empty
  152. # file to the tape, otherwise I should do a cat here.
  153. else
  154. mv -f /etc/tar-backup/temp.level-0 /etc/tar-backup/misc.level-0
  155. fi
  156. ${TAPE_STATUS}
  157. else
  158. echo "No miscellaneous files specified"
  159. fi
  160. # Caveat: some versions of `mt' use `-t' instead of `-f'.
  161. mt -f "${TAPE_FILE}" rewind
  162. mt -f "${TAPE_FILE}" offl
  163. ) 2>&1 | tee -a "${LOGFILE}"
  164. echo "Sending the dump log to ${ADMINISTRATOR}"
  165. mail -s "Results of backup started ${startdate}" ${ADMINISTRATOR} < "${LOGFILE}"
  166. # eof