/
dirvish-restore.pl
executable file
·320 lines (281 loc) · 9.18 KB
/
dirvish-restore.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/perl
# dirvish-locate
# 1.3.X series
# Copyright 2005 by the dirvish project
# http://www.dirvish.org
#
# Last Revision : $Rev: 656 $
# Revision date : $Date: 2009-02-08 00:23:29 +0100 (So, 08 Feb 2009) $
# Last Changed by : $Author: tex $
# Stored as : $HeadURL: https://secure.id-schulz.info/svn/tex/priv/dirvish_1_3_1/dirvish-restore.pl $
# Reference : http://wiki.dirvish.org/index.cgi?RestoreSomeFiles
#########################################################################
# #
# Licensed under the Open Software License version 2.0 #
# #
# This program is free software; you can redistribute it #
# and/or modify it under the terms of the Open Software #
# License, version 2.0 by Lauwrence E. Rosen. #
# #
# This program is distributed in the hope that it will be #
# useful, but WITHOUT ANY WARRANTY; without even the implied #
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR #
# PURPOSE. See the Open Software License for details. #
# #
#########################################################################
#
#----------------------------------------------------------------------------
# Revision information
#----------------------------------------------------------------------------
my %CodeID = (
Rev => '$Rev: 656 $',
Date => '$Date: 2009-02-08 00:23:29 +0100 (So, 08 Feb 2009) $',
Author => '$Author: tex $',
URL =>
'$HeadURL: https://secure.id-schulz.info/svn/tex/priv/dirvish_1_3_1/dirvish-restore.pl $',
);
$VERSION = $CodeID{URL};
$VERSION =~ s#^.*dirvish_##; # strip off the front
$VERSION =~ s#\/.*##; # strip off the rear after the last /
$VERSION =~ s#[_-]#.#g; # _ or - to "."
#----------------------------------------------------------------------------
# Modules and includes
#----------------------------------------------------------------------------
use Time::ParseDate;
use POSIX qw(strftime);
use File::Find;
use Getopt::Long;
use strict;
use warnings;
use Dirvish;
#----------------------------------------------------------------------------
# Initialisation
#----------------------------------------------------------------------------
$SIG{TERM} = \&sigterm; # handles "kill <PID>"
$SIG{INT} = \&sigterm; # handles Ctrl+C or "kill -2 <PID>"
my $KILLCOUNT = 1000;
my $MAXCOUNT = 100;
my $Options = reset_options( \&usage, @ARGV ); # initialize the %$Options hash
load_master_config( 'f', $Options );
GetOptions(
$Options, qw(
version
dry-run|no-run
help|?
)
)
or &usage();
my $Vault = shift;
my $Branch = "default";
$Vault =~ /:/ and ( $Vault, $Branch ) = split( /:/, $Vault );
my $Pattern = shift;
my $Image = shift;
$Vault && length($Pattern) or &usage();
if ( $Pattern =~ m/^(\*|\?)/ ) {
$Pattern = "." . $Pattern; # prepend dot if asterisk or question mark is
# the first character. Make rsync-like patterns like *.xml work.
}
my $fullpattern = $Pattern;
my $partpattern = undef;
$fullpattern =~ /\$$/ or $fullpattern .= '[^/]*$';
( $partpattern = $fullpattern ) =~ s/^\^//;
my $bank = undef;
for $b ( @{ $$Options{bank} } ) {
-d "$b/$Vault" and $bank = $b;
}
$bank or seppuku 220, "No such vault: $Vault";
my @invault = ();
if ($Image) {
push( @invault, $Image );
}
else {
opendir VAULT, "$bank/$Vault" or seppuku 221, "cannot open vault: $Vault";
@invault = readdir(VAULT);
closedir VAULT;
}
# Load vault specific configuration, we need access to the tree value
loadconfig('f', "$bank/$Vault/dirvish/$Branch.conf", $Options);
# build a list of avilable images
my @images = ();
for my $image (@invault) {
$image eq 'dirvish' and next;
my $imdir = "$bank/$Vault/$image";
-f "$imdir/summary" or next;
( -l $imdir && $imdir =~ /current/ ) and next; # skip 'current'-symlink
my $conf = loadconfig( 'R', "$imdir/summary", $Options ) or next;
$$conf{Status} eq 'success' || $$conf{Status} =~ /^warn/
or next;
$$conf{'Backup-complete'} or next;
$Branch && $$conf{branch} ne $Branch and next;
unshift @images,
{
imdir => $imdir,
image => $$conf{Image},
branch => $$conf{branch},
created => $$conf{'Backup-complete'},
};
}
my $imagecount = 0;
my $pathcount = 0;
my $path = undef;
my %match = ();
# search the images
foreach my $image ( sort( imsort_locate @images ) ) {
my $imdir = $$image{imdir};
my $index = undef;
-f "$imdir/index.bz2" and $index = "bzip2 -d -c $imdir/index.bz2|";
-f "$imdir/index.gz" and $index = "gzip -d -c $imdir/index|";
-f "$imdir/index" and $index = "<$imdir/index";
$index or next;
++$imagecount;
# can't use three-fold open here, see above
open( INDEX, $index ) or next;
while (<INDEX>) {
chomp;
m($partpattern) or next;
# this parse operation is too slow. It might be faster as a
# split with trimmed leading whitespace and remerged modtime
my $f = { image => $image };
(
$$f{inode}, $$f{blocks}, $$f{perms}, $$f{links}, $$f{owner},
$$f{group}, $$f{bytes}, $$f{mtime}, $$f{path}
)
= m<^
\s*(\S+) # inode
\s+(\S+) # block count
\s+(\S+) # perms
\s+(\S+) # link count
\s+(\S+) # owner
\s+(\S+) # group
\s+(\S+) # byte count
\s+(\S+\s+\S+\s+\S+) # date
\s+(\S.*) # path
$>x;
$$f{perms} =~ /^[dl]/ and next;
$$f{path} =~ m($fullpattern) or next;
exists( $match{ $$image{image} } ) or ++$imagecount;
++$pathcount;
push @{ $match{ $$image{image} } }, $f;
}
if ( $pathcount >= $KILLCOUNT ) {
print "dirvish-restore: too many paths match pattern, interrupting search\n";
last;
}
}
printf "Found %d matches in %d images\n", $pathcount, $imagecount;
$pathcount >= $MAXCOUNT
and printf "Pattern '%s' too vague, listing paths only.\n", $Pattern;
my $last = undef;
my $linesize = 0;
# Print results
print "Select an image to restore from:\n" unless $Image;
my $num = 0;
@images = ();
for my $image ( sort keys %match ) {
$last = undef;
print "[$num] - $image\n";
$num++;
push( @images, $image );
if ( scalar( @{ $match{$image} } ) > 10 ) {
print "\t" . scalar( @{ $match{$image} } ) . " files.\n";
}
else {
for my $hit ( @{ $match{$image} } ) {
print "\tFile: $$hit{path}\n";
}
}
}
if ($num) {
print "Please select an image to restore from: ";
my $sel = <STDIN>;
chomp($sel);
if ( $sel < scalar(@images) ) {
print "You chose to restore from image: [$sel] - $images[$sel]\n";
print "These files will be restored:\n";
my $image = $images[$sel];
if(scalar( @{ $match{$image} } ) > 10) {
# open pipe to PAGER (or less)
my $pager = $ENV{PAGER} || "less";
$pager = `which $pager`;
chomp($pager);
if(-x $pager) {
open(PAGER, "|$pager");
print PAGER "dirvish-restore: Will restore these files:\n";
print PAGER "Note: Present files will be moved to <filename>.orig.\n\n";
for my $hit ( @{ $match{$image} } ) {
print PAGER "File: $$hit{path}\n";
}
close(PAGER);
} else {
print "\t" . scalar( @{ $match{$image} } ) . " files.\n";
}
} else {
for my $hit ( @{ $match{$image} } ) {
print "\tFile: $$hit{path}\n";
}
}
print "Do you really want to restore these files into their former location? [y/N] ";
$sel = <STDIN>;
chomp($sel);
if ( $sel =~ m/y/i ) {
print "Restoring the files ...\n";
my $ostatus = 0;
for my $hit ( @{ $match{$image} } ) {
print "\tFile: $$hit{path}\n" unless $$Options{quiet};
my $srcpath = $$hit{path};
$srcpath =~ s/$$Options{tree}//;
my $src = "$bank/$Vault/$image/tree".$srcpath;
my $dest = $$hit{path};
my $cmd = "rsync -qrltHpgoDx --numeric-ids --backup --suffix=orig \"$src\" \"$dest\"";
#print "$cmd\n" unless $$Options{quiet};
my $status = (system($cmd) >> 8) unless $$Options{'dry-run'};
$ostatus = $status if (!$$Options{'dry-run'} && $status != 0);
}
print "Done.\n";
exit $ostatus;
}
else {
print "Aborting.\n";
exit 255;
}
}
else {
print "Invalid selection.\n";
exit 255;
}
}
exit 0;
#----------------------------------------------------------------------------
# Subs
#----------------------------------------------------------------------------
# Sort images
sub imsort_locate {
## WARNING: don't mess with the sort order, it is needed so that if
## WARNING: all images are expired the newest will be retained.
$$a{branch} cmp $$b{branch}
|| $$a{created} cmp $$b{created};
}
sub usage {
my $message = shift(@_);
length($message) and print STDERR $message, "\n\n";
print STDERR <<EOUSAGE;
USAGE
dirvish-restorn vault[:branch] pattern [image]
Pattern can be any PCRE.
EOUSAGE
exit 255;
}
# Handle SIGTERM (SIG-15)
sub sigterm {
print STDERR "Received SIGTERM. Aborting running backup ...";
# kill childs - kill(TERM, -$$):
use POSIX;
my $cnt = kill( SIGTERM, -$$ );
no POSIX;
print STDERR "Signaled $cnt processes in current processgroup";
# quit
exit;
}
#----------------------------------------------------------------------------
# EOF
#----------------------------------------------------------------------------