level-1 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. if [ ! -w / ]; then
  11. echo The backup must be run as root,
  12. echo or else some files will fail to be dumped.
  13. exit 1
  14. else
  15. false
  16. fi
  17. # Get the values of BACKUP_DIRS and BACKUP_FILES, and other variables.
  18. . ./backup-specs
  19. # Maybe sleep until around specified or default hour.
  20. #
  21. if [ "${1}" != "now" ]; then
  22. if [ "${1}"x != x ]; then
  23. spec=${1}
  24. else
  25. spec=${BACKUP_HOUR}
  26. fi
  27. pausetime=`date | awk '{hr=substr($4,1,2);\\
  28. mn=substr($4,4,2);\\
  29. if((hr+0)<spec+0)\\
  30. print 3600*(spec-hr)-60*mn;\\
  31. else\\
  32. print 3600*(spec+(24-hr))-60*mn; }' spec=$spec`
  33. clear
  34. cat ./dont_touch
  35. sleep ${pausetime}
  36. fi
  37. # start doing things
  38. here=`pwd`
  39. LOGFILE=log-`date | awk '{print $2 "-" $3 "-" $6}'`-level-1
  40. HOST=`hostname | sed 's/\..*//'`
  41. TAR_PART1="/usr/local/bin/tar -c --multi-volume --one-file-system --block=${BLOCKING} --sparse --volno-file=${VOLNO_FILE}"
  42. # Only use --info-script if DUMP_REMIND_SCRIPT was defined in backup-specs
  43. if [ x != "x${DUMP_REMIND_SCRIPT}" ]; then
  44. TAR_PART1="${TAR_PART1} --info-script=${DUMP_REMIND_SCRIPT}"
  45. fi
  46. # Make sure the log file did not already exist. Create it.
  47. if [ -f ${LOGFILE} ] ; then
  48. echo Log file ${LOGFILE} already exists.
  49. exit 1
  50. else
  51. touch ${LOGFILE}
  52. fi
  53. mt -f ${TAPE_FILE} rewind
  54. rm ${VOLNO_FILE}
  55. set ${BACKUP_DIRS}
  56. while [ $# -ne 0 ] ; do
  57. host=`echo ${1} | sed 's/:.*$//'`
  58. fs=`echo ${1} | sed 's/^.*://'`
  59. date=`date`
  60. fsname=`echo ${1} | sed 's/\//:/g'`
  61. # This filename must be absolute; it is opened on the machine that runs tar.
  62. TAR_PART2="--listed=/etc/tar-backup/temp.level-1"
  63. TAR_PART3="--label='level 1 backup of ${fs} on ${host} at ${date}' -C ${fs} ."
  64. echo Backing up ${1} at ${date} | tee -a ${LOGFILE}
  65. echo Last full dump on this filesystem: | tee -a ${LOGFILE}
  66. if [ ${HOST} != ${host} ] ; then
  67. rsh ${host} "ls -l /etc/tar-backup/${fsname}.level-0; \
  68. cp /etc/tar-backup/${fsname}.level-0 /etc/tar-backup/temp.level-1" \
  69. 2>&1 | tee -a ${LOGFILE}
  70. else
  71. ls -l /etc/tar-backup/${fsname}.level-0 2>&1 | tee -a ${LOGFILE}
  72. cp /etc/tar-backup/${fsname}.level-0 /etc/tar-backup/temp.level-1 2>&1 | tee -a ${LOGFILE}
  73. fi
  74. # Actually back things up.
  75. if [ ${HOST} != ${host} ] ; then
  76. rsh ${host} ${TAR_PART1} -f ${HOST}:${TAPE_FILE} ${TAR_PART2} ${TAR_PART3} 2>&1 | tee -a ${LOGFILE}
  77. else
  78. # Using `sh -c exec' causes nested quoting and shell substitution
  79. # to be handled here in the same way rsh handles it.
  80. sh -c "exec ${TAR_PART1} -f ${TAPE_FILE} ${TAR_PART2} ${TAR_PART3}" 2>&1 | tee -a ${LOGFILE}
  81. fi
  82. # This doesn't presently work, of course, because $? is set to the exit
  83. # status of the last thing in the pipeline of the previous command,
  84. # namely `tee'. We really want the exit status of the sh command
  85. # running tar, but getting this seems to be nontrivial. --friedman
  86. if [ $? -ne 0 ] ; then
  87. echo Backup of ${1} failed. | tee -a ${LOGFILE}
  88. # I'm assuming that the tar will have written an empty
  89. # file to the tape, otherwise I should do a cat here.
  90. else
  91. if [ ${HOST} != ${host} ] ; then
  92. rsh ${host} mv -f /etc/tar-backup/temp.level-1 /etc/tar-backup/${fsname}.level-1 2>&1 | tee -a ${LOGFILE}
  93. else
  94. mv -f /etc/tar-backup/temp.level-1 /etc/tar-backup/${fsname}.level-1 2>&1 | tee -a ${LOGFILE}
  95. fi
  96. fi
  97. ${TAPE_STATUS} | tee -a ${LOGFILE}
  98. sleep 60
  99. shift
  100. done
  101. # Dump any individual files requested.
  102. if [ x != "x${BACKUP_FILES}" ] ; then
  103. date=`date`
  104. TAR_PART2="--listed=/etc/tar-backup/temp.level-1"
  105. TAR_PART3="--label='Incremental backup of miscellaneous files at ${date}'"
  106. echo Backing up miscellaneous files at ${date} | tee -a ${LOGFILE}
  107. echo Last full dump of these files: | tee -a ${LOGFILE}
  108. ls -l /etc/tar-backup/misc.level-0 2>&1 | tee -a ${LOGFILE}
  109. rm -f /etc/tar-backup/temp.level-1 2>&1 | tee -a ${LOGFILE}
  110. cp /etc/tar-backup/misc.level-0 /etc/tar-backup/temp.level-1 2>&1 | tee -a ${LOGFILE}
  111. echo Backing up miscellaneous files at ${date} | tee -a ${LOGFILE}
  112. # Using `sh -c exec' causes nested quoting and shell substitution
  113. # to be handled here in the same way rsh handles it.
  114. sh -c "exec ${TAR_PART1} -f ${TAPE_FILE} ${TAR_PART2} ${TAR_PART3} \
  115. ${BACKUP_FILES}" 2>&1 | tee -a ${LOGFILE}
  116. # This doesn't presently work, of course, because $? is set to the exit
  117. # status of the last thing in the pipeline of the previous command,
  118. # namely `tee'. We really want the exit status of the sh command
  119. # running tar, but getting this seems to be nontrivial. --friedman
  120. if [ $? -ne 0 ] ; then
  121. echo Backup of miscellaneous files failed. | tee -a ${LOGFILE}
  122. # I'm assuming that the tar will have written an empty
  123. # file to the tape, otherwise I should do a cat here.
  124. else
  125. mv -f /etc/tar-backup/temp.level-1 /etc/tar-backup/misc.level-1 2>&1 | tee -a ${LOGFILE}
  126. fi
  127. ${TAPE_STATUS} | tee -a ${LOGFILE}
  128. else
  129. echo No miscellaneous files specified | tee -a ${LOGFILE}
  130. false
  131. fi
  132. mt -f ${TAPE_FILE} rewind
  133. mt -f ${TAPE_FILE} offl
  134. echo Sending the dump log to ${ADMINISTRATOR}
  135. cat ${LOGFILE} | sed -f logfile.sed > ${LOGFILE}.tmp
  136. /usr/ucb/mail -s "Results of backup on `date`" ${ADMINISTRATOR} < ${LOGFILE}.tmp
  137. rm -f ${LOGFILE}.tmp