Imported Upstream version 3.3.3
[debian/amanda] / server-src / amcleanupdisk.pl
1 #!@PERL@
2 # Copyright (c) 2008-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 Amanda::Config qw( :init :getconf config_dir_relative );
27 use Amanda::Util qw( :constants );
28 use Amanda::Paths;
29 use Amanda::Constants;
30 use Amanda::Process;
31 use Amanda::Logfile;
32 use Amanda::Holding;
33 use Amanda::Debug qw( debug );
34 my $kill_enable=0;
35 my $process_alive=0;
36 my $verbose=0;
37 my $clean_holding=0;
38
39 sub usage() {
40     print "Usage: amcleanupdisk [-v] [-r] conf\n";
41     exit 1;
42 }
43
44 Amanda::Util::setup_application("amcleanupdisk", "server", $CONTEXT_CMDLINE);
45
46 my $config_overrides = new_config_overrides($#ARGV+1);
47
48 debug("Arguments: " . join(' ', @ARGV));
49 Getopt::Long::Configure(qw(bundling));
50 GetOptions(
51     'version' => \&Amanda::Util::version_opt,
52     'v' => \$verbose,
53     'r' => \$clean_holding,
54     'help|usage' => \&usage,
55     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
56 ) or usage();
57
58 my $config_name = shift @ARGV or usage;
59
60 set_config_overrides($config_overrides);
61 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
62 my ($cfgerr_level, @cfgerr_errors) = config_errors();
63 if ($cfgerr_level >= $CFGERR_WARNINGS) {
64     config_print_errors();
65     if ($cfgerr_level >= $CFGERR_ERRORS) {
66         die("errors processing config file");
67     }
68 }
69
70 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
71
72 my $amcleanupdisk="$amlibexecdir/amcleanupdisk";
73
74 if ( ! -e "$CONFIG_DIR/$config_name" ) {
75     die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
76 }
77 if ( ! -d "$CONFIG_DIR/$config_name" ) {
78     die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
79 }
80
81 my $stdout;
82 open($stdout, ">&STDOUT") if $verbose;;
83 my @hfiles = Amanda::Holding::all_files($stdout);
84 close $stdout if $verbose;
85 @hfiles = Amanda::Holding::merge_all_files(@hfiles);
86 while (@hfiles) {
87     my $hfile = pop @hfiles;
88     if ($hfile->{'header'}->{'type'} == $Amanda::Header::F_DUMPFILE) {
89         if ($hfile->{'filename'} =~ /\.tmp$/) {
90             print "Rename tmp holding file: $hfile->{'filename'}\n" if $verbose;
91             Amanda::Holding::rename_tmp($hfile->{'filename'}, 0);
92         } else {
93             # normal holding file
94         }
95     } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_CONT_DUMPFILE) {
96         # orphan cont_dumpfile
97         if ($clean_holding) {
98             print "Remove orphan chunk file: $hfile->{'filename'}\n" if $verbose;
99             unlink $hfile->{'filename'};
100         } else {
101             print "orphan chunk file: $hfile->{'filename'}\n" if $verbose;
102         }
103     } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_EMPTY) {
104         # empty file
105         if ($clean_holding) {
106             print "Remove empty file: $hfile->{'filename'}\n" if $verbose;
107             unlink $hfile->{'filename'};
108         } else {
109             print "empty holding file: $hfile->{'filename'}\n" if $verbose;
110         }
111     } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_WEIRD) {
112         # weird file
113         if ($clean_holding) {
114             print "Remove non amanda file: $hfile->{'filename'}\n" if $verbose;
115             unlink $hfile->{'filename'};
116         } else {
117             print "non amanda holding file: $hfile->{'filename'}\n" if $verbose;
118         }
119     } else {
120         # any other file
121         if ($clean_holding) {
122             print "Remove file: $hfile->{'filename'}\n" if $verbose;
123             unlink $hfile->{'filename'};
124         } else {
125             print "unknown holding file: $hfile->{'filename'}\n" if $verbose;
126         }
127     }
128 }
129
130 Amanda::Holding::dir_unlink();
131
132 Amanda::Util::finish_application();