-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestore
executable file
·305 lines (290 loc) · 9.98 KB
/
restore
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
#! /usr/bin/perl
use strict;
use Cwd 'abs_path';
use File::Path;
use IO::Compress::Gzip;
my $usage =
"usage: restore [-n] [-f] [-i] [-v]... backup_dir [-x exclude]... [dest]\n" .
" -n = dry run\n" .
" -f = overwrite existing files\n" .
" -i = interactive shell\n" .
" -v = verbose (may be repeated)\n" .
" exclude is a directory name relative to backup_dir\n" .
" eg. restore /media/sf_backup/home/markn/src/less-481 less-481.restored\n" .
" or. restore /media/sf_backup/home/markn /home/markn.new\n";
my $interactive_help =
"ls[-l] [PATH] List files and directories\n" .
"cd PATH Change working directory\n" .
"pwd Print working directory\n" .
"get PATH Restore file.\n" .
"[-+]v Change verboseness\n";
my $verbose = 0;
my $dryrun = 0;
my $force = 0;
my $interactive = 0;
my $stats;
exit (main() ? 0 : 1);
# --------------------------------------------------------------------
sub main {
my $args = parse(@ARGV);
die $usage if not $args;
$$args{dest} = '.' if not $$args{dest};
return interactive($$args{backup_dir}, $$args{dest}) if $interactive;
print "START ", $0, " ", scalar gmtime(), "\n" if $verbose;
my $ok = restore($$args{backup_dir}, $$args{dest}, $$args{excludes});
print "END ", $0, " ", scalar gmtime(), "\n" if $verbose;
return $ok;
}
# --------------------------------------------------------------------
sub interactive {
my ($backup_dir, $dest_dir) = @_;
chdir($backup_dir);
print("chdir $backup_dir; now ",Cwd::getcwd(),"\n") if $verbose > 1;
my $term;
my $tty;
if (eval "require Term::ReadLine::Tiny") {
$term = new Term::ReadLine::Tiny;
}
if (not $term) {
if (not open $tty, "/dev/tty") {
print "cannot open tty: $!\n";
return;
}
}
for (;;) {
my $line;
if ($term) {
$line = $term->readline(">");
} else {
print ">";
$line = <$tty>;
}
chomp $line;
my ($cmd, $arg) = $line =~ /^\s*([^\s]+)\s*(.*)/;
if (not $cmd) {
;
} elsif ($cmd eq 'q' or $cmd eq 'quit' or $cmd eq 'exit') {
last;
} elsif ($cmd eq 'ls' or $cmd eq 'ls-l') {
if ($arg =~ /^\s*-l\s+(.*)/) {
$arg = $1;
$cmd = 'ls-l';
}
dir_list($arg, $cmd eq 'ls-l');
} elsif ($cmd eq 'cd') {
if (not chdir($arg)) {
print "cannot chdir $arg: $!\n";
}
} elsif ($cmd eq 'pwd') {
print Cwd::getcwd(), "\n";
} elsif ($cmd eq 'restore' or $cmd eq 'get') {
$arg = Cwd::abs_path($arg);
if (not $arg) {
print $interactive_help;
} elsif (substr($arg, 0, length($backup_dir)) ne $backup_dir) {
print "$arg is not under $backup_dir\n";
} else {
my %stats;
$stats = \%stats;
my $suffix = substr($arg, length($backup_dir));
restore($arg, "$dest_dir/$suffix");
$stats = undef;
print_stats(\%stats);
}
} elsif ($cmd eq '-f') {
$force = 1;
print "force $force\n";
} elsif ($cmd eq '+f') {
$force = 0;
print "force $force\n";
} elsif ($cmd eq '-v') {
++$verbose;
print "verbose $verbose\n";
} elsif ($cmd eq '+v') {
--$verbose if $verbose > 0;
print "verbose $verbose\n";
} elsif ($cmd eq 'h' or $cmd eq '?' or $cmd eq 'help') {
print $interactive_help;
} else {
print "$cmd?\n";
}
}
close $tty if $tty;
}
# --------------------------------------------------------------------
sub dir_list {
my ($dir, $long) = @_;
$dir = Cwd::getcwd() if not $dir;
my $d;
if (not opendir $d, $dir) {
print "cannot open dir $dir: $!\n";
return;
}
while (my $entry = readdir $d) {
next if $entry eq '.' or $entry eq '..';
my $file = "$dir/$entry";
if ($entry =~ /^\d{10}$/ and -f $file) {
print "B $entry (",htime($entry,$long),")\n";
} else {
my $vers = versions($file);
my $ch = ($vers < 0) ? '? ' : ($vers == 0) ? 'd ' : sprintf('%-2d', $vers);
print "$ch $entry\n";
}
}
closedir $d;
}
# --------------------------------------------------------------------
# >0 = arg is a file (leaf); return number of versions
# 0 = arg is a dir
# -1 = unrelated file or error
sub versions {
my ($dir) = @_;
return -1 if not -d $dir;
my $d;
return -1 if not opendir $d, $dir;
my $vers = 0;
while (my $entry = readdir $d) {
++$vers if $entry =~ /^\d{10}$/;
}
closedir $d;
return $vers;
}
# --------------------------------------------------------------------
sub print_stats {
my ($stats) = @_;
printf "%d restored, %d skipped (-f to override), %d failed\n",
$stats->{ok}, $stats->{exists}, $stats->{fail};
}
# --------------------------------------------------------------------
sub restore {
my ($backup_dir, $dest_dir, $excludes) = @_;
print "RESTOR $backup_dir to $dest_dir\n" if $verbose > 1;
my $ok = 1;
my $latest_version = 0;
my $curr_file;
# List all backup files, choose the latest version of each.
my $find = "find ".quote($backup_dir);
foreach my $exclude (@$excludes) {
$find .= " -path ".quote($exclude)." -type d -prune -o";
}
$find .= " -regex '.*/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' -print";
print "EXEC $find\n" if $verbose > 1;
if (not open FILES, "$find|") {
print "ERROR cannot execute \"$find\": $!\n";
$ok = 0;
} else {
while (<FILES>) {
chomp;
my ($file, $version) = m|^(.*)/([^/]+)/*$|;
print "check ($_) file $file, version $version\n" if $verbose > 1;
if (substr($file,0,length($backup_dir)) ne $backup_dir) {
print "$file is not under $backup_dir ???\n";
} else {
$file = substr($file, length($backup_dir)+1);
if (length($file) == 0) {
($backup_dir,$file) = $backup_dir =~ m|^(.*)/([^/]+)/*$|;
$dest_dir =~ s:/[^/]+$::;
}
}
next if not $file or not $version;
if (defined $curr_file and $file ne $curr_file) {
# This is a new file. We've seen all versions of
# the previous file so restore the best one.
$ok = 0 if not
restore_file($backup_dir, $dest_dir, $curr_file, $latest_version);
$latest_version = 0;
}
$latest_version = $version if $version > $latest_version;
$curr_file = $file;
}
# Restore the final file.
$ok = 0 if $latest_version and not
restore_file($backup_dir, $dest_dir, $curr_file, $latest_version);
close FILES;
}
print "DONE $backup_dir to $dest_dir\n" if $verbose > 1;
return $ok;
}
# --------------------------------------------------------------------
sub restore_file {
my ($backup_dir, $dest_dir, $file, $version) = @_;
my $backup_file = "$backup_dir/$file/$version";
my $dest_file = "$dest_dir/$file";
$dest_file =~ s/%(..)/chr(hex($1))/gex;
if (not $force and -r $dest_file) {
print "EXISTS $dest_file\n" if $verbose;
$stats->{exists}++;
return 1;
}
print "UNZIP $dest_file\n" if $verbose;
my $dest_dir = $dest_file;
$dest_dir =~ s:/[^/]+$::; # remove the final (file) component
my $cmd = "if [ -f ".quote($dest_dir)." ]; then rm -f ".quote($dest_dir)."; fi; mkdir -p ".quote($dest_dir)." && gunzip -c ".quote($backup_file)." > ".quote($dest_file);
print "EXEC $cmd\n" if $verbose > 2;
return 1 if $dryrun;
if (system($cmd) != 0) {
$stats->{fail}++;
return 0
}
my ($dev,$ino,$mode) = stat($backup_file);
chmod ($mode & 0777), $dest_file;
utime $version, $version, $dest_file;
$stats->{ok}++;
return 1;
}
# --------------------------------------------------------------------
sub parse {
my %args;
my @excludes;
while (@_) {
my $arg = shift @_;
if ($arg eq '-x') {
push @excludes, (shift @_);
} elsif ($arg eq '-?') {
return undef;
} elsif ($arg eq '-f') {
$force = 1;
} elsif ($arg eq '-i') {
$interactive = 1;
} elsif ($arg eq '-n') {
$dryrun = 1;
} elsif ($arg eq '-v') {
$verbose++;
} elsif ($arg eq '-r') {
; # compat: remove
} elsif ($arg =~ /^-/) {
printf "ERROR invalid command line option: $arg\n";
return undef;
} else {
if (not defined $args{backup_dir}) {
if (not -d $arg) {
print "ERROR $arg is not a directory\n";
return undef;
}
$args{backup_dir} = Cwd::abs_path($arg);
} elsif (not defined $args{dest}) {
mkdir $arg if not -d $arg;
$args{dest} = Cwd::abs_path($arg);
} else {
print "ERROR too many arguments\n";
return undef;
}
}
}
return undef if not $args{dest};
$args{excludes} = \@excludes;
return \%args;
}
# --------------------------------------------------------------------
sub htime {
my ($t) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday) = gmtime($t);
return sprintf("%d/%d/%d@%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min);
}
# --------------------------------------------------------------------
sub quote {
my ($str) = @_;
return "'$str'" if $str !~ /'/;
print "WARN cannot quote $str\n" if $str =~ /"/;
return "\"$str\"";
}