line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
3
|
|
|
3
|
|
1366
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
97
|
|
3
|
3
|
|
|
3
|
|
10
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
84
|
|
4
|
3
|
|
|
3
|
|
9
|
use Fcntl qw( O_CREAT O_EXCL ); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
139
|
|
5
|
3
|
|
|
3
|
|
1397
|
use POSIX (); |
|
3
|
|
|
|
|
82568
|
|
|
3
|
|
|
|
|
69
|
|
6
|
3
|
|
|
3
|
|
1491
|
use URI; |
|
3
|
|
|
|
|
10748
|
|
|
3
|
|
|
|
|
6207
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our @DEFAULT_EXCLUDE = qw( .cache .cpanm .gvfs Downloads Dropbox Trash ); |
9
|
|
|
|
|
|
|
our $SSH = 'ssh'; |
10
|
|
|
|
|
|
|
our $SSH_KEYGEN = 'ssh-keygen'; |
11
|
|
|
|
|
|
|
our $LOCK; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
$ENV{HOME} ||= 'ENVIRONMENT_HOME_IS_NOT_SET'; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
END { |
16
|
1
|
50
|
|
1
|
|
446
|
unlink $LOCK if $LOCK; |
17
|
|
|
|
|
|
|
} |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub run_rsync { |
20
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
21
|
0
|
|
|
|
|
0
|
my $uri = $self->{destination}; |
22
|
0
|
|
|
|
|
0
|
my $lock = sprintf '%s.backup.lock', $self->{config}; |
23
|
0
|
|
|
|
|
0
|
my @options = qw( -az --delete-after --numeric-ids --relative ); |
24
|
|
|
|
|
|
|
|
25
|
0
|
0
|
|
|
|
0
|
push @options, map { qq(--exclude=$_) } @{ $self->{exclude} || [] }; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
26
|
0
|
0
|
|
|
|
0
|
push @options, '--verbose' if $self->{verbose}; |
27
|
0
|
|
|
|
|
0
|
push @options, @{ $self->{source} }; |
|
0
|
|
|
|
|
0
|
|
28
|
0
|
|
|
|
|
0
|
push @options, sprintf '%s@%s:%s/incoming', $uri->userinfo, $self->remote_host, $uri->path; |
29
|
|
|
|
|
|
|
|
30
|
0
|
0
|
|
|
|
0
|
IO::File->new->open($lock, O_CREAT | O_EXCL) or die "Already backing up. ($lock)\n"; |
31
|
0
|
|
|
|
|
0
|
$LOCK = $lock; |
32
|
0
|
|
|
|
|
0
|
$self->_system(rsync => @options); |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub create_sibs_config { |
36
|
1
|
|
|
1
|
|
343
|
my $self = shift; |
37
|
1
|
|
|
|
|
8
|
my $tmp = sprintf '%s.tmp', $self->{config}; |
38
|
|
|
|
|
|
|
|
39
|
1
|
50
|
|
|
|
61
|
open my $CONFIG, '>', $tmp or die "Cannot write $tmp: $!\n"; |
40
|
1
|
|
|
|
|
2
|
local $_; |
41
|
1
|
|
33
|
|
|
8
|
$self->_log($@ || "Creating '$self->{config}' from user input..."); |
42
|
|
|
|
|
|
|
|
43
|
1
|
|
|
|
|
8
|
print $CONFIG "{\n"; |
44
|
1
|
50
|
|
|
|
3
|
printf $CONFIG " email => '%s',\n", $_ if $self->_read('email'); |
45
|
1
|
50
|
|
|
|
11
|
printf $CONFIG " exclude => [qw( %s )],\n", $_ if $self->_read('exclude'); |
46
|
1
|
50
|
|
|
|
6
|
printf $CONFIG " source => [qw( %s )],\n", $_ if $self->_read('source'); |
47
|
1
|
50
|
|
|
|
4
|
printf $CONFIG " destination => '%s',\n", $_ if $self->_read('destination'); |
48
|
1
|
|
|
|
|
5
|
print $CONFIG "}\n"; |
49
|
|
|
|
|
|
|
|
50
|
1
|
50
|
|
|
|
35
|
close $CONFIG or die "Could not write '$tmp': $!\n"; |
51
|
1
|
50
|
|
|
|
51
|
rename $tmp, $self->{config} or die "Could not write '$self->{config}: $!'\n"; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub add_backup_host_to_ssh_config { |
55
|
2
|
|
|
2
|
|
3222
|
my $self = shift; |
56
|
2
|
|
|
|
|
8
|
my $moniker = $self->remote_host; |
57
|
2
|
|
|
|
|
7
|
my $file = $self->ssh_file('config'); |
58
|
|
|
|
|
|
|
|
59
|
2
|
100
|
|
|
|
41
|
if(-r $file) { |
60
|
1
|
50
|
|
|
|
25
|
open my $CONFIG, '<', $file or die "Could not open $file: $!"; |
61
|
1
|
|
|
|
|
8
|
while(<$CONFIG>) { |
62
|
2
|
100
|
|
|
|
23
|
next unless /Host\s+$moniker/; |
63
|
1
|
|
|
|
|
5
|
$self->_log("Host $moniker exists in $file."); |
64
|
1
|
|
|
|
|
9
|
return 1; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
1
|
|
|
|
|
13
|
$self->_log("Adding $moniker to $file"); |
69
|
1
|
50
|
|
|
|
64
|
open my $CONFIG, '>>', $file or die "Cannot write to $file: $!"; |
70
|
1
|
|
|
|
|
4
|
printf $CONFIG "\nHost %s\n", $self->remote_host; |
71
|
1
|
|
|
|
|
5
|
printf $CONFIG " Hostname %s\n", $self->{destination}->host; |
72
|
1
|
|
|
|
|
19
|
printf $CONFIG " IdentityFile %s\n", $self->ssh_file('sibs_dsa'); |
73
|
1
|
|
|
|
|
42
|
close $CONFIG; |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub create_identity_file { |
77
|
1
|
|
|
1
|
|
10025
|
my $self = shift; |
78
|
1
|
|
|
|
|
6
|
my $file = $self->ssh_file('sibs_dsa'); |
79
|
1
|
|
|
|
|
3
|
my $identity; |
80
|
|
|
|
|
|
|
|
81
|
1
|
50
|
|
|
|
21
|
if(-r $file) { |
82
|
0
|
|
|
|
|
0
|
$self->_log("Identity file '$file' exists"); |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
else { |
85
|
1
|
|
|
|
|
7
|
$self->_log("Creating $file with empty password using ssh-keygen ..."); |
86
|
1
|
|
|
|
|
13
|
$self->_system($SSH_KEYGEN => qw( -q -b 4096 -t rsa ), -N => '', -f => $file); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
1
|
|
|
|
|
17
|
$self->_log("Copying pub key to remote host ..."); |
90
|
1
|
50
|
|
|
|
45
|
open my $IDENTITY, '<', "$file.pub" or die "Cannot read $file.pub: $!"; |
91
|
1
|
|
|
1
|
|
12
|
$self->run_sibs_remote(sub { readline $IDENTITY }, 'remote-init'); |
|
1
|
|
|
|
|
27
|
|
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub remote_add_pub_key { |
95
|
2
|
|
|
2
|
|
2080
|
my($self, $key) = @_; |
96
|
2
|
|
|
|
|
7
|
my $file = $self->ssh_file('authorized_keys'); |
97
|
|
|
|
|
|
|
|
98
|
2
|
100
|
|
|
|
40
|
if(-r $file) { |
99
|
1
|
|
|
|
|
3
|
my $match = quotemeta $key; |
100
|
1
|
50
|
|
|
|
23
|
open my $AUTHORIZED_KEYS, '<', $file or die "Could not open $file: $!"; |
101
|
1
|
|
|
|
|
19
|
while(<$AUTHORIZED_KEYS>) { |
102
|
1
|
50
|
|
|
|
16
|
next unless /$match/; |
103
|
1
|
|
|
|
|
4
|
$self->_log("Remote host has pub key"); |
104
|
1
|
|
|
|
|
8
|
return 0; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
1
|
50
|
|
|
|
79
|
open my $AUTHORIZED_KEYS, '>>', $file or die "Could not append to $file: $!\n"; |
109
|
1
|
|
|
|
|
5
|
print $AUTHORIZED_KEYS $key; |
110
|
1
|
50
|
|
|
|
10
|
print $AUTHORIZED_KEYS "\n" unless $key =~ /\n$/; |
111
|
1
|
|
|
|
|
27
|
close $AUTHORIZED_KEYS; |
112
|
1
|
|
|
|
|
7
|
$self->_log("Pub key added to remote authorized_keys."); |
113
|
1
|
|
|
|
|
4
|
return 1; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub remote_host { |
117
|
4
|
|
|
4
|
|
228
|
my $self = shift; |
118
|
4
|
|
|
|
|
29
|
my $moniker = 'sibs-' .$self->{destination}->host; |
119
|
|
|
|
|
|
|
|
120
|
4
|
|
|
|
|
129
|
$moniker =~ s/\./-/g; |
121
|
4
|
|
|
|
|
26
|
$moniker; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub load_config { |
125
|
1
|
|
|
1
|
|
3
|
my $self = shift; |
126
|
1
|
|
|
|
|
2
|
my $config; |
127
|
|
|
|
|
|
|
|
128
|
1
|
50
|
|
|
|
21
|
open my $CONFIG, '<', $self->{config} or die "Cannot read $self->{config}: $! Run '$0 setup'\n"; |
129
|
1
|
|
|
|
|
12
|
$config = join '', <$CONFIG>; |
130
|
1
|
|
|
1
|
|
52
|
$config = eval <<" CONFIG"; |
|
1
|
|
|
1
|
|
5
|
|
|
1
|
|
|
1
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
31
|
|
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
16
|
|
|
1
|
|
|
|
|
75
|
|
131
|
|
|
|
|
|
|
use strict; |
132
|
|
|
|
|
|
|
use warnings; |
133
|
|
|
|
|
|
|
use File::Basename; |
134
|
|
|
|
|
|
|
$config |
135
|
|
|
|
|
|
|
CONFIG |
136
|
|
|
|
|
|
|
|
137
|
1
|
50
|
|
|
|
3
|
$config or die "Invalid config file: ($@)\n"; |
138
|
1
|
|
50
|
|
|
3
|
$config->{exclude} ||= [ @DEFAULT_EXCLUDE ]; |
139
|
1
|
|
50
|
|
|
3
|
$config->{source} ||= [ $ENV{HOME} ]; |
140
|
1
|
|
50
|
|
|
7
|
$config->{destination} = URI->new($config->{destination} || ''); |
141
|
|
|
|
|
|
|
|
142
|
1
|
|
|
|
|
7392
|
@{$self}{keys %$config} = values %$config; |
|
1
|
|
|
|
|
8
|
|
143
|
|
|
|
|
|
|
|
144
|
1
|
|
|
|
|
4
|
for my $m (qw( scheme host path userinfo )) { |
145
|
4
|
50
|
|
|
|
263
|
next if $config->{destination}->$m; |
146
|
0
|
|
|
|
|
0
|
die "[$self->{config}] Missing '$m' part for 'destination' URI\n"; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
1
|
50
|
|
|
|
17
|
$config->{destination}->scheme eq 'rsync' or die "[$self->{config}] Only rsync:// is supported for 'destination' URI\n"; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub run_sibs_remote { |
153
|
1
|
|
|
1
|
|
4
|
my($self, @args) = @_; |
154
|
1
|
50
|
|
0
|
|
7
|
my $stdin = ref $args[0] eq 'CODE' ? shift @args : sub { '' }; |
|
0
|
|
|
|
|
0
|
|
155
|
1
|
|
|
|
|
22
|
my @remote = ( $SSH => '-l' => $self->{destination}->userinfo, $self->remote_host ); |
156
|
|
|
|
|
|
|
|
157
|
1
|
50
|
|
|
|
7
|
unshift @args, '--silent' if $self->{silent}; |
158
|
1
|
50
|
|
|
|
8
|
unshift @args, '--verbose' if $self->{verbose}; |
159
|
1
|
|
|
|
|
7
|
push @remote, qq(perl - @args); |
160
|
|
|
|
|
|
|
|
161
|
1
|
50
|
|
|
|
3229
|
open my $SSH, '|-', @remote or die "Cannot start 'sibs @args' remote: $!"; |
162
|
1
|
50
|
|
|
|
41
|
open my $SELF, '<', __FILE__ or die "Cannot read $0: $!"; |
163
|
1
|
|
|
|
|
288
|
print $SSH $_ while <$SELF>; |
164
|
1
|
|
|
|
|
4
|
print $SSH "\n__DATA__\n"; |
165
|
1
|
|
|
|
|
12
|
print $SSH $self->$stdin; |
166
|
1
|
|
|
|
|
420
|
close $SSH; # TODO: do i need to wait? |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
sub ssh_file { |
170
|
7
|
|
|
7
|
|
1012
|
my($self, $file) = @_; |
171
|
|
|
|
|
|
|
|
172
|
7
|
100
|
|
|
|
32
|
if(!$self->{ssh_dir}) { |
173
|
2
|
50
|
50
|
|
|
241
|
mkdir "$ENV{HOME}/.ssh" or die "Could not mkdir $ENV{HOME}/.ssh: $!" unless -d "$ENV{HOME}/.ssh"; |
174
|
2
|
|
|
|
|
48
|
chmod 0700, "$ENV{HOME}/.ssh"; |
175
|
2
|
|
|
|
|
9
|
$self->{ssh_dir} = "$ENV{HOME}/.ssh"; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
|
178
|
7
|
50
|
|
|
|
18
|
return $self->{ssh_dir} unless $file; |
179
|
7
|
|
|
|
|
29
|
return join '/', $self->{ssh_dir}, $file; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub _backup_name { |
183
|
0
|
|
0
|
0
|
|
0
|
POSIX::strftime($_[0]->{format} || '%d-%H', localtime); |
184
|
|
|
|
|
|
|
} |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
sub _log { |
187
|
8
|
|
|
8
|
|
14
|
my $self = shift; |
188
|
8
|
|
|
|
|
322
|
my $min = (localtime)[1]; |
189
|
8
|
|
|
|
|
82
|
my $hour = (localtime)[2]; |
190
|
|
|
|
|
|
|
|
191
|
8
|
50
|
|
|
|
41
|
return if $self->{silent}; |
192
|
0
|
|
|
|
|
0
|
warn sprintf "[%02s:%02s] %s\n", $hour, $min, join ' ', @_; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
sub _read { |
196
|
0
|
|
|
0
|
|
0
|
my($self, $k) = @_; |
197
|
0
|
|
|
|
|
0
|
my $v = $self->{$k}; |
198
|
0
|
0
|
|
|
|
0
|
$v = join ' ', @$v if ref $v eq 'ARRAY'; |
199
|
0
|
|
|
|
|
0
|
local $| = 1; |
200
|
0
|
|
|
|
|
0
|
print $k; |
201
|
0
|
0
|
|
|
|
0
|
printf " ($v)", if $v; |
202
|
0
|
|
|
|
|
0
|
print ": "; |
203
|
0
|
|
|
|
|
0
|
$_ = ; |
204
|
0
|
|
|
|
|
0
|
chomp; |
205
|
0
|
|
0
|
|
|
0
|
$_ ||= $v; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
sub _system { |
209
|
1
|
|
|
1
|
|
5
|
my($self, $program, @options) = @_; |
210
|
|
|
|
|
|
|
|
211
|
1
|
|
|
|
|
3
|
for my $path (qw( /bin /usr/bin /usr/local/bin )) { |
212
|
3
|
50
|
|
|
|
53
|
next unless -x "$path/$program"; |
213
|
0
|
|
|
|
|
0
|
$program = "$path/$program"; |
214
|
0
|
|
|
|
|
0
|
last; |
215
|
|
|
|
|
|
|
} |
216
|
|
|
|
|
|
|
|
217
|
1
|
100
|
|
|
|
4
|
$self->_log(join ' ', map { length $_ ? $_ : '""' } $program, @options); |
|
10
|
|
|
|
|
26
|
|
218
|
1
|
|
|
|
|
3781
|
system $program => @options; |
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
sub run { |
222
|
4
|
|
|
4
|
|
21
|
my($self, @args) = @_; |
223
|
4
|
|
|
|
|
4
|
my $action = 'help'; |
224
|
4
|
|
|
|
|
4
|
my $i = 0; |
225
|
|
|
|
|
|
|
|
226
|
4
|
|
|
|
|
12
|
while($i < @args) { |
227
|
6
|
100
|
50
|
|
|
44
|
$self->{config} = splice @args, $i, 1, () and next if -f $args[$i]; |
228
|
5
|
100
|
50
|
|
|
20
|
$self->{verbose} = splice @args, $i, 1, () and next if $args[$i] =~ /^--?v/; |
229
|
3
|
100
|
50
|
|
|
17
|
$self->{silent} = splice @args, $i, 1, () and next if $args[$i] =~ /^--?s/; |
230
|
1
|
|
|
|
|
2
|
$i++; |
231
|
|
|
|
|
|
|
} |
232
|
|
|
|
|
|
|
|
233
|
4
|
100
|
|
|
|
10
|
$action = shift @args if @args; |
234
|
4
|
|
66
|
|
|
13
|
$self->{config} ||= "$ENV{HOME}/.sibs.conf"; |
235
|
|
|
|
|
|
|
|
236
|
4
|
50
|
|
|
|
22
|
if($action eq 'setup') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
237
|
0
|
|
|
|
|
0
|
$self->create_sibs_config until eval { $self->load_config }; |
|
0
|
|
|
|
|
0
|
|
238
|
0
|
|
|
|
|
0
|
$self->_log("Created $self->{config}"); |
239
|
0
|
|
|
|
|
0
|
$self->add_backup_host_to_ssh_config; |
240
|
0
|
|
|
|
|
0
|
$self->create_identity_file; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
elsif($action eq 'backup') { |
243
|
0
|
|
|
|
|
0
|
$self->load_config; |
244
|
0
|
|
|
|
|
0
|
$self->run_rsync; |
245
|
0
|
|
|
|
|
0
|
$self->run_sibs_remote('remote-archive', $self->{destination}->path, $self->_backup_name); |
246
|
|
|
|
|
|
|
} |
247
|
|
|
|
|
|
|
elsif($action eq 'man') { |
248
|
0
|
|
|
|
|
0
|
exec perldoc => 'App::sibs'; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
elsif($action eq 'remote-init') { |
251
|
0
|
|
|
|
|
0
|
$self->remote_add_pub_key(eval 'do { local $/; }'); |
252
|
|
|
|
|
|
|
} |
253
|
|
|
|
|
|
|
elsif($action eq 'remote-archive') { |
254
|
0
|
|
|
|
|
0
|
my($dir, $name) = @args; |
255
|
0
|
0
|
|
|
|
0
|
chdir $dir or die "Cannot chdir $dir: $!\n"; |
256
|
0
|
0
|
|
|
|
0
|
$self->_system(rm => -r => $name) if -d $name; |
257
|
0
|
|
|
|
|
0
|
$self->_system(cp => "-al" => "incoming" => $name); |
258
|
0
|
|
|
|
|
0
|
$self->_system(touch => $name); |
259
|
|
|
|
|
|
|
} |
260
|
|
|
|
|
|
|
elsif($action eq 'version') { |
261
|
0
|
|
|
|
|
0
|
require App::sibs; |
262
|
0
|
|
|
|
|
0
|
print App::sibs->VERSION, "\n"; |
263
|
|
|
|
|
|
|
} |
264
|
|
|
|
|
|
|
elsif(!$ENV{HARNESS_IS_VERBOSE}) { |
265
|
0
|
|
|
|
|
0
|
print <<' HELP'; |
266
|
|
|
|
|
|
|
sibs man |
267
|
|
|
|
|
|
|
sibs setup |
268
|
|
|
|
|
|
|
sibs backup |
269
|
|
|
|
|
|
|
sibs version |
270
|
|
|
|
|
|
|
HELP |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
|
273
|
4
|
|
|
|
|
11
|
return 0; |
274
|
|
|
|
|
|
|
} |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
exit +(bless {})->run(@ARGV) unless defined wantarray; |
277
|
|
|
|
|
|
|
bless {}; |