Imported Upstream version 3.2.0
[debian/amanda] / server-src / amdump.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 POSIX qw(WIFEXITED WEXITSTATUS strftime);
26
27 use Amanda::Config qw( :init :getconf );
28 use Amanda::Util qw( :constants );
29 use Amanda::Logfile qw( :logtype_t log_add );
30 use Amanda::Debug qw( debug );
31 use Amanda::Paths;
32
33 ##
34 # Main
35
36 sub usage {
37     my ($msg) = @_;
38     print STDERR <<EOF;
39 Usage: amdump <conf> [--no-taper] [-o configoption]* [host/disk]*
40 EOF
41     print STDERR "$msg\n" if $msg;
42     exit 1;
43 }
44
45 Amanda::Util::setup_application("amdump", "server", $CONTEXT_DAEMON);
46
47 my $config_overrides = new_config_overrides($#ARGV+1);
48 my @config_overrides_opts;
49
50 my $opt_no_taper = 0;
51 Getopt::Long::Configure(qw(bundling));
52 GetOptions(
53     'help|usage|?' => \&usage,
54     'no-taper' => \$opt_no_taper,
55     'o=s' => sub {
56         push @config_overrides_opts, "-o" . $_[1];
57         add_config_override_opt($config_overrides, $_[1]);
58     },
59 ) or usage();
60
61 usage("No config specified") if (@ARGV < 1);
62
63 my $config_name = shift @ARGV;
64 set_config_overrides($config_overrides);
65 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
66 my ($cfgerr_level, @cfgerr_errors) = config_errors();
67 if ($cfgerr_level >= $CFGERR_WARNINGS) {
68     config_print_errors();
69     if ($cfgerr_level >= $CFGERR_ERRORS) {
70         die("errors processing config file");
71     }
72 }
73
74 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
75
76 # useful info for below
77 my @hostdisk = @ARGV;
78 my $logdir = getconf($CNF_LOGDIR);
79 my @now = localtime;
80 my $longdate = strftime "%a %b %e %H:%M:%S %Z %Y", @now;
81 my $timestamp = strftime "%Y%m%d%H%M%S", @now;
82 my $datestamp = strftime "%Y%m%d", @now;
83 my $starttime_locale_independent = strftime "%Y-%m-%d %H:%M:%S %Z", @now;
84 my $trace_log_filename = "$logdir/log";
85 my $amdump_log_filename = "$logdir/amdump";
86 my $exit_code = 0;
87 my $amdump_log = \*STDERR;
88
89 ##
90 # subs for below
91
92 sub amdump_log {
93     print $amdump_log "amdump: ", @_, "\n";
94 }
95
96 sub check_exec {
97     my ($prog) = @_;
98     return if -x $prog;
99
100     log_add($L_ERROR, "Can't execute $prog");
101 }
102
103 sub run_subprocess {
104     my ($proc, @args) = @_;
105     check_exec($proc);
106
107     my $pid = POSIX::fork();
108     if ($pid == 0) {
109         my $null = POSIX::open("/dev/null", POSIX::O_RDWR);
110         POSIX::dup2($null, 0);
111         POSIX::dup2($null, 1);
112         POSIX::dup2(fileno($amdump_log), 2);
113         close($amdump_log);
114         exec $proc, @args;
115         die "Could not exec $proc: $!";
116     }
117     waitpid($pid, 0);
118     my $s = $? >> 8;
119     debug("$proc exited with code $s");
120     if ($?) {
121         if ($exit_code == 0) {
122             debug("ignoring failing exit code $s from $proc");
123         } else {
124             debug("recording failing exit code $s from $proc for amdump exit");
125             $exit_code = $s;
126         }
127     }
128 }
129
130 sub wait_for_hold {
131     my $holdfile = "$CONFIG_DIR/$config_name/hold";
132     if (-f $holdfile) {
133         debug("waiting for hold file '$holdfile' to be removed");
134         while (-f $holdfile) {
135             sleep(60);
136         }
137     }
138 }
139
140 sub bail_already_running {
141     my $msg = "An Amanda process is already running - please run amcleanup manually";
142     debug($msg);
143     amdump_log($msg);
144
145     # put together a fake logfile and send an amreport
146     my $fakelogfile = "$AMANDA_TMPDIR/fakelog.$$";
147     open(my $fakelog, ">", $fakelogfile)
148         or die("cannot open a fake log to send an report - situation is dire");
149     print $fakelog <<EOF;
150 INFO amdump amdump pid $$
151 START driver date $timestamp
152 ERROR amdump $msg
153 EOF
154     run_subprocess("$sbindir/amreport", $config_name, '--from-amdump', '-l', $fakelogfile);
155     unlink($fakelogfile);
156
157     # and we're done here
158     exit 1;
159 }
160
161 sub do_amcleanup {
162     return unless -f $amdump_log_filename || -f $trace_log_filename;
163
164     # logfiles are still around.  First, try an amcleanup -p to see if
165     # the actual processes are already dead
166     debug("runing amcleanup -p");
167     run_subprocess("$sbindir/amcleanup", '-p', $config_name);
168
169     # and check again
170     return unless -f $amdump_log_filename || -f $trace_log_filename;
171
172     bail_already_running();
173 }
174
175 sub start_logfiles {
176     debug("beginning trace log");
177     # start the trace log by simply writing an INFO line to it
178     log_add($L_INFO, "amdump pid $$");
179
180     # but not so fast!  What if another process has also appended such a line?
181     open(my $tl, "<", $trace_log_filename)
182         or die("could not open trace log file '$trace_log_filename': $!");
183     if (<$tl> !~ /^INFO amdump amdump pid $$/) {
184         # we didn't get there first, so bail out
185         debug("another amdump raced with this one, and won");
186         bail_already_running();
187     }
188     close($tl);
189
190     # redirect the amdump_log to the proper filename instead of stderr
191     # note that perl will overwrite STDERR if we don't set $amdump_log to
192     # undef first.. stupid perl.
193     debug("beginning amdump log");
194     $amdump_log = undef;
195     open($amdump_log, ">", $amdump_log_filename)
196         or die("could not open amdump log file '$amdump_log_filename': $!");
197 }
198
199 sub planner_driver_pipeline {
200     my $planner = "$amlibexecdir/planner";
201     my $driver = "$amlibexecdir/driver";
202     my @no_taper = $opt_no_taper? ('--no-taper'):();
203
204     check_exec($planner);
205     check_exec($driver);
206
207     # Perl's open3 is an embarassment to the language.  We'll do this manually.
208     debug("invoking planner | driver");
209     my ($rpipe, $wpipe) = POSIX::pipe();
210
211     my $pl_pid = POSIX::fork();
212     if ($pl_pid == 0) {
213         ## child
214         my $null = POSIX::open("/dev/null", POSIX::O_RDWR);
215         POSIX::dup2($null, 0);
216         POSIX::close($null);
217         POSIX::dup2($wpipe, 1);
218         POSIX::close($rpipe);
219         POSIX::close($wpipe);
220         POSIX::dup2(fileno($amdump_log), 2);
221         close($amdump_log);
222         exec $planner,
223             # note that @no_taper must follow --starttime
224             $config_name, '--starttime', $timestamp, @no_taper, @config_overrides_opts, @hostdisk;
225         die "Could not exec $planner: $!";
226     }
227     debug(" planner: $pl_pid");
228
229     my $dr_pid = POSIX::fork();
230     if ($dr_pid == 0) {
231         ## child
232         my $null = POSIX::open("/dev/null", POSIX::O_RDWR);
233         POSIX::dup2($rpipe, 0);
234         POSIX::close($rpipe);
235         POSIX::close($wpipe);
236         POSIX::dup2(fileno($amdump_log), 1); # driver does lots of logging to stdout..
237         POSIX::close($null);
238         POSIX::dup2(fileno($amdump_log), 2);
239         close($amdump_log);
240         exec $driver,
241             $config_name, @no_taper, @config_overrides_opts;
242         die "Could not exec $driver: $!";
243     }
244     debug(" driver: $dr_pid");
245
246     POSIX::close($rpipe);
247     POSIX::close($wpipe);
248
249     my $first_bad_exit = 0;
250     for (my $i = 0; $i < 2; $i++) {
251         my $dead = wait();
252         die("Error waiting: $!") if ($dead <= 0);
253         my $s = $? >> 8;
254         debug("planner finished with exit code $s") if $dead == $pl_pid;
255         debug("driver finished with exit code $s") if $dead == $dr_pid;
256         my $exit = WIFEXITED($?)? WEXITSTATUS($?) : 1;
257         $first_bad_exit = $exit if ($exit && !$first_bad_exit)
258     }
259     $exit_code |= $first_bad_exit;
260 }
261
262 sub do_amreport {
263     debug("running amreport");
264     run_subprocess("$sbindir/amreport", $config_name, '--from-amdump');
265 }
266
267 sub roll_trace_logs {
268     my $t = getconf($CNF_USETIMESTAMPS)? $timestamp : $datestamp;
269     debug("renaming trace log");
270     Amanda::Logfile::log_rename($t)
271 }
272
273 sub trim_trace_logs {
274     debug("trimming old trace logs");
275     run_subprocess("$amlibexecdir/amtrmlog", $config_name);
276 }
277
278 sub trim_indexes {
279     debug("trimming old indexes");
280     run_subprocess("$amlibexecdir/amtrmidx", $config_name);
281 }
282
283 sub roll_amdump_logs {
284     debug("renaming amdump log and trimming old amdump logs (beyond tapecycle+2)");
285
286     # rename all the way along the tapecycle
287     my $days = getconf($CNF_TAPECYCLE) + 2;
288     for (my $i = $days-1; $i >= 1; $i--) {
289         next unless -f "$amdump_log_filename.$i";
290         rename("$amdump_log_filename.$i", "$amdump_log_filename.".($i+1));
291     }
292
293     # now swap the current logfile in
294     rename("$amdump_log_filename", "$amdump_log_filename.1");
295 }
296
297 # now do the meat of the amdump work; these operations are ported directly
298 # from the old amdump.sh script
299
300 # wait for $confdir/hold to disappear
301 wait_for_hold();
302
303 # look for a current logfile, and if found run amcleanup -p, and if that fails
304 # bail out
305 do_amcleanup();
306
307 # start up the log file
308 start_logfiles();
309
310 # amstatus needs a lot of forms of the time, I guess
311 amdump_log("start at $longdate");
312 amdump_log("datestamp $datestamp");
313 amdump_log("starttime $timestamp");
314 amdump_log("starttime-locale-independent $starttime_locale_independent");
315
316 # run the planner and driver, the one piped to the other
317 planner_driver_pipeline();
318
319 my $end_longdate = strftime "%a %b %e %H:%M:%S %Z %Y", localtime;
320 amdump_log("end at $end_longdate");
321
322 # send the dump report
323 do_amreport();
324
325 # do some house-keeping
326 roll_trace_logs();
327 trim_trace_logs();
328 trim_indexes();
329 roll_amdump_logs();
330
331 debug("exiting with code $exit_code");
332 exit($exit_code);