home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / tar-1.11.8-src.tgz / tar.out / fsf / tar / scripts / level-0 < prev    next >
Text File  |  1996-09-28  |  7KB  |  201 lines

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