annotate archiver.pl @ 23:a3c35eae25dc

added count in archivemany removed abort on wrong user in archiver (problem with screen)
author casties
date Fri, 23 Sep 2005 19:16:17 +0200
parents fdf4ceb36db1
children 8b9d91963de7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
1 #!/usr/local/bin/perl -w
30497c6a3eca Initial revision
casties
parents:
diff changeset
2
30497c6a3eca Initial revision
casties
parents:
diff changeset
3 use strict;
30497c6a3eca Initial revision
casties
parents:
diff changeset
4
30497c6a3eca Initial revision
casties
parents:
diff changeset
5 use XML::LibXML;
30497c6a3eca Initial revision
casties
parents:
diff changeset
6
30497c6a3eca Initial revision
casties
parents:
diff changeset
7 # MPIWG libraries
30497c6a3eca Initial revision
casties
parents:
diff changeset
8 use lib '/usr/local/mpiwg/archive';
30497c6a3eca Initial revision
casties
parents:
diff changeset
9 use MPIWGStor;
30497c6a3eca Initial revision
casties
parents:
diff changeset
10
30497c6a3eca Initial revision
casties
parents:
diff changeset
11 # make output unbuffered
30497c6a3eca Initial revision
casties
parents:
diff changeset
12 $|=1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
13
30497c6a3eca Initial revision
casties
parents:
diff changeset
14 #######################################################
30497c6a3eca Initial revision
casties
parents:
diff changeset
15 # internal parameters
30497c6a3eca Initial revision
casties
parents:
diff changeset
16 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
17
30497c6a3eca Initial revision
casties
parents:
diff changeset
18 # program version
23
a3c35eae25dc added count in archivemany
casties
parents: 18
diff changeset
19 my $version = "0.7.1 (ROC 23.9.2005)";
11
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
20
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
21 # short help
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
22 my $help = "MPIWG archiver $version
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
23 use: archiver [options] docpath
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
24 options:
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
25 -debug show debugging info
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
26 -premigrate don't delete archived files
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
27 -force archive even if already archived
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
28 ";
11
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
29
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
30 # read command line parameters
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
31 my $args = MPIWGStor::parseargs;
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
32 if (! scalar(%$args)) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
33 print $help, "\n";
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
34 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
35 }
11
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
36
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
37 # debug level
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
38 $debug = (exists $$args{'debug'}) ? $$args{'debug'} : 0;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
39
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
40 # force archiving
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
41 my $force_archive = (exists $$args{'force'}) ? $$args{'force'} : 0;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
42
30497c6a3eca Initial revision
casties
parents:
diff changeset
43 # rewrite XML file (necessary for archive date!)
30497c6a3eca Initial revision
casties
parents:
diff changeset
44 my $fix_xml = 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
45 my $xml_changed = 0;
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
46
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
47 # XML namespace (not really implemented!)
30497c6a3eca Initial revision
casties
parents:
diff changeset
48 my $namespace = "";
30497c6a3eca Initial revision
casties
parents:
diff changeset
49
30497c6a3eca Initial revision
casties
parents:
diff changeset
50 # archive name (archive-path element, usually == $docdir)
30497c6a3eca Initial revision
casties
parents:
diff changeset
51 my $archname;
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
52
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
53 # archive storage date (now)
30497c6a3eca Initial revision
casties
parents:
diff changeset
54 my $archdate = stime(time);
30497c6a3eca Initial revision
casties
parents:
diff changeset
55
30497c6a3eca Initial revision
casties
parents:
diff changeset
56 # delete "junk" files before archiving
30497c6a3eca Initial revision
casties
parents:
diff changeset
57 my $delete_junk_files = 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
58
30497c6a3eca Initial revision
casties
parents:
diff changeset
59 # delete data files after archiving
30497c6a3eca Initial revision
casties
parents:
diff changeset
60 my $delete_data_files = 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
61
11
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
62 # don't delete archived files with "-premigrate"
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
63 if (exists $$args{'premigrate'}) {
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
64 $delete_data_files = not $$args{'premigrate'};
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
65 }
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
66 if ($delete_data_files) {
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
67 logger('INFO', "going to remove successfully archived files from disk");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
68 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
69
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
70
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
71 #######################################################
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
72 # external programs
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
73 #
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
74 my $archprog = "/opt/tivoli/tsm/client/ba/bin/dsmc";
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
75 if (! -x $archprog) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
76 logger('ABORT', "TSM client program '$archprog' missing!");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
77 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
78 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
79 my $checkprog = "/usr/local/mpiwg/archive/metacheck";
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
80 if (! -x $checkprog) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
81 logger('ABORT', "meta data checking program '$checkprog' missing!");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
82 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
83 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
84 # log file for archiver
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
85 my $log_file = "/var/log/mpiwg-archiver.log";
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
86 if (! open LOG, ">>$log_file") {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
87 logger('ABORT', "unable to write log file '$log_file'!");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
88 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
89 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
90
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
91 #######################################################
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
92 # check parameters that were passed to the program
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
93 #
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
94
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
95 my $docdir = $$args{'path'};
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
96 # strip double slashes
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
97 $docdir =~ s/\/\//\//;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
98 # strip trailing slashes
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
99 $docdir =~ s/\/+$//;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
100 if (! -d $docdir) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
101 logger('ABORT', "document directory \'$docdir\' doesn't exist!");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
102 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
103 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
104
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
105 my $metafile = "$docdir/index.meta";
30497c6a3eca Initial revision
casties
parents:
diff changeset
106 if (! -f $metafile) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
107 logger('ABORT', "metadata index file \'$metafile\' doesn't exist!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
108 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
109 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
110
30497c6a3eca Initial revision
casties
parents:
diff changeset
111 #######################################################
30497c6a3eca Initial revision
casties
parents:
diff changeset
112 # internal variables
30497c6a3eca Initial revision
casties
parents:
diff changeset
113 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
114
30497c6a3eca Initial revision
casties
parents:
diff changeset
115 # number of errors
30497c6a3eca Initial revision
casties
parents:
diff changeset
116 my $errcnt = 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
117 # number of warnings
30497c6a3eca Initial revision
casties
parents:
diff changeset
118 my $warncnt = 0;
30497c6a3eca Initial revision
casties
parents:
diff changeset
119
30497c6a3eca Initial revision
casties
parents:
diff changeset
120 #######################################################
30497c6a3eca Initial revision
casties
parents:
diff changeset
121 # subroutines
30497c6a3eca Initial revision
casties
parents:
diff changeset
122 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
123
30497c6a3eca Initial revision
casties
parents:
diff changeset
124 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
125 # $files = read_resource_meta($rootnode)
30497c6a3eca Initial revision
casties
parents:
diff changeset
126 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
127 # checks general resource meta information and reads the list of files
30497c6a3eca Initial revision
casties
parents:
diff changeset
128 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
129 sub read_resource_meta {
30497c6a3eca Initial revision
casties
parents:
diff changeset
130 my ($rootnode) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
131 my %files;
30497c6a3eca Initial revision
casties
parents:
diff changeset
132 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
133 # archive path
30497c6a3eca Initial revision
casties
parents:
diff changeset
134 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
135 # get archive-path
30497c6a3eca Initial revision
casties
parents:
diff changeset
136 $archname = MPIWGStor::sstrip($rootnode->findvalue('child::archive-path'));
30497c6a3eca Initial revision
casties
parents:
diff changeset
137 if (! $archname) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
138 logger('ABORT', "archive-name element missing!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
139 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
140 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
141
30497c6a3eca Initial revision
casties
parents:
diff changeset
142 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
143 # files
30497c6a3eca Initial revision
casties
parents:
diff changeset
144 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
145 my @filenodes = $rootnode->findnodes('child::file');
30497c6a3eca Initial revision
casties
parents:
diff changeset
146 foreach my $fn (@filenodes) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
147 my $name = MPIWGStor::sstrip($fn->findvalue('child::name'));
30497c6a3eca Initial revision
casties
parents:
diff changeset
148 my $path = MPIWGStor::sstrip($fn->findvalue('child::path'));
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
149 logger('DEBUG', "FILE ($path)$name");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
150 my $f = ($path) ? "$path/$name" : "$name";
30497c6a3eca Initial revision
casties
parents:
diff changeset
151 $files{$f} = $name;
30497c6a3eca Initial revision
casties
parents:
diff changeset
152 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
153
30497c6a3eca Initial revision
casties
parents:
diff changeset
154 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
155 # archive-storage-date
30497c6a3eca Initial revision
casties
parents:
diff changeset
156 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
157 my $stordatenode = ($rootnode->find('child::archive-storage-date'))->get_node(1);
30497c6a3eca Initial revision
casties
parents:
diff changeset
158 if ($stordatenode) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
159 logger('WARNING', "archive storage date exists! Resource already archived?");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
160 $warncnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
161 # delete old date
30497c6a3eca Initial revision
casties
parents:
diff changeset
162 $stordatenode->removeChildNodes;
30497c6a3eca Initial revision
casties
parents:
diff changeset
163 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
164 # create new storage date node
30497c6a3eca Initial revision
casties
parents:
diff changeset
165 $stordatenode = $rootnode->addNewChild($namespace, "archive-storage-date");
30497c6a3eca Initial revision
casties
parents:
diff changeset
166 # move after archive-path
30497c6a3eca Initial revision
casties
parents:
diff changeset
167 $rootnode->insertAfter($stordatenode, ($rootnode->find('child::archive-path'))->get_node(1));
30497c6a3eca Initial revision
casties
parents:
diff changeset
168 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
169 $stordatenode->appendTextNode($archdate);
30497c6a3eca Initial revision
casties
parents:
diff changeset
170 $xml_changed++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
171 return \%files;
30497c6a3eca Initial revision
casties
parents:
diff changeset
172 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
173
30497c6a3eca Initial revision
casties
parents:
diff changeset
174
30497c6a3eca Initial revision
casties
parents:
diff changeset
175 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
176 # $%files = run_archive
30497c6a3eca Initial revision
casties
parents:
diff changeset
177 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
178 # runs the archiver program on $docdir and returns a list of archived files
30497c6a3eca Initial revision
casties
parents:
diff changeset
179 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
180 sub run_archive {
30497c6a3eca Initial revision
casties
parents:
diff changeset
181 my %files;
30497c6a3eca Initial revision
casties
parents:
diff changeset
182 print LOG "START archiver $version $archdate\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
183 my $archcmd = $archprog;
30497c6a3eca Initial revision
casties
parents:
diff changeset
184 $archcmd .= " archive -archsymlinkasfile=no -subdir=yes";
30497c6a3eca Initial revision
casties
parents:
diff changeset
185 $archcmd .= " -description='$archname'";
30497c6a3eca Initial revision
casties
parents:
diff changeset
186 $archcmd .= " '$docdir/'";
30497c6a3eca Initial revision
casties
parents:
diff changeset
187
30497c6a3eca Initial revision
casties
parents:
diff changeset
188 print LOG "CMD: $archcmd\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
189 if (open ARCH, "$archcmd 2>&1 |") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
190 while (<ARCH>) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
191 chomp;
30497c6a3eca Initial revision
casties
parents:
diff changeset
192 print LOG "ARCH: $_\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
193 if (/Normal File-->\s+[\d,]+\s+(.*)\s+\[Sent\]/) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
194 print " ARCH: file '$1'\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
195 $files{$1} = "ok";
30497c6a3eca Initial revision
casties
parents:
diff changeset
196 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
197 if (/^Archive processing of .* finished without failure./) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
198 print " ARCH: OK\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
199 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
200 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
201 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
202 logger('ABORT', "unable to start archive command '$archcmd'!!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
203 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
204 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
205
30497c6a3eca Initial revision
casties
parents:
diff changeset
206 return \%files;
30497c6a3eca Initial revision
casties
parents:
diff changeset
207 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
208
30497c6a3eca Initial revision
casties
parents:
diff changeset
209
30497c6a3eca Initial revision
casties
parents:
diff changeset
210 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
211 # check_files(\%files_to_archive, \%archived_files)
30497c6a3eca Initial revision
casties
parents:
diff changeset
212 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
213 # compares the list of archived and to be archived files
30497c6a3eca Initial revision
casties
parents:
diff changeset
214 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
215 sub check_files {
30497c6a3eca Initial revision
casties
parents:
diff changeset
216 my ($to_archive, $archived) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
217
30497c6a3eca Initial revision
casties
parents:
diff changeset
218 my $nt = scalar keys %$to_archive;
30497c6a3eca Initial revision
casties
parents:
diff changeset
219 my $na = scalar keys %$archived;
30497c6a3eca Initial revision
casties
parents:
diff changeset
220
30497c6a3eca Initial revision
casties
parents:
diff changeset
221 foreach my $ft (sort keys %$to_archive) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
222 my $fp = "$docdir/$ft";
30497c6a3eca Initial revision
casties
parents:
diff changeset
223 #print " fp: $fp\n";
30497c6a3eca Initial revision
casties
parents:
diff changeset
224 if ($$archived{$fp}) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
225 logger('DEBUG', "$ft archived OK");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
226 $$archived{$fp} = "OK";
30497c6a3eca Initial revision
casties
parents:
diff changeset
227 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
228 logger('ERROR', "file '$ft' missing from archive!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
229 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
230 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
231 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
232
30497c6a3eca Initial revision
casties
parents:
diff changeset
233 foreach my $fa (sort keys %$archived) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
234 if ($$archived{$fa} ne "OK") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
235 my ($fn, $fp) = MPIWGStor::split_file_path($fa);
30497c6a3eca Initial revision
casties
parents:
diff changeset
236 if ($MPIWGStor::index_files{$fn}) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
237 logger('DEBUG', "$fa ignored");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
238 $na--;
30497c6a3eca Initial revision
casties
parents:
diff changeset
239 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
240 logger('WARNING', "$fa archived but not in list!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
241 $warncnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
242 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
243 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
244 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
245
30497c6a3eca Initial revision
casties
parents:
diff changeset
246 if ($nt > $na) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
247 logger('WARNING', "less files were archived ($na vs. $nt)!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
248 } elsif ($na > $nt) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
249 logger('WARNING', "more files were archived ($na vs. $nt)!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
250 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
251
30497c6a3eca Initial revision
casties
parents:
diff changeset
252 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
253
30497c6a3eca Initial revision
casties
parents:
diff changeset
254
30497c6a3eca Initial revision
casties
parents:
diff changeset
255 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
256 # delete_files(\%files)
30497c6a3eca Initial revision
casties
parents:
diff changeset
257 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
258 # deletes the files from the list (of absolute files) and their directories
30497c6a3eca Initial revision
casties
parents:
diff changeset
259 # if they are empty
30497c6a3eca Initial revision
casties
parents:
diff changeset
260 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
261 sub delete_files {
30497c6a3eca Initial revision
casties
parents:
diff changeset
262 my ($files) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
263 my %dirs;
30497c6a3eca Initial revision
casties
parents:
diff changeset
264
30497c6a3eca Initial revision
casties
parents:
diff changeset
265 foreach my $f (sort keys %$files) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
266 my ($fn, $fp) = MPIWGStor::split_file_path($f);
30497c6a3eca Initial revision
casties
parents:
diff changeset
267 # collect all unique directories
30497c6a3eca Initial revision
casties
parents:
diff changeset
268 if ($fp && (! $dirs{$fp})) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
269 $dirs{$fp} = $fp;
30497c6a3eca Initial revision
casties
parents:
diff changeset
270 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
271 # don't delete index files
30497c6a3eca Initial revision
casties
parents:
diff changeset
272 next if ($MPIWGStor::index_files{$fn});
30497c6a3eca Initial revision
casties
parents:
diff changeset
273 # no file no delete
30497c6a3eca Initial revision
casties
parents:
diff changeset
274 next unless (-f $f);
30497c6a3eca Initial revision
casties
parents:
diff changeset
275 # delete files
30497c6a3eca Initial revision
casties
parents:
diff changeset
276 if (unlink $f) {
23
a3c35eae25dc added count in archivemany
casties
parents: 18
diff changeset
277 logger('DEBUG', "remove $f ($fn)");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
278 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
279 logger('ERROR', "unable to delete $f!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
280 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
281 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
282 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
283 # try to delete all empty directories
30497c6a3eca Initial revision
casties
parents:
diff changeset
284 my @dirkeys = sort keys %dirs;
30497c6a3eca Initial revision
casties
parents:
diff changeset
285 # starting at the end to get to the subdirectories first
30497c6a3eca Initial revision
casties
parents:
diff changeset
286 for (my $i = $#dirkeys; $i >= 0; $i--) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
287 my $d = $dirkeys[$i];
30497c6a3eca Initial revision
casties
parents:
diff changeset
288 # dont't remove document dir (shouldn't be empty anyway)
30497c6a3eca Initial revision
casties
parents:
diff changeset
289 next if ($d eq $docdir);
30497c6a3eca Initial revision
casties
parents:
diff changeset
290 if (-d $d) {
23
a3c35eae25dc added count in archivemany
casties
parents: 18
diff changeset
291 logger('DEBUG', "remove dir $d");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
292 rmdir $d;
30497c6a3eca Initial revision
casties
parents:
diff changeset
293 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
294 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
295 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
296
30497c6a3eca Initial revision
casties
parents:
diff changeset
297
30497c6a3eca Initial revision
casties
parents:
diff changeset
298 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
299 # delete_all_files(\%files, $dir)
30497c6a3eca Initial revision
casties
parents:
diff changeset
300 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
301 # deletes all files with names from the list %files
30497c6a3eca Initial revision
casties
parents:
diff changeset
302 # in the directory $dir and its subdirectories
30497c6a3eca Initial revision
casties
parents:
diff changeset
303 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
304 sub delete_all_files {
30497c6a3eca Initial revision
casties
parents:
diff changeset
305 my ($files, $dir) = @_;
30497c6a3eca Initial revision
casties
parents:
diff changeset
306
30497c6a3eca Initial revision
casties
parents:
diff changeset
307 if (! opendir DIR, $dir) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
308 logger('ERROR', "unable to read directory $dir!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
309 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
310 return;
30497c6a3eca Initial revision
casties
parents:
diff changeset
311 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
312 my @fl = readdir DIR;
30497c6a3eca Initial revision
casties
parents:
diff changeset
313 closedir DIR;
30497c6a3eca Initial revision
casties
parents:
diff changeset
314
30497c6a3eca Initial revision
casties
parents:
diff changeset
315 foreach my $f (@fl) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
316 next if ($f =~ /^\.{1,2}$/);
30497c6a3eca Initial revision
casties
parents:
diff changeset
317 if ($$files{$f}) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
318 # $f is in the file list
30497c6a3eca Initial revision
casties
parents:
diff changeset
319 if (-f "$dir/$f") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
320 # $f is a file
30497c6a3eca Initial revision
casties
parents:
diff changeset
321 if (unlink "$dir/$f") {
23
a3c35eae25dc added count in archivemany
casties
parents: 18
diff changeset
322 logger('DEBUG', "removed $f");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
323 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
324 logger('ERROR', "unable to delete $f!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
325 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
326 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
327 } elsif (-d _) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
328 # $f is a directory (unlink won't work)
30497c6a3eca Initial revision
casties
parents:
diff changeset
329 if ((system 'rm', '-r', "$dir/$f") == 0) {
23
a3c35eae25dc added count in archivemany
casties
parents: 18
diff changeset
330 logger('DEBUG', "removed directory $f");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
331 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
332 logger('ERROR', "unable to delete directory $f!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
333 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
334 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
335 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
336 logger('ERROR', "funny object $dir/$f!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
337 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
338 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
339 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
340 # $f is not in the list
30497c6a3eca Initial revision
casties
parents:
diff changeset
341 if (-d "$dir/$f") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
342 # recurse into directories
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
343 logger('DEBUG', "enter $dir/$f");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
344 delete_all_files($files, "$dir/$f");
30497c6a3eca Initial revision
casties
parents:
diff changeset
345 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
346 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
347 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
348 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
349
30497c6a3eca Initial revision
casties
parents:
diff changeset
350
30497c6a3eca Initial revision
casties
parents:
diff changeset
351 #######################################################
30497c6a3eca Initial revision
casties
parents:
diff changeset
352 # main
30497c6a3eca Initial revision
casties
parents:
diff changeset
353 #
30497c6a3eca Initial revision
casties
parents:
diff changeset
354
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
355 logger('START', "archiver $version at $archdate");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
356
30497c6a3eca Initial revision
casties
parents:
diff changeset
357 # make shure the right user is running this program
30497c6a3eca Initial revision
casties
parents:
diff changeset
358 my $user = getlogin;
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
359 if (($user ne "archive")&&($user ne "root")) {
23
a3c35eae25dc added count in archivemany
casties
parents: 18
diff changeset
360 logger("WARNING", "you ($user) should be archive or root user to run this program!");
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
361 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
362
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
363 # check for .archived file
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
364 if (-f "$docdir/.archived") {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
365 if (not $force_archive) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
366 logger('ABORT', "already archived! (.archived file exists)");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
367 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
368 } else {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
369 logger('WARNING', "resource already archived? (.archived file exists)");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
370 $warncnt++;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
371 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
372 }
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
373
30497c6a3eca Initial revision
casties
parents:
diff changeset
374 # use metacheck first
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
375 if (open CHECK, "$checkprog -add-files $docdir |") {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
376 my @errors;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
377 my $msg;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
378 while (<CHECK>) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
379 chomp;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
380 if (/^ERROR/) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
381 push @errors, $_;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
382 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
383 $msg = $_;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
384 }
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
385 if ($msg =~ /^DONE/) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
386 logger('DEBUG', "checking index file: $msg");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
387 logger('INFO', "resource '$docdir' check OK");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
388 } else {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
389 logger('DEBUG', "errors checking index file:\n " . join("\n ", @errors) . "\n $msg");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
390 logger('ABORT', "resource '$docdir' check failed!");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
391 exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
392 }
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
393 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
394 logger('ABORT', "unable to run $checkprog");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
395 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
396 }
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
397 # if (system("$checkprog $docdir >/dev/null") == 0) {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
398 # logger('INFO', "resource '$docdir' check OK");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
399 # } else {
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
400 # logger('ABORT', "resource '$docdir' check failed!!");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
401 # exit 1;
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
402 # }
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
403
30497c6a3eca Initial revision
casties
parents:
diff changeset
404 # read index.meta file
30497c6a3eca Initial revision
casties
parents:
diff changeset
405 my ($document, $rootnode) = MPIWGStor::read_xml($metafile);
30497c6a3eca Initial revision
casties
parents:
diff changeset
406
30497c6a3eca Initial revision
casties
parents:
diff changeset
407 # check file and add archive date
30497c6a3eca Initial revision
casties
parents:
diff changeset
408 my $files_to_archive = read_resource_meta($rootnode);
30497c6a3eca Initial revision
casties
parents:
diff changeset
409
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
410 logger('INFO', (scalar keys %$files_to_archive) . " files to archive");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
411
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
412 # remove .archived file
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
413 if (-f "$docdir/.archived") {
30497c6a3eca Initial revision
casties
parents:
diff changeset
414 if (unlink "$docdir/.archived") {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
415 logger('WARNING', "existing .archived file has been removed!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
416 $warncnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
417 } else {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
418 logger('ERROR', "unable to remove existing .archived file!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
419 $errcnt++;
30497c6a3eca Initial revision
casties
parents:
diff changeset
420 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
421 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
422
30497c6a3eca Initial revision
casties
parents:
diff changeset
423 # remove junk files
30497c6a3eca Initial revision
casties
parents:
diff changeset
424 if ($delete_junk_files) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
425 delete_all_files(\%MPIWGStor::junk_files, $docdir);
30497c6a3eca Initial revision
casties
parents:
diff changeset
426 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
427
30497c6a3eca Initial revision
casties
parents:
diff changeset
428 # write new index.meta
30497c6a3eca Initial revision
casties
parents:
diff changeset
429 if ($errcnt > 0) {
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
430 logger('ABORT', "there were errors!");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
431 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
432 } else {
30497c6a3eca Initial revision
casties
parents:
diff changeset
433 if ($fix_xml) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
434 MPIWGStor::write_xml($document, $metafile);
30497c6a3eca Initial revision
casties
parents:
diff changeset
435 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
436 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
437
30497c6a3eca Initial revision
casties
parents:
diff changeset
438 # start archiving
5
1a377102b1ce small fix for number of hash keys
casties
parents: 1
diff changeset
439 my $archived_files = run_archive();
1a377102b1ce small fix for number of hash keys
casties
parents: 1
diff changeset
440 my $num_archfiles = scalar keys %$archived_files;
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
441
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
442 logger('INFO', "$num_archfiles files archived");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
443
30497c6a3eca Initial revision
casties
parents:
diff changeset
444 # check list of archived files
30497c6a3eca Initial revision
casties
parents:
diff changeset
445 check_files($files_to_archive, $archived_files);
30497c6a3eca Initial revision
casties
parents:
diff changeset
446
30497c6a3eca Initial revision
casties
parents:
diff changeset
447 # delete files if all went OK
30497c6a3eca Initial revision
casties
parents:
diff changeset
448 if ($errcnt == 0) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
449 system("touch", "$docdir/.archived");
30497c6a3eca Initial revision
casties
parents:
diff changeset
450 # remove junk files (again)
30497c6a3eca Initial revision
casties
parents:
diff changeset
451 if ($delete_junk_files) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
452 delete_all_files(\%MPIWGStor::junk_files, $docdir);
30497c6a3eca Initial revision
casties
parents:
diff changeset
453 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
454 # remove archived files
30497c6a3eca Initial revision
casties
parents:
diff changeset
455 if ($delete_data_files) {
30497c6a3eca Initial revision
casties
parents:
diff changeset
456 delete_files($archived_files);
30497c6a3eca Initial revision
casties
parents:
diff changeset
457 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
458 }
30497c6a3eca Initial revision
casties
parents:
diff changeset
459
18
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
460 logger('INFO', "$warncnt warnings");
fdf4ceb36db1 fixed problem with dir names in metacheck
casties
parents: 11
diff changeset
461 logger('INFO', "$errcnt errors");
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
462 if ($errcnt > 0) {
11
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
463 logger('ABORT', "there were errors! ($num_archfiles files archived) at " . stime(time));
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
464 exit 1;
30497c6a3eca Initial revision
casties
parents:
diff changeset
465 } else {
11
b19df18aa19a updated command line option parsing and logging
casties
parents: 5
diff changeset
466 logger('DONE', "$num_archfiles files archived at " . stime(time));
0
30497c6a3eca Initial revision
casties
parents:
diff changeset
467 }