EZ.pm

filter: Statementstmt 75.6% 224 / 296 Branchbran 58.3% 43 / 84 Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.0% 43 / 50 Podpod 100% 8 / 8 totaltotal 72.5% 322 / 452 CC 54 SCAR 21.5
CC
54
Cov
71%
_rsync2
28.6
_full_backup_chunked
12.0
_inc_backup_chunked
12.0
CRAP 8.6
↑ Summary EZ/Dir.pm »
1 package Backup::EZ;
2 11 $Backup::EZ::VERSION = '0.45';
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use Config::General;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Devel::Confess 'color';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Time::localtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use Unix::Syslog qw(:macros :subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use File::Slurp qw(slurp read_dir);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 11 ▶use Backup::EZ::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 11 ▶use Data::Printer alias => 'pdump';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 11 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 #
20 # CONSTANTS
21 #
22 11 ▶use constant EXCLUDE_FILE => '/etc/ezbackup/ezbackup_exclude.rsync';
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶use constant CONF => '/etc/ezbackup/ezbackup.conf';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶use constant COPIES => 30;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶use constant DEST_HOSTNAME => 'localhost';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶use constant DEST_APPEND_MACH_ID => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶use constant USE_SUDO => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 11 ▶use constant IGNORE_VANISHED => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 11 ▶use constant DEFAULT_ARCHIVE_OPTS => '-az';
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶use constant ARCHIVE_NO_RECURSE_OPTS => '-dlptgoDz';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 =head1 NAME
33  
34 Backup::EZ - Simple backups based on rsync
35  
36 =head1 VERSION
37  
38 version 0.45
39  
40 =cut
41  
42 =head1 SYNOPSIS
43  
44 use Backup::EZ;
45  
46 my $ez = Backup::EZ->new;
47 $ez->backup;
48  
49 =head1 DESCRIPTION
50  
51 Backup::EZ is backup software that is designed to be as easy to use
52 as possible, yet provide a robust solution
53  
54 If you only want to run backups, see the included command line utility
55 "ezbackup". See the README for configuration instructions.
56  
57 =head1 SUBROUTINES/METHODS
58  
59 =head2 new
60  
61 optional args:
62 conf => $config_file
63 dryrun => $bool,
64 exclude_file => $rsync_excl_file
65  
66 =cut
67  
68 sub new {
69 22 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=14.1
Pod documented
70 22 my %a = @_;
71  
72 22 my $self = {};
73  
74 # uncoverable branch true
75 22 ▶ if ( $ENV{VERBOSE} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'VERBOSE'}) { } 0 22 22
76 0 setlogmask( LOG_UPTO(LOG_DEBUG) );
77 0 $self->{syslog_option} = LOG_PID | LOG_PERROR;
78 }
79 else {
80 22 setlogmask( LOG_UPTO(LOG_INFO) );
81 22 $self->{syslog_option} = LOG_PID;
82 }
83  
84 22 _read_conf( $self, @_ );
85  
86 22 ▶ if ( $a{dryrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a{"dryrun"}) 14 8 22
87 14 $self->{dryrun} = 1;
88 }
89  
90 22 ▶ if ( !defined $a{exclude_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $a{'exclude_file'}) { } 0 22 22
91 0 $self->{exclude_file} = EXCLUDE_FILE;
92 }
93 else {
94 22 $self->{exclude_file} = $a{exclude_file};
95 }
96  
97 22 bless $self, $class;
98 22 return $self;
99 }
100  
101 sub _debug {
102 75 ▶ my $self = shift;
Subroutine _debug: called CC=1 SCAR=0.0
103 75 my $msg = shift;
104  
105 75 my $line = (caller)[2];
106  
107 75 openlog "ezbackup", $self->{syslog_option}, LOG_SYSLOG;
108 75 syslog LOG_DEBUG, "($line) $msg";
109 75 closelog;
110 }
111  
112 #sub _error {
113 # my $self = shift;
114 # my $msg = shift;
115 #
116 # openlog "ezbackup", $self->{syslog_option}, LOG_LOCAL7;
117 # syslog LOG_ERR, $msg;
118 # closelog;
119 #}
120  
121 sub _info {
122 8 ▶ my $self = shift;
Subroutine _info: called CC=1 SCAR=0.0
123 8 my $msg = shift;
124  
125 8 openlog "ezbackup", $self->{syslog_option}, LOG_SYSLOG;
126 8 syslog LOG_INFO, $msg;
127 8 closelog;
128 }
129  
130 sub _read_conf {
131 22 ▶ my $self = shift;
Subroutine _read_conf: called CC=9 SCAR=22.0
132 22 my %a = @_;
133  
134 # uncoverable branch false
135 22 ▶ my $conf = $a{conf} ? $a{conf} : CONF;
Branch: 1 branch
BranchTrueFalseTotal
$a{'conf'} ? : 22 0 22
136  
137 22 my $config = Config::General->new(
138 -ConfigFile => $conf,
139 -ForceArray => 1,
140 -LowerCaseNames => 1,
141 -AutoTrue => 1,
142  
143 );
144  
145 22 my %conf = $config->getall;
146 22 _debug( $self, Dumper \%conf );
147  
148 22 foreach my $key ( keys %conf ) {
149  
150 111 ▶ if ( !defined $conf{backup_host} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"backup_host"}) 2 109 111
151 2 $conf{backup_host} = DEST_HOSTNAME;
152 }
153  
154 111 ▶ if ( !defined $conf{copies} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"copies"}) 2 109 111
155 2 $conf{copies} = COPIES;
156 }
157  
158 111 ▶ if ( !defined $conf{append_machine_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"append_machine_id"}) 1 110 111
159 1 $conf{append_machine_id} = DEST_APPEND_MACH_ID;
160 }
161  
162 111 ▶ if ( !defined $conf{use_sudo} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"use_sudo"}) 19 92 111
163 19 $conf{use_sudo} = USE_SUDO;
164 }
165  
166 111 ▶ if ( !defined $conf{ignore_vanished} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $conf{"ignore_vanished"}) 21 90 111
167 21 $conf{ignore_vanished} = IGNORE_VANISHED;
168 }
169 }
170  
171 22 ▶ if ( ref( $conf{dir} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conf{"dir"} ne "ARRAY") 2 20 22
172 2 $conf{dir} = [ $conf{dir} ];
173 }
174  
175 22 $self->{conf} = \%conf;
176 }
177  
178 sub _get_dirs {
179 11 ▶ my $self = shift;
Subroutine _get_dirs: called CC=2 SCAR=6.9
180  
181 11 my @dirs;
182  
183 11 foreach my $dir ( @{ $self->{conf}->{dir} } ) {
184 20 push( @dirs, Backup::EZ::Dir->new($dir) );
185 }
186  
187 10 $self->_debug( Dumper \@dirs );
188 10 return @dirs;
189 }
190  
191 =head2 get_conf_dirs
192  
193 Returns a list Backup::EZ::Dir objects as read from the conf file.
194  
195 =cut
196  
197 sub get_conf_dirs {
198 2 ▶ my $self = shift;
Subroutine get_conf_dirs: called CC=1 SCAR=0.0
Pod documented
199 2 return $self->_get_dirs;
200 }
201  
202 sub _ssh {
203 35 ▶ my $self = shift;
Subroutine _ssh: called CC=7 SCAR=23.6
204 35 my $cmd = shift;
205 35 my $dryrun = shift;
206  
207 35 my $sshcmd;
208 35 my $login = $self->_get_dest_login;
209  
210 # uncoverable branch false
211 35 ▶ if ( $self->_is_unit_test ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_unit_test) { } 35 0 35
212  
213 # unit testing
214 35 $sshcmd = "$cmd";
215 }
216 else {
217 0 ▶ if ($cmd !~ /^ *sudo /) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cmd =~ /^ *sudo /) 0 0 0
218 0 ▶ $cmd = sprintf( "%s $cmd", $self->{conf}->{use_sudo} ? 'sudo' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'conf'}{'use_sudo'} ? : 0 0 0
219 }
220  
221 0 my $ssh_opts = [];
222 0 ▶ if ( $self->{conf}->{ssh_opts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"ssh_opts"}) 0 0 0
223 0 push @$ssh_opts, $self->{conf}->{ssh_opts};
224 }
225 0 $sshcmd = sprintf( 'ssh %s %s %s', join( ' ', @{$ssh_opts}), $login, $cmd );
226 }
227  
228 35 $self->_debug($sshcmd);
229 35 ▶ return undef if $dryrun;
Branch: 1 branch
BranchTrueFalseTotal
if $dryrun 2 33 35
230  
231 33 my @out = `$sshcmd`;
232  
233 # uncoverable branch true
234 33 ▶ confess if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 33 33
235  
236 33 return @out;
237 }
238  
239 sub _get_dest_username {
240 43 ▶ my $self = shift;
Subroutine _get_dest_username: called CC=3 SCAR=11.1
241  
242 43 ▶ if ( $self->{conf}->{backup_user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"backup_user"}) 5 38 43
243 5 return $self->{conf}->{backup_user};
244 }
245  
246 38 ▶ if ( $ENV{USER} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"USER"}) 0 38 38
247 0 return $ENV{USER};
248 }
249  
250 38 my $whoami = `whoami`;
251 38 chomp $whoami;
252  
253 38 return $whoami;
254 }
255  
256 sub _get_dest_hostname {
257 43 ▶ my $self = shift;
Subroutine _get_dest_hostname: called CC=1 SCAR=0.0
258  
259 43 return $self->{conf}->{backup_host};
260 }
261  
262 sub _get_dest_tmp_dir {
263 25 ▶ my $self = shift;
Subroutine _get_dest_tmp_dir: called CC=1 SCAR=0.0
264  
265 25 return sprintf( "%s/%s", $self->get_dest_dir, ".tmp" );
266 }
267  
268 sub _get_dest_backup_dir {
269 0 ▶ my $self = shift;
Subroutine _get_dest_backup_dir: not called CC=1 SCAR=6.9
270  
271 0 return sprintf( "%s/%s", $self->get_dest_dir, $self->{datestamp} );
272 }
273  
274 sub _is_unit_test {
275 43 ▶ my $self = shift;
Subroutine _is_unit_test: called CC=2 SCAR=7.0
276  
277 # uncoverable branch false
278 43 ▶ if ( $0 =~ /\.t$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($0 =~ /\.t$/) 43 0 43
279 43 return 1;
280 }
281  
282 0 return 0;
283 }
284  
285 sub _get_dest_login {
286 43 ▶ my $self = shift;
Subroutine _get_dest_login: called CC=1 SCAR=0.0
287  
288 43 my $username = $self->_get_dest_username;
289 43 my $hostname = $self->_get_dest_hostname;
290  
291 43 return sprintf( '%s@%s', $username, $hostname );
292 }
293  
294 # Unused?
295 # Commenting out to make it clear these are unused
296 # Could consider removing
297 #sub _rsync_no_recursion {
298 # my $self = shift;
299 # my $dir = shift;
300 # my @extra_options = @_;
301 #
302 # my $rsync_opts = '-dlptgoD';
303 # my $cmd;
304 # my $login;
305 #
306 # if ( $self->{dryrun} ) {
307 # push( @extra_options, '--dry-run' );
308 # }
309 #
310 # $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) );
311 # $login = $self->_get_dest_login;
312 #
313 # # uncoverable branch false
314 # if ( $self->_is_unit_test ) {
315 # $cmd = sprintf(
316 # "rsync %s $rsync_opts %s/ %s%s",
317 # join( ' ', @extra_options ), $dir,
318 # $self->_get_dest_tmp_dir, $dir
319 # );
320 # }
321 # else {
322 # $cmd = sprintf(
323 # "rsync %s $rsync_opts -e ssh %s/ %s:%s%s",
324 # join( ' ', @extra_options ),
325 # $dir, $login, $self->_get_dest_tmp_dir, $dir
326 # );
327 # }
328 #
329 # $cmd .= " --exclude-from " . $self->{exclude_file};
330 #
331 # $self->_debug($cmd);
332 # system($cmd);
333 #
334 # # uncoverable branch true
335 # confess if $?;
336 #}
337  
338 sub _rsync2 {
339 8 ▶ my $self = shift;
Subroutine _rsync2: called CC=17 SCAR=33.6
340 8 my %a = @_;
341  
342 8 ▶ my $dir = $a{dir} or confess "missing dir arg";
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $a{"dir"} or confess("missing dir arg") 0 8 8
343 8 my $link_dest = $a{link_dest};
344 8 ▶ my $archive_opts = $a{archive_opts} || '-az';
Condition50%1/2
$a{'archive_opts'} || '-az'
l!l
80
MC/DC0%0/1
$a{'archive_opts'} || '-az'
$a{'archive_opts'}
Truth table50%1/2
A
A= $a{'archive_opts'}
Aresult
11
00
345 8 ▶ my $extra_opts = $a{extra_opts} || [];
Condition50%1/2
$a{'extra_opts'} || []
l!l
80
MC/DC0%0/1
$a{'extra_opts'} || []
$a{'extra_opts'}
Truth table50%1/2
A
A= $a{'extra_opts'}
Aresult
11
00
346 8 ▶ my $ssh_opts = $a{ssh_opts} || [];
Condition50%1/2
$a{'ssh_opts'} || []
l!l
08
MC/DC0%0/1
$a{'ssh_opts'} || []
$a{'ssh_opts'}
Truth table50%1/2
A
A= $a{'ssh_opts'}
Aresult
11
00
347  
348 8 my $cmd;
349 my $login;
350  
351 8 ▶ if ( $self->{dryrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dryrun"}) 2 6 8
352 2 push @$extra_opts, '--dry-run';
353 }
354  
355 8 ▶ if ($link_dest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_dest) 0 8 8
356 0 push @$extra_opts, sprintf '--link-dest "%s"', $link_dest;
357 }
358  
359 8 ▶ if ( $self->{exclude_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclude_file"}) 8 0 8
360 8 push @$extra_opts, sprintf '--exclude-from "%s"', $self->{exclude_file};
361 }
362  
363 8 ▶ if ( $self->{conf}->{extra_rsync_opts} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"extra_rsync_opts"}) 0 8 8
364 0 push @$extra_opts, $self->{conf}->{extra_rsync_opts};
365 }
366  
367 8 ▶ if ( $self->{conf}->{ssh_opts} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"ssh_opts"}) 0 8 8
368 0 push @$ssh_opts, $self->{conf}->{ssh_opts};
369 }
370  
371 8 $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) );
372 8 $login = $self->_get_dest_login;
373  
374 # uncoverable branch false
375 8 ▶ if ( $self->_is_unit_test ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_unit_test) { } 8 0 8
376 8 $cmd = sprintf(
377 'rsync -s %s %s "%s/" "%s%s"',
378 join(' ', @$extra_opts),
379 $archive_opts, # archive options
380 $dir, # src dir
381 $self->_get_dest_tmp_dir, # dest tmp dir
382 $dir, # dest sub dir
383 );
384 }
385 else {
386 $cmd = sprintf(
387 'rsync -s %s %s -e "ssh %s" "%s/" %s:"%s%s"',
388 0 join( ' ', @{$extra_opts} ), # extra rsync options
389 $archive_opts, # archive options
390 0 join( ' ', @{$ssh_opts} ), # ssh options
391 $dir, # src dir
392 $login, # login
393 $self->_get_dest_tmp_dir, # dest tmp dir
394 $dir, # dest sub dir
395 );
396 }
397  
398 #
399 # fail safe bailout in case of bug
400 #
401 8 my @cmd = split(/\s+/, $cmd);
402 8 my $link_cnt = 0;
403 8 foreach my $c (@cmd) {
404 60 ▶ if ($c =~ /link-dest/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /link-dest/) 0 60 60
405 0 $link_cnt++
406 }
407 }
408  
409 8 ▶ if ($link_cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_cnt > 1) 0 8 8
410 0 confess "too many link-dest args: $cmd";
411 }
412  
413 #
414 # ok execute
415 #
416 8 $self->_debug($cmd);
417 8 system($cmd);
418  
419 # uncoverable branch true
420 # uncoverable branch true
421 8 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 8 0 8
422 8 my $exit = $? >> 8;
423 8 ▶ if ($exit == 24 && $self->{conf}->{ignore_vanished}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit == 24 and $self->{'conf'}{'ignore_vanished'}) { } 0 8 8
Condition33%1/3
$exit == 24 and $self->{"conf"}{"ignore_vanished"}
!ll&&!rl&&r
800
MC/DC0%0/2
$exit == 24 and $self->{"conf"}{"ignore_vanished"}
$exit == 24 $self->{"conf"}{"ignore_vanished"}
Truth table33%1/3
A and B
A= $exit == 24B= $self->{"conf"}{"ignore_vanished"}
ABresult
0X0
100
111
424 0 $self->_debug("ignoring vanished files");
425 }
426 else {
427 8 confess;
428 }
429 }
430 }
431  
432 # Unused?
433 # Commenting out to make it clear these are unused
434 # Could consider removing
435 #sub _rsync {
436 # my $self = shift;
437 # my $dir = shift;
438 # my @extra_options = @_;
439 #
440 # my $cmd;
441 # my $login;
442 #
443 # if ( $self->{dryrun} ) {
444 # push( @extra_options, '--dry-run' );
445 # }
446 #
447 # $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) );
448 # $login = $self->_get_dest_login;
449 #
450 # # uncoverable branch false
451 # if ( $self->_is_unit_test ) {
452 # $cmd = sprintf(
453 # "rsync %s -a %s/ %s%s",
454 # join( ' ', @extra_options ), $dir,
455 # $self->_get_dest_tmp_dir, $dir
456 # );
457 # }
458 # else {
459 # $cmd = sprintf(
460 # "rsync %s -aze ssh %s/ %s:%s%s",
461 # join( ' ', @extra_options ),
462 # $dir, $login, $self->_get_dest_tmp_dir, $dir
463 # );
464 # }
465 #
466 # $cmd .= " --exclude-from " . $self->{exclude_file};
467 #
468 # $self->_debug($cmd);
469 # system($cmd);
470 #
471 # # uncoverable branch true
472 # confess if $?;
473 #}
474  
475 sub _full_backup_chunked {
476 0 ▶ my $self = shift;
Subroutine _full_backup_chunked: not called CC=3 SCAR=24.8
477 0 my $dir = shift;
478  
479 0 $self->_rsync2(
480 dir => $dir->dirname,
481 archive_opts => ARCHIVE_NO_RECURSE_OPTS,
482 extra_opts => $dir->excludes(),
483 );
484  
485 0 my @entries = read_dir( $dir->dirname, prefix => 1 );
486  
487 0 foreach my $entry (@entries) {
488 0 ▶ if ( -d $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $entry) 0 0 0
489 0 $self->_rsync2(
490 dir => $entry,
491 archive_opts => DEFAULT_ARCHIVE_OPTS,
492 extra_opts => $dir->excludes(),
493 );
494 }
495 }
496 }
497  
498 sub _full_backup {
499 8 ▶ my $self = shift;
Subroutine _full_backup: called CC=2 SCAR=7.4
500 8 my $dir = shift;
501  
502 8 ▶ if ( $dir->chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->chunked) { } 0 8 8
503 0 $self->_full_backup_chunked($dir);
504 }
505 else {
506 8 $self->_rsync2(
507 dir => $dir->dirname,
508 archive_opts => DEFAULT_ARCHIVE_OPTS,
509 extra_opts => $dir->excludes(),
510 );
511 }
512 }
513  
514 sub _inc_backup_chunked {
515 0 ▶ my $self = shift;
Subroutine _inc_backup_chunked: not called CC=3 SCAR=24.8
516 0 my $dir = shift;
517 0 my $last_backup_dir = shift;
518 0 my $link_dest = shift;
519  
520 0 $self->_rsync2(
521 dir => $dir->dirname,
522 archive_opts => ARCHIVE_NO_RECURSE_OPTS,
523 extra_opts => $dir->excludes(),
524 link_dest => $link_dest,
525 );
526  
527 0 my @entries = read_dir( $dir->dirname, prefix => 0 );
528  
529 0 foreach my $entry (@entries) {
530  
531 0 my $abs_entry = sprintf( '%s/%s', $dir->dirname, $entry );
532  
533 0 ▶ if ( -d $abs_entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $abs_entry) 0 0 0
534 0 $self->_rsync2(
535 dir => $abs_entry,
536 archive_opts => DEFAULT_ARCHIVE_OPTS,
537 extra_opts => $dir->excludes(),
538 link_dest => sprintf( '%s/%s', $link_dest, $entry ),
539 );
540 }
541 }
542 }
543  
544 sub _inc_backup {
545 0 ▶ my $self = shift;
Subroutine _inc_backup: not called CC=2 SCAR=17.9
546 0 my $dir = shift;
547 0 my $last_backup_dir = shift;
548  
549 0 my $link_dest = sprintf(
550 "%s/%s/%s",
551 $self->get_dest_dir, #
552 $last_backup_dir, #
553 $dir->dirname, #
554 );
555  
556 0 ▶ if ( $dir->chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->chunked) { } 0 0 0
557 0 $self->_inc_backup_chunked( $dir, $last_backup_dir, $link_dest );
558 }
559 else {
560 0 $self->_rsync2(
561 dir => $dir->dirname,
562 archive_opts => DEFAULT_ARCHIVE_OPTS,
563 extra_opts => $dir->excludes(),
564 link_dest => $link_dest,
565 );
566 }
567 }
568  
569 sub _mk_dest_dir {
570 26 ▶ my $self = shift;
Subroutine _mk_dest_dir: called CC=1 SCAR=0.0
571 26 my $dir = shift;
572 26 my $dryrun = shift;
573  
574 26 my $cmd = sprintf( "mkdir -p %s", $dir );
575 26 $self->_ssh( $cmd, $dryrun );
576 }
577  
578 sub _set_datestamp {
579 9 ▶ my $self = shift;
Subroutine _set_datestamp: called CC=1 SCAR=0.0
580  
581 9 my $t = localtime;
582 9 $self->{datestamp} = sprintf(
583 "%04d-%02d-%02d_%02d:%02d:%02d",
584 $t->year + 1900,
585 $t->mon + 1,
586 $t->mday, $t->hour, $t->min, $t->sec
587 );
588 }
589  
590 =head2 dump_conf
591  
592 Does what it says.
593  
594 =cut
595  
596 sub dump_conf {
597 0 ▶ my $self = shift;
Subroutine dump_conf: not called CC=1 SCAR=6.9
Pod documented
598  
599 0 pdump $self->{conf};
600 }
601  
602 =head2 backup
603  
604 Invokes the backup process. Takes no args.
605  
606 =cut
607  
608 sub backup {
609 9 ▶ my $self = shift;
Subroutine backup: called CC=4 SCAR=15.4
Pod documented
610  
611 9 $self->_mk_dest_dir( $self->get_dest_dir );
612 9 my @backups = $self->get_list_of_backups;
613 9 $self->_set_datestamp;
614 9 $self->_mk_dest_dir( $self->_get_dest_tmp_dir, $self->{dryrun} );
615  
616 9 foreach my $dir ( $self->_get_dirs ) {
617  
618 8 my $dirname = $dir->dirname();
619 8 ▶ if ( -d $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dirname) { } 8 0 8
620  
621 8 $self->_info("backing up $dirname");
622  
623 8 ▶ if ( !@backups ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @backups) { } 8 0 8
624  
625 # full
626 8 $self->_full_backup($dir);
627 }
628 else {
629 # incremental
630 0 $self->_inc_backup( $dir, $backups[$#backups] );
631 }
632 }
633 else {
634 0 $self->_info("skipping $dirname because it does not exist");
635 }
636  
637 }
638  
639 $self->_ssh(
640 sprintf( "mv %s %s",
641 $self->_get_dest_tmp_dir, $self->_get_dest_backup_dir ),
642 $self->{dryrun}
643 0 );
644  
645 0 $self->expire();
646  
647 0 return 1;
648 }
649  
650 =head2 expire
651  
652 Expire backups. Gets a list of current backups and removes old ones that are
653 beyond the cutoff (see "copies" in the conf file).
654  
655 =cut
656  
657 sub expire {
658 0 ▶ my $self = shift;
Subroutine expire: not called CC=3 SCAR=24.8
Pod documented
659  
660 0 my @list = $self->get_list_of_backups;
661  
662 0 ▶ while ( scalar(@list) > $self->{conf}->{copies} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @list > $self->{"conf"}{"copies"}) 0 0 0
663  
664 0 my $subdir = shift @list;
665 0 my $del_dir = sprintf( "%s/%s", $self->get_dest_dir, $subdir );
666  
667 my $cmd = sprintf( "%s rm -rf $del_dir",
668 0 ▶ $self->{conf}->{use_sudo} ? 'sudo' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'conf'}{'use_sudo'} ? : 0 0 0
669  
670 0 $self->_ssh($cmd);
671 }
672 }
673  
674 =head2 get_backup_host
675  
676 Returns the backup_host name.
677  
678 =cut
679  
680 sub get_backup_host {
681 0 ▶ my $self = shift;
Subroutine get_backup_host: not called CC=1 SCAR=6.9
Pod documented
682 0 return $self->{conf}->{backup_host};
683 }
684  
685 =head2 get_dest_dir
686  
687 Returns the dest_dir.
688  
689 =cut
690  
691 sub get_dest_dir {
692 47 ▶ my $self = shift;
Subroutine get_dest_dir: called CC=4 SCAR=15.4
Pod documented
693  
694 47 my $hostname = hostname();
695 47 $hostname =~ s/\..+$//;
696  
697 47 ▶ if ( $self->{conf}->{append_machine_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"append_machine_id"}) 3 44 47
698  
699 # uncoverable branch true
700 3 ▶ if ( !-f '/etc/machine-id' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f "/etc/machine-id") 0 3 3
701  
702 # uncoverable statement count:2
703 0 my $data_uuid = Data::UUID->new;
704 0 my $uuid = $data_uuid->create_str();
705  
706 # uncoverable statement count:3
707 0 ▶ open my $fh, ">/etc/machine-id"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">/etc/machine-id" or confess("failed to open /etc/machine-id: $!") 0 0 0
708 or confess "failed to open /etc/machine-id: $!";
709 0 print $fh "$uuid\n";
710 0 close($fh);
711 }
712  
713 3 my $uuid = slurp("/etc/machine-id");
714 3 chomp $uuid;
715  
716 3 $hostname = "$hostname-$uuid";
717 }
718  
719 47 return sprintf( "%s/%s", $self->{conf}->{dest_dir}, $hostname );
720 }
721  
722 =head2 get_list_of_backups
723  
724 Returns an array of backups. They are in the format of "YYYY-MM-DD_HH:MM:SS".
725 =cut
726  
727 sub get_list_of_backups {
728 9 ▶ my $self = shift;
Subroutine get_list_of_backups: called CC=3 SCAR=14.2
Pod documented
729  
730 9 my @backups;
731  
732 9 my @list = $self->_ssh( sprintf( "ls %s", $self->get_dest_dir ) );
733  
734 9 foreach my $e (@list) {
735 0 chomp $e;
736  
737 0 ▶ if ( $e =~ /^\d\d\d\d-\d\d-\d\d_\d\d:\d\d:\d\d$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e =~ /^\d\d\d\d-\d\d-\d\d_\d\d:\d\d:\d\d$/) 0 0 0
738 0 push( @backups, $e );
739 }
740 }
741  
742 9 return @backups;
743 }
744  
745 11 1;
↑ Summary EZ/Dir.pm »