-
Notifications
You must be signed in to change notification settings - Fork 34
/
multiple_backups.pl
52 lines (42 loc) · 1.35 KB
/
multiple_backups.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
#!/usr/bin/perl
# Author: Trizen
# Date: 11 September 2023
# https://github.com/trizen
# Create multiple backups of a list of filenames and update them as necessary.
use 5.036;
use Getopt::Long;
use File::Basename qw(basename);
use File::Copy qw(copy);
use File::Spec::Functions qw(catfile curdir);
my $backup_dir = curdir();
sub usage ($exit_code = 0) {
print <<"EOT";
usage: $0 [options] [filenames]
options:
--dir=s : directory where to save the backups (default: $backup_dir)
EOT
exit($exit_code);
}
GetOptions("d|dir=s" => \$backup_dir,
'h|help' => sub { usage(0) },)
or die("Error in command line arguments\n");
my %timestamps = (
"1h" => 1 / 24,
"1d" => 1,
"3d" => 3,
"30d" => 30,
"1y" => 365,
);
@ARGV || usage(2);
foreach my $file (@ARGV) {
say ":: Processing: $file";
foreach my $key (sort keys %timestamps) {
my $checkpoint_time = $timestamps{$key};
my $backup_file = catfile($backup_dir, basename($file) . '.' . $key);
if (not -e $backup_file or ((-M $backup_file) >= $checkpoint_time)) {
say " > writing backup: $backup_file";
copy($file, $backup_file)
or warn "Can't copy <<$file>> to <<$backup_file>>: $!";
}
}
}