| 1 | package Dackup; | ||||||||||
| 2 | 1 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 1 | ▶ | use MooseX::StrictConstructor; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use MooseX::Types::Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use Dackup::Cache; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 1 | ▶ | use Dackup::Entry; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 1 | ▶ | use Dackup::Target::CloudFiles; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use Dackup::Target::Filesystem; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use Dackup::Target::S3; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 1 | ▶ | use Dackup::Target::SSH; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 1 | ▶ | use Data::Stream::Bulk::Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 1 | ▶ | use DBI; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | ▶ | use Devel::CheckOS qw(os_is); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 1 | ▶ | use File::HomeDir; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 1 | ▶ | use List::Util qw(sum); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 1 | ▶ | use Number::DataRate; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | 1 | ▶ | use Path::Class; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | 1 | ▶ | use Term::ProgressBar::Simple; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 19 | |||||||||||
| 20 | 1 | our $VERSION = '0.44'; | |||||||||
| 21 | |||||||||||
| 22 | has 'directory' => ( | ||||||||||
| 23 | is => 'ro', | ||||||||||
| 24 | isa => 'Path::Class::Dir', | ||||||||||
| 25 | required => 0, | ||||||||||
| 26 | coerce => 1, | ||||||||||
| 27 | 0 | ▶ | default => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||
| 28 | 0 | my $self = shift; | |||||||||
| 29 | 0 | ▶ | return dir( File::HomeDir->my_data, | ||||||||
|
Branch: 1 branch
| |||||||||||
| 30 | ( os_is('MicrosoftWindows') ? 'Perl' : '.perl' ), 'Dackup' ); | ||||||||||
| 31 | }, | ||||||||||
| 32 | 1 | ); | |||||||||
| 33 | 1 | has 'source' => ( | |||||||||
| 34 | is => 'ro', | ||||||||||
| 35 | isa => 'Dackup::Target', | ||||||||||
| 36 | required => 1, | ||||||||||
| 37 | ); | ||||||||||
| 38 | 1 | has 'destination' => ( | |||||||||
| 39 | is => 'ro', | ||||||||||
| 40 | isa => 'Dackup::Target', | ||||||||||
| 41 | required => 1, | ||||||||||
| 42 | ); | ||||||||||
| 43 | 1 | has 'delete' => ( | |||||||||
| 44 | is => 'rw', | ||||||||||
| 45 | isa => 'Bool', | ||||||||||
| 46 | required => 1, | ||||||||||
| 47 | ); | ||||||||||
| 48 | 1 | has 'dry_run' => ( | |||||||||
| 49 | is => 'rw', | ||||||||||
| 50 | isa => 'Bool', | ||||||||||
| 51 | default => 0, | ||||||||||
| 52 | ); | ||||||||||
| 53 | 1 | has 'verbose' => ( | |||||||||
| 54 | is => 'rw', | ||||||||||
| 55 | isa => 'Bool', | ||||||||||
| 56 | default => 0, | ||||||||||
| 57 | ); | ||||||||||
| 58 | 1 | has 'cache' => ( | |||||||||
| 59 | is => 'rw', | ||||||||||
| 60 | isa => 'Dackup::Cache', | ||||||||||
| 61 | ); | ||||||||||
| 62 | 1 | has 'throttle' => ( | |||||||||
| 63 | is => 'ro', | ||||||||||
| 64 | isa => 'Str', | ||||||||||
| 65 | ); | ||||||||||
| 66 | |||||||||||
| 67 | 1 | __PACKAGE__->meta->make_immutable; | |||||||||
| 68 | |||||||||||
| 69 | 1 | ▶ | sub BUILD { | ||||||||
|
Subroutine
BUILD: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||
| 70 | 1 | my $self = shift; | |||||||||
| 71 | 1 | my $directory = $self->directory; | |||||||||
| 72 | |||||||||||
| 73 | 1 | ▶ | unless ( -d $directory ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | 0 | ▶ | $directory->mkpath | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | || confess "Unable to create directory $directory: $!"; | ||||||||||
| 76 | } | ||||||||||
| 77 | |||||||||||
| 78 | 1 | my $filename = file( $directory, 'dackup.db' ); | |||||||||
| 79 | 1 | my $cache = Dackup::Cache->new( filename => $filename ); | |||||||||
| 80 | 1 | $self->cache($cache); | |||||||||
| 81 | 1 | $self->source->dackup($self); | |||||||||
| 82 | 1 | $self->destination->dackup($self); | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 2 | ▶ | sub backup { | ||||||||
|
Subroutine
backup: called CC=12 SCAR=29.7
Pod
undocumented
| |||||||||||
| 86 | 2 | my $self = shift; | |||||||||
| 87 | 2 | my $source = $self->source; | |||||||||
| 88 | 2 | my $destination = $self->destination; | |||||||||
| 89 | 2 | my $delete = $self->delete; | |||||||||
| 90 | 2 | my $dry_run = $self->dry_run; | |||||||||
| 91 | 2 | my $verbose = $self->verbose; | |||||||||
| 92 | |||||||||||
| 93 | 2 | my $source_entries = $source->entries; | |||||||||
| 94 | 2 | my $destination_entries = $destination->entries; | |||||||||
| 95 | |||||||||||
| 96 | 2 | my ( $entries_to_update, $entries_to_delete ) | |||||||||
| 97 | = $self->_calc( $source_entries, $destination_entries ); | ||||||||||
| 98 | |||||||||||
| 99 | 2 | my $total = sum map { $_->size } @$entries_to_update; | |||||||||
| 100 | 2 | ▶ | $total += scalar(@$entries_to_delete) if $delete; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 101 | 2 | ▶ | $total = 0 unless defined $total; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 102 | |||||||||||
| 103 | 2 | my $progress = Term::ProgressBar::Simple->new($total); | |||||||||
| 104 | 2 | $progress->message( | |||||||||
| 105 | 'Updating ' . scalar(@$entries_to_update) . ' files' ); | ||||||||||
| 106 | 2 | ▶ | $progress->message( 'Deleting ' . scalar(@$entries_to_delete) . ' files' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 107 | if $delete; | ||||||||||
| 108 | |||||||||||
| 109 | 2 | foreach my $entry (@$entries_to_update) { | |||||||||
| 110 | 3 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 0 | my $source_name = $source->name($entry); | |||||||||
| 112 | 0 | my $destination_name = $destination->name($entry); | |||||||||
| 113 | 0 | $progress->message("$source_name -> $destination_name"); | |||||||||
| 114 | } | ||||||||||
| 115 | 3 | ▶ | $destination->update( $source, $entry ) unless $dry_run; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 116 | 3 | $progress += $entry->size; | |||||||||
| 117 | } | ||||||||||
| 118 | |||||||||||
| 119 | 2 | ▶ | if ($delete) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 120 | 0 | foreach my $entry (@$entries_to_delete) { | |||||||||
| 121 | 0 | ▶ | if ($verbose) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | 0 | my $name = $destination->name($entry); | |||||||||
| 123 | 0 | $progress->message("Deleting $name"); | |||||||||
| 124 | } | ||||||||||
| 125 | 0 | ▶ | $destination->delete($entry) unless $dry_run; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 126 | 0 | $progress++; | |||||||||
| 127 | } | ||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 2 | $progress->message( 'Updated ' . scalar(@$entries_to_update) . ' files' ); | |||||||||
| 131 | 2 | ▶ | $progress->message( 'Deleted ' . scalar(@$entries_to_delete) . ' files' ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 132 | if $delete; | ||||||||||
| 133 | |||||||||||
| 134 | 2 | return scalar(@$entries_to_update); | |||||||||
| 135 | } | ||||||||||
| 136 | |||||||||||
| 137 | 2 | ▶ | sub _calc { | ||||||||
|
Subroutine
_calc: called CC=8 SCAR=21.2
| |||||||||||
| 138 | 2 | my ( $self, $source_entries, $destination_entries ) = @_; | |||||||||
| 139 | 2 | my %source_entries; | |||||||||
| 140 | my %destination_entries; | ||||||||||
| 141 | |||||||||||
| 142 | 2 | $source_entries{ $_->key } = $_ foreach @$source_entries; | |||||||||
| 143 | 2 | $destination_entries{ $_->key } = $_ foreach @$destination_entries; | |||||||||
| 144 | |||||||||||
| 145 | 2 | my @entries_to_update; | |||||||||
| 146 | my @entries_to_delete; | ||||||||||
| 147 | |||||||||||
| 148 | 2 | foreach my $key ( sort keys %source_entries ) { | |||||||||
| 149 | 6 | my $source_entry = $source_entries{$key}; | |||||||||
| 150 | 6 | my $destination_entry = $destination_entries{$key}; | |||||||||
| 151 | 6 | ▶ | if ($destination_entry) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | 3 | ▶ | if ( $source_entry->md5_hex eq $destination_entry->md5_hex ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 153 | |||||||||||
| 154 | # warn "$key same"; | ||||||||||
| 155 | } else { | ||||||||||
| 156 | |||||||||||
| 157 | # warn "$key different"; | ||||||||||
| 158 | 0 | push @entries_to_update, $source_entry; | |||||||||
| 159 | } | ||||||||||
| 160 | } else { | ||||||||||
| 161 | |||||||||||
| 162 | # warn "$key missing"; | ||||||||||
| 163 | 3 | push @entries_to_update, $source_entry; | |||||||||
| 164 | } | ||||||||||
| 165 | } | ||||||||||
| 166 | |||||||||||
| 167 | 2 | foreach my $key ( sort keys %destination_entries ) { | |||||||||
| 168 | 3 | my $source_entry = $source_entries{$key}; | |||||||||
| 169 | 3 | my $destination_entry = $destination_entries{$key}; | |||||||||
| 170 | 3 | ▶ | unless ($source_entry) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | |||||||||||
| 172 | # warn "$key to delete"; | ||||||||||
| 173 | 0 | push @entries_to_delete, $destination_entry; | |||||||||
| 174 | } | ||||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 2 | return \@entries_to_update, \@entries_to_delete; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | 1 | 1; | |||||||||
| 181 | |||||||||||
| 182 | __END__ | ||||||||||