Brackup/Target.pm

filter: Statementstmt 83.3% 95 / 114 Branchbran 59.0% 26 / 44 Conditioncond 41.6% 10 / 24 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 63.6% 14 / 22 Podpod 0.0% 0 / 15 totaltotal 62.9% 146 / 232 CC 39 SCAR 27.2
CC
39
Cov
72%
gc
27.1
prune
20.8
new
5.6
CRAP 15.2
« Brackup/Restore.pm ↑ Summary Brackup/Target/GoogleAppEngine.pm »
1 package Brackup::Target;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 13 ▶use Brackup::InventoryDatabase;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 13 ▶use Brackup::TargetBackupStatInfo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 13 ▶use Brackup::Util 'tempfile';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 13 ▶use Brackup::DecryptedFile;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 4 ▶sub new {
Subroutine new: called CC=5 SCAR=17.3
Pod undocumented
12 4 my ($class, $confsec) = @_;
13 4 my $self = bless {}, $class;
14 4 $self->{name} = $confsec->name;
15 4 ▶ $self->{name} =~ s/^TARGET://
Branch: 1 branch
BranchTrueFalseTotal
$self->{"name"} =~ s/^TARGET:// or die 'No target found matching ' . $confsec->name 4 0 4
16 or die "No target found matching " . $confsec->name;
17 die "Target name must be only a-z, A-Z, 0-9, and _."
18 4 ▶ unless $self->{name} =~ /^\w+/;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"name"} =~ /^\w+/ 0 4 4
19  
20 4 $self->{keep_backups} = $confsec->value("keep_backups");
21 $self->{inv_db} =
22 4 ▶ Brackup::InventoryDatabase->new($confsec->value("inventorydb_file") ||
Condition40%2/5
$confsec->value('inventorydb_file') || $confsec->value('inventory_db') || "$ENV{'HOME'}/.brackup-target-$self->{'name'}.invdb"
l!l
40
$confsec->value("inventorydb_file") or $confsec->value("inventory_db")
l!l&&r!l&&!r
400
MC/DC0%0/2
$confsec->value('inventorydb_file') || $confsec->value('inventory_db') || "$ENV{'HOME'}/.brackup-target-$self->{'name'}.invdb"
$confsec->value("inventorydb_file") $confsec->value("inventory_db")
Truth table33%1/3
A or B
A= $confsec->value("inventorydb_file")B= $confsec->value("inventory_db")
ABresult
1X1
011
000
23 $confsec->value("inventory_db") ||
24 "$ENV{HOME}/.brackup-target-$self->{name}.invdb",
25 $confsec);
26  
27 4 return $self;
28 }
29  
30 5 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
Pod undocumented
31 5 my $self = shift;
32 5 return $self->{name};
33 }
34  
35 # return hashref of key/value pairs you want returned to you during a restore
36 # you should include anything you need to restore.
37 # keys must match /^\w+$/
38 0 ▶sub backup_header {
Subroutine backup_header: not called CC=1 SCAR=6.9
Pod undocumented
39 return {}
40 0 }
41  
42 # returns bool
43 0 ▶sub has_chunk {
Subroutine has_chunk: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my ($self, $chunk) = @_;
45 0 die "ERROR: has_chunk not implemented in sub-class $self";
46 }
47  
48 # returns true on success, or returns false or dies otherwise.
49 0 ▶sub store_chunk {
Subroutine store_chunk: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my ($self, $chunk) = @_;
51 0 die "ERROR: store_chunk not implemented in sub-class $self";
52 }
53  
54 # returns true on success, or returns false or dies otherwise.
55 0 ▶sub delete_chunk {
Subroutine delete_chunk: not called CC=1 SCAR=6.9
Pod undocumented
56 0 my ($self, $chunk) = @_;
57 0 die "ERROR: delete_chunk not implemented in sub-class $self";
58 }
59  
60 # returns a list of names of all chunks
61 0 ▶sub chunks {
Subroutine chunks: not called CC=1 SCAR=6.9
Pod undocumented
62 0 my ($self) = @_;
63 0 die "ERROR: chunks not implemented in sub-class $self";
64 }
65  
66 125 ▶sub inventory_db {
Subroutine inventory_db: called CC=1 SCAR=0.0
Pod undocumented
67 125 my $self = shift;
68 125 return $self->{inv_db};
69 }
70  
71 48 ▶sub add_to_inventory {
Subroutine add_to_inventory: called CC=1 SCAR=0.0
Pod undocumented
72 48 my ($self, $pchunk, $schunk) = @_;
73 48 my $key = $pchunk->inventory_key;
74 48 my $db = $self->inventory_db;
75 48 $db->set($key => $schunk->inventory_value);
76 }
77  
78 # return stored chunk, given positioned chunk, or undef. no
79 # need to override this, unless you have a good reason.
80 69 ▶sub stored_chunk_from_inventory {
Subroutine stored_chunk_from_inventory: called CC=2 SCAR=6.9
Pod undocumented
81 69 my ($self, $pchunk) = @_;
82 69 my $key = $pchunk->inventory_key;
83 69 my $db = $self->inventory_db;
84 69 ▶ my $invval = $db->get($key)
Branch: 1 branch
BranchTrueFalseTotal
my $invval = $db->get($key) or return undef 19 50 69
85 or return undef;
86 19 return Brackup::StoredChunk->new_from_inventory_value($pchunk, $invval);
87 }
88  
89 # return a list of TargetBackupStatInfo objects representing the
90 # stored backup metafiles on this target.
91 0 ▶sub backups {
Subroutine backups: not called CC=1 SCAR=6.9
Pod undocumented
92 0 my ($self) = @_;
93 0 die "ERROR: backups method not implemented in sub-class $self";
94 }
95  
96 # downloads the given backup name to the current directory (with
97 # *.brackup extension)
98 0 ▶sub get_backup {
Subroutine get_backup: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my ($self, $name) = @_;
100 0 die "ERROR: get_backup method not implemented in sub-class $self";
101 }
102  
103 # deletes the given backup from this target
104 0 ▶sub delete_backup {
Subroutine delete_backup: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my ($self, $name) = @_;
106 0 die "ERROR: delete_backup method not implemented in sub-class $self";
107 }
108  
109 # removes old metafiles from this target
110 1 ▶sub prune {
Subroutine prune: called CC=15 SCAR=30.4
Pod undocumented
111 1 my ($self, %opt) = @_;
112  
113 my $keep_backups = $opt{keep_backups} || $self->{keep_backups}
114 1 ▶ or die "ERROR: keep_backups option not set\n";
Branch: 1 branch
BranchTrueFalseTotal
my $keep_backups = $opt{"keep_backups"} || $self->{"keep_backups"} or die "ERROR: keep_backups option not set\n" 1 0 1
Condition33%1/3
$opt{'keep_backups'} || $self->{'keep_backups'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$opt{'keep_backups'} || $self->{'keep_backups'}
$opt{'keep_backups'} $self->{'keep_backups'}
Truth table33%1/3
A || B
A= $opt{'keep_backups'}B= $self->{'keep_backups'}
ABresult
1X1
011
000
115 1 ▶ die "ERROR: keep_backups option must be at least 1\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $keep_backups > 0 0 1 1
116 unless $keep_backups > 0;
117  
118 # select backups to delete
119 1 my (%backups, @backups_to_delete) = ();
120 1 foreach my $backup_name (map {$_->filename} $self->backups) {
121 2 $backup_name =~ /^(.+)-\d+$/;
122 2 ▶ $backups{$1} ||= [];
Condition100%2/2
$backups{$1} ||= []
l!l
11
MC/DC100%1/1
$backups{$1} ||= []
$backups{$1}
Truth table100%2/2
A
A= $backups{$1}
Aresult
11
00
123 2 push @{ $backups{$1} }, $backup_name;
124 }
125 1 foreach my $source (keys %backups) {
126 1 ▶ next if $opt{source} && $source ne $opt{source};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"source"} and $source ne $opt{"source"} 0 1 1
Condition33%1/3
$opt{"source"} and $source ne $opt{"source"}
!ll&&!rl&&r
100
MC/DC0%0/2
$opt{"source"} and $source ne $opt{"source"}
$opt{"source"} $source ne $opt{"source"}
Truth table33%1/3
A and B
A= $opt{"source"}B= $source ne $opt{"source"}
ABresult
0X0
100
111
127 1 my @b = reverse sort @{ $backups{$source} };
128 1 ▶ push @backups_to_delete, splice(@b, ($keep_backups > $#b+1) ? $#b+1 : $keep_backups);
Branch: 1 branch
BranchTrueFalseTotal
$keep_backups > $#b + 1 ? : 0 1 1
129 }
130  
131 1 ▶ warn ($opt{dryrun} ? "Pruning:\n" : "Pruned:\n") if $opt{verbose};
Branch: 2 branches
BranchTrueFalseTotal
if $opt{"verbose"} 0 1 1
$opt{'dryrun'} ? : 0 0 0
132 1 foreach my $backup_name (@backups_to_delete) {
133 1 ▶ warn " $backup_name\n" if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 1 1
134 1 ▶ $self->delete_backup($backup_name) unless $opt{dryrun};
Branch: 1 branch
BranchTrueFalseTotal
unless $opt{"dryrun"} 1 0 1
135 }
136 1 return scalar @backups_to_delete;
137 }
138  
139 # removes orphaned chunks in the target
140 2 ▶sub gc {
Subroutine gc: called CC=20 SCAR=33.0
Pod undocumented
141 2 my ($self, %opt) = @_;
142  
143 # get all chunks and then loop through metafiles to detect
144 # referenced ones
145 2 my %chunks = map {$_ => 1} $self->chunks;
146 2 my $total_chunks = scalar keys %chunks;
147 2 my $tempfile = +(tempfile())[1];
148 2 my @backups = $self->backups;
149 2 BACKUP: foreach my $i (0 .. $#backups) {
150 2 my $backup = $backups[$i];
151 warn sprintf "Collating chunks from backup %s [%d/%d]\n",
152 $backup->filename, $i+1, scalar(@backups)
153 2 ▶ if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 2 2
154 2 $self->get_backup($backup->filename, $tempfile);
155 2 my $decrypted_backup = new Brackup::DecryptedFile(filename => $tempfile);
156 2 my $parser = Brackup::Metafile->open($decrypted_backup->name);
157 2 $parser->readline; # skip header
158 2 ▶ ITEM: while (my $it = $parser->readline) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $it = $parser->readline) 30 2 32
159 30 ▶ next ITEM unless $it->{Chunks};
Branch: 1 branch
BranchTrueFalseTotal
unless $it->{"Chunks"} 6 24 30
160 24 ▶ my @item_chunks = map { (split /;/)[3] } grep { $_ } split(/\s+/, $it->{Chunks} || "");
Condition50%1/2
$it->{'Chunks'} || ''
l!l
240
MC/DC0%0/1
$it->{'Chunks'} || ''
$it->{'Chunks'}
Truth table50%1/2
A
A= $it->{'Chunks'}
Aresult
11
00
161 24 delete $chunks{$_} for (@item_chunks);
162 }
163 }
164 2 my @orphaned_chunks = keys %chunks;
165  
166 # report orphaned chunks
167 2 ▶ if (@orphaned_chunks && $opt{verbose} && $opt{verbose} >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@orphaned_chunks and $opt{"verbose"} and $opt{"verbose"} >= 2) 0 2 2
Condition33%2/6
@orphaned_chunks and $opt{"verbose"} and $opt{"verbose"} >= 2
!ll&&!rl&&r
200
@orphaned_chunks and $opt{"verbose"}
!ll&&!rl&&r
020
MC/DC0%0/3
@orphaned_chunks and $opt{"verbose"} and $opt{"verbose"} >= 2
@orphaned_chunks $opt{"verbose"} $opt{"verbose"} >= 2
Truth table25%1/4
A and B and C
A= @orphaned_chunksB= $opt{"verbose"}C= $opt{"verbose"} >= 2
ABCresult
0XX0
10X0
1100
1111
168 0 warn "Orphaned chunks:\n";
169 0 warn " $_\n" for (@orphaned_chunks);
170 }
171  
172 # remove orphaned chunks
173 2 ▶ if (@orphaned_chunks && ! $opt{dryrun}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@orphaned_chunks and not $opt{"dryrun"}) 2 0 2
Condition33%1/3
@orphaned_chunks and not $opt{"dryrun"}
!ll&&!rl&&r
002
MC/DC0%0/2
@orphaned_chunks and not $opt{"dryrun"}
@orphaned_chunks not $opt{"dryrun"}
Truth table33%1/3
A and not(B)
A= @orphaned_chunksB= not $opt{"dryrun"}
ABresult
0X0
100
111
174 2 my $confirm = 'y';
175 2 ▶ if ($opt{interactive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"interactive"}) 0 2 2
176 0 printf "Run gc, removing %d/%d orphaned chunks? [y/N] ",
177 scalar @orphaned_chunks, $total_chunks;
178 0 $confirm = <>;
179 }
180  
181 2 ▶ if (lc substr($confirm,0,1) eq 'y') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc substr($confirm, 0, 1) eq "y") 2 0 2
182 2 ▶ warn "Removing orphaned chunks\n" if $opt{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $opt{"verbose"} 0 2 2
183 2 $self->delete_chunk($_) for (@orphaned_chunks);
184  
185 # delete orphaned chunks from inventory
186 2 my $inventory_db = $self->inventory_db;
187 2 ▶ while (my ($k, $v) = $inventory_db->each) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $inventory_db->each) 27 2 29
188 27 $v =~ s/ .*$//; # strip value back to hash
189 27 ▶ $inventory_db->delete($k) if exists $chunks{$v};
Branch: 1 branch
BranchTrueFalseTotal
if exists $chunks{$v} 3 24 27
190 }
191 }
192 }
193  
194 2 ▶ return wantarray ? ( scalar @orphaned_chunks, $total_chunks ) : scalar @orphaned_chunks;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
195 }
196  
197  
198  
199 13 1;
200  
201 __END__
« Brackup/Restore.pm ↑ Summary Brackup/Target/GoogleAppEngine.pm »