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