level-1 6.4 KB

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