Tape changer script
Posted: November 30th, 2012 | Author: karol | Filed under: Backup | No Comments »One of great features of GNU tar is that it can create multi-volume archives. This translates to ability of using multiple tapes when tar is used with a tape changer/library device. The only trick is to automate process of changing tapes. Such automation can be achieved by providing a script changing tape via “-F” argument to tar. Here, there is the script that I use with my changer (16x LTO5 tapes).
#!/bin/bash # ## script to change tapes in the changer - to be called from ## tar multi-volume command (-F option) ## Karol M. Sep 2012 ## get the range tapes to be used and currently used tape ## tape to be used is current tape slot + 1 ## if the last tape in the range jump to the first one ## conf_dir="/etc/backups" if [ ! -r $conf_dir/.tapes_to_use ] ; then exit 1 fi tapes_to_use=(`cat $conf_dir/.tapes_to_use`) first_tape=${tapes_to_use[0]} number_of_tapes=${#tapes_to_use[*]} let "number_of_tapes -= 1" last_tape=${tapes_to_use[$number_of_tapes]} if [ -r $conf_dir/.current_tape ] ; then current_tape=`cat $conf_dir/.current_tape` if [ $current_tape -eq $last_tape ] ; then tape_to_write=$first_tape else tape_to_write=$current_tape let "tape_to_write += 1" fi else tape_to_write=$first_tape fi ## unload current tape and load tape from ## the $tape_to_write slot ## /usr/sbin/mtx unload /usr/sbin/mtx load $tape_to_write if [ $? ] ; then echo $tape_to_write > $conf_dir/.current_tape exit 0 else exit 2 fi
The script needs two files to operate. One contains tape slot numbers to use (you may use only a subset of tapes for your backup(s)). This file is called .tapes_to_use (note leading period character). Another file named .current_tape contains the slot number of a tape currently being used. This is what these files contain in my environment:
cat .tapes_to_use
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
cat .current_tape
8
What the script would do with the above content of .tapes_to_use and .current_tape is to load the tape from slot 9 next time when it is called by tar command. After successfully loading the tape from slot 9, it will modify file .current_tape to read 9, as 9 becomes currently used tape, and then it will terminate with exit code 0 which tells tar that tapes have been successfully changed.
The file .current_tape does not have to exist, but .tapes_to_use does. If .current_tape does not exist the first tape listed in .tapes_to_use will be loaded. Note that when the script is called with the last tape already in use, it will go back to the first tape. Modify, if you do not want this behavior.
Leave a Reply