Util/Backup.pm

filter: Statementstmt 93.8% 92 / 98 Branchbran 66.6% 32 / 48 Conditioncond 39.1% 9 / 23 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 75.2% 149 / 198 CC 35 SCAR 23.1
CC
35
Cov
79%
_backup
17.4
_file_diff
13.9
_backupdir
7.0
CRAP 10.1
« Util/Query.pm ↑ Summary Util/OS.pm »
1 package Dev::Util::Backup;
2  
3 2 ▶use Dev::Util::Syntax;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Archive::Tar;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 2 our $VERSION = version->declare("v2.19.42");
14  
15 2 our @EXPORT_OK = qw(
16 backup
17 );
18  
19 2 our %EXPORT_TAGS = ( all => \@EXPORT_OK );
20  
21 #where to save backup files
22 2 our $BACKUPDIR = '';
23  
24 #should we preserve atime, mtime, and mode of the original
25 #file in all backups ?
26 2 our $PRESERVE_FILE_ATTRS = 1;
27  
28 # Backup file or directory
29 4 ▶sub backup {
Subroutine backup: called CC=2 SCAR=6.9
Pod documented
30 4 ▶ return -f $_[0] ? _backupfile(@_) : _backupdir(@_);
Branch: 1 branch
BranchTrueFalseTotal
-f $_[0] ? : 3 1 4
31 }
32  
33 # Backup file -- takes file name and returns new file name
34 # This sub can DIE -- so use eval
35 3 ▶sub _backupfile {
Subroutine _backupfile: called CC=5 SCAR=17.1
36 3 my $filename = shift;
37  
38 3 ▶ croak "$filename is not a file\n" unless ( -e $filename );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $filename 0 3 3
39  
40 #backup file will have _yyyymmdd extention added to it
41 3 my $newfile = _backup($filename);
42  
43 #preserve mode, atime, and utime of old file
44 3 ▶ if ($PRESERVE_FILE_ATTRS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dev::Util::Backup::PRESERVE_FILE_ATTRS) 3 0 3
45 3 my @stat = stat $filename;
46  
47 3 utime( $stat[8], $stat[9], $newfile );
48 3 chmod $stat[2], $newfile;
49  
50 #preserve ownership if possible
51 3 ▶ chown $stat[4], $stat[5], $newfile
Branch: 1 branch
BranchTrueFalseTotal
if $Dev::Util::Backup::REAL_USER_ID == 0 or $Dev::Util::Backup::REAL_USER_ID == $stat[4] 3 0 3
Condition33%1/3
$Dev::Util::Backup::REAL_USER_ID == 0 or $Dev::Util::Backup::REAL_USER_ID == $stat[4]
l!l&&r!l&&!r
030
MC/DC0%0/2
$Dev::Util::Backup::REAL_USER_ID == 0 or $Dev::Util::Backup::REAL_USER_ID == $stat[4]
$Dev::Util::Backup::REAL_USER_ID == 0 $Dev::Util::Backup::REAL_USER_ID == $stat[4]
Truth table33%1/3
A or B
A= $Dev::Util::Backup::REAL_USER_ID == 0B= $Dev::Util::Backup::REAL_USER_ID == $stat[4]
ABresult
1X1
011
000
52 if ( $REAL_USER_ID == 0 || $REAL_USER_ID == $stat[4] );
53 }
54  
55 3 return $newfile;
56 }
57  
58 # Backup directory -- takes file name, optional compression level (2-9) and
59 # returns new archive file name
60 # This sub can DIE -- so use eval
61 1 ▶sub _backupdir {
Subroutine _backupdir: called CC=6 SCAR=19.5
62 1 my ( $dir, $level ) = @_;
63  
64 1 ▶ $level = 5 if ( !defined($level) || $level < 2 || $level > 9 );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $level or $level < 2 or $level > 9 1 0 1
Condition33%2/6
not defined $level or $level < 2 or $level > 9
l!l&&r!l&&!r
100
not defined $level or $level < 2
l!l&&r!l&&!r
100
MC/DC0%0/3
not defined $level or $level < 2 or $level > 9
not defined $level $level < 2 $level > 9
Truth table25%1/4
not(A) or B or C
A= not defined $levelB= $level < 2C= $level > 9
ABCresult
1XX1
01X1
0011
0000
65  
66 1 ▶ croak "$dir is not a directory\n" unless ( -d $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 1 1
67  
68 1 my @files;
69 1 my $tar = Archive::Tar->new();
70  
71 # "promote" warnings from File::Find to errors
72 0 ▶ local $SIG{ __WARN__ } = sub { croak $_[0] };
Subroutine __ANON__: not called CC=1 SCAR=1.2
73  
74 #recursivelly add files to tar
75 find(
76 4 ▶ { wanted => sub { push( @files, $_ ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
77 1 no_chdir => 1
78 },
79 $dir
80 );
81  
82 #save archive
83  
84 1 ▶ my $tmpout = IO::File->new_tmpfile() || croak "Failed to create tmpfile\n";
Condition50%1/2
'IO::File'->new_tmpfile || croak("Failed to create tmpfile\n")
l!l
10
MC/DC0%0/1
'IO::File'->new_tmpfile || croak("Failed to create tmpfile\n")
'IO::File'->new_tmpfile
Truth table50%1/2
A
A= 'IO::File'->new_tmpfile
Aresult
11
00
85 1 binmode($tmpout);
86  
87 1 $tar->add_files(@files);
88 1 $tar->write( $tmpout, $level );
89  
90 #backup file will have _yyyymmdd extention added to it
91 1 return _backup( $dir, $tmpout );
92 }
93  
94 # Perform file backup if necessary
95 # Arguments: $filename -- file/dir to backup
96 # Returns backup file name
97 4 ▶sub _backup {
Subroutine _backup: called CC=14 SCAR=28.6
98 4 my ( $filename, $fh ) = @_;
99  
100 4 ▶ my $input = $fh ? $fh : $filename;
Branch: 1 branch
BranchTrueFalseTotal
$fh ? : 1 3 4
101  
102 4 $filename =~ s/\/$//; #remove trailing slash from paths
103  
104 4 ▶ my $ext = -d $filename ? ".tar.gz" : "";
Branch: 1 branch
BranchTrueFalseTotal
-d $filename ? : 1 3 4
105 4 my $mtime = ( stat $filename )[9];
106 4 my ( $mday, $mon, $year ) = ( localtime($mtime) )[ 3 .. 5 ];
107  
108 4 ▶ if ( $BACKUPDIR ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dev::Util::Backup::BACKUPDIR ne "") 0 4 4
109  
110 #backup in BUDIR directory relative to dirname
111 0 my ( $name, $path ) = fileparse($filename);
112  
113 0 ▶ my $budir
Branch: 1 branch
BranchTrueFalseTotal
$Dev::Util::Backup::BACKUPDIR =~ m[^/]u ? : 0 0 0
114 = $BACKUPDIR =~ /^\//
115 ? $BACKUPDIR
116 : File::Spec->catfile( $path, $BACKUPDIR );
117  
118 #try to create backup dir if it does not exist
119 0 ▶ croak("Failed to create backup dir $BACKUPDIR\n")
Branch: 1 branch
BranchTrueFalseTotal
unless -d $budir or &Dev::Util::Backup::mkdir($budir, 488) 0 0 0
Condition0%0/3
-d $budir or &Dev::Util::Backup::mkdir($budir, 488)
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $budir or &Dev::Util::Backup::mkdir($budir, 488)
-d $budir &Dev::Util::Backup::mkdir($budir, 488)
Truth table0%0/3
A or B
A= -d $budirB= &Dev::Util::Backup::mkdir($budir, 488)
ABresult
1X1
011
000
120 unless ( -d $budir || mkdir( $budir, 0750 ) );
121  
122 0 $filename = File::Spec->catfile( $budir, $name );
123 }
124  
125 4 my $newfile = q{}; # EMPTY_STR
126 4 my $basefile = q{};
127 4 my $lastbackup = q{};
128 4 my $count = 0;
129  
130 4 $newfile = $basefile
131 = sprintf( "%s_%d%02d%02d", $filename, $year + 1900, $mon + 1, $mday );
132  
133 #find next available backup -- keep appending _counter to
134 #basefile name until available extention is found
135  
136 4 ▶ for ( $count = 0; -e "$newfile$ext"; $count++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "$newfile$ext") 2 4 6
137 2 $newfile = $basefile . "_" . ( $count + 1 );
138 }
139  
140 4 $newfile = $basefile;
141  
142 4 ▶ if ($count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count) 2 2 4
143  
144 # more then 1 backup exists -- last backup has
145 # count-1 extention (if count-1 == 0 -> exception: lastbackup=$basefile)
146 2 ▶ $lastbackup = $count - 1 > 0 ? "${basefile}_" . ( $count - 1 ) : $basefile;
Branch: 1 branch
BranchTrueFalseTotal
$count - 1 > 0 ? : 0 2 2
147 2 $newfile .= "_$count";
148 }
149  
150 4 ▶ if ( $lastbackup ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lastbackup ne "") 2 2 4
151  
152 # last backup exists -- check if current file
153 # is different from backup
154 2 ▶ _file_diff( $input, "$lastbackup$ext" ) || return "$lastbackup$ext";
Branch: 1 branch
BranchTrueFalseTotal
_file_diff($input, "$lastbackup$ext") or return "$lastbackup$ext" 1 1 2
155 }
156  
157 #backup file
158 3 ▶ seek( $input, 0, 0 ) if ( ref($input) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $input 1 2 3
159 3 ▶ copy( $input, "$newfile$ext", 4096 ) || croak("$!\n");
Branch: 1 branch
BranchTrueFalseTotal
copy($input, "$newfile$ext", 4096) or croak("$!\n") 3 0 3
160 3 return "$newfile$ext";
161 }
162  
163 # return true if files are different
164 # f1, f2 can either be file names or open file handles (by ref)
165 # NOTE: modifies filehandle position to 0
166 2 ▶sub _file_diff {
Subroutine _file_diff: called CC=12 SCAR=26.3
167 2 my ( $f1, $f2 ) = @_;
168  
169 2 my ( @files, $fh, $ref, $n1, $n2 );
170  
171 2 foreach ( $f1, $f2 ) {
172 4 $ref = ref($_);
173 4 ▶ $fh = $ref ? $_ : IO::File->new( $_, "r" )
Branch: 2 branches
BranchTrueFalseTotal
$fh = $ref ? $_ : "IO::File"->new($_, "r") or croak("Failed to create file: $!\n") 4 0 4
$ref ? : 0 4 4
174 or croak "Failed to create file: $!\n";
175  
176 4 push(
177 @files,
178 { fh => $fh,
179 ref => $ref,
180 size => ( $fh->stat() )[7]
181 }
182 );
183 4 seek( $fh, 0, 0 );
184 }
185  
186 2 ▶ return 1 unless ( $files[0]->{ size } == $files[1]->{ size } );
Branch: 1 branch
BranchTrueFalseTotal
unless $files[0]{"size"} == $files[1]{"size"} 1 1 2
187  
188 1 my ( $buf1, $buf2 );
189 1 my $diff = 0;
190  
191 1 ▶ while ( !$diff
Branch: 1 branch
BranchTrueFalseTotal
if (not $diff and $n1 = &Dev::Util::Backup::read($files[0]{"fh"}, \$buf1, 4096) and $n2 = &Dev::Util::Backup::read($files[1]{"fh"}, \$buf2, 4096)) 1 1 2
Condition66%4/6
not $diff and $n1 = &Dev::Util::Backup::read($files[0]{"fh"}, \$buf1, 4096) and $n2 = &Dev::Util::Backup::read($files[1]{"fh"}, \$buf2, 4096)
!ll&&!rl&&r
101
not $diff and $n1 = &Dev::Util::Backup::read($files[0]{"fh"}, \$buf1, 4096)
!ll&&!rl&&r
011
MC/DC33%1/3
not $diff and $n1 = &Dev::Util::Backup::read($files[0]{"fh"}, \$buf1, 4096) and $n2 = &Dev::Util::Backup::read($files[1]{"fh"}, \$buf2, 4096)
not $diff $n1 = &Dev::Util::Backup::read($files[0]{"fh"}, \$buf1, 4096) $n2 = &Dev::Util::Backup::read($files[1]{"fh"}, \$buf2, 4096)
Truth table50%2/4
not(A) and B and C
A= not $diffB= $n1 = &Dev::Util::Backup::read($files[0]{"fh"}, \$buf1, 4096)C= $n2 = &Dev::Util::Backup::read($files[1]{"fh"}, \$buf2, 4096)
ABCresult
0XX0
10X0
1100
1111
192 && ( $n1 = read( $files[0]->{ fh }, $buf1, 4096 ) )
193 && ( $n2 = read( $files[1]->{ fh }, $buf2, 4096 ) ) )
194 {
195 1 ▶ $diff = 1 if ( $n1 != $n2 || $buf1 ne $buf2 );
Branch: 1 branch
BranchTrueFalseTotal
if $n1 != $n2 or $buf1 ne $buf2 0 1 1
Condition33%1/3
$n1 != $n2 or $buf1 ne $buf2
l!l&&r!l&&!r
001
MC/DC0%0/2
$n1 != $n2 or $buf1 ne $buf2
$n1 != $n2 $buf1 ne $buf2
Truth table33%1/3
A or B
A= $n1 != $n2B= $buf1 ne $buf2
ABresult
1X1
011
000
196 }
197  
198 #close/restore filehandles
199 1 foreach (@files) {
200 2 ▶ if ( $_->{ ref } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->{'ref'}) { } 0 2 2
201 0 seek( $_->{ fh }, 0, 0 );
202 }
203 else {
204 2 $_->{ fh }->close();
205 }
206 }
207  
208 1 return $diff;
209 }
210  
211 2 1; # End of Dev::Util::OS
212  
213 =pod
214  
215 =encoding utf-8
216  
217 =head1 NAME
218  
219 Dev::Util::Backup - Simple backup functions for files and dirs
220  
221 =head1 VERSION
222  
223 Version v2.19.42
224  
225 =head1 SYNOPSIS
226  
227 The backup function will make a copy of a file or dir with the date of the file appended.
228 It returns the name of the new file. Directories are backed up by C<tar> and C<gz>.
229  
230 use Dev::Util::Backup qw(backup);
231  
232 my $backup_file = backup('myfile');
233 say $backup_file;
234  
235 my $backup_dir = backup('mydir/');
236 say $backup_dir;
237  
238 Will produce:
239  
240 myfile_20251025
241 mydir_20251025.tar.gz
242  
243 If the file has changed, calling C<backup('myfile')> again will create C<myfile_20251025_1>.
244 Each time C<backup> is called the appended counter will increase by 1 if C<myfile> has
245 changed since the last time it was called.
246  
247 If the file has not changed, no new backup will be created.
248  
249 =head2 Examples
250  
251 The C<bu> program in the examples dir will take a list of files and dirs as args and make
252 backups of them using C<backup>.
253  
254 =head1 EXPORT
255  
256 backup
257  
258 =head1 SUBROUTINES
259  
260 =head2 B<backup(FILE|DIR)>
261  
262 Return the name of the backup file.
263  
264 my $backup_file = backup('myfile');
265 my $backup_dir = backup('mydir/');
266  
267  
268 =head1 AUTHOR
269  
270 Matt Martini, C<< <matt at imaginarywave.com> >>
271  
272 =head1 BUGS
273  
274 Please report any bugs or feature requests to C<bug-dev-util at rt.cpan.org>, or through
275 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dev-Util>. I will
276 be notified, and then you'll automatically be notified of progress on your bug as I make changes.
277  
278 =head1 SUPPORT
279  
280 You can find documentation for this module with the perldoc command.
281  
282 perldoc Dev::Util::Backup
283  
284 You can also look for information at:
285  
286 =over 4
287  
288 =item * RT: CPAN's request tracker (report bugs here)
289  
290 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=Dev-Util>
291  
292 =item * Search CPAN
293  
294 L<https://metacpan.org/release/Dev-Util>
295  
296 =back
297  
298 =head1 ACKNOWLEDGMENTS
299  
300 =head1 LICENSE AND COPYRIGHT
301  
302 This software is Copyright © 2001-2026 by Matt Martini.
303  
304 This is free software, licensed under:
305  
306 The GNU General Public License, Version 3, June 2007
307  
308 =cut
309  
310 __END__
« Util/Query.pm ↑ Summary Util/OS.pm »