Imported Upstream version 3.3.0
[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::Logfile qw( log_rename get_current_log_timestamp $amanda_log_trace_log );
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 Getopt::Long::Configure(qw{bundling});
39 GetOptions(
40     'config=s' => sub { $config = $_[1]; },
41     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
42 ) or usage();
43
44 if (@ARGV < 1) {
45     die "USAGE: amdump_client [--config <config>] <config-overwrites> [list|dump|check] <diskname>";
46 }
47
48 my $cmd = $ARGV[0];
49
50 set_config_overrides($config_overrides);
51 config_init($CONFIG_INIT_CLIENT, undef);
52 $config = getconf($CNF_CONF) if !defined $config;
53 print "config: $config\n";
54 config_init($CONFIG_INIT_CLIENT | $CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_OVERLAY, $config);
55 my ($cfgerr_level, @cfgerr_errors) = config_errors();
56 if ($cfgerr_level >= $CFGERR_WARNINGS) {
57     config_print_errors();
58     if ($cfgerr_level >= $CFGERR_ERRORS) {
59         die "Errors processing config file";
60     }
61 }
62
63 #Amanda::Debug::add_amanda_log_handler($amanda_log_trace_log);
64
65 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
66
67 my $amservice = $sbindir . '/amservice';
68 my $amdump_server = getconf($CNF_AMDUMP_SERVER);
69 my $auth = getconf($CNF_AUTH);
70
71 my @cmd = ($amservice, '-f', '/dev/null', '-s', $amdump_server, $auth, 'amdumpd');
72
73 debug("cmd: @cmd");
74 my $amservice_out;
75 my $amservice_in;
76 my $err = Symbol::gensym;
77 my $pid = open3($amservice_in, $amservice_out, $err, @cmd);
78 my @disks;
79
80 debug("send: CONFIG $config");
81 print {$amservice_in} "CONFIG $config\n";
82 if ($cmd eq 'list') {
83     debug("send: LIST");
84     print {$amservice_in} "LIST\n";
85     get_list(1);
86 } elsif ($cmd eq 'dump') {
87     # check if diskname on the command line
88     if ($ARGV[1]) {
89         # get the list of dle
90         debug ("send: LIST");
91         print {$amservice_in} "LIST\n";
92         get_list(0);
93
94         #find the diskname that match
95         for (my $i=1; $i <= $#ARGV; $i++) {
96             for my $diskname (@disks) {
97                 if (Amanda::Logfile::match_disk($ARGV[$i], $diskname)) {
98                     debug("send: DISK " . Amanda::Util::quote_string($diskname));
99                     print {$amservice_in} "DISK " . Amanda::Util::quote_string($diskname) . "\n";
100                     my $a = <$amservice_out>;
101                     print if ($a != /^DISK /)
102                 }
103             }
104         }
105     }
106     debug("send: DUMP");
107     print {$amservice_in} "DUMP\n";
108     get_server_data();
109 } elsif ($cmd eq 'check') {
110     # check if diskname on the command line
111     if ($ARGV[1]) {
112         # get the list of dle
113         debug ("send: LIST");
114         print {$amservice_in} "LIST\n";
115         get_list(0);
116
117         #find the diskname that match
118         for (my $i=1; $i <= $#ARGV; $i++) {
119             for my $diskname (@disks) {
120                 if (Amanda::Logfile::match_disk($ARGV[$i], $diskname)) {
121                     debug("send: DISK " . Amanda::Util::quote_string($diskname));
122                     print {$amservice_in} "DISK " . Amanda::Util::quote_string($diskname) . "\n";
123                     my $a = <$amservice_out>;
124                     print if ($a != /^DISK /)
125                 }
126             }
127         }
128     }
129     debug("send: CHECK");
130     print {$amservice_in} "CHECK\n";
131     get_server_data();
132 } else {
133     usage();
134 }
135 debug("send: END");
136 print {$amservice_in} "END\n";
137
138 sub get_list {
139     my $verbose = shift;
140
141     while (<$amservice_out>) {
142         return if /^CONFIG/;
143         return if /^ENDLIST/;
144         print if $verbose;
145         chomp;
146         push @disks, Amanda::Util::unquote_string($_);
147     }
148 }
149
150 sub get_server_data {
151     while (<$amservice_out>) {
152         if (/^ENDDUMP/) {
153             print "The backup is finished\n";
154             return;
155         }
156         if (/^ENDCHECK/) {
157             print "The check is finished\n";
158             return;
159         }
160         print;
161         return if /^CONFIG/;
162         return if /^BUSY/;
163     }
164 }
165
166 sub usage {
167     print STDERR "USAGE: amdump_client [--config <config>] <config-overwrites> [list|dump|check] <diskname>";
168 }
169
170 Amanda::Util::finish_application();
171 exit;