bin/archive-dir

Tue, 17 Jan 2017 21:31:24 -0500

author
Meredith Howard <mhoward@roomag.org>
date
Tue, 17 Jan 2017 21:31:24 -0500
changeset 476
0938f35f78f9
child 477
eeafc178ddc4
permissions
-rwxr-xr-x

add archive-dir

476
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
1 #!/usr/bin/env perl
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
2
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
3 use warnings;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
4 use strict;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
5 use Time::Piece;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
6 use Path::Tiny;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
7 use Getopt::Long::Descriptive;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
8
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
9 sub main {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
10 my ($opt, $usage) = describe_options(
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
11 'archive-dir %o <directory> ...',
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
12 ['dest|d=s' => "destination path"],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
13 ['subdir' => hidden => {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
14 one_of => [
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
15 ['year|y' => 'file by year'],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
16 ['month|m' => 'file by month'],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
17 ],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
18 default => 'year',
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
19 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
20 ],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
21 ['age|a=i' => "minimum age in days for archival", {default => 60}],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
22 ['yes|y' => "actually move files"],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
23 ['help|h' => "print usage message and exit", {shortcircuit => 1}],
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
24 );
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
25 print($usage->text), exit if $opt->help;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
26
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
27 archive_dir($opt, $_) for @ARGV;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
28 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
29
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
30 sub archive_dir {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
31 my $opt = shift;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
32 my $dir = path(shift);
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
33 my $destdir = path($opt->dest // '.');
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
34 $destdir = $dir->child($destdir) if $destdir->is_relative;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
35 my $destfmt = ($opt->subdir eq 'month') ? '%Y-%m' : '%Y';
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
36 my $nowish = time;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
37 my $age = $opt->age * 24 * 60 * 60;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
38
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
39 for my $child ($dir->children) {
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
40 next if $child->is_dir && ($child eq $destdir || $child =~ /^(?:\d{4}|\d\d)$/);
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
41
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
42 my $mtime = $child->stat->mtime;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
43 next unless ($nowish - $mtime) >= $age;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
44
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
45 my $dest = $destdir->child(localtime($mtime)->strftime($destfmt));
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
46
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
47 print "$dest \t $child\n";
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
48
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
49 next unless $opt->yes;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
50 $dest->mkpath;
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
51 $child->move($dest . '/' . $child->relative($dir));
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
52 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
53 }
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
54
0938f35f78f9 add archive-dir
Meredith Howard <mhoward@roomag.org>
parents:
diff changeset
55 main();

mercurial