Imported Upstream version 3.3.3
[debian/amanda] / client-src / amdump_client.pl
1 #! @PERL@
2 # Copyright (c) 2010-2012 Zmanda Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20
21 use lib '@amperldir@';
22 use strict;
23 use warnings;
24
25 use Getopt::Long;
26 use Symbol;
27 use IPC::Open3;
28
29 use Amanda::Util qw( :constants );
30 use Amanda::Config qw( :init :getconf );
31 use Amanda::Paths;
32 use Amanda::Util qw ( match_disk );
33 use Amanda::Debug qw( debug );
34
35 Amanda::Util::setup_application("amdump_client", "client", $CONTEXT_CMDLINE);
36
37 my $config;
38 my $config_overrides = new_config_overrides($#ARGV+1);
39
40 debug("Arguments: " . join(' ', @ARGV));
41 Getopt::Long::Configure(qw{bundling});
42 GetOptions(
43     'version' => \&Amanda::Util::version_opt,
44     'config=s' => sub { $config = $_[1]; },
45     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
46 ) or usage();
47
48 if (@ARGV < 1) {
49     die "USAGE: amdump_client [--config <config>] <config-overwrites> [list|dump|check] <diskname>";
50 }
51
52 my $cmd = $ARGV[0];
53
54 set_config_overrides($config_overrides);
55 config_init($CONFIG_INIT_CLIENT, undef);
56 $config = getconf($CNF_CONF) if !defined $config;
57 print "config: $config\n";
58 config_init($CONFIG_INIT_CLIENT | $CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_OVERLAY, $config);
59 my ($cfgerr_level, @cfgerr_errors) = config_errors();
60 if ($cfgerr_level >= $CFGERR_WARNINGS) {
61     config_print_errors();
62     if ($cfgerr_level >= $CFGERR_ERRORS) {
63         die "Errors processing config file";
64     }
65 }
66
67 #Amanda::Debug::add_amanda_log_handler($amanda_log_trace_log);
68
69 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
70
71 my $amservice = $sbindir . '/amservice';
72 my $amdump_server = getconf($CNF_AMDUMP_SERVER);
73 my $auth = getconf($CNF_AUTH);
74
75 my @cmd = ($amservice, '-f', '/dev/null', '-s', $amdump_server, $auth, 'amdumpd');
76
77 debug("cmd: @cmd");
78 my $amservice_out;
79 my $amservice_in;
80 my $err = Symbol::gensym;
81 my $pid = open3($amservice_in, $amservice_out, $err, @cmd);
82 my @disks;
83
84 debug("send: CONFIG $config");
85 print {$amservice_in} "CONFIG $config\n";
86 if ($cmd eq 'list') {
87     debug("send: LIST");
88     print {$amservice_in} "LIST\n";
89     get_list(1);
90 } elsif ($cmd eq 'dump') {
91     # check if diskname on the command line
92     if ($ARGV[1]) {
93         # get the list of dle
94         debug ("send: LIST");
95         print {$amservice_in} "LIST\n";
96         get_list(0);
97
98         #find the diskname that match
99         for (my $i=1; $i <= $#ARGV; $i++) {
100             for my $diskname (@disks) {
101                 if (match_disk($ARGV[$i], $diskname)) {
102                     debug("send: DISK " . Amanda::Util::quote_string($diskname));
103                     print {$amservice_in} "DISK " . Amanda::Util::quote_string($diskname) . "\n";
104                     my $a = <$amservice_out>;
105                     print if ($a !~ /^DISK /)
106                 }
107             }
108         }
109     }
110     debug("send: DUMP");
111     print {$amservice_in} "DUMP\n";
112     get_server_data();
113 } elsif ($cmd eq 'check') {
114     # check if diskname on the command line
115     if ($ARGV[1]) {
116         # get the list of dle
117         debug ("send: LIST");
118         print {$amservice_in} "LIST\n";
119         get_list(0);
120
121         #find the diskname that match
122         for (my $i=1; $i <= $#ARGV; $i++) {
123             for my $diskname (@disks) {
124                 if (match_disk($ARGV[$i], $diskname)) {
125                     debug("send: DISK " . Amanda::Util::quote_string($diskname));
126                     print {$amservice_in} "DISK " . Amanda::Util::quote_string($diskname) . "\n";
127                     my $a = <$amservice_out>;
128                     print if ($a != /^DISK /)
129                 }
130             }
131         }
132     }
133     debug("send: CHECK");
134     print {$amservice_in} "CHECK\n";
135     get_server_data();
136 } else {
137     usage();
138 }
139 debug("send: END");
140 print {$amservice_in} "END\n";
141
142 sub get_list {
143     my $verbose = shift;
144
145     while (<$amservice_out>) {
146         return if /^CONFIG/;
147         return if /^ENDLIST/;
148         print if $verbose;
149         chomp;
150         push @disks, Amanda::Util::unquote_string($_);
151     }
152 }
153
154 sub get_server_data {
155     while (<$amservice_out>) {
156         if (/^ENDDUMP/) {
157             print "The backup is finished\n";
158             return;
159         }
160         if (/^ENDCHECK/) {
161             print "The check is finished\n";
162             return;
163         }
164         print;
165         return if /^CONFIG/;
166         return if /^BUSY/;
167     }
168 }
169
170 sub usage {
171     print STDERR "USAGE: amdump_client [--config <config>] <config-overwrites> [list|dump|check] <diskname>";
172 }
173
174 Amanda::Util::finish_application();
175 exit;