56a13299cc856f74c3393bd999420460925dc0f2
[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 if ( "@USE_VERSION_SUFFIXES@" eq "yes" ) {
80     $SUF = "-@VERSION@";
81 } else {
82     $SUF = "";
83 }
84
85 chomp ($tapeDevice = `$sbindir/amgetconf$SUF tapedev 2>&1`);
86 die "tapedev not found in amanda.conf"
87         if !$tapeDevice or $tapeDevice eq "" or
88             $tapeDevice =~ m/no such parameter/;
89 chomp ($changerDevice = `$sbindir/amgetconf$SUF changerdev 2>&1`);
90 chomp $changerDevice;
91 die "changerdev not found in amanda.conf"
92         if !$changerDevice or $changerDevice eq "" or
93             $changerDevice =~ m/no such parameter/;
94
95 #
96 # Initialise a few global variables
97 #
98
99 $current_label = "";
100 #$current_slot = 0;
101 $max_slot = 1;
102
103 @dow = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
104 @moy = ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
105         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
106
107 sub do_time {
108         my (@t);
109         my ($r);
110
111         ###
112         # Get the local time for the value.
113         ###
114
115         @t = localtime (time ());
116
117         ###
118         # Return the result.
119         ###
120
121         $r = sprintf "%s %s %2d %2d:%02d:%02d %4d",
122           $dow[$t[6]],
123           $moy[$t[4]],
124           $t[3],
125           $t[2], $t[1], $t[0],
126           1900 + $t[5];
127
128         return $r;
129 }
130
131
132 sub is_mounted {
133     my $device = shift @_;
134     my ($directory) = ($device =~ m/file\:(.*)$/);
135     if ( -d "$directory/data" ) { return 1;}
136     else {return 0;}
137 }
138
139 sub request {
140     my $label = shift @_;
141     my $answer;
142     open (TTY, "+</dev/tty") or die "Can't open tty.\n";
143     print TTY "Insert Disk with label $label\n";
144     read TTY,$answer,1;
145     close TTY;
146     return $answer;
147 }
148
149 sub print_label {
150     my $label = shift @_;
151     open (TTY, "+</dev/tty") or die "Can't open tty.\n";
152     print TTY "The current Disk has label $label\n";
153     close TTY;
154 }
155
156 sub get_label {
157     my $device = shift @_;
158     my ($directory) = ($device =~ m/file\:(.*)$/);
159     my @dir_list =  glob("$directory/data/*");
160     while ( ($_= shift(@dir_list)) ) {
161         if ( /0+\.([\w\d]+)/ ) {return $1;}
162     }
163     return "no label";
164 }
165
166
167 sub Load {
168     my $device = shift @_;
169     my ($directory) = ($device =~ m/file\:(.*)$/);
170     my $label;
171     print LOG &do_time(), ": enter: Load $device\n";
172     if ( ! &is_mounted($device) ) {
173         print LOG &do_time(), ": mounting $directory\n";
174         system "mount $directory";
175     }
176     $label = get_label $device;
177     &print_label($label);
178     print LOG &do_time(), ": current label: $label\n";
179     print LOG &do_time(), ": leave: Load\n";
180
181 }
182
183 sub Unload {
184     my $device = shift @_;
185     my ($directory) = ($device =~ m/file\:(.*)$/);
186     print LOG &do_time(), ": enter: Unload $device\n";
187     if ( &is_mounted($device) ) {
188         print LOG &do_time(), ": ejecting $directory\n";
189         system "eject $directory";
190     }
191     print LOG &do_time(), ": leave: Unload\n";
192 }
193
194
195
196 #
197 # Main program
198 #
199
200 #
201 # Initialise
202 #
203
204
205 $opt_slot = 0;                                  # perl -w fodder
206 $opt_info = 0;                                  # perl -w fodder
207 $opt_reset = 0;                                 # perl -w fodder
208 $opt_eject = 0;                                 # perl -w fodder
209 $opt_search = 0;                                # perl -w fodder
210 $opt_label = 0;                                 # perl -w fodder
211
212 GetOptions("slot=s", "info", "reset", "eject", "search=s", "label=s"); 
213
214
215 if ( $opt_slot ) {
216     print LOG &do_time(), ": Loading slot $opt_slot requested\n";
217     if ( ! &is_mounted ($tapeDevice) ) {
218         &request ("any");
219         &Load ($tapeDevice);
220     }
221     $current_label = &get_label ($tapeDevice);
222     print LOG &do_time(), ": current label: $current_label\n";
223     print LOG &do_time(), ": 1 $tapeDevice\n";
224     print "1 $tapeDevice\n";
225     exit 0;
226 }
227
228 if ( $opt_info ) {
229     print LOG &do_time(), ": info requested\n";
230     $current_label = &get_label ($tapeDevice);
231     print LOG &do_time(), ": current label: $current_label\n";
232     print LOG &do_time(), ": 1 $max_slot 1 1\n";
233     print "1 $max_slot 1 1\n";
234     exit 0;
235 }
236
237 if ( $opt_reset ) {
238     print LOG &do_time(), ": reset requested\n";
239     &Unload ($tapeDevice);
240     &request ("any");
241     &Load ($tapeDevice);
242     $current_label = &get_label ($tapeDevice);
243     print LOG &do_time(), ": current label: $current_label\n";
244     print LOG &do_time(), ": 1 $tapeDevice\n";
245     print "1 $tapeDevice\n";
246     exit 0;
247 }
248
249 if ( $opt_eject ) {
250     print LOG &do_time(), ": eject requested\n";
251     &Unload ($tapeDevice);
252     print LOG &do_time(), ": 1 $tapeDevice\n";
253     print "1 $tapeDevice\n";
254     exit 0;
255 }
256
257 if ( $opt_search ) {
258     print LOG &do_time(), ": search label $opt_search requested\n";
259     $retry = 0;
260     $current_label = &get_label ($tapeDevice);
261     print LOG &do_time(), ": current label: $current_label\n";
262     while ( $opt_search ne $current_label && ++$retry < 5) {
263         &Unload ($tapeDevice);
264         &request ($opt_search);
265         &Load ($tapeDevice);
266         $current_label = &get_label ($tapeDevice);
267         print LOG &do_time(), ": search label: $opt_search\n";
268         print LOG &do_time(), ": current label: $current_label\n";
269     }
270     if ( $retry >= 5) {
271         print LOG &do_time(), ": disk not found\n";
272         print "disk not found\n";
273         exit 1;
274     } else {
275         print LOG &do_time(), ": 1 $tapeDevice\n";
276         print "1 $tapeDevice\n";
277         exit 0;
278     }
279 }
280
281 if ( $opt_label ) {
282     print LOG &do_time(), ": label $opt_label requested\n";
283     # no operation
284     print LOG &do_time(), ": 1 $tapeDevice\n";
285     print "1 $tapeDevice\n";
286     exit 0;
287 }
288
289 print "$progname: No command was received.  Exiting.\n";
290 exit 1;