]> git.sur5r.net Git - bacula/bacula/blob - bacula/examples/autochangers/mtx-changer.StorageTek-HPA4853
First cut restore GUI
[bacula/bacula] / bacula / examples / autochangers / mtx-changer.StorageTek-HPA4853
1 #!/bin/sh
2 #
3 # Bacula interface to mtx autoloader
4 #
5 #   Created  JAN/23/02 by Ludwig Jaffe
6 #
7 #  Works with the HP A4853 DLT Library
8 #         and the Storagetek Timberwolf 9730 DLT Library
9 #
10 #TAPEDRIVE0 holds the device/name of your 1st and only DLT drive (Bacula supports only 1 drive currently)
11 #
12 #Read TAPEDRIVE from command line parameters
13 if [ -z "$4" ] ; then
14   TAPEDRIVE0=/dev/st0 
15 else
16   TAPEDRIVE0=$4 
17 fi
18
19 #Delay in seconds the tape needs to load the tape. Needed to stop bacula from using the tape too early.
20 TAPEDELAY=65   #The StorageTek Timberwolf 9730 with DLT7000 needs approx. 50 seconds to load. 65 sec gives safety
21 MTXCHVERBOSE=1
22 if [ -z "$1" ] ; then
23   echo ""
24   echo "The mtx-changer script for bacula"
25   echo "---------------------------------"
26   echo ""
27   echo "usage: mtx-changer <changer-device> <command> [slot] [devicename of tapedrive]"
28   echo "       mtx-changer"
29   echo ""
30   echo "Valid commands:"
31   echo ""
32   echo "unload          Unloads a tape into the slot"
33   echo "                from where it was loaded."
34   echo "load <slot>     Loads a tape from the slot <slot>"
35   echo "list            Lists full storage slots"
36   echo "loaded          Gives slot from where the tape was loaded."
37   echo "                0 means the tape drive is empty."
38   echo "slots           Gives Number of aviable slots." 
39   echo ""
40   echo "Example:"
41   echo "  mtx-changer /dev/changer load 1   loads a tape from slot1"
42   echo ""
43   exit
44 fi
45
46
47 case "$2" in 
48    unload)
49 #     At first do mt -f /dev/st0 offline to unload the tape because HP A4853 aka Timberwolf9730
50 #     refuses to unload the tape from the drive if the DLT streamer did not unloaded it!!!
51 #
52       #Check if you want to fool me
53       if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Checking if drive is loaded before we unload. I Request loaded" ; fi
54       mtx -f $1 status >/tmp/mtx.$$
55       rm -f /tmp/mtxloaded
56       cat /tmp/mtx.$$ | grep "^Data Transfer Element 0:Full" | awk "{print \$7}" > /tmp/mtxloaded
57       rm -f /tmp/mtx.$$
58       read LOADEDVOL </tmp/mtxloaded
59       if [ -z "$LOADEDVOL" ] ; then
60          LOADEDVOL=0
61          echo "mtx-changer:  *** Don't fool me! *** The Drive $TAPEDRIVE0 is empty."
62          echo ""
63       else
64         if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mt -f $TAPEDRIVE0 offline to rewind and unload the tape!" ; fi
65         mt -f $TAPEDRIVE0 offline
66         if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mtx -f $1 $2"  ; fi
67         mtx -f $1 $2
68       fi
69       ;;
70
71    load)
72       #Let's check if drive is loaded before we load it
73       if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Checking if drive is loaded before we load. I Request loaded" ; fi
74       mtx -f $1 status >/tmp/mtx.$$
75       rm -f /tmp/mtxloaded
76       cat /tmp/mtx.$$ | grep "^Data Transfer Element 0:Full" | awk "{print \$7}" > /tmp/mtxloaded
77       rm -f /tmp/mtx.$$
78       read LOADEDVOL </tmp/mtxloaded 
79       if [ -z "$LOADEDVOL" ] ; then 
80          LOADEDVOL=0
81          echo "mtx-changer: The Drive $TAPEDRIVE0 is empty." 
82       else
83          #Check if you want to fool me
84          if [ $LOADEDVOL -eq $3 ] ; then 
85            echo "mtx-changer: *** Don't fool me! *** Tape $LOADEDVOL is already in drive $TAPEDRIVE0!"
86            echo "" 
87            exit
88          fi
89          echo "mtx-changer: The Drive $TAPEDRIVE0 is loaded with the tape from slot $LOADEDVOL"
90          echo "mtx-changer: Unloading..."
91          if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mt -f $TAPEDRIVE0 offline to rewind and unload the tape!" ; fi
92          mt -f $TAPEDRIVE0 offline
93          if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mtx -f $1 unload"  ; fi
94          mtx -f $1 unload
95       fi
96       #rm -f /tmp/mtxloaded
97       #It is now insured that the drive is empty
98       #
99       #Now we can load the drive as desired 
100       if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Doing mtx -f $1 $2 $3" ; fi
101       mtx -f $1 $2 $3
102       if [ $? -eq 0 ] ; then
103         #Wait until the tape is fully loaded in the dlt drive. It takes about 50 seconds. For safety wait 65 seconds
104         echo "Successfully loaded tape $3 into drive $TAPEDRIVE0."
105         echo "Waiting $TAPEDELAY seconds for the drive to fully load the tape ..."
106         sleep $TAPEDELAY
107         echo "$TAPEDELAY seconds are gone. The tape should be fully loaded now."
108         echo "Loading finished." ;
109         exit 0
110       else
111         echo "ERROR loading tape $3 into drive $TAPEDRIVE0. Maybe, slot $3 is empty!"
112         exit $?
113       fi
114       ;;
115
116    list) 
117       if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Requested list"; fi
118 #old      mtx -f $1 status | grep "^[     ]*Storage Element [0-9]*:.*Full" | awk "{print \$3}" | sed "s/:.*$/ /g" | tr -d "[\r\n]"
119 #from mtx bacula 1.29:
120 #     mtx -f $1 status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3}" | sed "s/:.*$/ /g" | tr -d "[\r\n]"
121       mtx -f $1 status | grep " *Storage Element [0-9]*:.*Full" | awk "{print \$3 \$4}" | sed "s/Full *\(:VolumeTag=\)*//"
122
123       ;;
124
125    loaded)
126       if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Request loaded" ; fi
127       mtx -f $1 status >/tmp/mtx.$$
128       cat /tmp/mtx.$$ | grep "^Data Transfer Element 0:Full" | awk "{print \$7}" 
129       cat /tmp/mtx.$$ | grep "^Data Transfer Element 0:Empty" | awk "{print 0}"  
130       rm -f /tmp/mtx.$$
131       ;;
132
133    slots)
134       if [ $MTXCHVERBOSE -eq 1 ] ; then echo "mtx-changer: Request slots" ; fi
135       mtx -f $1 status | grep "[  ]Storage Changer" | awk "{print \$5}"
136       ;;
137 esac