blib/lib/Dir/Rocknroll.pm

filter: Statementstmt 26.2% 112 / 426 Branchbran 0.6% 1 / 148 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 51.7% 29 / 56 Podpod n/a totaltotal 20.7% 142 / 683 CC 90 SCAR 49.3
CC
90
Cov
19%
roll
552.0
_run
306.0
_myexec
90.0
CRAP 138.4
↑ Summary
1 package Dir::Rocknroll ;
2  
3 ####################################################
4 #
5 # rocknroll : Rsync fOr baCKup and Roll
6 #
7 # Jacquelin Charbonnel - CNRS/Mathrice/LAREMA - 2006-09-04
8 #
9 # $Id: Rocknroll.pm 488 2012-09-01 19:14:00Z jaclin $
10 #
11 ####################################################
12  
13 2 require Exporter ;
14 2 @ISA = qw(Exporter);
15 2 @EXPORT=qw() ;
16 2 @EXPORT_OK = qw( );
17  
18 2 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 #our $VERSION = "0.".eval{'$Rev: 488 $'=~/(\d+)/;$1;} ;
23 2 our $VERSION = 0.34 ;
24  
25 2 ▶use Data::Dumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Sys::Syslog ;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use File::Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 ▶use Getopt::Long ;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 2 ▶use File::Basename ;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use FileHandle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use DirHandle ;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use FindBin ;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use Net::SMTP ;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use Sys::Hostname;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use Config::General ;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use Config::General::Extended ;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 2 ▶use Dir::Which q/which/;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 2 my $this_prog = 'rocknroll' ;
40 2 my $NEW_EXT = "_running_snapshot_" ;
41 2 my $OLD_EXT = "_snapshot_to_delete_" ;
42 2 my $CONFFILE = "$this_prog.conf" ;
43 2 my $CONFPATH = $FindBin::Bin.":".$FindBin::Bin."/../etc:/etc:/etc/${this_prog}.d" ;
44 2 my $FACILITY = "local7" ;
45 2 my %arg_conf ;
46  
47 2 my $default_conf = {
48 "continue" => 0
49 , "debug" => 0
50 , "dry-run" => 0
51 , "refresh" => 0
52 , "link-dest" => ""
53 , "mail_from" => 'root@localhost'
54 , "mail_to" => 'root@localhost'
55 , "max_runtime" => 360 # 6h
56 , "no-links" => 0
57 , "no-roll" => 0
58 , "rsync_path" => "/usr/bin/rsync"
59 , "rsync_retcode_ok" => 0
60 , "rsync_retcode_warn" => 24
61 , "ro" => ["--stats"]
62 , "ro_default" => "--hard-links --archive -e ssh"
63 , "send_warn" => 0
64 , "smtp_server" => "localhost"
65 , "update" => 0
66 , "use_syslog" => 1
67 , "verbose" => 0
68 } ;
69  
70 2 my ($log,@files,$init,@excludes,@rsync_options,$dry_run,$config,$config_file,$arg) ;
71  
72 ############################################################
73  
74 {
75 package _Log ;
76 2 require Exporter ;
77 2 our @ISA = qw(Exporter);
78 2 our @EXPORT=qw() ;
79  
80 2 ▶ use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 ▶ use Data::Dumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 2 ▶ use Dir::Which q/which/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 2 ▶ use Sys::Syslog ;
Subroutine BEGIN: called CC=1 SCAR=0.0
84  
85 #my ($DEBUG,$INFO,$WARN,$ERR) = (1,2,3,4) ;
86  
87 #--------------------
88 sub new
89 0 ▶ {
Subroutine new: not called CC=1 SCAR=6.9
90 0 my($type,$cmdline,$level) = @_ ;
91 0 my($this) ;
92  
93 # %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
94  
95 0 $this->{"level"} = $level ;
96 0 $this->{"cmdline"} = $cmdline ;
97 0 $this->{"log"} = [] ;
98 0 $this->{"criticity"} = 0 ;
99  
100 0 bless $this,$type ;
101  
102 0 openlog($this_prog,"ndelay,pid", "local0") ;
103 0 return $this ;
104 }
105  
106 sub set_level
107 0 ▶ {
Subroutine set_level: not called CC=1 SCAR=6.9
108 0 my($this,$level) = @_ ;
109 0 $this->{"level"} = $level ;
110 }
111  
112 sub send
113 0 ▶ {
Subroutine send: not called CC=3 SCAR=24.8
114 0 my ($this)=@_ ;
115  
116 0 ▶ return unless scalar(@{$this->{"log"}}>0) ;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar(@{$this->{"log"};} > 0) 0 0 0
117  
118 0 my $smtp_server = $config->get("smtp_server") ;
119 0 my $smtp ;
120  
121 0 ▶ if ($smtp = Net::SMTP->new($smtp_server))
Branch: 1 branch
BranchTrueFalseTotal
if ($smtp = 'Net::SMTP'->new($smtp_server)) { } 0 0 0
122 {
123 #die Dumper $smtp_server ;
124 0 $smtp->mail($config->get("mail_from"));
125 0 $smtp->to($config->get("mail_to"));
126  
127 0 $smtp->data();
128 0 $smtp->datasend("From: ".$config->get("mail_from")."\n");
129 0 $smtp->datasend("To: ".$config->get("mail_to")."\n");
130 0 $smtp->datasend("Subject: $this_prog ".$this->{"criticity"}."\n");
131 0 $smtp->datasend("\n");
132 0 $smtp->datasend("running command : $this_prog ".$this->{"cmdline"}."\n");
133 0 $smtp->datasend("output :\n");
134 0 $smtp->datasend(join("\n",@{$this->{"log"}}));
135 0 $smtp->dataend();
136 0 $smtp->quit;
137 }
138 else
139 {
140 0 $this->warn("can't connect to the SMTP server '$smtp_server'") ;
141 0 print join("\n",@$log) ;
142 }
143 }
144 #--------------------
145 sub debug
146 0 ▶ {
Subroutine debug: not called CC=2 SCAR=17.9
147 0 my($this,$msg) = @_ ;
148 0 ▶ print "$msg\n" if $this->{"level"}==2 ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"level"} == 2 0 0 0
149 }
150 sub info
151 0 ▶ {
Subroutine info: not called CC=3 SCAR=24.8
152 0 my($this,$msg) = @_ ;
153 0 ▶ if ($this->{"level"}>=1)
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"level"} >= 1) 0 0 0
154 {
155 0 ▶ if ($config->get("use_syslog")==1) { syslog("info",$msg) ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get("use_syslog") == 1) 0 0 0
156 0 print "$msg\n" ;
157 }
158 }
159 sub warn
160 0 ▶ {
Subroutine warn: not called CC=2 SCAR=17.9
161 0 my($this,$msg) = @_ ;
162 0 ▶ if ($config->get("use_syslog")==1) { syslog("warn",$msg) ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get('use_syslog') == 1) { } 0 0 0
163 0 else { print "$msg\n" ; }
164 }
165 sub crit
166 0 ▶ {
Subroutine crit: not called CC=2 SCAR=17.9
167 0 my($this,$msg) = @_ ;
168 0 ▶ if ($config->get("use_syslog")==1) { syslog("crit",$msg) ; }
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get("use_syslog") == 1) 0 0 0
169 0 die "$msg\nExecution aborted !\n" ;
170 }
171 sub warn_by_mail
172 0 ▶ {
Subroutine warn_by_mail: not called CC=4 SCAR=30.0
173 0 my($this,$msg) = @_ ;
174  
175 0 $this->{"criticity"} = "warn" ;
176 0 ▶ if ($config->get("mail_to")=~/\S/ && $config->get("send_warn")==1)
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get('mail_to') =~ /\S/ and $config->get('send_warn') == 1) { } 0 0 0
Condition0%0/3
$config->get("mail_to") =~ /\S/ and $config->get("send_warn") == 1
!ll&&!rl&&r
000
MC/DC0%0/2
$config->get("mail_to") =~ /\S/ and $config->get("send_warn") == 1
$config->get("mail_to") =~ /\S/ $config->get("send_warn") == 1
Truth table0%0/3
A and B
A= $config->get("mail_to") =~ /\S/B= $config->get("send_warn") == 1
ABresult
0X0
100
111
177 {
178 0 push(@{$this->{"log"}},"WARN: $msg") ;
179 0 ▶ syslog("info",split(/\n/,$msg)) if $config->get("use_syslog")==1 ;
Branch: 1 branch
BranchTrueFalseTotal
if $config->get("use_syslog") == 1 0 0 0
180 }
181 else
182 0 { $this->warn($msg) ; }
183 }
184 sub crit_by_mail
185 0 ▶ {
Subroutine crit_by_mail: not called CC=3 SCAR=24.8
186 0 my($this,$msg) = @_ ;
187  
188 0 $this->{"criticity"} = "crit" ;
189 0 ▶ if ($config->get("mail_to")=~/\S/)
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get("mail_to") =~ /\S/) 0 0 0
190 {
191 0 push(@{$this->{"log"}},"CRIT: $msg") ;
192 0 ▶ syslog("info",$msg) if $config->get("use_syslog")==1 ;
Branch: 1 branch
BranchTrueFalseTotal
if $config->get("use_syslog") == 1 0 0 0
193 }
194 0 $this->crit($msg) ;
195 }
196 }
197 2 ▶END { $log->send() if $log ; closelog() ; }
Subroutine END: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
if $log 0 2 2
198  
199 ############################################################
200  
201 {
202 package _Config ;
203 2 require Exporter ;
204 2 our @ISA = qw(Exporter);
205 2 our @EXPORT=qw() ;
206  
207 2 ▶ use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 2 ▶ use Data::Dumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
209 2 ▶ use Dir::Which q/which/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
210  
211 #--------------------
212 sub new
213 0 ▶ {
Subroutine new: not called CC=2 SCAR=17.9
214 0 my($type,%h) = @_ ;
215 0 my($this) ;
216  
217 0 ▶ %h = map { /^-/ ? lc(substr($_,1)) : $_ ; } %h ;
Branch: 1 branch
BranchTrueFalseTotal
/^-/ ? : 0 0 0
218  
219 0 $this->{"args"} = \%h ;
220  
221 0 bless $this,$type ;
222 0 return $this ;
223 }
224  
225 #--------------------
226 sub init
227 0 ▶ {
Subroutine init: not called CC=1 SCAR=6.9
228 0 my($this,$default,$arg) = @_ ;
229  
230 0 $this->{"default"} = $default ;
231 0 $this->{"arg"} = $arg ;
232 }
233  
234 #--------------------
235 sub get_arg
236 0 ▶ {
Subroutine get_arg: not called CC=2 SCAR=17.9
237 0 my($this,$arg) = @_ ;
238 0 ▶ carp "arg '$arg' undefined" unless exists($this->{"args"}{$arg}) ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"args"}{$arg} 0 0 0
239 0 return $this->{"args"}{$arg} ;
240 }
241 #--------------------
242 sub load
243 0 ▶ {
Subroutine load: not called CC=5 SCAR=34.0
244 0 my($this) = @_ ;
245 0 my $file = $this->get_arg("file") ;
246 0 my $path = $this->get_arg("path") ;
247  
248 0 my $confname = which(-entry=>$file
249 ,-defaultpath=>$path
250 ) ;
251 0 ▶ return unless defined $confname ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $confname 0 0 0
252
253 0 ▶ my $conffile=new Config::General(-ConfigFile=>$confname,-ExtendedAccess => 1) or $log->warn("can't read config file $confname") ;
Branch: 1 branch
BranchTrueFalseTotal
my $conffile = "Config::General"->new("-ConfigFile", $confname, "-ExtendedAccess", 1) or $log->warn("can't read config file $confname") 0 0 0
254 0 my %conf ;
255 {
256 0 my %this_conf = $conffile->getall() ;
257 0 for my $k (keys %this_conf)
258 {
259 0 ▶ die "unknown parameter '$k' in $file\n" unless exists $this->{"default"}{$k} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $this->{"default"}{$k} 0 0 0
260 0 $conf{$k} = $this_conf{$k} ;
261 }
262 }
263 # die Dumper \%conf ;
264 0 $this->{"conf"} = \%conf ;
265 0 $this->{"confname"} = $confname ;
266 }
267 sub get
268 0 ▶ {
Subroutine get: not called CC=4 SCAR=30.0
269 0 my($this,$var) = @_ ;
270  
271 0 ▶ return $this->{"arg"}{$var} if exists($this->{"arg"}{$var}) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"arg"}{$var} 0 0 0
272 0 ▶ return $this->{"conf"}{$var} if exists($this->{"conf"}{$var}) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"conf"}{$var} 0 0 0
273 0 ▶ return $this->{"default"}{$var} if exists($this->{"default"}{$var}) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $this->{"default"}{$var} 0 0 0
274 0 die "'$var' not found in config\n" ;
275 }
276 }
277  
278 ############################################################
279  
280 {
281 2 package _RocknRoll ;
282 2 require Exporter ;
283 2 our @ISA = qw(Exporter);
284 2 our @EXPORT=qw() ;
285  
286 2 ▶ use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
287 2 ▶ use Data::Dumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
288 2 ▶ use Dir::Which q/which/ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
289 2 ▶ use File::stat ;
Subroutine BEGIN: called CC=1 SCAR=0.0
290  
291 #--------------------
292 sub new
293 0 ▶ {
Subroutine new: not called CC=3 SCAR=24.8
294 0 my($type,$dstdir,$interval) = @_ ;
295 0 my($this) ;
296  
297 0 $this->{"dstdir"} = $dstdir ;
298 0 $this->{"interval"} = $interval ;
299 0 $this->{NORMAL} = 1 ;
300 0 @{$this->{rsync_opt}} = () ;
301  
302 # get files
303 0 ▶ my $d = new DirHandle($dstdir) or $log->crit("can't read directory $dstdir: $!\n") ;
Branch: 1 branch
BranchTrueFalseTotal
my $d = "DirHandle"->new($dstdir) or $log->crit("can't read directory ${dstdir}: $!\n") 0 0 0
304 0 @{$this->{"files"}} = $d->read() ;
305  
306 0 bless $this,$type ;
307  
308 0 ▶ $this->check_if_running() unless $config->get("continue") ;
Branch: 1 branch
BranchTrueFalseTotal
unless $config->get("continue") 0 0 0
309  
310 0 return $this ;
311 }
312 #--------------------
313 sub get_archives
314 0 ▶ {
Subroutine get_archives: not called CC=1 SCAR=6.9
315 0 my($this) = @_ ;
316 0 my $interval = $this->{"interval"} ;
317  
318 0 return grep /^$interval.\d+$/,@{$this->{"files"}} ;
319 }
320 #--------------------
321 sub check_if_running
322 0 ▶ {
Subroutine check_if_running: not called CC=3 SCAR=24.8
323 0 my($this) = @_ ;
324 0 my $dstdir = $this->{"dstdir"} ;
325 0 my $interval = $this->{"interval"} ;
326 0 my @files = @{$this->{"files"}} ;
327 0 my $running = ".$interval.running" ;
328  
329 0 ▶ if ( grep /^$running$/,@files )
Branch: 1 branch
BranchTrueFalseTotal
if (grep /^$running$/, @files) 0 0 0
330 {
331 0 $this->{"running_exists"} = 1 ;
332 0 my $st = stat($this->{"dstdir"}."/$running") ;
333 0 ▶ if (time()-$st->ctime < $config->get("max_runtime")*60)
Branch: 1 branch
BranchTrueFalseTotal
if (time - $st->ctime < $config->get('max_runtime') * 60) { } 0 0 0
334 {
335 0 $log->crit_by_mail(sprintf("a directory '$dstdir/$running' (with ctime<%dmin) found.",$config->get("max_runtime"))) ;
336 }
337 else
338 {
339 0 $log->warn_by_mail("a directory '$running' already exists in '$dstdir', it will be overwritten.") ;
340 0 push(@{$this->{rsync_opt}},"--delete") ;
341 }
342 }
343 }
344  
345 #--------------------
346 sub read_control
347 0 ▶ {
Subroutine read_control: not called CC=3 SCAR=24.8
348 0 my($this,$nb) = @_ ;
349  
350 0 my $dstdir = $this->{"dstdir"} ;
351 0 my $interval = $this->{"interval"} ;
352  
353 0 ▶ if (open F,"$dstdir/.$interval.ctl")
Branch: 1 branch
BranchTrueFalseTotal
if (open F, "$dstdir/.$interval.ctl") { } 0 0 0
354 {
355 0 my $line = <F> ;
356 0 close(F) ;
357 0 ▶ if ($line=~/^\s*nb_archives\s*:\s*\d+\s*$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*nb_archives\s*:\s*\d+\s*$/) { } 0 0 0
358 {
359 0 my ($nb) = $line=~/^\s*nb_archives\s*:\s*(\d+)\s*$/ ;
360 0 return $nb ;
361 }
362 0 else { return -1 ; }
363 }
364 0 else { return -1 ; }
365 }
366 #--------------------
367 sub write_control
368 0 ▶ {
Subroutine write_control: not called CC=2 SCAR=17.9
369 0 my($this,$nb) = @_ ;
370  
371 0 my $dstdir = $this->{"dstdir"} ;
372 0 my $interval = $this->{"interval"} ;
373  
374 0 ▶ open F,">$dstdir/.$interval.ctl" or die "$!" ;
Branch: 1 branch
BranchTrueFalseTotal
open F, ">$dstdir/.$interval.ctl" or die "$!" 0 0 0
375 0 print F "nb_archives:$nb" ;
376 0 close(F) ;
377 }
378 #--------------------
379 sub check_if_complete
380 0 ▶ {
Subroutine check_if_complete: not called CC=6 SCAR=37.4
381 0 my($this) = @_ ;
382  
383 0 my $dstdir = $this->{"dstdir"} ;
384 0 my $interval = $this->{"interval"} ;
385  
386 0 my @archives = $this->get_archives() ;
387 0 ▶ die("No archive found in $dstdir for interval $interval !\n"
Branch: 1 branch
BranchTrueFalseTotal
if @archives == 0 0 0 0
388 ."(you must do '$this_prog --init n $interval $dstdir' first),\n\twhere n is the number of archives expected\n")
389 if @archives==0 ;
390  
391 0 my $found = @archives ;
392 0 my $require = $this->read_control() ;
393  
394 0 ▶ if ($require!=-1)
Branch: 1 branch
BranchTrueFalseTotal
if ($require != -1) { } 0 0 0
395 {
396 0 ▶ if ($require > $found)
Branch: 2 branches
BranchTrueFalseTotal
if ($require > $found) { } 0 0 0
elsif ($require < $found) { } 0 0 0
397 {
398 0 my $diff = $require-$found ;
399 0 my $un = $diff==1 ;
400 0 ▶ $log->warn_by_mail(sprintf("%s archive%s missing in %s",$diff,$un?"":"s",$this->{"dstdir"})) ;
Branch: 1 branch
BranchTrueFalseTotal
$un ? : 0 0 0
401 0 $this->{NORMAL} = 0 ;
402 }
403 elsif ($require < $found)
404 {
405 0 $log->warn_by_mail("$require archives required, but $found found !!") ;
406 0 $this->{NORMAL} = 0 ;
407 # que faire ?
408 }
409 }
410 else
411 {
412 0 $log->warn_by_mail("no control file found in ".$this->{"dstdir"}.", $found required archives supposed") ;
413 0 $this->write_control($found) ;
414 }
415 0 $this->{"require"} = $require ;
416 0 $this->{"found"} = $found ;
417 }
418 #--------------------
419 sub mkdirs
420 0 ▶ {
Subroutine mkdirs: not called CC=4 SCAR=30.0
421 0 my($this,$nb) = @_ ;
422  
423 0 my $dstdir = $this->{"dstdir"} ;
424 0 my $interval = $this->{"interval"} ;
425 0 my @files = grep /^$interval.\d+$/,@{$this->{"files"}} ;
426 0 my $running = ".$interval.running" ;
427  
428 0 ▶ die("'$interval' archives already exist on $dstdir, abort !\n") if @files!=0 ;
Branch: 1 branch
BranchTrueFalseTotal
if @files != 0 0 0 0
429  
430 0 for my $i (1..$nb)
431 {
432 0 my $dir = "$dstdir/$interval.$i" ;
433 0 ▶ mkdir $dir or die("can't create $dir, abort !\n") ;
Branch: 1 branch
BranchTrueFalseTotal
mkdir $dir or die "can't create $dir, abort !\n" 0 0 0
434 }
435 0 $this->write_control($nb) ;
436 }
437 #--------------------
438 sub rock
439 0 ▶ {
Subroutine rock: not called CC=7 SCAR=40.3
440 0 my($this,$srcdir) = @_ ;
441  
442 0 my $dstdir = $this->{"dstdir"} ;
443 0 my $interval = $this->{"interval"} ;
444 0 my @files = @{$this->{"files"}} ;
445 0 ▶ my $running = ($config->get("refresh") || $config->get("update"))
Branch: 1 branch
BranchTrueFalseTotal
$config->get('refresh') || $config->get('update') ? : 0 0 0
Condition0%0/3
$config->get("refresh") or $config->get("update")
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->get("refresh") or $config->get("update")
$config->get("refresh") $config->get("update")
Truth table0%0/3
A or B
A= $config->get("refresh")B= $config->get("update")
ABresult
1X1
011
000
446 ? "$interval.1"
447 : ".$interval.running" ;
448  
449 0 my $jro = join(" ",@{$config->get("ro")}) ;
450 0 ▶ push(@{$this->{rsync_opt}},"--delete") if $config->get("update") ;
Branch: 1 branch
BranchTrueFalseTotal
if $config->get("update") 0 0 0
451 0 my $require = $this->{"require"} ;
452  
453 # options
454 0 my $ld ;
455 0 ▶ if ($config->get("no-links")==1)
Branch: 1 branch
BranchTrueFalseTotal
if ($config->get('no-links') == 1) { } 0 0 0
456 {
457 0 $ld = "" ;
458 }
459 else
460 {
461 0 ▶ $ld = exists($this->{"linkdest"})
Branch: 1 branch
BranchTrueFalseTotal
exists $this->{'linkdest'} ? : 0 0 0
462 ? "--link-dest=$this->{'linkdest'}"
463 : "--link-dest=../${interval}.1" ;
464 }
465  
466 my $cmd = sprintf("%s %s %s %s $ld $srcdir $dstdir/$running"
467 , $config->get("rsync_path")
468 0 , join(" ",@{$this->{rsync_opt}})
469 , $config->get("ro_default")
470 , $jro
471 ) ;
472  
473 0 my $retcode = main::_myexec($cmd) ;
474 0 ▶ $log->crit_by_mail("$dstdir/$running not found after rsync execution") unless -d "$dstdir/$running" ;
Branch: 1 branch
BranchTrueFalseTotal
unless -d "$dstdir/$running" 0 0 0
475 0 return $retcode ;
476 }
477 #--------------------
478 sub roll
479 0 ▶ {
Subroutine roll: not called CC=23 SCAR=63.1
480 0 my($this) = @_ ;
481  
482 0 my $dstdir = $this->{"dstdir"} ;
483 0 my $interval = $this->{"interval"} ;
484 0 my $require = $this->{"require"} ;
485 0 my @files = grep /^$interval.\d+$/,@{$this->{"files"}} ;
486 0 my $running = ".$interval.running" ;
487 0 my $found = $this->{"found"} ;
488 0 my (@to_roll,@exist,@next_rank,@steps,@roll,@place) ;
489  
490 # determine the future rank of each archive
491 0 my ($i,$j) = (1,2) ;
492  
493 0 ▶ while ($found>0)
Branch: 1 branch
BranchTrueFalseTotal
if ($found > 0) 0 0 0
494 {
495 0 ▶ if (scalar(grep(/^$interval.$i$/,@files)) == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (scalar grep(/^$interval.$i$/, @files) == 1) { } 0 0 0
496 {
497 # this archive exists
498 0 $found-- ;
499 0 ▶ $j=-1 if $j>$require || $j==0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $j > $require or $j == 0 0 0 0
Condition0%0/3
$j > $require or $j == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$j > $require or $j == 0
$j > $require $j == 0
Truth table0%0/3
A or B
A= $j > $requireB= $j == 0
ABresult
1X1
011
000
500 0 push @to_roll,$i ;
501 0 $exist[$i] = 1 ;
502 0 $place[$i] = 1 ;
503 0 $next_rank[$i] = $j ;
504 0 $j++ ;
505 }
506 else
507 {
508 0 $exist[$i] = 0 ;
509 }
510 0 $i++ ;
511 }
512  
513 # determine the order of future rename operation
514 0 my $again ;
515 do
516 0 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$again and do { do { $again = 0; foreach $i (reverse @to_roll) { next unless $exist[$i] == 1; next if defined $roll[$i] and $roll[$i] == 1; next if $next_rank[$i] != -1 and defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1; $log->debug('exist: ' . Dumper(\@exist)); $log->debug('roll: ' . Dumper(\@roll)); $log->debug('next_rank: ' . Dumper(\@next_rank)); $log->debug('place: ' . Dumper(\@place)); $again = 1; if ($next_rank[$i] == -1) { push @steps, [$i, -1]; $place[$i] = 0; $roll[$i] = 1; next; } ; unless (defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1) { push @steps, [$i, $next_rank[$i]]; $place[$next_rank[$i]] = 1; $place[$i] = 0; $roll[$i] = 1; } ; } } } 0 0 0
517 0 $again = 0 ;
518 0 for $i (reverse @to_roll)
519 {
520 0 ▶ next unless $exist[$i]==1 ; # this archive doesn't exist
Branch: 1 branch
BranchTrueFalseTotal
unless $exist[$i] == 1 0 0 0
521 0 ▶ next if defined($roll[$i]) && $roll[$i]==1 ; # this archive has already rolled
Branch: 1 branch
BranchTrueFalseTotal
if defined $roll[$i] and $roll[$i] == 1 0 0 0
Condition0%0/3
defined $roll[$i] and $roll[$i] == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $roll[$i] and $roll[$i] == 1
defined $roll[$i] $roll[$i] == 1
Truth table0%0/3
A and B
A= defined $roll[$i]B= $roll[$i] == 1
ABresult
0X0
100
111
522 0 ▶ next if $next_rank[$i]!=-1 && defined($place[$next_rank[$i]]) && $place[$next_rank[$i]]==1 ; # next place is not free
Branch: 1 branch
BranchTrueFalseTotal
if $next_rank[$i] != -1 and defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1 0 0 0
Condition0%0/6
$next_rank[$i] != -1 and defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1
!ll&&!rl&&r
000
$next_rank[$i] != -1 and defined $place[$next_rank[$i]]
!ll&&!rl&&r
000
MC/DC0%0/3
$next_rank[$i] != -1 and defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1
$next_rank[$i] != -1 defined $place[$next_rank[$i]] $place[$next_rank[$i]] == 1
Truth table0%0/4
A and B and C
A= $next_rank[$i] != -1B= defined $place[$next_rank[$i]]C= $place[$next_rank[$i]] == 1
ABCresult
0XX0
10X0
1100
1111
523 0 $log->debug("exist: ".Dumper \@exist) ;
524 0 $log->debug("roll: ".Dumper \@roll) ;
525 0 $log->debug("next_rank: ".Dumper \@next_rank) ;
526 0 $log->debug("place: ".Dumper \@place) ;
527 0 $again = 1 ; # at least one archive performed
528  
529 # oldest(s) archives to delete
530 0 ▶ if ($next_rank[$i]==-1)
Branch: 1 branch
BranchTrueFalseTotal
if ($next_rank[$i] == -1) 0 0 0
531 {
532 0 push @steps,[$i,-1] ;
533 0 $place[$i] = 0 ;
534 0 $roll[$i] = 1 ;
535 0 next ;
536 }
537  
538 # is the next rank free ?
539 #if (! (defined($next_rank[$i]) && defined($place[$next_rank[$i]]) && $place[$next_rank[$i]]==1))
540 0 ▶ if (! (defined($place[$next_rank[$i]]) && $place[$next_rank[$i]]==1))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1) 0 0 0
Condition0%0/3
defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $place[$next_rank[$i]] and $place[$next_rank[$i]] == 1
defined $place[$next_rank[$i]] $place[$next_rank[$i]] == 1
Truth table0%0/3
A and B
A= defined $place[$next_rank[$i]]B= $place[$next_rank[$i]] == 1
ABresult
0X0
100
111
541 {
542 0 push @steps,[$i,$next_rank[$i]] ;
543 0 $place[$next_rank[$i]] = 1 ;
544 0 $place[$i] = 0 ;
545 0 $roll[$i] = 1 ;
546 }
547 }
548 } while $again ;
549 0 $log->debug("NORMAL: ".$this->{NORMAL}) ;
550 0 $log->debug("steps: ".Dumper \@steps) ;
551 0 for my $s (@steps)
552 {
553 0 my($old,$new) = @$s ;
554 0 my $msg ;
555  
556 0 ▶ if ($new==-1)
Branch: 1 branch
BranchTrueFalseTotal
if ($new == -1) { } 0 0 0
557 {
558 my $path = sprintf("%s/%s.%d"
559 ,$this->{"dstdir"}
560 0 ,$this->{"interval"}
561 ,$old
562 ) ;
563 0 $msg = sprintf("delete $path") ;
564 0 ▶ File::Path::Tiny::rm($path) or $log->crit("can't remove $path: $!") ;
Branch: 1 branch
BranchTrueFalseTotal
File::Path::Tiny::rm($path) or $log->crit("can't remove ${path}: $!") 0 0 0
565 }
566 else
567 {
568 my $src = sprintf("%s/%s.%d",
569 ,$this->{"dstdir"}
570 0 ,$this->{"interval"}
571 ,$old
572 ) ;
573 my $dst = sprintf("%s/%s.%d"
574 ,$this->{"dstdir"}
575 0 ,$this->{"interval"}
576 ,$new
577 ) ;
578 0 $msg = "rename $src $dst" ;
579 0 ▶ rename($src,$dst) or $log->crit("can't rename $src to $dst: $!") ;
Branch: 1 branch
BranchTrueFalseTotal
rename $src, $dst or $log->crit("can't rename $src to ${dst}: $!") 0 0 0
580 }
581 0 $log->info($msg) ;
582 0 ▶ $log->warn_by_mail($msg) if $this->{NORMAL}==0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"NORMAL"} == 0 0 0 0
583 }
584 {
585 0 my $src = sprintf("%s/.%s.running",$this->{"dstdir"},$this->{"interval"}) ;
586 0 my $dst = sprintf("%s/%s.1",$this->{"dstdir"},$this->{"interval"}) ;
587 0 my $msg = sprintf("rename $src $dst") ;
588 0 $log->info($msg) ;
589 0 ▶ $log->warn_by_mail($msg) if $this->{NORMAL}==0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"NORMAL"} == 0 0 0 0
590 0 ▶ rename($src,$dst) or $log->crit("can't rename $src to $dst: $!") ;
Branch: 1 branch
BranchTrueFalseTotal
rename $src, $dst or $log->crit("can't rename $src to ${dst}: $!") 0 0 0
591 }
592 }
593 }
594  
595 ############################################################
596  
597 #--------------------
598 sub _man
599 2 ▶{
Subroutine _man: not called CC=1 SCAR=0.4
600 2 ▶ use Pod::Perldoc ;
Subroutine BEGIN: called CC=1 SCAR=0.0
601  
602 0 @ARGV = ($this_prog) ;
603 0 exit(Pod::Perldoc->run($this_prog,undef)) ;
604 }
605  
606 #--------------------
607 sub _usage
608 0 ▶{
Subroutine _usage: not called CC=1 SCAR=6.9
609 0 my($msg) = @_ ;
610  
611 0 print(<< "EOF") ;
612 Rsync fOr baCKup (and roll) - v$VERSION
613  
614 $msg
615  
616 Usage: $this_prog --init n tag dstdir (1)
617 $this_prog options tag srcdir dstdir (2)
618 $this_prog --man
619  
620 common options :
621 --debug (1)(2)
622 --excludes=DIR (2)
623 --ro rsync_option (2)
624 --link-dest=DIR (2)
625 --no-roll (2)
626 --refresh (2)
627 --update (2)
628 --continue (2)
629  
630 example :
631 $this_prog --init 7 daily /var/snapshots/home
632 initialize a set of 7 archives, named 'daily'
633  
634 $this_prog daily /home /var/snapshots/home
635 rsync a new archive of /home to the set 'daily' located into
636 /var/snapshots/home, and roll the existing archives in the set
637  
638 EOF
639 0 exit(0) ;
640 }
641  
642 #--------------------
643 sub _myexec
644 0 ▶{
Subroutine _myexec: not called CC=9 SCAR=45.0
645 0 my ($cmd) = @_ ;
646  
647 0 $log->info("exec: $cmd") ;
648 0 my @output ;
649 0 ▶ my $fcmd = new FileHandle("$cmd 2>&1|") or $log->crit_by_mail("can't execure $cmd: $!") ;
Branch: 1 branch
BranchTrueFalseTotal
my $fcmd = "FileHandle"->new("$cmd 2>&1|") or $log->crit_by_mail("can't execure ${cmd}: $!") 0 0 0
650 0 my $output ;
651 0 ▶ while (<$fcmd>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fcmd)) 0 0 0
652 {
653 0 chomp ;
654 0 $log->info("> $_") ;
655 0 push(@output,"> $_") ;
656 }
657 0 my $rc = $fcmd->close() ;
658  
659 0 ▶ if ($rc)
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
660 {
661 # all rights
662 0 $log->info(sprintf("return: %d (rc=$rc)",$?>>8)) ;
663 0 return 2 ;
664 }
665  
666 0 ▶ $log->crit_by_mail(sprintf("can't execure $cmd (rc=$rc, \$!==$!)")) if ($!) ;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
667  
668 # some issues remains...
669 0 my $retcode = $?>>8 ;
670 0 $log->info(sprintf("return: %d",$retcode)) ;
671 0 my %ok_codes = map { $_ => 1 ; } split(/[^\d]+/,$config->get("rsync_retcode_ok")) ;
672 0 my %warn_codes = map { $_ => 1 ; } split(/[^\d]+/,$config->get("rsync_retcode_warn")) ;
673  
674 # case known as OK
675 0 ▶ return 2 if (exists($ok_codes{$retcode}) && $ok_codes{$retcode}==1) ;
Branch: 1 branch
BranchTrueFalseTotal
if exists $ok_codes{$retcode} and $ok_codes{$retcode} == 1 0 0 0
Condition0%0/3
exists $ok_codes{$retcode} and $ok_codes{$retcode} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $ok_codes{$retcode} and $ok_codes{$retcode} == 1
exists $ok_codes{$retcode} $ok_codes{$retcode} == 1
Truth table0%0/3
A and B
A= exists $ok_codes{$retcode}B= $ok_codes{$retcode} == 1
ABresult
0X0
100
111
676  
677 # case not known as WARNING -> ERROR
678 0 ▶ unless (exists($warn_codes{$retcode}) && $warn_codes{$retcode}==1)
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $warn_codes{$retcode} and $warn_codes{$retcode} == 1) 0 0 0
Condition0%0/3
exists $warn_codes{$retcode} and $warn_codes{$retcode} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
exists $warn_codes{$retcode} and $warn_codes{$retcode} == 1
exists $warn_codes{$retcode} $warn_codes{$retcode} == 1
Truth table0%0/3
A and B
A= exists $warn_codes{$retcode}B= $warn_codes{$retcode} == 1
ABresult
0X0
100
111
679 {
680 0 $log->crit_by_mail(sprintf("'$cmd' returns %d (not found in OK et WARN codes)\n%s",$retcode,join("\n",@output))) ;
681 0 return 0 ;
682 }
683  
684 # other case : WARNING
685 0 $log->warn_by_mail(sprintf("'$cmd' returns %d\n%s",$retcode,join("\n",@output))) ;
686 0 return 1 ;
687 }
688  
689 #--------------------
690  
691 sub _run
692 0 ▶{
Subroutine _run: not called CC=17 SCAR=57.2
693 0 my $cmdline = join(" ",@ARGV) ;
694 0 $Data::Dumper::Terse = 1;
695  
696 0 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions(\%arg_conf, "init=s", \$init, "c=s", \$config_file, "continue", "help", \&Dir::Rocknroll::_usage, "man", \&Dir::Rocknroll::_man, "debug", "dry-run", "link-dest=s", "mail_from=s", "mail_to=s", "max_runtime=i", "no-links", "no-roll", "refresh", "rsync_retcode_ok=s", "rsync_retcode_warn=s", "ro=s\@", "ro_default=s", "send_warn=i", "smtp_server=s", "update", "use_syslog=i", "verbose") or _usage("") 0 0 0
697 \%arg_conf
698 , "init=s" => \$init
699 , "c=s" => \$config_file
700 , "continue" # start again with an existing .running archive
701 , "help" => \&_usage
702 , "man" => \&_man
703 , "debug"
704 , "dry-run"
705 , "link-dest=s"
706 , "mail_from=s"
707 , "mail_to=s"
708 , "max_runtime=i"
709 , "no-links"
710 , "no-roll"
711 , "refresh" # update archive .1, without deleting any files
712 , "rsync_retcode_ok=s"
713 , "rsync_retcode_warn=s"
714 , "ro=s@"
715 , "ro_default=s"
716 , "send_warn=i"
717 , "smtp_server=s"
718 , "update" # update archive .1, and delete obsolete files
719 , "use_syslog=i"
720 , "verbose"
721 ) or _usage("") ;
722  
723 # load conf file
724 0 ▶if ($config_file)
Branch: 1 branch
BranchTrueFalseTotal
if ($config_file) { } 0 0 0
725 {
726 0 $config = new _Config(-file=>$config_file,-path=>"") ;
727 }
728 else
729 {
730 0 $config = new _Config(
731 -file=>"$this_prog.conf"
732 , -path=>$CONFPATH
733 ) ;
734 }
735 0 $config->init($default_conf,\%arg_conf) ;
736 0 $config->load() ;
737  
738 0 ▶$log = new _Log($cmdline,$config->get("debug")?2:$config->get("verbose")?1:0) ;
Branch: 2 branches
BranchTrueFalseTotal
$config->get('debug') ? : 0 0 0
$config->get('verbose') ? : 0 0 0
739 0 $log->debug("loading config file ".$config->{"confname"}) ;
740 0 $log->debug("config: ".Dumper $config) ;
741  
742 0 ▶my $interval = shift(@ARGV) or _usage("wrong number of arguments") ;
Branch: 1 branch
BranchTrueFalseTotal
my $interval = shift @ARGV or _usage("wrong number of arguments") 0 0 0
743  
744 0 my ($srcdir,$dstdir) ;
745  
746 0 ▶if ($init)
Branch: 1 branch
BranchTrueFalseTotal
if ($init) { } 0 0 0
747 {
748 0 ▶ $init =~ /^\d+$/ or _usage("'$init' isn't numeric") ;
Branch: 1 branch
BranchTrueFalseTotal
$init =~ /^\d+$/ or _usage("'${init}' isn't numeric") 0 0 0
749 0 ▶ $dstdir = shift(@ARGV) or _usage("dstdir is missing") ;
Branch: 1 branch
BranchTrueFalseTotal
$dstdir = shift @ARGV or _usage("dstdir is missing") 0 0 0
750 }
751 else
752 {
753 0 ▶ $srcdir = shift(@ARGV) or _usage("srcdir is missing") ;
Branch: 1 branch
BranchTrueFalseTotal
$srcdir = shift @ARGV or _usage("srcdir is missing") 0 0 0
754 0 ▶ $dstdir = shift(@ARGV) or _usage("dstdir is missing") ;
Branch: 1 branch
BranchTrueFalseTotal
$dstdir = shift @ARGV or _usage("dstdir is missing") 0 0 0
755 }
756  
757 0 ▶_usage("wrong number of arguments") if scalar(@ARGV)!=0 ;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ARGV != 0 0 0 0
758  
759 0 my $rocknRoll = new _RocknRoll($dstdir,$interval) ;
760  
761 0 ▶if ($init)
Branch: 1 branch
BranchTrueFalseTotal
if ($init) { } 0 0 0
762 {
763 0 $rocknRoll->mkdirs($init) ;
764 }
765 else
766 {
767 0 $rocknRoll->check_if_complete() ;
768  
769 0 ▶ if ($rocknRoll->rock($srcdir)==0)
Branch: 1 branch
BranchTrueFalseTotal
if ($rocknRoll->rock($srcdir) == 0) 0 0 0
770 {
771 0 $log->crit_by_mail("aborted before rolling archives") ;
772 0 exit 1 ;
773 }
774 0 ▶ $rocknRoll->roll() unless $config->get("no-roll")
Branch: 1 branch
BranchTrueFalseTotal
unless $config->get("no-roll") or $config->get("refresh") or $config->get("update") 0 0 0
Condition0%0/6
$config->get("no-roll") or $config->get("refresh") or $config->get("update")
l!l&&r!l&&!r
000
$config->get("no-roll") or $config->get("refresh")
l!l&&r!l&&!r
000
MC/DC0%0/3
$config->get("no-roll") or $config->get("refresh") or $config->get("update")
$config->get("no-roll") $config->get("refresh") $config->get("update")
Truth table0%0/4
A or B or C
A= $config->get("no-roll")B= $config->get("refresh")C= $config->get("update")
ABCresult
1XX1
01X1
0011
0000
775 || $config->get("refresh")
776 || $config->get("update")
777 ;
778 }
779 }
780  
781 2 1;
782 __END__
↑ Summary