2 # Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
18 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
21 use lib '@amperldir@';
26 use Amanda::Config qw( :init :getconf config_dir_relative );
27 use Amanda::Util qw( :constants );
29 use Amanda::Constants;
33 use Amanda::Debug qw( debug );
40 print "Usage: amcleanupdisk [-v] [-r] conf\n";
44 Amanda::Util::setup_application("amcleanupdisk", "server", $CONTEXT_CMDLINE);
46 my $config_overrides = new_config_overrides($#ARGV+1);
48 debug("Arguments: " . join(' ', @ARGV));
49 Getopt::Long::Configure(qw(bundling));
51 'version' => \&Amanda::Util::version_opt,
53 'r' => \$clean_holding,
54 'help|usage' => \&usage,
55 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
58 my $config_name = shift @ARGV or usage;
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");
70 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
72 my $amcleanupdisk="$amlibexecdir/amcleanupdisk";
74 if ( ! -e "$CONFIG_DIR/$config_name" ) {
75 die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
77 if ( ! -d "$CONFIG_DIR/$config_name" ) {
78 die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
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);
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);
95 } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_CONT_DUMPFILE) {
96 # orphan cont_dumpfile
98 print "Remove orphan chunk file: $hfile->{'filename'}\n" if $verbose;
99 unlink $hfile->{'filename'};
101 print "orphan chunk file: $hfile->{'filename'}\n" if $verbose;
103 } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_EMPTY) {
105 if ($clean_holding) {
106 print "Remove empty file: $hfile->{'filename'}\n" if $verbose;
107 unlink $hfile->{'filename'};
109 print "empty holding file: $hfile->{'filename'}\n" if $verbose;
111 } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_WEIRD) {
113 if ($clean_holding) {
114 print "Remove non amanda file: $hfile->{'filename'}\n" if $verbose;
115 unlink $hfile->{'filename'};
117 print "non amanda holding file: $hfile->{'filename'}\n" if $verbose;
121 if ($clean_holding) {
122 print "Remove file: $hfile->{'filename'}\n" if $verbose;
123 unlink $hfile->{'filename'};
125 print "unknown holding file: $hfile->{'filename'}\n" if $verbose;
130 Amanda::Holding::dir_unlink();
132 Amanda::Util::finish_application();