level-1 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. # Make sure the log file did not already exist. Create it.
  43. if [ -f $LOGFILE ] ; then
  44. echo Log file $LOGFILE already exists.
  45. exit 1
  46. else
  47. touch $LOGFILE
  48. fi
  49. mt -f $TAPE_FILE rewind
  50. rm $VOLNO_FILE
  51. set $BACKUP_DIRS
  52. while [ $# -ne 0 ] ; do
  53. host=`echo $1 | sed 's/:.*$//'`
  54. fs=`echo $1 | sed 's/^.*://'`
  55. date=`date`
  56. fsname=`echo $1 | sed 's/\//:/g'`
  57. # This filename must be absolute; it is opened on the machine that runs tar.
  58. TAR_PART2="--listed=/etc/tar-backup/temp.level-1"
  59. TAR_PART3="--label='level 1 backup of $fs on $host at $date' -C $fs ."
  60. echo Backing up $1 at $date | tee -a $LOGFILE
  61. echo Last full dump on this filesystem: | tee -a $LOGFILE
  62. if [ $HOST != $host ] ; then
  63. rsh $host "ls -l /etc/tar-backup/$fsname.level-0; \
  64. cp /etc/tar-backup/$fsname.level-0 /etc/tar-backup/temp.level-1" \
  65. 2>&1 | tee -a $LOGFILE
  66. else
  67. ls -l /etc/tar-backup/$fsname.level-0 2>&1 | tee -a $LOGFILE
  68. cp /etc/tar-backup/$fsname.level-0 /etc/tar-backup/temp.level-1 2>&1 | tee -a $LOGFILE
  69. fi
  70. # Actually back things up.
  71. if [ $HOST != $host ] ; then
  72. rsh $host $TAR_PART1 -f $HOST:$TAPE_FILE $TAR_PART2 $TAR_PART3 2>&1 | tee -a $LOGFILE
  73. else
  74. # Using `sh -c exec' causes nested quoting and shell substitution
  75. # to be handled here in the same way rsh handles it.
  76. sh -c "exec $TAR_PART1 -f $TAPE_FILE $TAR_PART2 $TAR_PART3" 2>&1 | tee -a $LOGFILE
  77. fi
  78. if [ $? -ne 0 ] ; then
  79. echo Backup of $1 failed. | tee -a $LOGFILE
  80. # I'm assuming that the tar will have written an empty
  81. # file to the tape, otherwise I should do a cat here.
  82. else
  83. if [ $HOST != $host ] ; then
  84. rsh $host mv -f /etc/tar-backup/temp.level-1 /etc/tar-backup/$fsname.level-1 2>&1 | tee -a $LOGFILE
  85. else
  86. mv -f /etc/tar-backup/temp.level-1 /etc/tar-backup/$fsname.level-1 2>&1 | tee -a $LOGFILE
  87. fi
  88. fi
  89. $TAPE_STATUS | tee -a $LOGFILE
  90. sleep 60
  91. shift
  92. done
  93. # Dump any individual files requested.
  94. if [ x != "x$BACKUP_FILES" ] ; then
  95. date=`date`
  96. TAR_PART2="--listed=/etc/tar-backup/temp.level-1"
  97. TAR_PART3="--label='Incremental backup of miscellaneous files at $date'"
  98. echo Backing up miscellaneous files at $date | tee -a $LOGFILE
  99. echo Last full dump of these files: | tee -a $LOGFILE
  100. ls -l /etc/tar-backup/misc.level-0 2>&1 | tee -a $LOGFILE
  101. rm -f /etc/tar-backup/temp.level-1 2>&1 | tee -a $LOGFILE
  102. cp /etc/tar-backup/misc.level-0 /etc/tar-backup/temp.level-1 2>&1 | tee -a $LOGFILE
  103. echo Backing up miscellaneous files at $date | tee -a $LOGFILE
  104. # Using `sh -c exec' causes nested quoting and shell substitution
  105. # to be handled here in the same way rsh handles it.
  106. sh -c "exec $TAR_PART1 -f $TAPE_FILE $TAR_PART2 $TAR_PART3 \
  107. $BACKUP_FILES" 2>&1 | tee -a $LOGFILE
  108. if [ $? -ne 0 ] ; then
  109. echo Backup of miscellaneous files failed. | tee -a $LOGFILE
  110. # I'm assuming that the tar will have written an empty
  111. # file to the tape, otherwise I should do a cat here.
  112. else
  113. mv -f /etc/tar-backup/temp.level-1 /etc/tar-backup/misc.level-1 2>&1 | tee -a $LOGFILE
  114. fi
  115. $TAPE_STATUS | tee -a $LOGFILE
  116. else
  117. echo No miscellaneous files specified | tee -a $LOGFILE
  118. false
  119. fi
  120. mt -f $TAPE_FILE rewind
  121. mt -f $TAPE_FILE offl
  122. echo Sending the dump log to $ADMINISTRATOR
  123. cat $LOGFILE | sed -f logfile.sed > $LOGFILE.tmp
  124. /usr/ucb/mail -s "Results of backup on `date`" $ADMINISTRATOR < $LOGFILE.tmp