2 # Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved.
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.
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
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
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
25 use Amanda::Config qw( :init :getconf config_dir_relative );
26 use Amanda::Util qw( :constants );
28 use Amanda::Constants;
32 use Amanda::Debug qw( debug );
39 print "Usage: amcleanupdisk [-v] [-r] conf\n";
43 Amanda::Util::setup_application("amcleanupdisk", "server", $CONTEXT_CMDLINE);
45 my $config_overrides = new_config_overrides($#ARGV+1);
47 debug("Arguments: " . join(' ', @ARGV));
48 Getopt::Long::Configure(qw(bundling));
50 'version' => \&Amanda::Util::version_opt,
52 'r' => \$clean_holding,
53 'help|usage' => \&usage,
54 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
57 my $config_name = shift @ARGV or usage;
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");
69 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
71 my $amcleanupdisk="$amlibexecdir/amcleanupdisk";
73 if ( ! -e "$CONFIG_DIR/$config_name" ) {
74 die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
76 if ( ! -d "$CONFIG_DIR/$config_name" ) {
77 die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
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);
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);
94 } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_CONT_DUMPFILE) {
95 # orphan cont_dumpfile
97 print "Remove orphan chunk file: $hfile->{'filename'}\n" if $verbose;
98 unlink $hfile->{'filename'};
100 print "orphan chunk file: $hfile->{'filename'}\n" if $verbose;
102 } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_EMPTY) {
104 if ($clean_holding) {
105 print "Remove empty file: $hfile->{'filename'}\n" if $verbose;
106 unlink $hfile->{'filename'};
108 print "empty holding file: $hfile->{'filename'}\n" if $verbose;
110 } elsif ($hfile->{'header'}->{'type'} == $Amanda::Header::F_WEIRD) {
112 if ($clean_holding) {
113 print "Remove non amanda file: $hfile->{'filename'}\n" if $verbose;
114 unlink $hfile->{'filename'};
116 print "non amanda holding file: $hfile->{'filename'}\n" if $verbose;
120 if ($clean_holding) {
121 print "Remove file: $hfile->{'filename'}\n" if $verbose;
122 unlink $hfile->{'filename'};
124 print "unknown holding file: $hfile->{'filename'}\n" if $verbose;
129 Amanda::Holding::dir_unlink();
131 Amanda::Util::finish_application();