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
|
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use IPC::Run3;
use Path::Tiny;
use Getopt::Long qw/ GetOptions /;
my %options = ();
GetOptions(
\%options,
qw{ verbose encrypt sign delete }
) or die "Error in command line arguments\n";
$options{encrypt} = $ENV{LEO_ENCRYPT};
$options{sign} = $ENV{LEO_SIGN};
$options{delete} = $ENV{LEO_DELETE};
my $gpg_fingerprint = "D9AE4AEEE1F1B3598E81D9DFB67D55D482A799FD";
my $archive_dir = "/tmp/archive";
my $ymd = ymd(); # YYYY-MM-DD.
path($archive_dir)->mkpath; # Create archive directory.
my $prof;
my %profile = (
journal => [qw( documents/andinus.org.gpg
documents/archive.org.gpg )],
emacs => [qw( .emacs.d .elfeed .org-timestamps )],
config => [qw( .config .kshrc .kshrc.d .tmux.conf .xsession .remind
.screenlayout .mg .mbsyncrc .fehbg .profile .plan
.authinfo.gpg .Xresources )],
);
# Add more directories to %profile.
foreach my $tmp_prof (qw( emails music projects documents videos pictures
downloads .ssh .password-store .mozilla )) {
$profile{$tmp_prof} = [$tmp_prof];
}
# Aliases.
$profile{ssh} = $profile{".ssh"};
$profile{pass} = $profile{".password-store"};
$profile{mozilla} = $profile{".mozilla"};
HelpMessage() and exit 0 if scalar @ARGV == 0;
foreach my $arg ( @ARGV ) {
$prof = $arg; # Set $prof.
if ( $profile{ $arg } ) {
say "++++++++********++++++++";
# No encryption for journal profile.
my $tmp = $options{encrypt} and undef $options{encrypt}
if $prof eq "journal" and $options{encrypt};
# Deref the array here because we want flattened list.
archive("$archive_dir/${arg}_$ymd.tar", $profile{$arg}->@*);
$options{encrypt} = $tmp if $prof eq "journal";
} else {
warn "[WARN] leo: no such option :: $arg \n";
}
}
# User must pass $tar_file first.
sub archive {
my $tar_file = shift @_;
my @archive_paths = @_;
say "Archive: $tar_file";
warn "[WARN] $tar_file exists, might overwrite.\n" if -e $tar_file;
print "\n";
# All paths should be relative to $ENV{HOME}.
tar_create($tar_file, "-C", $ENV{HOME}, @_);
$? # tar returns 1 on errors.
? die "Archive creation failed :: $?\n"
# Print absolute paths for all archived files/directories.
: say path($_)->absolute($ENV{HOME}), " archived."
foreach @archive_paths;
print "\n" and tar_list($tar_file) if $options{verbose};
encrypt_sign($tar_file) if $options{encrypt} or $options{sign};
}
# Encrypt, Sign archives.
sub encrypt_sign() {
my $file = shift @_;
my @options = ();
push @options, "--recipient", $gpg_fingerprint, "--encrypt"
if $options{encrypt};
push @options, "--sign" if $options{sign};
push @options, "--verbose" if $options{verbose};
say "\nEncrypt/Sign: $file";
warn "[WARN] $file.gpg exists, might overwrite.\n" if -e "$file.gpg";
run3 ["gpg2", "--yes", "-o", "$file.gpg", @options, $file];
$? # We assume non-zero is an error.
? die "Encrypt/Sign failed :: $?\n"
: print "\nOutput: $file.gpg";
print " [Encrypted]" if $options{encrypt};
print " [Signed]" if $options{sign};
print "\n";
unlink $file and say "$file deleted."
or warn "[WARN] Could not delete $file: $!\n"
if $options{delete};
}
sub HelpMessage {
say qq{Archive files to $archive_dir.
Profile:};
foreach my $prof (sort keys %profile) {
next if substr($prof, 0, 1) eq "."; # Profiles starting with a
# dot will have alias.
print " $prof\n";
print " $_\n" foreach $profile{$prof}->@*;
}
say qq{
Options:
--encrypt
Encrypt files with $gpg_fingerprint
--sign
Sign files with $gpg_fingerprint
--delete
Delete the archive after running gpg2
--verbose};
}
sub tar_create { run3 ["/bin/tar", "cf", @_]; }
sub tar_list { run3 ["/bin/tar", "tvf", @_]; }
sub ymd {
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime(time);
$year += 1900; # $year contains the number of years since 1900.
# $mon the month in the range 0..11 , with 0 indicating January
# and 11 indicating December.
my @months = qw( 01 02 03 04 05 06 07 08 09 10 11 12 );
my $month = $months[$mon];
return "$year-$month-$mday";
}
|