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