#!/bin/sh
# tape -- simple shell for OnStream DI-30 tape drive under RedHat Linux 6.2
# http://www.jpsdomain.org/linux/OnStream_DI-30-RedHat_Backup_mini-HOWTO.html
# v0.9 27-Feb-2001 JP Vossen (jp@jpsdomain.org)
# v0.9.1 06-Jun-2001 JPV Added eject options
# v0.9.2 15-Jun-2001 JPV Was "tar -tvbf 64 /dev..." but should have been
#	 "tar tvb 64 -f /dev..."  Thanks to David Burleigh for pointing this out.

MYVER="v0.9.2 15-Jun-2001"

# Copyright 2001 JP Vossen
# This script is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
# General Public License for more details.

# In no event shall the author be liable for any damages whatsoever
# (including, without limitation, damages for loss of business profits,
# business interruption, loss of business information, or any other
# pecuniary loss) arising out of the use of or inability to use this script.

# NOTES:
# 0. Requires afio if you use the afio options (duh!):
#	http://www.jpsdomain.org/public/public.html#rpms.
# 1. Requires a symlink of /dev/tape and /dev/ntape to your tape device.
#	e.g. for an OnStream DI-30:
#		"ln -s /dev/ht0 /dev/tape && ln -s /dev/nht0 /dev/ntape"
# 2. Assumes (actually forces) only one archive per tape.
# 3. The DI-30 requires a block size of 32k in ALL operations.
#      e.g. tar uses blocks of 512b, so 32k=512b*64
# 4. The parameters specified here will work for the DI-30.  Your mileage
#	may vary.
# 5. See my "jpbackup" script for much better afio code.
# 6. mt does not really need the "-f /dev/tape" if the TAPE environment
#	variable is correctly set.

# See how we were called.
case "$1" in
  tbackup)
        if [ -z "$2" ]; then
            echo ""
            echo "Must specify a directory to backup!"
            exit 2
        fi
        tar cvb 64 -f /dev/tape $2
	;;
  ttoc)
        tar tvb 64 -f /dev/tape
	;;
  tverify)
        cd /
        tar dvb 64 -f /dev/tape
	;;
  trestore)
        mt rewind
        # e.g. tape trestore
        # e.g. tape trestore root/mydoc*
        tar xvb 64 -f /dev/tape $2
	;;
  backup)
        if [ -z "$2" ]; then
            echo ""
            echo "Must specify a directory to backup!"
            exit 2
        fi
        find $2 | afio -ovZ -b 32k -c 64 -M 10m -Q "-c6" /dev/tape
	;;
  toc)
        afio -tvxZ -b 32k -M 10m /dev/tape
	;;
  verify)
        cd /
        afio -rvxZ -b 32k -M 10m /dev/tape
	;;
  restore)
        mt rewind
        if [ -z "$2" ]; then
            # e.g. tape restore
            afio -ivxZ -b 32k -M 10m /dev/tape
        else
            # e.g. tape restore root/mydoc*
            afio -ivxZ -b 32k -M 10m -y "$2" /dev/tape
        fi
	;;
  offline|eject)
        echo ""
        echo "Note: this will not eject the tape, but may help allow you to"
        echo "manually eject it."
        mt -f /dev/tape offline
	;;
  erase)
        mt -f /dev/tape rewind
        mt -f /dev/tape erase
	;;
  status)
        mt -f /dev/tape status
	;;
  retension)
        # This spelling is correct
        mt -f /dev/tape retension
	;;
  retention)
        echo ""
        echo "Next time you may want to spell it retension..."
        mt -f /dev/tape retension
	;;
  rewind)
        mt -f /dev/tape rewind
	;;
  findnext)
        if [ -z "$2" ]; then
            echo ""
            echo "Must specify the number of archives to skip!"
            exit 2
        fi
        # Goto the start of the next archive.
        # See mt man page: fsfm, bsf, bsfm, asf, eod, etc.
        # Note NON-rewinding device...
        mt -f /dev/ntape fsf $2
	;;
  end)
        # Goto the end of the existing data.
        # Note NON-rewinding device...
        mt -f /dev/ntape eod
	;;
  *)
        echo "$0 ${MYVER} Copyright 2001 JP Vossen"
        echo "    http://www.jpsdomain.org/"
        echo "    Licensed under the GNU GENERAL PUBLIC LICENSE:"
        echo "    See http://www.gnu.org/copyleft/gpl.html for full text and details."
        echo ""
	echo "Usage:	$0 [tbackup [dir]|ttoc|tverify|trestore [dir]]    - using tar"
	echo "	$0 [backup [dir]|toc|verify|restore [dir]]        - using afio"
	echo "	$0 [offline|eject|erase|status|retension|rewind|findnext|end]   - tape device"
        echo ""
        echo "Notes:"
        echo "1. You need to be in / for a verify to work ($0 does this for you)..."
        echo "2. If you are in / a restore will over-write the existing"
        echo "   directory.  If not in /, a new structure will be created."
        echo "3. findnext will find the next archive on the tape, but this"
        echo "   script will never create more than one archive per tape."
        echo "4. You should really read this code and understand what it"
        echo "   does before trying to use it."
        echo ""
	exit 1
        ;;
esac

