EZ.pm

filter: Statementstmt 76.1% 224 / 294 Branchbran 61.2% 43 / 80 (8 uncoverable) Conditioncond 44.4% 4 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 86.0% 43 / 50 Podpod 100% 8 / 8 totaltotal 73.5% 322 / 446 (8 uncoverable) CC 52 SCAR 21.2
CC
52
Cov
72%
_rsync2
28.6
_full_backup_chunked
12.0
_inc_backup_chunked
12.0
CRAP 8.3
↑ Summary EZ/Dir.pm »
1 package Backup::EZ;
2 11 $Backup::EZ::VERSION = '0.46';
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.46
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 22 ▶sub new {
Subroutine new: called CC=4 SCAR=14.1
Pod documented
69 22 my $class = shift;
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 75 ▶sub _debug {
Subroutine _debug: called CC=1 SCAR=0.0
102 75 my $self = shift;
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 8 ▶sub _info {
Subroutine _info: called CC=1 SCAR=0.0
122 8 my $self = shift;
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 22 ▶sub _read_conf {
Subroutine _read_conf: called CC=9 SCAR=22.0
131 22 my $self = shift;
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 11 ▶sub _get_dirs {
Subroutine _get_dirs: called CC=2 SCAR=6.9
179 11 my $self = shift;
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 2 ▶sub get_conf_dirs {
Subroutine get_conf_dirs: called CC=1 SCAR=0.0
Pod documented
198 2 my $self = shift;
199 2 return $self->_get_dirs;
200 }
201  
202 35 ▶sub _ssh {
Subroutine _ssh: called CC=5 SCAR=17.1
203 35 my $self = shift;
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 #
218 # this breaks stuff. commenting out for now.
219 #
220 # if ($cmd !~ /^ *sudo /) {
221 # $cmd = sprintf( "%s $cmd", $self->{conf}->{use_sudo} ? 'sudo' : '' );
222 # }
223  
224 0 my $ssh_opts = [];
225 0 ▶ if ( $self->{conf}->{ssh_opts} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"ssh_opts"}) 0 0 0
226 0 push @$ssh_opts, $self->{conf}->{ssh_opts};
227 }
228 0 $sshcmd = sprintf( 'ssh %s %s %s', join( ' ', @{$ssh_opts}), $login, $cmd );
229 }
230  
231 35 $self->_debug($sshcmd);
232 35 ▶ return undef if $dryrun;
Branch: 1 branch
BranchTrueFalseTotal
if $dryrun 2 33 35
233  
234 33 my @out = `$sshcmd`;
235  
236 # uncoverable branch true
237 33 ▶ confess if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 33 33
238  
239 33 return @out;
240 }
241  
242 43 ▶sub _get_dest_username {
Subroutine _get_dest_username: called CC=3 SCAR=11.1
243 43 my $self = shift;
244  
245 43 ▶ if ( $self->{conf}->{backup_user} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"backup_user"}) 5 38 43
246 5 return $self->{conf}->{backup_user};
247 }
248  
249 38 ▶ if ( $ENV{USER} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"USER"}) 0 38 38
250 0 return $ENV{USER};
251 }
252  
253 38 my $whoami = `whoami`;
254 38 chomp $whoami;
255  
256 38 return $whoami;
257 }
258  
259 43 ▶sub _get_dest_hostname {
Subroutine _get_dest_hostname: called CC=1 SCAR=0.0
260 43 my $self = shift;
261  
262 43 return $self->{conf}->{backup_host};
263 }
264  
265 25 ▶sub _get_dest_tmp_dir {
Subroutine _get_dest_tmp_dir: called CC=1 SCAR=0.0
266 25 my $self = shift;
267  
268 25 return sprintf( "%s/%s", $self->get_dest_dir, ".tmp" );
269 }
270  
271 0 ▶sub _get_dest_backup_dir {
Subroutine _get_dest_backup_dir: not called CC=1 SCAR=6.9
272 0 my $self = shift;
273  
274 0 return sprintf( "%s/%s", $self->get_dest_dir, $self->{datestamp} );
275 }
276  
277 43 ▶sub _is_unit_test {
Subroutine _is_unit_test: called CC=2 SCAR=7.0
278 43 my $self = shift;
279  
280 # uncoverable branch false
281 43 ▶ if ( $0 =~ /\.t$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($0 =~ /\.t$/) 43 0 43
282 43 return 1;
283 }
284  
285 0 return 0;
286 }
287  
288 43 ▶sub _get_dest_login {
Subroutine _get_dest_login: called CC=1 SCAR=0.0
289 43 my $self = shift;
290  
291 43 my $username = $self->_get_dest_username;
292 43 my $hostname = $self->_get_dest_hostname;
293  
294 43 return sprintf( '%s@%s', $username, $hostname );
295 }
296  
297 # Unused?
298 # Commenting out to make it clear these are unused
299 # Could consider removing
300 #sub _rsync_no_recursion {
301 # my $self = shift;
302 # my $dir = shift;
303 # my @extra_options = @_;
304 #
305 # my $rsync_opts = '-dlptgoD';
306 # my $cmd;
307 # my $login;
308 #
309 # if ( $self->{dryrun} ) {
310 # push( @extra_options, '--dry-run' );
311 # }
312 #
313 # $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) );
314 # $login = $self->_get_dest_login;
315 #
316 # # uncoverable branch false
317 # if ( $self->_is_unit_test ) {
318 # $cmd = sprintf(
319 # "rsync %s $rsync_opts %s/ %s%s",
320 # join( ' ', @extra_options ), $dir,
321 # $self->_get_dest_tmp_dir, $dir
322 # );
323 # }
324 # else {
325 # $cmd = sprintf(
326 # "rsync %s $rsync_opts -e ssh %s/ %s:%s%s",
327 # join( ' ', @extra_options ),
328 # $dir, $login, $self->_get_dest_tmp_dir, $dir
329 # );
330 # }
331 #
332 # $cmd .= " --exclude-from " . $self->{exclude_file};
333 #
334 # $self->_debug($cmd);
335 # system($cmd);
336 #
337 # # uncoverable branch true
338 # confess if $?;
339 #}
340  
341 8 ▶sub _rsync2 {
Subroutine _rsync2: called CC=17 SCAR=33.6
342 8 my $self = shift;
343 8 my %a = @_;
344  
345 8 ▶ my $dir = $a{dir} or confess "missing dir arg";
Branch: 1 branch
BranchTrueFalseTotal
my $dir = $a{"dir"} or confess("missing dir arg") 8 0 8
346 8 my $link_dest = $a{link_dest};
347 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
348 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
349 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
350  
351 8 my $cmd;
352 my $login;
353  
354 8 ▶ if ( $self->{dryrun} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dryrun"}) 2 6 8
355 2 push @$extra_opts, '--dry-run';
356 }
357  
358 8 ▶ if ($link_dest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_dest) 0 8 8
359 0 push @$extra_opts, sprintf '--link-dest "%s"', $link_dest;
360 }
361  
362 8 ▶ if ( $self->{exclude_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"exclude_file"}) 8 0 8
363 8 push @$extra_opts, sprintf '--exclude-from "%s"', $self->{exclude_file};
364 }
365  
366 8 ▶ if ( $self->{conf}->{extra_rsync_opts} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"extra_rsync_opts"}) 0 8 8
367 0 push @$extra_opts, $self->{conf}->{extra_rsync_opts};
368 }
369  
370 8 ▶ if ( $self->{conf}->{ssh_opts} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"ssh_opts"}) 0 8 8
371 0 push @$ssh_opts, $self->{conf}->{ssh_opts};
372 }
373  
374 8 $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) );
375 8 $login = $self->_get_dest_login;
376  
377 # uncoverable branch false
378 8 ▶ if ( $self->_is_unit_test ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_unit_test) { } 8 0 8
379 8 $cmd = sprintf(
380 'rsync -s %s %s "%s/" "%s%s"',
381 join(' ', @$extra_opts),
382 $archive_opts, # archive options
383 $dir, # src dir
384 $self->_get_dest_tmp_dir, # dest tmp dir
385 $dir, # dest sub dir
386 );
387 }
388 else {
389 $cmd = sprintf(
390 'rsync -s %s %s -e "ssh %s" "%s/" %s:"%s%s"',
391 0 join( ' ', @{$extra_opts} ), # extra rsync options
392 $archive_opts, # archive options
393 0 join( ' ', @{$ssh_opts} ), # ssh options
394 $dir, # src dir
395 $login, # login
396 $self->_get_dest_tmp_dir, # dest tmp dir
397 $dir, # dest sub dir
398 );
399 }
400  
401 #
402 # fail safe bailout in case of bug
403 #
404 8 my @cmd = split(/\s+/, $cmd);
405 8 my $link_cnt = 0;
406 8 foreach my $c (@cmd) {
407 60 ▶ if ($c =~ /link-dest/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c =~ /link-dest/) 0 60 60
408 0 $link_cnt++
409 }
410 }
411  
412 8 ▶ if ($link_cnt > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link_cnt > 1) 0 8 8
413 0 confess "too many link-dest args: $cmd";
414 }
415  
416 #
417 # ok execute
418 #
419 8 $self->_debug($cmd);
420 8 system($cmd);
421  
422 # uncoverable branch true
423 # uncoverable branch true
424 8 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 8 0 8
425 8 my $exit = $? >> 8;
426 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
427 0 $self->_debug("ignoring vanished files");
428 }
429 else {
430 8 confess;
431 }
432 }
433 }
434  
435 # Unused?
436 # Commenting out to make it clear these are unused
437 # Could consider removing
438 #sub _rsync {
439 # my $self = shift;
440 # my $dir = shift;
441 # my @extra_options = @_;
442 #
443 # my $cmd;
444 # my $login;
445 #
446 # if ( $self->{dryrun} ) {
447 # push( @extra_options, '--dry-run' );
448 # }
449 #
450 # $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) );
451 # $login = $self->_get_dest_login;
452 #
453 # # uncoverable branch false
454 # if ( $self->_is_unit_test ) {
455 # $cmd = sprintf(
456 # "rsync %s -a %s/ %s%s",
457 # join( ' ', @extra_options ), $dir,
458 # $self->_get_dest_tmp_dir, $dir
459 # );
460 # }
461 # else {
462 # $cmd = sprintf(
463 # "rsync %s -aze ssh %s/ %s:%s%s",
464 # join( ' ', @extra_options ),
465 # $dir, $login, $self->_get_dest_tmp_dir, $dir
466 # );
467 # }
468 #
469 # $cmd .= " --exclude-from " . $self->{exclude_file};
470 #
471 # $self->_debug($cmd);
472 # system($cmd);
473 #
474 # # uncoverable branch true
475 # confess if $?;
476 #}
477  
478 0 ▶sub _full_backup_chunked {
Subroutine _full_backup_chunked: not called CC=3 SCAR=24.8
479 0 my $self = shift;
480 0 my $dir = shift;
481  
482 0 $self->_rsync2(
483 dir => $dir->dirname,
484 archive_opts => ARCHIVE_NO_RECURSE_OPTS,
485 extra_opts => $dir->excludes(),
486 );
487  
488 0 my @entries = read_dir( $dir->dirname, prefix => 1 );
489  
490 0 foreach my $entry (@entries) {
491 0 ▶ if ( -d $entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $entry) 0 0 0
492 0 $self->_rsync2(
493 dir => $entry,
494 archive_opts => DEFAULT_ARCHIVE_OPTS,
495 extra_opts => $dir->excludes(),
496 );
497 }
498 }
499 }
500  
501 8 ▶sub _full_backup {
Subroutine _full_backup: called CC=2 SCAR=7.4
502 8 my $self = shift;
503 8 my $dir = shift;
504  
505 8 ▶ if ( $dir->chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->chunked) { } 0 8 8
506 0 $self->_full_backup_chunked($dir);
507 }
508 else {
509 8 $self->_rsync2(
510 dir => $dir->dirname,
511 archive_opts => DEFAULT_ARCHIVE_OPTS,
512 extra_opts => $dir->excludes(),
513 );
514 }
515 }
516  
517 0 ▶sub _inc_backup_chunked {
Subroutine _inc_backup_chunked: not called CC=3 SCAR=24.8
518 0 my $self = shift;
519 0 my $dir = shift;
520 0 my $last_backup_dir = shift;
521 0 my $link_dest = shift;
522  
523 0 $self->_rsync2(
524 dir => $dir->dirname,
525 archive_opts => ARCHIVE_NO_RECURSE_OPTS,
526 extra_opts => $dir->excludes(),
527 link_dest => $link_dest,
528 );
529  
530 0 my @entries = read_dir( $dir->dirname, prefix => 0 );
531  
532 0 foreach my $entry (@entries) {
533  
534 0 my $abs_entry = sprintf( '%s/%s', $dir->dirname, $entry );
535  
536 0 ▶ if ( -d $abs_entry ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $abs_entry) 0 0 0
537 0 $self->_rsync2(
538 dir => $abs_entry,
539 archive_opts => DEFAULT_ARCHIVE_OPTS,
540 extra_opts => $dir->excludes(),
541 link_dest => sprintf( '%s/%s', $link_dest, $entry ),
542 );
543 }
544 }
545 }
546  
547 0 ▶sub _inc_backup {
Subroutine _inc_backup: not called CC=2 SCAR=17.9
548 0 my $self = shift;
549 0 my $dir = shift;
550 0 my $last_backup_dir = shift;
551  
552 0 my $link_dest = sprintf(
553 "%s/%s/%s",
554 $self->get_dest_dir, #
555 $last_backup_dir, #
556 $dir->dirname, #
557 );
558  
559 0 ▶ if ( $dir->chunked ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir->chunked) { } 0 0 0
560 0 $self->_inc_backup_chunked( $dir, $last_backup_dir, $link_dest );
561 }
562 else {
563 0 $self->_rsync2(
564 dir => $dir->dirname,
565 archive_opts => DEFAULT_ARCHIVE_OPTS,
566 extra_opts => $dir->excludes(),
567 link_dest => $link_dest,
568 );
569 }
570 }
571  
572 26 ▶sub _mk_dest_dir {
Subroutine _mk_dest_dir: called CC=1 SCAR=0.0
573 26 my $self = shift;
574 26 my $dir = shift;
575 26 my $dryrun = shift;
576  
577 26 my $cmd = sprintf( "mkdir -p %s", $dir );
578 26 $self->_ssh( $cmd, $dryrun );
579 }
580  
581 9 ▶sub _set_datestamp {
Subroutine _set_datestamp: called CC=1 SCAR=0.0
582 9 my $self = shift;
583  
584 9 my $t = localtime;
585 9 $self->{datestamp} = sprintf(
586 "%04d-%02d-%02d_%02d:%02d:%02d",
587 $t->year + 1900,
588 $t->mon + 1,
589 $t->mday, $t->hour, $t->min, $t->sec
590 );
591 }
592  
593 =head2 dump_conf
594  
595 Does what it says.
596  
597 =cut
598  
599 0 ▶sub dump_conf {
Subroutine dump_conf: not called CC=1 SCAR=6.9
Pod documented
600 0 my $self = shift;
601  
602 0 pdump $self->{conf};
603 }
604  
605 =head2 backup
606  
607 Invokes the backup process. Takes no args.
608  
609 =cut
610  
611 9 ▶sub backup {
Subroutine backup: called CC=4 SCAR=15.4
Pod documented
612 9 my $self = shift;
613  
614 9 $self->_mk_dest_dir( $self->get_dest_dir );
615 9 my @backups = $self->get_list_of_backups;
616 9 $self->_set_datestamp;
617 9 $self->_mk_dest_dir( $self->_get_dest_tmp_dir, $self->{dryrun} );
618  
619 9 foreach my $dir ( $self->_get_dirs ) {
620  
621 8 my $dirname = $dir->dirname();
622 8 ▶ if ( -d $dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dirname) { } 8 0 8
623  
624 8 $self->_info("backing up $dirname");
625  
626 8 ▶ if ( !@backups ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @backups) { } 8 0 8
627  
628 # full
629 8 $self->_full_backup($dir);
630 }
631 else {
632 # incremental
633 0 $self->_inc_backup( $dir, $backups[$#backups] );
634 }
635 }
636 else {
637 0 $self->_info("skipping $dirname because it does not exist");
638 }
639  
640 }
641  
642 $self->_ssh(
643 sprintf( "mv %s %s",
644 $self->_get_dest_tmp_dir, $self->_get_dest_backup_dir ),
645 $self->{dryrun}
646 0 );
647  
648 0 $self->expire();
649  
650 0 return 1;
651 }
652  
653 =head2 expire
654  
655 Expire backups. Gets a list of current backups and removes old ones that are
656 beyond the cutoff (see "copies" in the conf file).
657  
658 =cut
659  
660 0 ▶sub expire {
Subroutine expire: not called CC=3 SCAR=24.8
Pod documented
661 0 my $self = shift;
662  
663 0 my @list = $self->get_list_of_backups;
664  
665 0 ▶ while ( scalar(@list) > $self->{conf}->{copies} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @list > $self->{"conf"}{"copies"}) 0 0 0
666  
667 0 my $subdir = shift @list;
668 0 my $del_dir = sprintf( "%s/%s", $self->get_dest_dir, $subdir );
669  
670 my $cmd = sprintf( "%s rm -rf $del_dir",
671 0 ▶ $self->{conf}->{use_sudo} ? 'sudo' : '' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'conf'}{'use_sudo'} ? : 0 0 0
672  
673 0 $self->_ssh($cmd);
674 }
675 }
676  
677 =head2 get_backup_host
678  
679 Returns the backup_host name.
680  
681 =cut
682  
683 0 ▶sub get_backup_host {
Subroutine get_backup_host: not called CC=1 SCAR=6.9
Pod documented
684 0 my $self = shift;
685 0 return $self->{conf}->{backup_host};
686 }
687  
688 =head2 get_dest_dir
689  
690 Returns the dest_dir.
691  
692 =cut
693  
694 47 ▶sub get_dest_dir {
Subroutine get_dest_dir: called CC=4 SCAR=15.4
Pod documented
695 47 my $self = shift;
696  
697 47 my $hostname = hostname();
698 47 $hostname =~ s/\..+$//;
699  
700 47 ▶ if ( $self->{conf}->{append_machine_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"conf"}{"append_machine_id"}) 3 44 47
701  
702 # uncoverable branch true
703 3 ▶ if ( !-f '/etc/machine-id' ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f "/etc/machine-id") 0 3 3
704  
705 # uncoverable statement count:2
706 0 my $data_uuid = Data::UUID->new;
707 0 my $uuid = $data_uuid->create_str();
708  
709 # uncoverable statement count:3
710 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
711 or confess "failed to open /etc/machine-id: $!";
712 0 print $fh "$uuid\n";
713 0 close($fh);
714 }
715  
716 3 my $uuid = slurp("/etc/machine-id");
717 3 chomp $uuid;
718  
719 3 $hostname = "$hostname-$uuid";
720 }
721  
722 47 return sprintf( "%s/%s", $self->{conf}->{dest_dir}, $hostname );
723 }
724  
725 =head2 get_list_of_backups
726  
727 Returns an array of backups. They are in the format of "YYYY-MM-DD_HH:MM:SS".
728 =cut
729  
730 9 ▶sub get_list_of_backups {
Subroutine get_list_of_backups: called CC=3 SCAR=14.2
Pod documented
731 9 my $self = shift;
732  
733 9 my @backups;
734  
735 9 my @list = $self->_ssh( sprintf( "ls %s", $self->get_dest_dir ) );
736  
737 9 foreach my $e (@list) {
738 0 chomp $e;
739  
740 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
741 0 push( @backups, $e );
742 }
743 }
744  
745 9 return @backups;
746 }
747  
748 11 1;
↑ Summary EZ/Dir.pm »