Newer
Older
tbku / tbku
  1. #!/bin/sh
  2. # Automated Backup Script For TundraWare Inc. Servers
  3. # Copyright (c) 2004-2013, TundraWare Inc., Des Plaines, IL
  4. # All Rights Reserved.
  5. # For Updates See: http://www.tundraware.com/Software/tbku
  6. # $Id: tbku,v 1.118 2014/10/22 22:17:55 tundra Exp $
  7.  
  8.  
  9. #####
  10. # Things User May Want To Change
  11. #####
  12.  
  13. ###
  14. # Program Locations
  15. ###
  16.  
  17. # Try to figure out where things are on this particular
  18. # system. If you can't, default to FreeBSD settings.
  19.  
  20. DATE=`which date`
  21. HOSTNAME=`which hostname`
  22. LS=`which ls`
  23. SED=`which sed`
  24. TAR=`which tar`
  25. TOUCH=`which touch`
  26. UNAME=`which uname`
  27.  
  28. DATE=${DATE:-"/bin/date"}
  29. HOSTNAME=${HOSTNAME:-"/bin/hostname"}
  30. LS=${LS:-"/bin/ls"}
  31. SED=${SED:-"/usr/bin/sed"}
  32. TAR=${TAR:-"/usr/bin/tar"}
  33. TOUCH=${TOUCH:-"/usr/bin/touch"}
  34. UNAME=${UNAME:-"/usr/bin/uname"}
  35.  
  36. ###
  37. # Default Directories
  38. ###
  39.  
  40. # You can override with corresponding environment
  41. # variable or change the default value here.
  42.  
  43. BKUDIR=${TBKUDIR:-"/bku"} # Where to write the backup files
  44. FILESETDIR=${TBKUSETS:-"$HOME/tbku"} # Where to find fileset files
  45.  
  46. ###
  47. # Tape Device
  48. ###
  49.  
  50. TAPEDEV=${TBKUTAPE:-"/dev/sa0"} # Default to FreeBSD SCSI tape
  51.  
  52. ###
  53. # File Naming
  54. ###
  55.  
  56. DATETIME=`${DATE} "+%Y%m%d"` # Datestamp used to uniquely ID backups
  57. MACHINE=`${HOSTNAME} -f`- # FQDN of the machine we're on
  58. OSTYPE=`${UNAME} -s`- # Operating System Type
  59. OSREV=`${UNAME} -r`- # Operating System Revision Level
  60. HWTYPE=`${UNAME} -p` # Processor Type
  61.  
  62. BKUNAME=${MACHINE}${OSTYPE}${OSREV}${HWTYPE} # Backup base name
  63. BKUNAME=${TBKUNAME:-$BKUNAME} # But the user can override it
  64.  
  65. ####
  66. # Automatic Deletion Of Previous Backups
  67. ####
  68.  
  69. # Use this with caution!
  70.  
  71. BKUDEL=${TBKUDEL:-"NO"} # Must be "YES" for autodelete
  72.  
  73.  
  74. ############## Nothing Below Should Need To Be Changed #############
  75.  
  76.  
  77. ALLSETS="allsets" # Special name to force every backup set to be done
  78. COMMON="auto" # Common name for automated backups
  79.  
  80. #####
  81. # Setup
  82. #####
  83.  
  84. umask 077 # Make sure only owner can read backups
  85. mkdir -p ${BKUDIR} # Make sure the backup directory exists
  86.  
  87.  
  88. #####
  89. # Figure Out Which Backup Sets To Do
  90. #####
  91.  
  92. cd $FILESETDIR
  93.  
  94. if [ $# -ne 0 ]
  95. then
  96. if [ _$1 = _$ALLSETS ]
  97. then
  98. LIST=`${LS} * 2>/dev/null`
  99. else
  100. for arg in $*
  101. do
  102. a=`${LS} *${arg} 2>/dev/null`
  103. LIST=$LIST" $a"
  104. done
  105. fi
  106.  
  107. else
  108. LIST=`${LS} $COMMON* 2>/dev/null`
  109. fi
  110.  
  111.  
  112. #####
  113. # Do The Actual Backups, And Leave Indication Of Start/Stop Times For Each Backup Set
  114. #####
  115.  
  116.  
  117. # If nothing matches / there is nothing to do, tell the user
  118.  
  119. LST=`echo $LIST | ${SED} "s/\ //"` # Get rid of spaces from concatenation above
  120. if [ "_${LST}" = "_" ]
  121. then
  122. echo "tbku: Nothing to do! Exiting ..."
  123. exit
  124. fi
  125.  
  126.  
  127. # Otherwise process each fileset
  128.  
  129. for x in $LIST
  130. do
  131. s=`echo $x | ${SED} "s/.*\.//"` # Get the suffix
  132.  
  133. BASENAME=$BKUNAME-$s
  134. STAMPEDNAME=${DATETIME}-${BASENAME}
  135.  
  136. #####
  137. # Distinguish Between Tape And File Backups
  138. #####
  139.  
  140. if [ "_${s}" = "_tape" ]
  141. then
  142. FULLNAME=$TAPEDEV
  143. else
  144. FULLNAME=$BKUDIR/$STAMPEDNAME.tar.gz
  145. fi
  146.  
  147. # Optionally, get rid of previous backups of this fileset
  148.  
  149. if [ _${TBKUDEL} = "_YES" ]
  150. then
  151. rm $BKUDIR/.$BASENAME* $BKUDIR/$BASENAME*
  152. fi
  153.  
  154. echo "Now Doing Backup Set: $s"
  155. ${TOUCH} $BKUDIR/.$STAMPEDNAME-begin
  156. ${TAR} -cvzT $FILESETDIR/$x -f $FULLNAME >$BKUDIR/$STAMPEDNAME.log 2>&1
  157. ${TOUCH} $BKUDIR/.$STAMPEDNAME-end
  158. done