home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / datbkr-1999030301.sh < prev    next >
Linux/UNIX/POSIX Shell Script  |  1999-03-02  |  7KB  |  327 lines

  1. #!/bin/sh
  2. #datbkr
  3. #boff!  Dave 'Kill a Cop' Cinege -- Pubic Lo Mein
  4. #http://www.psychosis.com/datbkr/
  5. VERSION=1999030301
  6.  
  7. #Evolving program to simplify local and remote ssh dat back-ups
  8.  
  9. #Cut and paste the user section into this file. It is sourced below
  10. #and will override any of the 'hardcoded' variables in this script
  11. CONFFILE="/etc/datbkr.conf"
  12.  
  13. ###############################################################################
  14. #Beginning of user config area.
  15. ###############################################################################
  16.  
  17. ###
  18. #Default global variables (BOOL)
  19. ###
  20.  
  21. VERBOSE=1            #verbose console output
  22. AUTOEJECT=0            #eject tape after backup
  23. AUTOPART=0            #make 10MB partition on unpartitioned tapes (otherwise fail)
  24. PROMPT=1            #confirm backup
  25. ESTSIZE=0            #estimate size and print
  26.  
  27. ###
  28. #Local machine variables.  (paths for this machine)
  29. ###
  30.  
  31. #default include listing for tar
  32. INCLUDE="*"
  33.  
  34. #exclude listing for tar.
  35. EXCLUDE="--exclude=cdrom --exclude=floppy --exclude=mnt --exclude=proc --exclude=tmp --exclude=*/lost+found"
  36.  
  37. CDTO="./"            #cd here before tar'ing
  38.  
  39. LOG="/root/tmp/tape.log"    #path and name of 'log' file. 
  40. DIR="/root/tmp/tape.dir"    #path and name of 'dir' (index) file.
  41.  
  42. MAILTO="root"            #mail report to. Leave blank for no report.
  43. RSH="/usr/bin/ssh"        #rsh command (ie ssh)
  44. RSHUSR="dat"            #user to rsh in as
  45.  
  46. ###
  47. #DAT machine variables. (local or remote machine. paths for THAT machine)
  48. ###
  49.  
  50. TAPEHOST="dathost"        #Hostname of machine with dat drive.
  51. TAPEDEV="/dev/tape"        #path to tape device
  52. BLKSIZE=512            #tavedev block size (512 or 1024 typical)
  53. MT="/usr/local/bin/mt-st"    #path to mt. MUST handle partitions (ie mt-st)
  54.  
  55. ###############################################################################
  56. #End of user config area
  57. ###############################################################################
  58.  
  59.  
  60. #Source file to override these defaults
  61. source () { . $@; }    #'source' is bash centric, '.' isn't
  62. [ -f $CONFFILE ] && source $CONFFILE
  63.  
  64. ###
  65. #initilization
  66. ###
  67.  
  68. trap "cleanup;exit" 2 3 20    #INT QUIT TSTP
  69.  
  70. usage () {
  71.     echo "datbkr $VERSION   call as: datbkr, datbkr-mt or datbkr-rshtar"
  72.     echo ""
  73.     echo "Usage: datbkr [OPTION]"
  74.     echo ""
  75.     echo "  -d CDTODIR     change dir to    (defaults $CDTO)"
  76.     echo "  -i INCLUDE     tar include list (defaults $INCLUDE)"
  77.     echo "  -b BLKSIZE    tape block size"
  78.     echo "  -f TAPEDEV    tape device"
  79.     echo "  -p        don't prompt to continue"
  80.     echo "  -s        estimate size and print"
  81.     echo "  -e        autoeject tape"
  82.     echo "  -m        autopartition tape"
  83.     echo "  -v        verbose console output"
  84.     echo "  -q        suppress console output"
  85.     echo ""
  86.     echo "  -h        display this help and exit"
  87.     echo ""
  88.     echo "Usage: datbkr-mt [-b BLKSIZE] [-f TAPEDEV] mt-arg[, mt-arg2, ...]"
  89.     echo
  90.     echo "Usage: datbkr-rshtar -- internal use"
  91.     echo ""
  92.  
  93.     exit
  94. }
  95.  
  96. while getopts  d:i:b:f:hpemsvq opt ; do
  97.     case "$opt" in
  98.         d) CDTO="$OPTARG"  ;;
  99.         i) INCLUDE="$OPTARG" ;;
  100.         b) BLKSIZE="$OPTARG"  ;;
  101.         f) TAPEDEV="$OPTARG"  ;;
  102.         h) usage ;;
  103.         p) PROMPT=0 ;;
  104.         e) AUTOEJECT=1  ;;
  105.         m) AUTOPART=1 ;;
  106.         s) ESTSIZE=1  ;;
  107.         v) VERBOSE=1  ;;
  108.         q) VERBOSE=0  ;;
  109.     esac
  110. done
  111.  
  112. [ $OPTIND -gt 1 ] && shift $(expr $OPTIND - 1)
  113.  
  114. WRAPPER="$0-mt -f $TAPEDEV -b $BLKSIZE"
  115. RSHTAR="$0-rshtar"
  116. TARHOST="$TAPEHOST:"
  117. RSHCMD="$RSH -l $RSHUSR $TAPEHOST"
  118.  
  119. if [ $VERBOSE -eq 1 ]
  120. then    SE="/dev/tty"
  121. else    SE="/dev/null"
  122. fi
  123.  
  124.  
  125. #See if tapehost is local (dat) machine or not. 
  126. #Don't use rsh call if it is. Fix me?
  127. LIP="$(host $TAPEHOST | grep '    A    ' | cut -f 3)"
  128. RIP="$(host $HOSTNAME | grep '    A    ' | cut -f 3)"
  129. [ "$LIP" = "$RIP" ] && RSHCMD="" && TARHOST=""
  130.  
  131. ###
  132. #END
  133. ###
  134.  
  135.  
  136. ###
  137. #Functions
  138. ###
  139. vb () { [ $VERBOSE -eq 1 ] && "$@" ; }
  140.  
  141. qt () { "$@" >/dev/null 2>&1 ; }
  142.  
  143. log () { "$@" >>$LOG 2>&1 ; }
  144.  
  145. tapefunc () { log $RSHCMD $WRAPPER "$@" ; }
  146.  
  147. cleanup () {
  148.  
  149.     #vb cat $LOG
  150.     [ "$MAILTO" != "" ] && mail -s "Backup Report -- $HOSTNAME `date`" $MAILTO <$LOG
  151.  
  152.     qt rm $LOG
  153.     qt rm $DIR
  154.     qt rm $DIR.gz
  155.     
  156.     stty echo
  157. }
  158.  
  159. checkacc () {
  160.     if [ ${?} -ne 0 ]; then
  161.         log echo "Device access error: $1"
  162.         cleanup
  163.         exit 1
  164.     fi
  165. }
  166.  
  167. rshtar () {
  168.     $RSH -l $RSHUSR "$@"
  169.     exit
  170. }
  171.  
  172. mtwrapper () {
  173.  
  174.     #set blocksize, enable partitions
  175.     qt $MT -f $TAPEDEV setblk $BLKSIZE
  176.     qt $MT -f $TAPEDEV stoptions can-partitions
  177.     
  178.     OIFS=$IFS
  179.     IFS=","
  180.     set -- $@
  181.     IFS=$OIFS
  182.     while [ $# -gt 0 ] ; do
  183.         #echo "arg: $1"
  184.         $MT -f $TAPEDEV $1
  185.         rc=${?}
  186.         if [ $rc -ne 0 ]; then
  187.             echo "$(basename $0) failure: $MT -f $TAPEDEV $1"
  188.             exit $rc
  189.         fi
  190.         shift
  191.     done
  192.  
  193.     #just in case
  194.     qt $MT -f $TAPEDEV setblk $BLKSIZE
  195.     exit
  196. }
  197.  
  198. ###
  199. #END
  200. ###
  201.  
  202. ##
  203. #int main () {        #yes delusions of C
  204. ##
  205.  
  206. case "$(basename $0)" in 
  207.     datbkr-mt)        mtwrapper "$@" ;;
  208.     datbkr-rshtar)        rshtar "$@"  ;;
  209. esac
  210.  
  211. #Create/clear status and index files
  212. : > $LOG; chmod 600 $LOG
  213. : > $DIR; chmod 600 $DIR
  214.  
  215. vb echo -n "Tape check: "
  216. tapefunc tell, tell
  217. checkacc "No tape in drive."
  218. vb echo "OK"
  219.  
  220. if [ $PROMPT -eq 1 ]; then
  221.  
  222.     vb echo ""
  223.     echo "All data will be erased on this tape!"
  224.     echo -n "Proceed? (y/N) "
  225.     read OPT
  226.     case $OPT in
  227.         y|Y)    ;;
  228.         ""|*) cleanup; exit 1  ;;
  229.     esac    
  230.     vb echo ""
  231. fi
  232.  
  233. #turn of echo
  234. stty -echo
  235.  
  236. vb echo -n "Check for tape partitions: "
  237. tapefunc setpart 1
  238.     if [ ${?} -ne 0 ]; then
  239.         if [ $AUTOPART -eq 1 ]; then        
  240.             vb  echo "None. Making 10MB partition."
  241.             log echo "Making 10MB partition."
  242.             tapefunc mkpart 10
  243.             checkacc "Make partition failed!"
  244.         else
  245.             vb echo "None. Failing."
  246.             log echo "Tape not partitioned, failing."
  247.             cleanup
  248.             exit 1
  249.         fi
  250.     fi
  251. vb echo "OK"
  252.  
  253. vb echo -n "Erase index partition: "
  254. tapefunc setpart 1, rewind, status, erase, rewind
  255. checkacc "Set 1"
  256. vb echo "Done"
  257.  
  258. vb echo -n "Erase data partition: "
  259. tapefunc setpart 0, rewind, status, erase, rewind
  260. checkacc "Set 2"
  261. vb echo "Done"
  262.  
  263. vb echo "Changing directory to: $CDTO"
  264.  
  265. CPWD=$(pwd)
  266. cd $CDTO
  267.  
  268. vb [ $ESTSIZE -eq 1 ] && echo -n "Estimating size: "
  269. vb [ $ESTSIZE -eq 1 ] && echo "$(du -csh $INCLUDE 2>/dev/null | sed -n /total/s/total//p)"
  270.  
  271. vb echo "Backing up: $INCLUDE"
  272.  
  273. log echo ""
  274. log echo "Volume Information"
  275. log echo "=========================================================================="
  276. log echo "Timestamp:    `date +%Y%m%d-%H:%M`"
  277. log echo "Hostname:    $HOSTNAME"
  278. log echo "PWD:        $(pwd)"
  279. log echo "Include:    $INCLUDE"
  280. log echo "Exclude:    $(echo $EXCLUDE|sed s/--exclude=//g)"
  281. log echo ""
  282. log echo "Tar output:"
  283.  
  284. eval tar -cpvv --totals --rsh-command=$RSHTAR -C \$CDTO \$EXCLUDE -f "\$TARHOST""\$TAPEDEV" \$INCLUDE " 2>>\$LOG" " | tarblocks 2>\$SE >\$DIR"
  285.  
  286. log echo "=========================================================================="
  287.  
  288. cd $CPWD
  289.  
  290. vb echo "Back up complete."
  291.  
  292. vb echo -n "Change to index partition: "
  293. tapefunc setpart 1
  294. checkacc "Set 3"
  295.  
  296. vb echo -n "Compress index: "
  297. gzip -9f $DIR
  298.  
  299. vb echo -n "Write index to tape: "
  300.  
  301. log echo ""
  302. log echo "Index Information"
  303. log echo "=========================================================================="
  304.  
  305. log tar -cvv --rsh-command=$RSHTAR -C $(dirname $LOG) -f "$TARHOST""$TAPEDEV" $(basename $LOG) "$(basename $DIR).gz"
  306. checkacc "Problem writing index to tape"
  307. log echo "=========================================================================="
  308. vb echo "Done"
  309.  
  310.  
  311. tapefunc rewind
  312.  
  313. if [ $AUTOEJECT -eq 1 ]; then
  314.     vb echo -n "Ejecting tape: "
  315.     log echo "Ejecting tape."
  316.     tapefunc offline
  317.     vb echo "Done"
  318. fi
  319.  
  320. vb echo && echo "All Operations Finished."
  321.  
  322. cleanup
  323.  
  324. ##
  325. #}
  326. ##
  327.