blib/lib/Sys/RotateBackup.pm

filter: Statementstmt 86.6% 117 / 135 Branchbran 68.0% 34 / 50 Conditioncond 45.4% 10 / 22 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 75.6% 180 / 238 CC 34 SCAR 23.4
CC
34
Cov
78%
_rotate
17.1
rotate
12.3
_cleanup
12.0
CRAP 10.4
↑ Summary
1 package Sys::RotateBackup;
2 {
3 3 $Sys::RotateBackup::VERSION = '0.12';
4 }
5 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 $Sys::RotateBackup::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: Rotate numbered backup directories
9  
10 3 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20  
21 3 ▶use Sys::FS ;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use Sys::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 3 has 'fs' => (
25 'is' => 'rw',
26 'isa' => 'Sys::FS',
27 'lazy' => 1,
28 'builder' => '_init_fs',
29 );
30  
31 3 has 'sys' => (
32 'is' => 'rw',
33 'isa' => 'Sys::Run',
34 'lazy' => 1,
35 'builder' => '_init_sys',
36 );
37  
38 3 has 'daily' => (
39 'is' => 'ro',
40 'isa' => 'Int',
41 'default' => 10,
42 );
43  
44 3 has 'weekly' => (
45 'is' => 'ro',
46 'isa' => 'Int',
47 'default' => 4,
48 );
49  
50 3 has 'monthly' => (
51 'is' => 'ro',
52 'isa' => 'Int',
53 'default' => 12,
54 );
55  
56 3 has 'yearly' => (
57 'is' => 'ro',
58 'isa' => 'Int',
59 'default' => 10,
60 );
61  
62 3 has 'vault' => (
63 'is' => 'rw',
64 'isa' => 'Str',
65 'required' => 1,
66 );
67  
68 3 with qw(Log::Tree::RequiredLogger);
69  
70 2 ▶sub _init_sys {
Subroutine _init_sys: called CC=1 SCAR=0.0
71 2 my $self = shift;
72  
73 2 my $Sys = Sys::Run::->new( { 'logger' => $self->logger(), } );
74  
75 2 return $Sys;
76 }
77  
78 2 ▶sub _init_fs {
Subroutine _init_fs: called CC=1 SCAR=0.0
79 2 my $self = shift;
80  
81 2 my $FS = Sys::FS::->new(
82 {
83 'logger' => $self->logger(),
84 'sys' => $self->sys(),
85 }
86 );
87  
88 2 return $FS;
89 }
90  
91 3 ▶sub rotate {
Subroutine rotate: called CC=8 SCAR=25.1
Pod documented
92 3 my $self = shift;
93 3 ▶ my $time = shift || time();
Condition50%1/2
shift() || time
l!l
30
MC/DC0%0/1
shift() || time
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
94  
95 3 my ( $sec, $min, $hour, $dom, $mon, $year, $dow, $doy, $isdst ) = localtime($time);
96 3 $year += 1900;
97 3 $mon++;
98 3 $dow++;
99 3 $doy++;
100 # dom is already correct (starting at 1, not 0 like to others ...)
101  
102 # rotate daily
103 3 $self->_rotate( 'daily', $self->daily() );
104  
105 # rotate weekly if dow == 1 && do_weekly
106 3 ▶ if ( $self->weekly() && $dow == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->weekly and $dow == 1) 0 3 3
Condition33%1/3
$self->weekly and $dow == 1
!ll&&!rl&&r
030
MC/DC0%0/2
$self->weekly and $dow == 1
$self->weekly $dow == 1
Truth table33%1/3
A and B
A= $self->weeklyB= $dow == 1
ABresult
0X0
100
111
107 0 $self->_rotate( 'weekly', $self->weekly() );
108 }
109  
110 # rotate monthly if dom == 1 && do_monthly
111 3 ▶ if ( $self->monthly() && $dom == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->monthly and $dom == 1) 0 3 3
Condition33%1/3
$self->monthly and $dom == 1
!ll&&!rl&&r
030
MC/DC0%0/2
$self->monthly and $dom == 1
$self->monthly $dom == 1
Truth table33%1/3
A and B
A= $self->monthlyB= $dom == 1
ABresult
0X0
100
111
112 0 $self->_rotate( 'monthly', $self->monthly() );
113 }
114  
115 # rotate yearly if dom == 1 && mon == 1 && do_yearly
116 3 ▶ if ( $self->yearly() && $doy == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->yearly and $doy == 1) 0 3 3
Condition33%1/3
$self->yearly and $doy == 1
!ll&&!rl&&r
030
MC/DC0%0/2
$self->yearly and $doy == 1
$self->yearly $doy == 1
Truth table33%1/3
A and B
A= $self->yearlyB= $doy == 1
ABresult
0X0
100
111
117 0 $self->_rotate( 'yearly', $self->yearly() );
118 }
119  
120 3 return 1;
121 }
122  
123 1 ▶sub cleanup {
Subroutine cleanup: called CC=4 SCAR=15.2
Pod documented
124 1 my $self = shift;
125  
126 # cleanup old backups copies
127  
128 # clean up dailys
129 1 $self->_cleanup( 'daily', $self->daily() );
130  
131 # clean up weeklys
132 1 ▶ if( $self->weekly() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->weekly) { } 0 1 1
133 0 $self->_cleanup( 'weekly', $self->weekly() );
134 } else {
135 1 my $weekly_dir = $self->fs()->filename( $self->vault(), 'weekly' );
136 1 $self->sys()->run_cmd( 'rm -rf ' . $weekly_dir );
137 }
138  
139 # clean up monthlys
140 1 ▶ if( $self->monthly() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->monthly) { } 1 0 1
141 1 $self->_cleanup( 'monthly', $self->monthly() );
142 } else {
143 0 my $monthly_dir = $self->fs()->filename( $self->vault(), 'monthly' );
144 0 $self->sys()->run_cmd( 'rm -rf ' . $monthly_dir );
145 }
146  
147 # clean up yearlys
148 1 ▶ if( $self->yearly() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->yearly) { } 0 1 1
149 0 $self->_cleanup( 'yearly', $self->yearly() );
150 } else {
151 1 my $yearly_dir = $self->fs()->filename( $self->vault(), 'yearly' );
152 1 $self->sys()->run_cmd( 'rm -rf ' . $yearly_dir );
153 }
154  
155 1 return 1;
156 }
157  
158 2 ▶sub _cleanup {
Subroutine _cleanup: called CC=11 SCAR=24.8
159 2 my $self = shift;
160 2 my $type = shift;
161 2 ▶ my $num = shift || 1;
Condition50%1/2
shift() || 1
l!l
20
MC/DC0%0/1
shift() || 1
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
162  
163 2 my $basedir = $self->fs()->filename( $self->vault(), $type );
164  
165 2 ▶ if(!-d $basedir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $basedir) 1 1 2
166 1 $self->logger()->log( message => 'Basedir '.$basedir.' not a directory. Aborting!', level => 'error', );
167 1 return;
168 }
169  
170 1 ▶ if(-d $basedir && opendir(my $DH, $basedir)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $basedir and opendir my $DH, $basedir) 1 0 1
Condition33%1/3
-d $basedir and opendir my $DH, $basedir
!ll&&!rl&&r
001
MC/DC0%0/2
-d $basedir and opendir my $DH, $basedir
-d $basedir opendir my $DH, $basedir
Truth table33%1/3
A and B
A= -d $basedirB= opendir my $DH, $basedir
ABresult
0X0
100
111
171 1 ▶ while(my $de = readdir($DH)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $de = readdir $DH)) 13 1 14
172 13 ▶ next if $de =~ m/^\./; # skip cur and upper dir
Branch: 1 branch
BranchTrueFalseTotal
if $de =~ /^\./ 2 11 13
173 11 ▶ next unless $de =~ m/^\d+$/; # skip non-numeric dirs
Branch: 1 branch
BranchTrueFalseTotal
unless $de =~ /^\d+$/ 0 11 11
174 11 ▶ next if $de < $num; # skip any valid rotational
Branch: 1 branch
BranchTrueFalseTotal
if $de < $num 4 7 11
175 7 my $path = $self->fs()->filename( $basedir, $de );
176 7 ▶ next unless -d $path; # skip any non-dirs
Branch: 1 branch
BranchTrueFalseTotal
unless -d $path 0 7 7
177  
178 7 ▶ if($self->sys()->run_cmd( 'rm -rf ' . $path )) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->sys->run_cmd('rm -rf ' . $path)) { } 7 0 7
179 7 $self->logger()->log( message => 'Removed superflous rotational directory: '.$path, level => 'debug', );
180 } else {
181 0 $self->logger()->log( message => 'Failed to remove superflous rotational directory: '.$path, level => 'debug', );
182 }
183 }
184 1 closedir($DH);
185 }
186  
187 1 return 1;
188 }
189  
190 3 ▶sub _rotate {
Subroutine _rotate: called CC=14 SCAR=28.4
191 3 my $self = shift;
192 3 my $type = shift;
193 3 my $num = shift;
194  
195 3 my $basedir = $self->fs()->filename( $self->vault(), $type );
196  
197 3 ▶ if ( $num > 500 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num > 500) { } 0 3 3
elsif ($num < 1) { } 0 3 3
198 0 $num = 500;
199 }
200 elsif ( $num < 1 ) {
201 0 $num = 10;
202 }
203  
204 # Create basedir for this type
205 3 ▶ if ( !-d $basedir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $basedir) 1 2 3
206 1 $self->fs()->makedir($basedir);
207 }
208  
209 # Remove the oldest directory
210 3 ▶ if ( -d $basedir . q{/} . $num ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $basedir . '/' . $num) 2 1 3
211 2 $self->sys()->run_cmd( 'rm -rf ' . $basedir . q{/} . $num );
212 }
213  
214 # Rotate the others
215 ## no critic (ProhibitCStyleForLoops)
216 3 ▶ for ( my $i = $num ; $i > 0 ; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 30 3 33
217 30 my $olddir = $basedir . q{/} . ( $i - 1 );
218 30 my $newdir = $basedir . q{/} . $i;
219 30 ▶ if ( -d $olddir && -w $basedir ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $olddir and -w $basedir) { } 19 11 30
elsif (-w $basedir) { } 11 0 11
Condition66%2/3
-d $olddir and -w $basedir
!ll&&!rl&&r
11019
MC/DC50%1/2
-d $olddir and -w $basedir
-d $olddir -w $basedir
Truth table66%2/3
A and B
A= -d $olddirB= -w $basedir
ABresult
0X0
100
111
220 19 $self->logger()->log( message => 'Moving from '.$olddir.' to '.$newdir, level => 'debug', );
221 19 my $cmd = q{mv -f "} . $olddir . q{" "} . $newdir . q{"};
222 19 $self->logger()->log( message => 'CMD: ' . $cmd, level => 'debug', );
223 19 $self->sys()->run_cmd($cmd);
224 }
225 elsif ( -w $basedir ) {
226 11 my $cmd = q{mkdir -p -m0700 "} . $newdir . q{"};
227 11 $self->logger()->log( message => 'CMD: ' . $cmd, level => 'debug', );
228 11 $self->sys()->run_cmd($cmd);
229 11 ▶ if ( $i > 1 ) { # do not create the 0 dir, or mv'ing inprogress will move to wrong destination
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 1) 9 2 11
230 9 $cmd = q{mkdir -p -m0700 "}.$olddir.q{"};
231 9 $self->logger()->log( message => 'CMD: ' . $cmd, level => 'debug', );
232 9 $self->sys()->run_cmd($cmd);
233 }
234 }
235 }
236 ## use critic
237  
238 # Create the current dir
239 3 ▶ if ( $type eq 'daily' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'daily') { } 3 0 3
240  
241 # Move inprogress
242 3 my $olddir = $basedir . '/inprogress';
243 3 my $newdir = $basedir . '/0';
244 3 ▶ if ( -d $olddir && -w $basedir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $olddir and -w $basedir) { } 2 1 3
Condition66%2/3
-d $olddir and -w $basedir
!ll&&!rl&&r
102
MC/DC50%1/2
-d $olddir and -w $basedir
-d $olddir -w $basedir
Truth table66%2/3
A and B
A= -d $olddirB= -w $basedir
ABresult
0X0
100
111
245 2 $self->logger()->log( message => 'Moving from '.$olddir.' to '.$newdir, level => 'debug', );
246 2 my $cmd = q{mv -f "} . $olddir . q{" "} . $newdir . q{"};
247 2 $self->logger()->log( message => 'CMD: ' . $cmd, level => 'debug', );
248 2 $self->sys()->run_cmd($cmd);
249 }
250 else {
251 1 $self->logger()->log( message => q{Can't move from }.$olddir.' to '.$newdir.'. '.$olddir.' not found or '.$basedir.' is not writeable.', level => 'debug', );
252 }
253 }
254 else {
255 0 my $daily = $self->fs()->filename( $self->vault(), 'daily', '0' );
256 0 my $newdir = $basedir . '/0';
257 0 my $cmd = q{};
258 0 ▶ if ( -x '/usr/bin/rsync' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-x '/usr/bin/rsync') { } 0 0 0
259 0 $cmd = '/usr/bin/rsync -a --whole-file --link-dest=' . $daily . q{/ } . $daily . q{/ } . $newdir . q{/};
260 }
261 else {
262 0 $cmd = '/bin/cp -al ' . $daily . q{/ } . $newdir . q{/};
263 }
264 0 $self->logger()->log( message => "CMD: $cmd", level => 'debug', );
265 0 $self->sys()->run_cmd($cmd);
266 }
267 3 return 1;
268 }
269  
270 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
271 3 __PACKAGE__->meta->make_immutable;
272  
273 3 1;
274  
275 __END__
↑ Summary