SimpleBackuper/Backup.pm

filter: Statementstmt 21.8% 43 / 197 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 78.9% 15 / 19 Podpod 0.0% 0 / 1 totaltotal 17.4% 58 / 332 CC 56 SCAR 72.2
CC
56
Cov
15%
Backup
2154.1
_get_block_to_delete
42.0
_proc_uid_gid
20.0
CRAP 1373.0
« SimpleBackuper/Restore.pm ↑ Summary SimpleBackuper/StorageCheck.pm »
1 package App::SimpleBackuper;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature ':5.14';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Const::Fast;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use App::SimpleBackuper::BackupDB;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use App::SimpleBackuper::_format;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use App::SimpleBackuper::_BlockDelete;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use App::SimpleBackuper::_BlocksInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 const my $SIZE_OF_TOP_FILES => 10;
16 const my $SAVE_DB_PERIOD => 60 * 60;
17 const my $PRINT_PROGRESS_PERIOD => 60;
18  
19 0 ▶sub _proc_uid_gid($$$) {
Subroutine _proc_uid_gid: not called CC=4 SCAR=30.0
20 0 my($uid, $gid, $uids_gids) = @_;
21
22 0 ▶ my $last_uid_gid = @$uids_gids ? $uids_gids->unpack( $uids_gids->[-1] )->{id} : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$uids_gids ? : 0 0 0
23
24 0 my $user_name = getpwuid($uid);
25 0 my($user) = grep { $_->{name} eq $user_name } map { $uids_gids->unpack($_) } @$uids_gids;
26 0 ▶ if(! $user) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($user) 0 0 0
27 0 $user = {id => ++$last_uid_gid, name => $user_name};
28 0 $uids_gids->upsert({ id => $user->{id} }, $user );
29 #printf "new owner user added (unix uid %d, name %s, internal uid %d)\n", $uid, $user_name, $user->{id};
30 }
31 0 $uid = $user->{id};
32
33 0 my $group_name = getgrgid($gid);
34 0 my($group) = grep { $_->{name} eq $group_name } map { $uids_gids->unpack($_) } @$uids_gids;
35 0 ▶ if(! $group) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group) 0 0 0
36 0 $group = {id => ++$last_uid_gid, name => $group_name};
37 0 $uids_gids->upsert({ id => $group->{id} }, $group );
38 #printf "new owner group added (unix gid %d, name %s, internal gid %d)\n", $gid, $group_name, $group->{id};
39 }
40 0 $gid = $group->{id};
41
42 0 return $uid, $gid;
43 }
44  
45 0 ▶sub _get_block_to_delete {
Subroutine _get_block_to_delete: not called CC=6 SCAR=37.4
46 0 my($state) = @_;
47
48 0 ▶ if(ref($state->{blocks2delete_prio2size2chunks}) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state->{"blocks2delete_prio2size2chunks"} eq "HASH") 0 0 0
49 $state->{blocks2delete_prio2size2chunks} = [
50 0 map {$state->{blocks2delete_prio2size2chunks}->{ $_ }}
51 0 sort {$a <=> $b}
52 0 keys %{ $state->{blocks2delete_prio2size2chunks} }
53 ];
54 }
55
56 0 ▶ return if ! @{ $state->{blocks2delete_prio2size2chunks} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$state->{"blocks2delete_prio2size2chunks"};} 0 0 0
57
58 0 ▶ if(ref($state->{blocks2delete_prio2size2chunks}->[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state->{"blocks2delete_prio2size2chunks"}[0] eq "HASH") 0 0 0
59 $state->{blocks2delete_prio2size2chunks}->[0] = [
60 0 map {$state->{blocks2delete_prio2size2chunks}->[0]->{ $_ }}
61 0 sort {$b <=> $a}
62 0 keys %{ $state->{blocks2delete_prio2size2chunks}->[0] }
63 ];
64 }
65
66 0 my $prio_basket = $state->{blocks2delete_prio2size2chunks}->[0];
67 0 my $size_basket = $prio_basket->[0];
68 0 my $block_id = shift @$size_basket;
69 0 ▶ shift @$prio_basket if ! @$size_basket;
Branch: 1 branch
BranchTrueFalseTotal
unless @$size_basket 0 0 0
70 0 ▶ shift @{ $state->{blocks2delete_prio2size2chunks} } if ! @$prio_basket;
Branch: 1 branch
BranchTrueFalseTotal
unless @$prio_basket 0 0 0
71
72 0 return $block_id;
73 }
74  
75 0 ▶sub Backup {
Subroutine Backup: not called CC=47 SCAR=76.8
Pod undocumented
76 0 my($options, $state) = @_;
77
78 0 my($backups, $files, $parts, $blocks) = @{ $state->{db} }{qw(backups files parts blocks)};
79
80 0 ▶ die "Backup '$options->{\"backup-name\"}' already exists" if grep { $backups->unpack($_)->{name} eq $options->{'backup-name'} } @$backups;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$backups->unpack($_)->{"name"} eq $options->{"backup-name"};} @$backups 0 0 0
81
82 0 $state->{$_} = 0 foreach qw(last_backup_id last_file_id last_block_id bytes_processed bytes_in_last_backup total_weight);
83
84 0 ▶ print "Preparing to backup: " if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
85 0 $state->{profile}->{init_ids} = - time();
86 0 foreach (@$backups) {
87 0 my $id = $backups->unpack($_)->{id};
88 0 ▶ $state->{last_backup_id} = $id if ! $state->{last_backup_id} or $state->{last_backup_id} < $id;
Branch: 1 branch
BranchTrueFalseTotal
if not $state->{"last_backup_id"} or $state->{"last_backup_id"} < $id 0 0 0
Condition0%0/3
not $state->{"last_backup_id"} or $state->{"last_backup_id"} < $id
l!l&&r!l&&!r
000
MC/DC0%0/2
not $state->{"last_backup_id"} or $state->{"last_backup_id"} < $id
not $state->{"last_backup_id"} $state->{"last_backup_id"} < $id
Truth table0%0/3
not(A) or B
A= not $state->{"last_backup_id"}B= $state->{"last_backup_id"} < $id
ABresult
1X1
011
000
89 }
90 #print "last backup id $state->{last_backup_id}, ";
91 0 foreach (@$files) {
92 0 my $file = $files->unpack($_);
93 0 ▶ $state->{last_file_id} = $file->{id} if ! $state->{last_file_id} or $state->{last_file_id} < $file->{id};
Branch: 1 branch
BranchTrueFalseTotal
if not $state->{"last_file_id"} or $state->{"last_file_id"} < $file->{"id"} 0 0 0
Condition0%0/3
not $state->{"last_file_id"} or $state->{"last_file_id"} < $file->{"id"}
l!l&&r!l&&!r
000
MC/DC0%0/2
not $state->{"last_file_id"} or $state->{"last_file_id"} < $file->{"id"}
not $state->{"last_file_id"} $state->{"last_file_id"} < $file->{"id"}
Truth table0%0/3
not(A) or B
A= not $state->{"last_file_id"}B= $state->{"last_file_id"} < $file->{"id"}
ABresult
1X1
011
000
94 0 ▶ if($file->{versions} and @{ $file->{versions} } and $file->{versions}->[-1]->{backup_id_max} == $state->{last_backup_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{"versions"} and @{$file->{"versions"};} and $file->{"versions"}[-1]{"backup_id_max"} == $state->{"last_backup_id"}) 0 0 0
Condition0%0/6
$file->{"versions"} and @{$file->{"versions"};} and $file->{"versions"}[-1]{"backup_id_max"} == $state->{"last_backup_id"}
!ll&&!rl&&r
000
$file->{"versions"} and @{$file->{"versions"};}
!ll&&!rl&&r
000
MC/DC0%0/3
$file->{"versions"} and @{$file->{"versions"};} and $file->{"versions"}[-1]{"backup_id_max"} == $state->{"last_backup_id"}
$file->{"versions"} @{$file->{"versions"};} $file->{"versions"}[-1]{"backup_id_max"} == $state->{"last_backup_id"}
Truth table0%0/4
A and B and C
A= $file->{"versions"}B= @{$file->{"versions"};}C= $file->{"versions"}[-1]{"backup_id_max"} == $state->{"last_backup_id"}
ABCresult
0XX0
10X0
1100
1111
95 0 $state->{bytes_in_last_backup} += $file->{versions}->[-1]->{size};
96 }
97 }
98 #print "last file id $state->{last_file_id}, ";
99 0 foreach (@$blocks) {
100 0 my $id = $blocks->unpack($_)->{id};
101 0 ▶ $state->{last_block_id} = $id if ! $state->{last_block_id} or $state->{last_block_id} < $id;
Branch: 1 branch
BranchTrueFalseTotal
if not $state->{"last_block_id"} or $state->{"last_block_id"} < $id 0 0 0
Condition0%0/3
not $state->{"last_block_id"} or $state->{"last_block_id"} < $id
l!l&&r!l&&!r
000
MC/DC0%0/2
not $state->{"last_block_id"} or $state->{"last_block_id"} < $id
not $state->{"last_block_id"} $state->{"last_block_id"} < $id
Truth table0%0/3
not(A) or B
A= not $state->{"last_block_id"}B= $state->{"last_block_id"} < $id
ABresult
1X1
011
000
102 }
103 #print "last block id $state->{last_block_id}, ";
104 0 $state->{profile}->{init_ids} += time;
105
106 0 ▶ print "total weight " if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
107 0 ▶ for(my $q = 0; $q <= $#$parts; $q++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($q <= $parts->$#*) 0 0 0
108 0 $state->{total_weight} += $parts->unpack($parts->[ $q ])->{size};
109 }
110 0 ▶ print fmt_weight($state->{total_weight}).", " if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
111
112 0 my $cur_backup = {name => $options->{'backup-name'}, id => ++$state->{last_backup_id}, files_cnt => 0, max_files_cnt => 0};
113 0 $backups->upsert({ id => $cur_backup->{id} }, $cur_backup);
114
115 {
116 0 $state->{blocks_info} = _BlocksInfo($options, $state);
117
118 0 $state->{blocks2delete_prio2size2chunks} = {};
119 0 foreach my $block (@$blocks) {
120 0 my $block_id = $blocks->unpack($block)->{id};
121 0 ▶ next if ! $block_id; # What's this?
Branch: 1 branch
BranchTrueFalseTotal
unless $block_id 0 0 0
122 0 my $block_info = $state->{blocks_info}->{ $block_id };
123 0 push @{ $state->{blocks2delete_prio2size2chunks}->{ $block_info->[0] }->{ $block_info->[1] } }, $block_id;
124 }
125
126 0 ▶ print " OK\n" if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
127 }
128
129 0 ▶ _print_progress($state) if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
130
131 0 my(%files_queues_by_priority, %dirs2upd);
132 0 ▶ while(my($mask, $priority) = each %{ $options->{files} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$options{"files"};}) 0 0 0
133 0 ▶ next if ! $priority;
Branch: 1 branch
BranchTrueFalseTotal
unless $priority 0 0 0
134 0 ▶ foreach my $path (glob $mask) {
Subroutine BEGIN: called CC=1 SCAR=0.2
135 0 ▶ if(grep { ~index($path, $_ =~ s/(?!\/)$/\//r) } map {@$_} values %files_queues_by_priority) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {~index($path, $_ =~ s[(?!/)$][/]ru);} map({@$_;} values %files_queues_by_priority)) 0 0 0
136 0 next;
137 }
138
139 # Remove child paths
140 0 foreach my $tasks (values %files_queues_by_priority) {
141 0 my $tasks_cnt = @$tasks;
142 0 @$tasks = grep { ! ~index($_->[0], $path =~ s/(?!\/)$/\//r) } @$tasks;
143 }
144
145 0 my $file_id = 0; {
146 0 my @path = split(/\//, $path, -1);
147 0 ▶ pop @path if @path and $path[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
if @path and $path[-1] eq "" 0 0 0
Condition0%0/3
@path and $path[-1] eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
@path and $path[-1] eq ""
@path $path[-1] eq ""
Truth table0%0/3
A and B
A= @pathB= $path[-1] eq ""
ABresult
0X0
100
111
148 0 pop @path;
149
150 0 my @cur_path;
151 0 foreach my $path_node (@path) {
152 0 push @cur_path, $path_node;
153
154 0 my $file = $files->find_by_parent_id_name($file_id, $path_node);
155 $file //= {
156 parent_id => $file_id,
157 id => ++$state->{last_file_id},
158 name => $path_node,
159 versions => [ {
160 backup_id_min => $state->{last_backup_id},
161 0 ▶ backup_id_max => 0,
Condition0%0/2
$file //= {"parent_id", $file_id, "id", ++$state->{"last_file_id"}, "name", $path_node, "versions", [{"backup_id_min", $state->{"last_backup_id"}, "backup_id_max", 0, "uid", 0, "gid", 0, "size", 0, "mode", 0, "mtime", 0, "block_id", 0, "symlink_to", undef, "parts", []}]}
l!l
00
MC/DC0%0/1
$file //= {"parent_id", $file_id, "id", ++$state->{"last_file_id"}, "name", $path_node, "versions", [{"backup_id_min", $state->{"last_backup_id"}, "backup_id_max", 0, "uid", 0, "gid", 0, "size", 0, "mode", 0, "mtime", 0, "block_id", 0, "symlink_to", undef, "parts", []}]}
$file
Truth table0%0/2
A
A= $file
Aresult
11
00
162 uid => 0,
163 gid => 0,
164 size => 0,
165 mode => 0,
166 mtime => 0,
167 block_id => 0,
168 symlink_to => undef,
169 parts => [],
170 } ],
171 };
172
173 0 ▶ $dirs2upd{join('/', @cur_path) || '/'} = {
Condition0%0/2
join('/', @cur_path) || '/'
l!l
00
MC/DC0%0/1
join('/', @cur_path) || '/'
join('/', @cur_path)
Truth table0%0/2
A
A= join('/', @cur_path)
Aresult
11
00
174 parent_id => $file_id,
175 filename => $path_node,
176 };
177
178 0 $files->upsert({ id => $file->{id}, parent_id => $file->{parent_id} }, $file);
179
180 0 $file_id = $file->{id};
181 }
182 }
183
184 0 push @{ $files_queues_by_priority{$priority} }, [ $path, $priority, $file_id ];
185 }
186 }
187 0 delete $files_queues_by_priority{ $_ } foreach grep {! @{ $files_queues_by_priority{ $_ } }} keys %files_queues_by_priority;
188
189 0 my $last_db_save = time;
190 0 my $last_print_progress = time;
191 0 ▶ while(%files_queues_by_priority) {
Branch: 1 branch
BranchTrueFalseTotal
if (%files_queues_by_priority) 0 0 0
192 0 my($priority) = sort {$b <=> $a} keys %files_queues_by_priority;
193 0 my $task = shift @{ $files_queues_by_priority{$priority} };
194 0 ▶ delete $files_queues_by_priority{$priority} if ! @{ $files_queues_by_priority{$priority} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$files_queues_by_priority{$priority};} 0 0 0
195 0 my @next = _file_proc( $task, $options, $state );
196 0 unshift @{ $files_queues_by_priority{ $_->[1] } }, $_ foreach reverse @next;
197
198 0 ▶ if($options->{verbose} and time - $last_print_progress > $PRINT_PROGRESS_PERIOD) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"verbose"} and &App::SimpleBackuper::time() - $last_print_progress > $PRINT_PROGRESS_PERIOD) 0 0 0
Condition0%0/3
$options->{"verbose"} and &App::SimpleBackuper::time() - $last_print_progress > $PRINT_PROGRESS_PERIOD
!ll&&!rl&&r
000
MC/DC0%0/2
$options->{"verbose"} and &App::SimpleBackuper::time() - $last_print_progress > $PRINT_PROGRESS_PERIOD
$options->{"verbose"} &App::SimpleBackuper::time() - $last_print_progress > $PRINT_PROGRESS_PERIOD
Truth table0%0/3
A and B
A= $options->{"verbose"}B= &App::SimpleBackuper::time() - $last_print_progress > $PRINT_PROGRESS_PERIOD
ABresult
0X0
100
111
199 0 _print_progress($state);
200 0 $last_print_progress = time;
201 }
202
203 0 ▶ if(time - $last_db_save > $SAVE_DB_PERIOD) {
Branch: 1 branch
BranchTrueFalseTotal
if (&App::SimpleBackuper::time() - $last_db_save > $SAVE_DB_PERIOD) 0 0 0
204 0 App::SimpleBackuper::BackupDB($options, $state);
205 0 $last_db_save = time;
206 }
207 }
208
209 0 ▶ while(my($full_path, $dir2upd) = each %dirs2upd) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %dirs2upd) 0 0 0
210 0 ▶ print "Updating dir $full_path..." if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
211 0 my $file = $files->find_by_parent_id_name($dir2upd->{parent_id}, $dir2upd->{filename});
212 0 my @stat = lstat($full_path);
213 0 ▶ if(@stat and $file->{versions}->[-1]->{backup_id_max} != $state->{last_backup_id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat and $file->{"versions"}[-1]{"backup_id_max"} != $state->{"last_backup_id"}) 0 0 0
Condition0%0/3
@stat and $file->{"versions"}[-1]{"backup_id_max"} != $state->{"last_backup_id"}
!ll&&!rl&&r
000
MC/DC0%0/2
@stat and $file->{"versions"}[-1]{"backup_id_max"} != $state->{"last_backup_id"}
@stat $file->{"versions"}[-1]{"backup_id_max"} != $state->{"last_backup_id"}
Truth table0%0/3
A and B
A= @statB= $file->{"versions"}[-1]{"backup_id_max"} != $state->{"last_backup_id"}
ABresult
0X0
100
111
214 0 my($uid, $gid) =_proc_uid_gid($stat[4], $stat[5], $state->{db}->{uids_gids});
215 0 ▶ if($file->{versions}->[-1]->{backup_id_max} == $state->{last_backup_id} - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{'versions'}[-1]{'backup_id_max'} == $state->{'last_backup_id'} - 1) { } 0 0 0
216 $file->{versions}->[-1] = {
217 0 %{ $file->{versions}->[-1] },
218 backup_id_max => $state->{last_backup_id},
219 0 uid => $uid,
220 gid => $gid,
221 size => $stat[7],
222 mode => $stat[2],
223 mtime => $stat[9],
224 block_id => 0,
225 symlink_to => undef,
226 parts => [],
227 };
228 } else {
229 0 push @{ $file->{versions} }, {
230 backup_id_min => $state->{last_backup_id},
231 backup_id_max => $state->{last_backup_id},
232 0 uid => $uid,
233 gid => $gid,
234 size => $stat[7],
235 mode => $stat[2],
236 mtime => $stat[9],
237 block_id => 0,
238 symlink_to => undef,
239 parts => [],
240 }
241 }
242 0 $files->upsert({ id => $file->{id}, parent_id => $file->{parent_id} }, $file);
243
244 0 my $backup = $backups->find_row({ id => $state->{last_backup_id} });
245 0 $backup->{files_cnt}++;
246 0 $backup->{max_files_cnt}++;
247 0 $backups->upsert({ id => $backup->{id} }, $backup );
248 }
249
250 0 ▶ print "OK\n" if $options->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"verbose"} 0 0 0
251 }
252
253 0 my $backup = $backups->find_row({ id => $state->{last_backup_id} });
254 0 $backup->{is_done} = 1;
255 0 $backups->upsert({ id => $backup->{id} }, $backup );
256
257 0 App::SimpleBackuper::BackupDB($options, $state);
258
259 0 ▶ _print_progress($state) if ! $options->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
if not $options->{"quiet"} 0 0 0
260 }
261  
262 0 ▶sub _print_progress {
Subroutine _print_progress: not called CC=2 SCAR=17.9
263 0 print "Progress: ";
264 0 ▶ if($_[0]->{bytes_in_last_backup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]{"bytes_in_last_backup"}) 0 0 0
265 0 printf "processed %s of %s in last backup, ", fmt_weight($_[0]->{bytes_processed}), fmt_weight($_[0]->{bytes_in_last_backup});
266 }
267 0 printf "total backups weight %s.\n", fmt_weight($_[0]->{total_weight});
268 }
269  
270 1 ▶use Text::Glob qw(match_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
271 1 ▶use Fcntl ':mode'; # For S_ISDIR & same
Subroutine BEGIN: called CC=1 SCAR=0.0
272 0 ▶use App::SimpleBackuper::RegularFile;
Subroutine BEGIN: called CC=1 SCAR=2.6
273  
274 sub _file_proc {
275 my($task, $options, $state) = @_;
276
277 confess "No task" if ! $task;
278 confess "No filepath" if ! $task->[0];
279
280 my @next;
281 my $file_time_spent = 0;
282 my $file_weight_spent = 0;
283
284 print "$task->[0]\n" if $options->{verbose};
285 print "\tparent #$task->[2], priority $task->[1]" if $options->{verbose};
286
287 my $priority = $task->[1];
288 while(my($mask, $p) = each %{ $options->{files} }) {
289 if(match_glob( $mask, $task->[0] )) {
290 $priority = $p;
291 print ", priority $priority by rule '\"$mask\": $p'" if $options->{verbose};
292 }
293 }
294
295 if(! $priority) { # Excluded by user
296 print " -> skip\n" if $options->{verbose};
297 return;
298 }
299
300 $state->{profile}->{fs} -= time;
301 $state->{profile}->{fs_lstat} -= time;
302 $file_time_spent -= time;
303 my @stat = lstat($task->[0]);
304 $file_time_spent += time;
305 $state->{profile}->{fs} += time;
306 $state->{profile}->{fs_lstat} += time;
307 if(! @stat) {
308 print ". Not exists\n" if $options->{verbose};
309 return;
310 }
311 else {
312 printf ", stat: %s:%s %o %s modified at %s", scalar getpwuid($stat[4]), scalar getgrgid($stat[5]), $stat[2], fmt_weight($stat[7]), fmt_datetime($stat[9]) if $options->{verbose};
313 }
314
315
316 my($backups, $blocks, $files, $parts, $uids_gids) = @{ $state->{db} }{qw(backups blocks files parts uids_gids)};
317
318
319 my($uid, $gid) = _proc_uid_gid($stat[4], $stat[5], $uids_gids);
320
321
322 my($file); {
323 my($filename) = $task->[0] =~ /([^\/]+)\/?$/;
324 $file = $files->find_by_parent_id_name($task->[2], $filename);
325 if($file) {
326 print ", is old file #$file->{id}" if $options->{verbose};
327 if($file->{versions}->[-1]->{backup_id_max} == $state->{last_backup_id}) {
328 print ", is already backuped.\n" if $options->{verbose};
329 return;
330 }
331 } else {
332 $file = {
333 parent_id => $task->[2],
334 id => ++$state->{last_file_id},
335 name => $filename,
336 versions => [],
337 };
338 print ", is new file #$file->{id}" if $options->{verbose};
339 }
340 }
341
342 $state->{bytes_processed} += $file->{versions}->[-1]->{size} if @{ $file->{versions} };
343
344 my %version = (
345 backup_id_min => $state->{last_backup_id},
346 backup_id_max => $state->{last_backup_id},
347 uid => $uid,
348 gid => $gid,
349 size => $stat[7],
350 mode => $stat[2],
351 mtime => $stat[9],
352 block_id => undef,
353 symlink_to => undef,
354 parts => [],
355 );
356
357 if(S_ISDIR $stat[2]) {
358 print ", is directory.\n" if $options->{verbose};
359 my $dh;
360
361 $state->{profile}->{fs} -= time;
362 $state->{profile}->{fs_read_dir} -= time;
363 $file_time_spent -= time;
364 if(! opendir($dh, $task->[0])) {
365 $state->{profile}->{fs} += time;
366 $state->{profile}->{fs_read_dir} += time;
367 push @{ $state->{fails}->{$!} }, $task->[0];
368 print ", can't read: $!\n" if $options->{verbose};
369 return;
370 }
371 my @files;
372 while(my $f = readdir($dh)) {
373 next if $f eq '.' or $f eq '..';
374 push @files, $f;
375 }
376 closedir($dh);
377 $file_time_spent += time;
378 $state->{profile}->{fs} += time;
379 $state->{profile}->{fs_read_dir} += time;
380
381 $version{block_id} = 0;
382
383 push @next, map { [$task->[0].($task->[0] =~ /\/$/ ? '' : '/').$_, $priority, $file->{id}] } sort @files;
384 }
385 elsif(S_ISLNK $stat[2]) {
386 $state->{profile}->{fs} -= time;
387 $state->{profile}->{fs_read_symlink} -= time;
388 $file_time_spent -= time;
389 $version{symlink_to} = readlink($task->[0]);
390 $file_time_spent += time;
391 $state->{profile}->{fs} += time;
392 $state->{profile}->{fs_read_symlink} += time;
393 if(defined $version{symlink_to}) {
394 print ", is symlink to $version{symlink_to}.\n" if $options->{verbose};
395 $version{block_id} = 0;
396 } else {
397 push @{ $state->{fails}->{$!} }, $task->[0];
398 print ", can't read: $!\n" if $options->{verbose};
399 return;
400 }
401 }
402 elsif(S_ISREG $stat[2]) {
403
404 print ", is regular file" if $options->{verbose};
405
406 $state->{profile}->{fs} -= time;
407 $state->{profile}->{fs_read} -= time;
408 $file_time_spent -= time;
409 my $reg_file = try {
410 App::SimpleBackuper::RegularFile->new($task->[0], $options, $state);
411 } catch {
412 1 while chomp;
413 push @{ $state->{fails}->{$_} }, $task->[0];
414 print ", can't read: '$_'\n" if $options->{verbose};
415 0;
416 };
417 $file_time_spent += time;
418 $state->{profile}->{fs} += time;
419 $state->{profile}->{fs_read} += time;
420 return if ! $reg_file;
421
422 if(@{ $file->{versions} } and $file->{versions}->[-1]->{mtime} == $version{mtime}) {
423 $version{parts} = $file->{versions}->[-1]->{parts}; # If mtime not changed then file not changed
424
425 $version{block_id} = $file->{versions}->[-1]->{block_id};
426
427 my $block = $blocks->find_row({ id => $version{block_id} });
428 confess "File has lost block #$version{block_id} in backup "
429 .$backups->find_row({ id => $version{backup_id_min} })->{name}
430 ."..".$backups->find_row({ id => $version{backup_id_max} })->{name}
431 if ! $block;
432 $block->{last_backup_id} = $state->{last_backup_id};
433 $blocks->upsert({ id => $block->{id} }, $block);
434
435 print ", mtime is not changed.\n" if $options->{verbose};
436 } else {
437 print @{ $file->{versions} } ? ", mtime changed.\n" : "\n" if $options->{verbose};
438 my $part_number = 0;
439 my %block_ids;
440 while(1) {
441 $state->{profile}->{fs} -= time;
442 $state->{profile}->{fs_read} -= time;
443 my $read_failed;
444 my $read = try {
445 $reg_file->read($part_number);
446 } catch {
447 1 while chomp;
448 push @{ $state->{fails}->{$_} }, $task->[0];
449 print ", can't read: $_\n" if $options->{verbose};
450 $read_failed = 1;
451 };
452 $state->{profile}->{fs} += time;
453 $state->{profile}->{fs_read} += time;
454 return if $read_failed;
455 last if ! $read;
456
457 print "\tpart #$part_number: " if $options->{verbose};
458
459 my %part = (
460 hash => undef,
461 size => undef,
462 aes_key => undef,
463 );
464 $state->{profile}->{math} -= time;
465 $state->{profile}->{math_hash} -= time;
466 $file_time_spent -= time;
467 $part{hash} = $reg_file->hash();
468 $file_time_spent += time;
469 $state->{profile}->{math} += time;
470 $state->{profile}->{math_hash} += time;
471 print "hash ".fmt_hex2base64($part{hash}).", " if $options->{verbose};
472
473
474 # Search for part with this hash
475 if(my $part = $parts->find_row({ hash => $part{hash} })) {
476 if($part->{block_id}) {
477 $block_ids{ $part->{block_id} }++;
478 }
479 $part{size} = $part->{size};
480 $part{aes_key} = $part->{aes_key};
481 $part{aes_iv} = $part->{aes_iv};
482 print "backuped earlier (".fmt_weight($read)." -> ".fmt_weight($part->{size}).");\n" if $options->{verbose};
483 } else {
484
485 print fmt_weight($read) if $options->{verbose};
486
487 $state->{profile}->{math} -= time;
488 $state->{profile}->{math_compress} -= time;
489 $file_time_spent -= time;
490 my $ratio = $reg_file->compress();
491 $file_time_spent += time;
492 $state->{profile}->{math} += time;
493 $state->{profile}->{math_compress} += time;
494 print ' compressed to '.fmt_weight($reg_file->size) if $options->{verbose};
495
496 ($part{aes_key}, $part{aes_iv}) = $reg_file->gen_keys();
497 $state->{profile}->{math} -= time;
498 $state->{profile}->{math_encrypt} -= time;
499 $file_time_spent -= time;
500 $reg_file->encrypt($part{aes_key}, $part{aes_iv});
501 $file_time_spent += time;
502 $state->{profile}->{math} += time;
503 $state->{profile}->{math_encrypt} += time;
504 print ', encrypted' if $options->{verbose};
505
506 $state->{total_weight} += $part{size} = $reg_file->size;
507 $file_weight_spent += $reg_file->size;
508
509 if($state->{total_weight} > $options->{space_limit}) {
510 print ", freeing up space by ".fmt_weight($state->{total_weight} - $options->{space_limit})."...\n" if $options->{verbose};
511 $file_time_spent += time;
512 while($state->{total_weight} > $options->{space_limit}) {
513 _free_up_space($options, $state, \%block_ids);
514 }
515 $file_time_spent -= time;
516 }
517
518 $state->{profile}->{storage} -= time;
519 $file_time_spent -= time;
520 $state->{storage}->put(fmt_hex2base64($part{hash}), $reg_file->data_ref);
521 $file_time_spent += time;
522 $state->{profile}->{storage} += time;
523
524 print " and stored;\n" if $options->{verbose};
525
526 $parts->upsert({ hash => $part{hash} }, \%part);
527 }
528
529 push @{ $version{parts} }, \%part;
530
531 $part_number++;
532
533 last if $read < 1;
534 }
535
536
537 my $block;
538 if(1 == %block_ids) {
539 $block = $blocks->find_row({ id => keys %block_ids });
540 die "Block #".join(', ', keys %block_ids)." wasn't found" if ! $block;
541 }
542 elsif(%block_ids) {
543 # Search for block with highest parts count
544 my $block_parts_cnt = 0;
545 foreach my $bi ( keys %block_ids ) {
546 my $b = $blocks->find_row({ id => $bi });
547 if(! $block_parts_cnt or $block_parts_cnt < $b->{parts_cnt}) {
548 $block_parts_cnt = $b->{parts_cnt};
549 $block = $b;
550 }
551 }
552
553 # Merge blocks to highest one
554 foreach my $bi ( keys %block_ids ) {
555 next if $bi == $block->{id};
556 $state->{profile}->{db_find_version_by_block} -= time;
557 for my $block_file_index ( 0 .. $#$files ) {
558 my $block_file = $files->unpack( $files->[ $block_file_index ] );
559 foreach my $version ( @{ $block_file->{versions} } ) {
560 next if $version->{block_id} != $bi;
561 $version->{block_id} = $block->{id};
562 $block->{parts_cnt} += @{ $version->{parts} };
563 foreach my $vpart (@{ $version->{parts} }) {
564 my $part = $parts->find_row({ hash => $vpart->{hash} });
565 next if $part->{block_id} == $block->{id};
566 $part->{block_id} = $block->{id};
567 $parts->upsert({ hash => $part->{hash} }, $part);
568 }
569 }
570 $files->[ $block_file_index ] = $files->pack( $block_file );
571 }
572 $state->{profile}->{db_find_version_by_block} += time;
573 $blocks->delete({ id => $bi });
574 }
575 } else {
576 $block = {
577 id => ++$state->{last_block_id},
578 parts_cnt => scalar @{ $version{parts} },
579 };
580 }
581
582 foreach my $part (@{ $version{parts} }) {
583 $part->{block_id} //= $block->{id};
584 $parts->upsert({ hash => $part->{hash} }, $part);
585 }
586
587
588 $block->{last_backup_id} = $state->{last_backup_id};
589 $blocks->upsert({ id => $block->{id} }, $block);
590
591 $version{block_id} = $block->{id};
592 }
593 }
594 else {
595 print ", skip not supported file type\n" if $options->{verbose};
596 return;
597 }
598
599
600 # If file version not changed, use old version with wider backup ids range
601 if( @{ $file->{versions} }
602 and (
603 $file->{versions}->[-1]->{backup_id_max} + 1 == $state->{last_backup_id}
604 or $file->{versions}->[-1]->{backup_id_max} == $state->{last_backup_id}
605 )
606 and $file->{versions}->[-1]->{uid} == $version{uid}
607 and $file->{versions}->[-1]->{gid} == $version{gid}
608 and $file->{versions}->[-1]->{size} == $version{size}
609 and $file->{versions}->[-1]->{mode} == $version{mode}
610 and $file->{versions}->[-1]->{mtime}== $version{mtime}
611 and (
612 defined $file->{versions}->[-1]->{symlink_to} == defined $version{symlink_to}
613 and (
614 ! defined $version{symlink_to}
615 or $file->{versions}->[-1]->{symlink_to} eq $version{symlink_to}
616 )
617 )
618 and join(' ', map { $_->{hash} } @{ $file->{versions}->[-1]->{parts} }) eq join(' ', map { $_->{hash} } @{ $version{parts} })
619 ) {
620 $file->{versions}->[-1]->{backup_id_max} = $state->{last_backup_id};
621 } else {
622 push @{ $file->{versions} }, \%version;
623 }
624
625 $files->upsert({ parent_id => $file->{parent_id}, id => $file->{id} }, $file );
626
627 my $backup = $backups->find_row({ id => $state->{last_backup_id} });
628 $backup->{files_cnt}++;
629 $backup->{max_files_cnt}++;
630 $backups->upsert({ id => $backup->{id} }, $backup );
631  
632
633 $state->{longest_files} ||= [];
634 if( @{ $state->{longest_files} } < $SIZE_OF_TOP_FILES
635 or $state->{longest_files}->[-1]->{time} < $file_time_spent
636 ) {
637 @{ $state->{longest_files} } = sort {$b->{time} <=> $a->{time}} (@{ $state->{longest_files} }, {time => $file_time_spent, path => $task->[0]});
638 splice @{ $state->{longest_files} }, $SIZE_OF_TOP_FILES;
639 }
640
641 if($file_weight_spent) {
642 $state->{heaviweightest_files} ||= [];
643 if( @{ $state->{heaviweightest_files} } < $SIZE_OF_TOP_FILES
644 or $state->{heaviweightest_files}->[-1]->{weight} < $file_weight_spent
645 ) {
646 @{ $state->{heaviweightest_files} } = sort {$b->{weight} <=> $a->{weight}}
647 (@{ $state->{heaviweightest_files} }, {weight => $file_weight_spent, path => $task->[0]});
648 splice @{ $state->{heaviweightest_files} }, $SIZE_OF_TOP_FILES;
649 }
650 }
651
652 return @next;
653 }
654  
655 sub _free_up_space {
656 my($options, $state, $protected_block_ids) = @_;
657
658 my($backups, $files, $blocks, $parts) = @{ $state->{db} }{qw(backups files blocks parts)};
659
660 my $deleted = 0;
661 while(1) {
662 my($block_id, @files) = _get_block_to_delete($state);
663 last if ! $block_id;
664 next if exists $protected_block_ids->{ $block_id };
665 my $block = $blocks->find_row({ id => $block_id });
666 next if ! $block;
667 next if $block->{last_backup_id} == $state->{last_backup_id};
668
669 $deleted += App::SimpleBackuper::_BlockDelete($options, $state, $block, $state->{blocks_info}->{ $block_id }->[2]);
670 last if $deleted;
671 }
672
673 die "Nothing to delete from storage for free space" if ! $deleted;
674 }
675  
676 1;
« SimpleBackuper/Restore.pm ↑ Summary SimpleBackuper/StorageCheck.pm »