Imported Upstream version 3.1.0
[debian/amanda] / changer-src / chg-iomega.pl
1 #! @PERL@ -w
2
3 # Catch for sh/csh on systems without #! ability.
4 eval '(exit $?0)' && eval 'exec @PERL@ -S $0 ${1+"$@"}'
5         & eval 'exec @PERL@ -S $0 $argv:q'
6                 if 0;
7
8
9 # This changer script is designed for IOMEGA or JAZZ disks of various sizes
10 # as well as any other removable disk media.
11 #
12 # This is a PURELY MANUAL changer. It requests insertion of disk media via
13 # messages on /dev/tty. So it cannot be used via crontab.
14 #
15 # Make sure you comply with any of the following.
16 # - Add statements 
17 #         tpchanger "chg-iomega"
18 #         tapedev "file:<mount_point_of_removable_disk>"
19 #         # (e.g. tapedev "file:/mnt/iomega" )
20 #         tapetype IOMEGA      
21 #
22 #         
23 #         define tapetype IOMEGA {
24 #             comment "IOMega 250 MB floppys"
25 #             length 250 mbytes
26 #             filemark 100 kbytes
27 #             speed 1 mbytes
28 #         }
29 #   to your /etc/amanda/<backup_set>/amanda.conf file
30 # - Add entry to /etc/fstab to specify mount point of removable disk
31 #   and make this disk mountable by any user.
32 # - Format all disks, add a "data" sub directory and label all disks
33 #   by a call to amlabel.
34 # - Be aware that as of version 2.4.4p1, amanda can't handle backups that are
35 #   larger than the size of the removable disk media. So make sure
36 #   /etc/amanda/<backup_set>/disklist specifies chunks smaller than the 
37 #   disk size.
38 #
39 # This script is built up out of bits and pieces of other scripts, in
40 # particular chg-chio.pl. That script was written by 
41 # Nick Hibma - nick.hibma@jrc.it
42
43 # Permission to freely use and distribute is granted (by me and was granted by
44 # the original authors).
45 #
46 # Christoph Pospiech <pospiech@de.ibm.com>
47 #
48
49 require 5.001;
50
51 ($progname = $0) =~ s#/.*/##;
52
53 use English;
54 use Getopt::Long;
55
56 delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV', 'PATH'};
57 $ENV{'PATH'} = "/usr/bin:/usr/sbin:/sbin:/bin";
58
59 $| = 1;
60
61 if (-d "@AMANDA_DBGDIR@") {
62         $logfile = "@AMANDA_DBGDIR@/changer.debug";
63 } else {
64         $logfile = "/dev/null";
65 }
66 die "$progname: cannot open $logfile: $ERRNO\n"
67         unless (open (LOG, ">> $logfile"));
68
69
70 #
71 # get the information from the configuration file
72 #
73
74 $prefix="@prefix@";
75 $prefix=$prefix;                # avoid warnings about possible typo
76 $exec_prefix="@exec_prefix@";
77 $exec_prefix=$exec_prefix;      # Ditto
78 $sbindir="@sbindir@";
79
80 chomp ($tapeDevice = `$sbindir/amgetconf tapedev 2>&1`);
81 die "tapedev not found in amanda.conf"
82         if !$tapeDevice or $tapeDevice eq "" or
83             $tapeDevice =~ m/no such parameter/;
84 chomp ($changerDevice = `$sbindir/amgetconf changerdev 2>&1`);
85 chomp $changerDevice;
86 die "changerdev not found in amanda.conf"
87         if !$changerDevice or $changerDevice eq "" or
88             $changerDevice =~ m/no such parameter/;
89
90 #
91 # Initialise a few global variables
92 #
93
94 $current_label = "";
95 #$current_slot = 0;
96 $max_slot = 1;
97
98 @dow = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
99 @moy = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
100         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
101
102 sub do_time {
103         my (@t);
104         my ($r);
105
106         ###
107         # Get the local time for the value.
108         ###
109
110         @t = localtime (time ());
111
112         ###
113         # Return the result.
114         ###
115
116         $r = sprintf "%s %s %2d %2d:%02d:%02d %4d",
117           $dow[$t[6]],
118           $moy[$t[4]],
119           $t[3],
120           $t[2], $t[1], $t[0],
121           1900 + $t[5];
122
123         return $r;
124 }
125
126
127 sub is_mounted {
128     my $device = shift @_;
129     my ($directory) = ($device =~ m/file\:(.*)$/);
130     if ( -d "$directory/data" ) { return 1;}
131     else {return 0;}
132 }
133
134 sub request {
135     my $label = shift @_;
136     my $answer;
137     open (TTY, "+</dev/tty") or die "Can't open tty.\n";
138     print TTY "Insert Disk with label $label\n";
139     read TTY,$answer,1;
140     close TTY;
141     return $answer;
142 }
143
144 sub print_label {
145     my $label = shift @_;
146     open (TTY, "+</dev/tty") or die "Can't open tty.\n";
147     print TTY "The current Disk has label $label\n";
148     close TTY;
149 }
150
151 sub get_label {
152     my $device = shift @_;
153     my ($directory) = ($device =~ m/file\:(.*)$/);
154     my @dir_list =  glob("$directory/data/*");
155     while ( ($_= shift(@dir_list)) ) {
156         if ( /0+\.([\w\d]+)/ ) {return $1;}
157     }
158     return "no label";
159 }
160
161
162 sub Load {
163     my $device = shift @_;
164     my ($directory) = ($device =~ m/file\:(.*)$/);
165     my $label;
166     print LOG &do_time(), ": enter: Load $device\n";
167     if ( ! &is_mounted($device) ) {
168         print LOG &do_time(), ": mounting $directory\n";
169         system "mount $directory";
170     }
171     $label = get_label $device;
172     &print_label($label);
173     print LOG &do_time(), ": current label: $label\n";
174     print LOG &do_time(), ": leave: Load\n";
175
176 }
177
178 sub Unload {
179     my $device = shift @_;
180     my ($directory) = ($device =~ m/file\:(.*)$/);
181     print LOG &do_time(), ": enter: Unload $device\n";
182     if ( &is_mounted($device) ) {
183         print LOG &do_time(), ": ejecting $directory\n";
184         system "eject $directory";
185     }
186     print LOG &do_time(), ": leave: Unload\n";
187 }
188
189
190
191 #
192 # Main program
193 #
194
195 #
196 # Initialise
197 #
198
199
200 $opt_slot = 0;                                  # perl -w fodder
201 $opt_info = 0;                                  # perl -w fodder
202 $opt_reset = 0;                                 # perl -w fodder
203 $opt_eject = 0;                                 # perl -w fodder
204 $opt_search = 0;                                # perl -w fodder
205 $opt_label = 0;                                 # perl -w fodder
206
207 GetOptions("slot=s", "info", "reset", "eject", "search=s", "label=s"); 
208
209
210 if ( $opt_slot ) {
211     print LOG &do_time(), ": Loading slot $opt_slot requested\n";
212     if ( ! &is_mounted ($tapeDevice) ) {
213         &request ("any");
214         &Load ($tapeDevice);
215     }
216     $current_label = &get_label ($tapeDevice);
217     print LOG &do_time(), ": current label: $current_label\n";
218     print LOG &do_time(), ": 1 $tapeDevice\n";
219     print "1 $tapeDevice\n";
220     exit 0;
221 }
222
223 if ( $opt_info ) {
224     print LOG &do_time(), ": info requested\n";
225     $current_label = &get_label ($tapeDevice);
226     print LOG &do_time(), ": current label: $current_label\n";
227     print LOG &do_time(), ": 1 $max_slot 1 1\n";
228     print "1 $max_slot 1 1\n";
229     exit 0;
230 }
231
232 if ( $opt_reset ) {
233     print LOG &do_time(), ": reset requested\n";
234     &Unload ($tapeDevice);
235     &request ("any");
236     &Load ($tapeDevice);
237     $current_label = &get_label ($tapeDevice);
238     print LOG &do_time(), ": current label: $current_label\n";
239     print LOG &do_time(), ": 1 $tapeDevice\n";
240     print "1 $tapeDevice\n";
241     exit 0;
242 }
243
244 if ( $opt_eject ) {
245     print LOG &do_time(), ": eject requested\n";
246     &Unload ($tapeDevice);
247     print LOG &do_time(), ": 1 $tapeDevice\n";
248     print "1 $tapeDevice\n";
249     exit 0;
250 }
251
252 if ( $opt_search ) {
253     print LOG &do_time(), ": search label $opt_search requested\n";
254     $retry = 0;
255     $current_label = &get_label ($tapeDevice);
256     print LOG &do_time(), ": current label: $current_label\n";
257     while ( $opt_search ne $current_label && ++$retry < 5) {
258         &Unload ($tapeDevice);
259         &request ($opt_search);
260         &Load ($tapeDevice);
261         $current_label = &get_label ($tapeDevice);
262         print LOG &do_time(), ": search label: $opt_search\n";
263         print LOG &do_time(), ": current label: $current_label\n";
264     }
265     if ( $retry >= 5) {
266         print LOG &do_time(), ": disk not found\n";
267         print "disk not found\n";
268         exit 1;
269     } else {
270         print LOG &do_time(), ": 1 $tapeDevice\n";
271         print "1 $tapeDevice\n";
272         exit 0;
273     }
274 }
275
276 if ( $opt_label ) {
277     print LOG &do_time(), ": label $opt_label requested\n";
278     # no operation
279     print LOG &do_time(), ": 1 $tapeDevice\n";
280     print "1 $tapeDevice\n";
281     exit 0;
282 }
283
284 print "$progname: No command was received.  Exiting.\n";
285 exit 1;