| 1 | package Backup::EZ; | ||||||||||||
| 2 | 11 | $Backup::EZ::VERSION = '0.43'; | |||||||||||
| 3 | 11 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 11 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 11 | ▶ | use warnings FATAL => 'all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 11 | ▶ | use Config::General; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | 11 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 11 | ▶ | use Devel::Confess 'color'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | 11 | ▶ | use Time::localtime; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 11 | ▶ | use Unix::Syslog qw(:macros :subs); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 11 | ▶ | use Data::UUID; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 11 | ▶ | use Sys::Hostname; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 11 | ▶ | use File::Slurp qw(slurp read_dir); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 11 | ▶ | use File::Spec; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 15 | 11 | ▶ | use Backup::EZ::Dir; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 16 | 11 | ▶ | use Data::Printer alias => 'pdump'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 11 | ▶ | use Data::Dumper; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | # | ||||||||||||
| 20 | # CONSTANTS | ||||||||||||
| 21 | # | ||||||||||||
| 22 | 11 | ▶ | use constant EXCLUDE_FILE => '/etc/ezbackup/ezbackup_exclude.rsync'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 11 | ▶ | use constant CONF => '/etc/ezbackup/ezbackup.conf'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 11 | ▶ | use constant COPIES => 30; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | 11 | ▶ | use constant DEST_HOSTNAME => 'localhost'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 26 | 11 | ▶ | use constant DEST_APPEND_MACH_ID => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | 11 | ▶ | use constant USE_SUDO => 0; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 28 | 11 | ▶ | use constant DEFAULT_ARCHIVE_OPTS => '-az'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 11 | ▶ | use constant ARCHIVE_NO_RECURSE_OPTS => '-dlptgoDz'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 30 | |||||||||||||
| 31 | =head1 NAME | ||||||||||||
| 32 | |||||||||||||
| 33 | Backup::EZ - Simple backups based on rsync | ||||||||||||
| 34 | |||||||||||||
| 35 | =head1 VERSION | ||||||||||||
| 36 | |||||||||||||
| 37 | version 0.43 | ||||||||||||
| 38 | |||||||||||||
| 39 | =cut | ||||||||||||
| 40 | |||||||||||||
| 41 | =head1 SYNOPSIS | ||||||||||||
| 42 | |||||||||||||
| 43 | use Backup::EZ; | ||||||||||||
| 44 | |||||||||||||
| 45 | my $ez = Backup::EZ->new; | ||||||||||||
| 46 | $ez->backup; | ||||||||||||
| 47 | |||||||||||||
| 48 | =head1 DESCRIPTION | ||||||||||||
| 49 | |||||||||||||
| 50 | Backup::EZ is backup software that is designed to be as easy to use | ||||||||||||
| 51 | as possible, yet provide a robust solution | ||||||||||||
| 52 | |||||||||||||
| 53 | If you only want to run backups, see the included command line utility | ||||||||||||
| 54 | "ezbackup". See the README for configuration instructions. | ||||||||||||
| 55 | |||||||||||||
| 56 | =head1 SUBROUTINES/METHODS | ||||||||||||
| 57 | |||||||||||||
| 58 | =head2 new | ||||||||||||
| 59 | |||||||||||||
| 60 | optional args: | ||||||||||||
| 61 | conf => $config_file | ||||||||||||
| 62 | dryrun => $bool, | ||||||||||||
| 63 | exclude_file => $rsync_excl_file | ||||||||||||
| 64 | |||||||||||||
| 65 | =cut | ||||||||||||
| 66 | |||||||||||||
| 67 | sub new { | ||||||||||||
| 68 | 22 | ▶ | my $class = shift; | ||||||||||
|
Subroutine
new: called CC=4 SCAR=14.1
Pod
documented
| |||||||||||||
| 69 | 22 | my %a = @_; | |||||||||||
| 70 | |||||||||||||
| 71 | 22 | my $self = {}; | |||||||||||
| 72 | |||||||||||||
| 73 | # uncoverable branch true | ||||||||||||
| 74 | 22 | ▶ | if ( $ENV{VERBOSE} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | 0 | setlogmask( LOG_UPTO(LOG_DEBUG) ); | |||||||||||
| 76 | 0 | $self->{syslog_option} = LOG_PID | LOG_PERROR; | |||||||||||
| 77 | } | ||||||||||||
| 78 | else { | ||||||||||||
| 79 | 22 | setlogmask( LOG_UPTO(LOG_INFO) ); | |||||||||||
| 80 | 22 | $self->{syslog_option} = LOG_PID; | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 22 | _read_conf( $self, @_ ); | |||||||||||
| 84 | |||||||||||||
| 85 | 22 | ▶ | if ( $a{dryrun} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 86 | 14 | $self->{dryrun} = 1; | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | 22 | ▶ | if ( !defined $a{exclude_file} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 0 | $self->{exclude_file} = EXCLUDE_FILE; | |||||||||||
| 91 | } | ||||||||||||
| 92 | else { | ||||||||||||
| 93 | 22 | $self->{exclude_file} = $a{exclude_file}; | |||||||||||
| 94 | } | ||||||||||||
| 95 | |||||||||||||
| 96 | 22 | bless $self, $class; | |||||||||||
| 97 | 22 | return $self; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | sub _debug { | ||||||||||||
| 101 | 75 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_debug: called CC=1 SCAR=0.0
| |||||||||||||
| 102 | 75 | my $msg = shift; | |||||||||||
| 103 | |||||||||||||
| 104 | 75 | my $line = (caller)[2]; | |||||||||||
| 105 | |||||||||||||
| 106 | 75 | openlog "ezbackup", $self->{syslog_option}, LOG_SYSLOG; | |||||||||||
| 107 | 75 | syslog LOG_DEBUG, "($line) $msg"; | |||||||||||
| 108 | 75 | closelog; | |||||||||||
| 109 | } | ||||||||||||
| 110 | |||||||||||||
| 111 | #sub _error { | ||||||||||||
| 112 | # my $self = shift; | ||||||||||||
| 113 | # my $msg = shift; | ||||||||||||
| 114 | # | ||||||||||||
| 115 | # openlog "ezbackup", $self->{syslog_option}, LOG_LOCAL7; | ||||||||||||
| 116 | # syslog LOG_ERR, $msg; | ||||||||||||
| 117 | # closelog; | ||||||||||||
| 118 | #} | ||||||||||||
| 119 | |||||||||||||
| 120 | sub _info { | ||||||||||||
| 121 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_info: called CC=1 SCAR=0.0
| |||||||||||||
| 122 | 8 | my $msg = shift; | |||||||||||
| 123 | |||||||||||||
| 124 | 8 | openlog "ezbackup", $self->{syslog_option}, LOG_SYSLOG; | |||||||||||
| 125 | 8 | syslog LOG_INFO, $msg; | |||||||||||
| 126 | 8 | closelog; | |||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | sub _read_conf { | ||||||||||||
| 130 | 22 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_read_conf: called CC=8 SCAR=20.8
| |||||||||||||
| 131 | 22 | my %a = @_; | |||||||||||
| 132 | |||||||||||||
| 133 | # uncoverable branch false | ||||||||||||
| 134 | 22 | ▶ | my $conf = $a{conf} ? $a{conf} : CONF; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 135 | |||||||||||||
| 136 | 22 | my $config = Config::General->new( | |||||||||||
| 137 | -ConfigFile => $conf, | ||||||||||||
| 138 | -ForceArray => 1, | ||||||||||||
| 139 | -LowerCaseNames => 1, | ||||||||||||
| 140 | -AutoTrue => 1, | ||||||||||||
| 141 | |||||||||||||
| 142 | ); | ||||||||||||
| 143 | |||||||||||||
| 144 | 22 | my %conf = $config->getall; | |||||||||||
| 145 | 22 | _debug( $self, Dumper \%conf ); | |||||||||||
| 146 | |||||||||||||
| 147 | 22 | foreach my $key ( keys %conf ) { | |||||||||||
| 148 | |||||||||||||
| 149 | 110 | ▶ | if ( !defined $conf{backup_host} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 150 | 2 | $conf{backup_host} = DEST_HOSTNAME; | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | 110 | ▶ | if ( !defined $conf{copies} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 154 | 2 | $conf{copies} = COPIES; | |||||||||||
| 155 | } | ||||||||||||
| 156 | |||||||||||||
| 157 | 110 | ▶ | if ( !defined $conf{append_machine_id} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | 1 | $conf{append_machine_id} = DEST_APPEND_MACH_ID; | |||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 110 | ▶ | if ( !defined $conf{use_sudo} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 162 | 19 | $conf{use_sudo} = USE_SUDO; | |||||||||||
| 163 | } | ||||||||||||
| 164 | } | ||||||||||||
| 165 | |||||||||||||
| 166 | 22 | ▶ | if ( ref( $conf{dir} ) ne 'ARRAY' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 167 | 2 | $conf{dir} = [ $conf{dir} ]; | |||||||||||
| 168 | } | ||||||||||||
| 169 | |||||||||||||
| 170 | 22 | $self->{conf} = \%conf; | |||||||||||
| 171 | } | ||||||||||||
| 172 | |||||||||||||
| 173 | sub _get_dirs { | ||||||||||||
| 174 | 11 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_dirs: called CC=2 SCAR=6.9
| |||||||||||||
| 175 | |||||||||||||
| 176 | 11 | my @dirs; | |||||||||||
| 177 | |||||||||||||
| 178 | 11 | foreach my $dir ( @{ $self->{conf}->{dir} } ) { | |||||||||||
| 179 | 20 | push( @dirs, Backup::EZ::Dir->new($dir) ); | |||||||||||
| 180 | } | ||||||||||||
| 181 | |||||||||||||
| 182 | 10 | $self->_debug( Dumper \@dirs ); | |||||||||||
| 183 | 10 | return @dirs; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | =head2 get_conf_dirs | ||||||||||||
| 187 | |||||||||||||
| 188 | Returns a list Backup::EZ::Dir objects as read from the conf file. | ||||||||||||
| 189 | |||||||||||||
| 190 | =cut | ||||||||||||
| 191 | |||||||||||||
| 192 | sub get_conf_dirs { | ||||||||||||
| 193 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_conf_dirs: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 194 | 2 | return $self->_get_dirs; | |||||||||||
| 195 | } | ||||||||||||
| 196 | |||||||||||||
| 197 | sub _ssh { | ||||||||||||
| 198 | 35 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_ssh: called CC=4 SCAR=13.9
| |||||||||||||
| 199 | 35 | my $cmd = shift; | |||||||||||
| 200 | 35 | my $dryrun = shift; | |||||||||||
| 201 | |||||||||||||
| 202 | 35 | my $sshcmd; | |||||||||||
| 203 | 35 | my $login = $self->_get_dest_login; | |||||||||||
| 204 | |||||||||||||
| 205 | # uncoverable branch false | ||||||||||||
| 206 | 35 | ▶ | if ( $self->_is_unit_test ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 207 | |||||||||||||
| 208 | # unit testing | ||||||||||||
| 209 | 35 | $sshcmd = "$cmd"; | |||||||||||
| 210 | } | ||||||||||||
| 211 | else { | ||||||||||||
| 212 | 0 | $sshcmd = sprintf( 'ssh %s %s', $login, $cmd ); | |||||||||||
| 213 | } | ||||||||||||
| 214 | |||||||||||||
| 215 | 35 | $self->_debug($sshcmd); | |||||||||||
| 216 | 35 | ▶ | return undef if $dryrun; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 217 | |||||||||||||
| 218 | 33 | my @out = `$sshcmd`; | |||||||||||
| 219 | |||||||||||||
| 220 | # uncoverable branch true | ||||||||||||
| 221 | 33 | ▶ | confess if $?; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 222 | |||||||||||||
| 223 | 33 | return @out; | |||||||||||
| 224 | } | ||||||||||||
| 225 | |||||||||||||
| 226 | sub _get_dest_username { | ||||||||||||
| 227 | 43 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_dest_username: called CC=3 SCAR=11.1
| |||||||||||||
| 228 | |||||||||||||
| 229 | 43 | ▶ | if ( $self->{conf}->{backup_user} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 230 | 5 | return $self->{conf}->{backup_user}; | |||||||||||
| 231 | } | ||||||||||||
| 232 | |||||||||||||
| 233 | 38 | ▶ | if ( $ENV{USER} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 234 | 0 | return $ENV{USER}; | |||||||||||
| 235 | } | ||||||||||||
| 236 | |||||||||||||
| 237 | 38 | my $whoami = `whoami`; | |||||||||||
| 238 | 38 | chomp $whoami; | |||||||||||
| 239 | |||||||||||||
| 240 | 38 | return $whoami; | |||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | sub _get_dest_hostname { | ||||||||||||
| 244 | 43 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_dest_hostname: called CC=1 SCAR=0.0
| |||||||||||||
| 245 | |||||||||||||
| 246 | 43 | return $self->{conf}->{backup_host}; | |||||||||||
| 247 | } | ||||||||||||
| 248 | |||||||||||||
| 249 | sub _get_dest_tmp_dir { | ||||||||||||
| 250 | 25 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_dest_tmp_dir: called CC=1 SCAR=0.0
| |||||||||||||
| 251 | |||||||||||||
| 252 | 25 | return sprintf( "%s/%s", $self->get_dest_dir, ".tmp" ); | |||||||||||
| 253 | } | ||||||||||||
| 254 | |||||||||||||
| 255 | sub _get_dest_backup_dir { | ||||||||||||
| 256 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_dest_backup_dir: not called CC=1 SCAR=6.9
| |||||||||||||
| 257 | |||||||||||||
| 258 | 0 | return sprintf( "%s/%s", $self->get_dest_dir, $self->{datestamp} ); | |||||||||||
| 259 | } | ||||||||||||
| 260 | |||||||||||||
| 261 | sub _is_unit_test { | ||||||||||||
| 262 | 43 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_is_unit_test: called CC=2 SCAR=7.0
| |||||||||||||
| 263 | |||||||||||||
| 264 | # uncoverable branch false | ||||||||||||
| 265 | 43 | ▶ | if ( $0 =~ /\.t$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 266 | 43 | return 1; | |||||||||||
| 267 | } | ||||||||||||
| 268 | |||||||||||||
| 269 | 0 | return 0; | |||||||||||
| 270 | } | ||||||||||||
| 271 | |||||||||||||
| 272 | sub _get_dest_login { | ||||||||||||
| 273 | 43 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_get_dest_login: called CC=1 SCAR=0.0
| |||||||||||||
| 274 | |||||||||||||
| 275 | 43 | my $username = $self->_get_dest_username; | |||||||||||
| 276 | 43 | my $hostname = $self->_get_dest_hostname; | |||||||||||
| 277 | |||||||||||||
| 278 | 43 | return sprintf( '%s@%s', $username, $hostname ); | |||||||||||
| 279 | } | ||||||||||||
| 280 | |||||||||||||
| 281 | # Unused? | ||||||||||||
| 282 | # Commenting out to make it clear these are unused | ||||||||||||
| 283 | # Could consider removing | ||||||||||||
| 284 | #sub _rsync_no_recursion { | ||||||||||||
| 285 | # my $self = shift; | ||||||||||||
| 286 | # my $dir = shift; | ||||||||||||
| 287 | # my @extra_options = @_; | ||||||||||||
| 288 | # | ||||||||||||
| 289 | # my $rsync_opts = '-dlptgoD'; | ||||||||||||
| 290 | # my $cmd; | ||||||||||||
| 291 | # my $login; | ||||||||||||
| 292 | # | ||||||||||||
| 293 | # if ( $self->{dryrun} ) { | ||||||||||||
| 294 | # push( @extra_options, '--dry-run' ); | ||||||||||||
| 295 | # } | ||||||||||||
| 296 | # | ||||||||||||
| 297 | # $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) ); | ||||||||||||
| 298 | # $login = $self->_get_dest_login; | ||||||||||||
| 299 | # | ||||||||||||
| 300 | # # uncoverable branch false | ||||||||||||
| 301 | # if ( $self->_is_unit_test ) { | ||||||||||||
| 302 | # $cmd = sprintf( | ||||||||||||
| 303 | # "rsync %s $rsync_opts %s/ %s%s", | ||||||||||||
| 304 | # join( ' ', @extra_options ), $dir, | ||||||||||||
| 305 | # $self->_get_dest_tmp_dir, $dir | ||||||||||||
| 306 | # ); | ||||||||||||
| 307 | # } | ||||||||||||
| 308 | # else { | ||||||||||||
| 309 | # $cmd = sprintf( | ||||||||||||
| 310 | # "rsync %s $rsync_opts -e ssh %s/ %s:%s%s", | ||||||||||||
| 311 | # join( ' ', @extra_options ), | ||||||||||||
| 312 | # $dir, $login, $self->_get_dest_tmp_dir, $dir | ||||||||||||
| 313 | # ); | ||||||||||||
| 314 | # } | ||||||||||||
| 315 | # | ||||||||||||
| 316 | # $cmd .= " --exclude-from " . $self->{exclude_file}; | ||||||||||||
| 317 | # | ||||||||||||
| 318 | # $self->_debug($cmd); | ||||||||||||
| 319 | # system($cmd); | ||||||||||||
| 320 | # | ||||||||||||
| 321 | # # uncoverable branch true | ||||||||||||
| 322 | # confess if $?; | ||||||||||||
| 323 | #} | ||||||||||||
| 324 | |||||||||||||
| 325 | sub _rsync2 { | ||||||||||||
| 326 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_rsync2: called CC=13 SCAR=28.7
| |||||||||||||
| 327 | 8 | my %a = @_; | |||||||||||
| 328 | |||||||||||||
| 329 | 8 | ▶ | my $dir = $a{dir} or confess "missing dir arg"; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 330 | 8 | my $link_dest = $a{link_dest}; | |||||||||||
| 331 | 8 | ▶ | my $archive_opts = $a{archive_opts} || '-az'; | ||||||||||
|
Condition50%1/2
$a{'archive_opts'} || '-az'
MC/DC0%0/1
$a{'archive_opts'} || '-az'
$a{'archive_opts'}
Truth table50%1/2
A
A= $a{'archive_opts'}
| |||||||||||||
| 332 | 8 | ▶ | my $extra_opts = $a{extra_opts} || []; | ||||||||||
|
Condition50%1/2
$a{'extra_opts'} || []
MC/DC0%0/1
$a{'extra_opts'} || []
$a{'extra_opts'}
Truth table50%1/2
A
A= $a{'extra_opts'}
| |||||||||||||
| 333 | |||||||||||||
| 334 | 8 | my $cmd; | |||||||||||
| 335 | my $login; | ||||||||||||
| 336 | |||||||||||||
| 337 | 8 | ▶ | if ( $self->{dryrun} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 338 | 2 | push @$extra_opts, '--dry-run'; | |||||||||||
| 339 | } | ||||||||||||
| 340 | |||||||||||||
| 341 | 8 | ▶ | if ($link_dest) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 342 | 0 | push @$extra_opts, sprintf '--link-dest "%s"', $link_dest; | |||||||||||
| 343 | } | ||||||||||||
| 344 | |||||||||||||
| 345 | 8 | ▶ | if ( $self->{exclude_file} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 346 | 8 | push @$extra_opts, sprintf '--exclude-from "%s"', $self->{exclude_file}; | |||||||||||
| 347 | } | ||||||||||||
| 348 | |||||||||||||
| 349 | 8 | ▶ | if ( $self->{conf}->{extra_rsync_opts} ){ | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 350 | 0 | push @$extra_opts, $self->{conf}->{extra_rsync_opts}; | |||||||||||
| 351 | } | ||||||||||||
| 352 | |||||||||||||
| 353 | 8 | $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) ); | |||||||||||
| 354 | 8 | $login = $self->_get_dest_login; | |||||||||||
| 355 | |||||||||||||
| 356 | # uncoverable branch false | ||||||||||||
| 357 | 8 | ▶ | if ( $self->_is_unit_test ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 358 | 8 | $cmd = sprintf( | |||||||||||
| 359 | 'rsync -s %s %s "%s/" "%s%s"', | ||||||||||||
| 360 | join(' ', @$extra_opts), | ||||||||||||
| 361 | $archive_opts, # archive options | ||||||||||||
| 362 | $dir, # src dir | ||||||||||||
| 363 | $self->_get_dest_tmp_dir, # dest tmp dir | ||||||||||||
| 364 | $dir, # dest sub dir | ||||||||||||
| 365 | ); | ||||||||||||
| 366 | } | ||||||||||||
| 367 | else { | ||||||||||||
| 368 | $cmd = sprintf( | ||||||||||||
| 369 | 'rsync -s %s %s -e ssh "%s/" %s:"%s%s"', | ||||||||||||
| 370 | 0 | join( ' ', @{$extra_opts} ), # extra rsync options | |||||||||||
| 371 | $archive_opts, # archive options | ||||||||||||
| 372 | $dir, # src dir | ||||||||||||
| 373 | $login, # login | ||||||||||||
| 374 | $self->_get_dest_tmp_dir, # dest tmp dir | ||||||||||||
| 375 | $dir, # dest sub dir | ||||||||||||
| 376 | ); | ||||||||||||
| 377 | } | ||||||||||||
| 378 | |||||||||||||
| 379 | # | ||||||||||||
| 380 | # fail safe bailout in case of bug | ||||||||||||
| 381 | # | ||||||||||||
| 382 | 8 | my @cmd = split(/\s+/, $cmd); | |||||||||||
| 383 | 8 | my $link_cnt = 0; | |||||||||||
| 384 | 8 | foreach my $c (@cmd) { | |||||||||||
| 385 | 60 | ▶ | if ($c =~ /link-dest/) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 386 | 0 | $link_cnt++ | |||||||||||
| 387 | } | ||||||||||||
| 388 | } | ||||||||||||
| 389 | |||||||||||||
| 390 | 8 | ▶ | if ($link_cnt > 1) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 391 | 0 | confess "too many link-dest args: $cmd"; | |||||||||||
| 392 | } | ||||||||||||
| 393 | |||||||||||||
| 394 | # | ||||||||||||
| 395 | # ok execute | ||||||||||||
| 396 | # | ||||||||||||
| 397 | 8 | $self->_debug($cmd); | |||||||||||
| 398 | 8 | system($cmd); | |||||||||||
| 399 | |||||||||||||
| 400 | # uncoverable branch true | ||||||||||||
| 401 | 8 | ▶ | confess if $?; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 402 | } | ||||||||||||
| 403 | |||||||||||||
| 404 | # Unused? | ||||||||||||
| 405 | # Commenting out to make it clear these are unused | ||||||||||||
| 406 | # Could consider removing | ||||||||||||
| 407 | #sub _rsync { | ||||||||||||
| 408 | # my $self = shift; | ||||||||||||
| 409 | # my $dir = shift; | ||||||||||||
| 410 | # my @extra_options = @_; | ||||||||||||
| 411 | # | ||||||||||||
| 412 | # my $cmd; | ||||||||||||
| 413 | # my $login; | ||||||||||||
| 414 | # | ||||||||||||
| 415 | # if ( $self->{dryrun} ) { | ||||||||||||
| 416 | # push( @extra_options, '--dry-run' ); | ||||||||||||
| 417 | # } | ||||||||||||
| 418 | # | ||||||||||||
| 419 | # $self->_mk_dest_dir( sprintf( "%s%s", $self->_get_dest_tmp_dir, $dir ) ); | ||||||||||||
| 420 | # $login = $self->_get_dest_login; | ||||||||||||
| 421 | # | ||||||||||||
| 422 | # # uncoverable branch false | ||||||||||||
| 423 | # if ( $self->_is_unit_test ) { | ||||||||||||
| 424 | # $cmd = sprintf( | ||||||||||||
| 425 | # "rsync %s -a %s/ %s%s", | ||||||||||||
| 426 | # join( ' ', @extra_options ), $dir, | ||||||||||||
| 427 | # $self->_get_dest_tmp_dir, $dir | ||||||||||||
| 428 | # ); | ||||||||||||
| 429 | # } | ||||||||||||
| 430 | # else { | ||||||||||||
| 431 | # $cmd = sprintf( | ||||||||||||
| 432 | # "rsync %s -aze ssh %s/ %s:%s%s", | ||||||||||||
| 433 | # join( ' ', @extra_options ), | ||||||||||||
| 434 | # $dir, $login, $self->_get_dest_tmp_dir, $dir | ||||||||||||
| 435 | # ); | ||||||||||||
| 436 | # } | ||||||||||||
| 437 | # | ||||||||||||
| 438 | # $cmd .= " --exclude-from " . $self->{exclude_file}; | ||||||||||||
| 439 | # | ||||||||||||
| 440 | # $self->_debug($cmd); | ||||||||||||
| 441 | # system($cmd); | ||||||||||||
| 442 | # | ||||||||||||
| 443 | # # uncoverable branch true | ||||||||||||
| 444 | # confess if $?; | ||||||||||||
| 445 | #} | ||||||||||||
| 446 | |||||||||||||
| 447 | sub _full_backup_chunked { | ||||||||||||
| 448 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_full_backup_chunked: not called CC=3 SCAR=24.8
| |||||||||||||
| 449 | 0 | my $dir = shift; | |||||||||||
| 450 | |||||||||||||
| 451 | 0 | $self->_rsync2( | |||||||||||
| 452 | dir => $dir->dirname, | ||||||||||||
| 453 | archive_opts => ARCHIVE_NO_RECURSE_OPTS, | ||||||||||||
| 454 | extra_opts => $dir->excludes(), | ||||||||||||
| 455 | ); | ||||||||||||
| 456 | |||||||||||||
| 457 | 0 | my @entries = read_dir( $dir->dirname, prefix => 1 ); | |||||||||||
| 458 | |||||||||||||
| 459 | 0 | foreach my $entry (@entries) { | |||||||||||
| 460 | 0 | ▶ | if ( -d $entry ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 461 | 0 | $self->_rsync2( | |||||||||||
| 462 | dir => $entry, | ||||||||||||
| 463 | archive_opts => DEFAULT_ARCHIVE_OPTS, | ||||||||||||
| 464 | extra_opts => $dir->excludes(), | ||||||||||||
| 465 | ); | ||||||||||||
| 466 | } | ||||||||||||
| 467 | } | ||||||||||||
| 468 | } | ||||||||||||
| 469 | |||||||||||||
| 470 | sub _full_backup { | ||||||||||||
| 471 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_full_backup: called CC=2 SCAR=7.4
| |||||||||||||
| 472 | 8 | my $dir = shift; | |||||||||||
| 473 | |||||||||||||
| 474 | 8 | ▶ | if ( $dir->chunked ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 475 | 0 | $self->_full_backup_chunked($dir); | |||||||||||
| 476 | } | ||||||||||||
| 477 | else { | ||||||||||||
| 478 | 8 | $self->_rsync2( | |||||||||||
| 479 | dir => $dir->dirname, | ||||||||||||
| 480 | archive_opts => DEFAULT_ARCHIVE_OPTS, | ||||||||||||
| 481 | extra_opts => $dir->excludes(), | ||||||||||||
| 482 | ); | ||||||||||||
| 483 | } | ||||||||||||
| 484 | } | ||||||||||||
| 485 | |||||||||||||
| 486 | sub _inc_backup_chunked { | ||||||||||||
| 487 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_inc_backup_chunked: not called CC=3 SCAR=24.8
| |||||||||||||
| 488 | 0 | my $dir = shift; | |||||||||||
| 489 | 0 | my $last_backup_dir = shift; | |||||||||||
| 490 | 0 | my $link_dest = shift; | |||||||||||
| 491 | |||||||||||||
| 492 | 0 | $self->_rsync2( | |||||||||||
| 493 | dir => $dir->dirname, | ||||||||||||
| 494 | archive_opts => ARCHIVE_NO_RECURSE_OPTS, | ||||||||||||
| 495 | extra_opts => $dir->excludes(), | ||||||||||||
| 496 | link_dest => $link_dest, | ||||||||||||
| 497 | ); | ||||||||||||
| 498 | |||||||||||||
| 499 | 0 | my @entries = read_dir( $dir->dirname, prefix => 0 ); | |||||||||||
| 500 | |||||||||||||
| 501 | 0 | foreach my $entry (@entries) { | |||||||||||
| 502 | |||||||||||||
| 503 | 0 | my $abs_entry = sprintf( '%s/%s', $dir->dirname, $entry ); | |||||||||||
| 504 | |||||||||||||
| 505 | 0 | ▶ | if ( -d $abs_entry ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 506 | 0 | $self->_rsync2( | |||||||||||
| 507 | dir => $abs_entry, | ||||||||||||
| 508 | archive_opts => DEFAULT_ARCHIVE_OPTS, | ||||||||||||
| 509 | extra_opts => $dir->excludes(), | ||||||||||||
| 510 | link_dest => sprintf( '%s/%s', $link_dest, $entry ), | ||||||||||||
| 511 | ); | ||||||||||||
| 512 | } | ||||||||||||
| 513 | } | ||||||||||||
| 514 | } | ||||||||||||
| 515 | |||||||||||||
| 516 | sub _inc_backup { | ||||||||||||
| 517 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_inc_backup: not called CC=2 SCAR=17.9
| |||||||||||||
| 518 | 0 | my $dir = shift; | |||||||||||
| 519 | 0 | my $last_backup_dir = shift; | |||||||||||
| 520 | |||||||||||||
| 521 | 0 | my $link_dest = sprintf( | |||||||||||
| 522 | "%s/%s/%s", | ||||||||||||
| 523 | $self->get_dest_dir, # | ||||||||||||
| 524 | $last_backup_dir, # | ||||||||||||
| 525 | $dir->dirname, # | ||||||||||||
| 526 | ); | ||||||||||||
| 527 | |||||||||||||
| 528 | 0 | ▶ | if ( $dir->chunked ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 529 | 0 | $self->_inc_backup_chunked( $dir, $last_backup_dir, $link_dest ); | |||||||||||
| 530 | } | ||||||||||||
| 531 | else { | ||||||||||||
| 532 | 0 | $self->_rsync2( | |||||||||||
| 533 | dir => $dir->dirname, | ||||||||||||
| 534 | archive_opts => DEFAULT_ARCHIVE_OPTS, | ||||||||||||
| 535 | extra_opts => $dir->excludes(), | ||||||||||||
| 536 | link_dest => $link_dest, | ||||||||||||
| 537 | ); | ||||||||||||
| 538 | } | ||||||||||||
| 539 | } | ||||||||||||
| 540 | |||||||||||||
| 541 | sub _mk_dest_dir { | ||||||||||||
| 542 | 26 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_mk_dest_dir: called CC=1 SCAR=0.0
| |||||||||||||
| 543 | 26 | my $dir = shift; | |||||||||||
| 544 | 26 | my $dryrun = shift; | |||||||||||
| 545 | |||||||||||||
| 546 | 26 | my $cmd = sprintf( "mkdir -p %s", $dir ); | |||||||||||
| 547 | 26 | $self->_ssh( $cmd, $dryrun ); | |||||||||||
| 548 | } | ||||||||||||
| 549 | |||||||||||||
| 550 | sub _set_datestamp { | ||||||||||||
| 551 | 9 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_set_datestamp: called CC=1 SCAR=0.0
| |||||||||||||
| 552 | |||||||||||||
| 553 | 9 | my $t = localtime; | |||||||||||
| 554 | 9 | $self->{datestamp} = sprintf( | |||||||||||
| 555 | "%04d-%02d-%02d_%02d:%02d:%02d", | ||||||||||||
| 556 | $t->year + 1900, | ||||||||||||
| 557 | $t->mon + 1, | ||||||||||||
| 558 | $t->mday, $t->hour, $t->min, $t->sec | ||||||||||||
| 559 | ); | ||||||||||||
| 560 | } | ||||||||||||
| 561 | |||||||||||||
| 562 | =head2 dump_conf | ||||||||||||
| 563 | |||||||||||||
| 564 | Does what it says. | ||||||||||||
| 565 | |||||||||||||
| 566 | =cut | ||||||||||||
| 567 | |||||||||||||
| 568 | sub dump_conf { | ||||||||||||
| 569 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
dump_conf: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 570 | |||||||||||||
| 571 | 0 | pdump $self->{conf}; | |||||||||||
| 572 | } | ||||||||||||
| 573 | |||||||||||||
| 574 | =head2 backup | ||||||||||||
| 575 | |||||||||||||
| 576 | Invokes the backup process. Takes no args. | ||||||||||||
| 577 | |||||||||||||
| 578 | =cut | ||||||||||||
| 579 | |||||||||||||
| 580 | sub backup { | ||||||||||||
| 581 | 9 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
backup: called CC=4 SCAR=15.4
Pod
documented
| |||||||||||||
| 582 | |||||||||||||
| 583 | 9 | $self->_mk_dest_dir( $self->get_dest_dir ); | |||||||||||
| 584 | 9 | my @backups = $self->get_list_of_backups; | |||||||||||
| 585 | 9 | $self->_set_datestamp; | |||||||||||
| 586 | 9 | $self->_mk_dest_dir( $self->_get_dest_tmp_dir, $self->{dryrun} ); | |||||||||||
| 587 | |||||||||||||
| 588 | 9 | foreach my $dir ( $self->_get_dirs ) { | |||||||||||
| 589 | |||||||||||||
| 590 | 8 | my $dirname = $dir->dirname(); | |||||||||||
| 591 | 8 | ▶ | if ( -d $dirname ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 592 | |||||||||||||
| 593 | 8 | $self->_info("backing up $dirname"); | |||||||||||
| 594 | |||||||||||||
| 595 | 8 | ▶ | if ( !@backups ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 596 | |||||||||||||
| 597 | # full | ||||||||||||
| 598 | 8 | $self->_full_backup($dir); | |||||||||||
| 599 | } | ||||||||||||
| 600 | else { | ||||||||||||
| 601 | # incremental | ||||||||||||
| 602 | 0 | $self->_inc_backup( $dir, $backups[$#backups] ); | |||||||||||
| 603 | } | ||||||||||||
| 604 | } | ||||||||||||
| 605 | else { | ||||||||||||
| 606 | 0 | $self->_info("skipping $dirname because it does not exist"); | |||||||||||
| 607 | } | ||||||||||||
| 608 | |||||||||||||
| 609 | } | ||||||||||||
| 610 | |||||||||||||
| 611 | $self->_ssh( | ||||||||||||
| 612 | sprintf( "mv %s %s", | ||||||||||||
| 613 | $self->_get_dest_tmp_dir, $self->_get_dest_backup_dir ), | ||||||||||||
| 614 | $self->{dryrun} | ||||||||||||
| 615 | 0 | ); | |||||||||||
| 616 | |||||||||||||
| 617 | 0 | $self->expire(); | |||||||||||
| 618 | |||||||||||||
| 619 | 0 | return 1; | |||||||||||
| 620 | } | ||||||||||||
| 621 | |||||||||||||
| 622 | =head2 expire | ||||||||||||
| 623 | |||||||||||||
| 624 | Expire backups. Gets a list of current backups and removes old ones that are | ||||||||||||
| 625 | beyond the cutoff (see "copies" in the conf file). | ||||||||||||
| 626 | |||||||||||||
| 627 | =cut | ||||||||||||
| 628 | |||||||||||||
| 629 | sub expire { | ||||||||||||
| 630 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
expire: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 631 | |||||||||||||
| 632 | 0 | my @list = $self->get_list_of_backups; | |||||||||||
| 633 | |||||||||||||
| 634 | 0 | ▶ | while ( scalar(@list) > $self->{conf}->{copies} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 635 | |||||||||||||
| 636 | 0 | my $subdir = shift @list; | |||||||||||
| 637 | 0 | my $del_dir = sprintf( "%s/%s", $self->get_dest_dir, $subdir ); | |||||||||||
| 638 | |||||||||||||
| 639 | my $cmd = sprintf( "%s rm -rf $del_dir", | ||||||||||||
| 640 | 0 | ▶ | $self->{conf}->{use_sudo} ? 'sudo' : '' ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 641 | |||||||||||||
| 642 | 0 | $self->_ssh($cmd); | |||||||||||
| 643 | } | ||||||||||||
| 644 | } | ||||||||||||
| 645 | |||||||||||||
| 646 | =head2 get_backup_host | ||||||||||||
| 647 | |||||||||||||
| 648 | Returns the backup_host name. | ||||||||||||
| 649 | |||||||||||||
| 650 | =cut | ||||||||||||
| 651 | |||||||||||||
| 652 | sub get_backup_host { | ||||||||||||
| 653 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_backup_host: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 654 | 0 | return $self->{conf}->{backup_host}; | |||||||||||
| 655 | } | ||||||||||||
| 656 | |||||||||||||
| 657 | =head2 get_dest_dir | ||||||||||||
| 658 | |||||||||||||
| 659 | Returns the dest_dir. | ||||||||||||
| 660 | |||||||||||||
| 661 | =cut | ||||||||||||
| 662 | |||||||||||||
| 663 | sub get_dest_dir { | ||||||||||||
| 664 | 47 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_dest_dir: called CC=4 SCAR=15.4
Pod
documented
| |||||||||||||
| 665 | |||||||||||||
| 666 | 47 | my $hostname = hostname(); | |||||||||||
| 667 | 47 | $hostname =~ s/\..+$//; | |||||||||||
| 668 | |||||||||||||
| 669 | 47 | ▶ | if ( $self->{conf}->{append_machine_id} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 670 | |||||||||||||
| 671 | # uncoverable branch true | ||||||||||||
| 672 | 3 | ▶ | if ( !-f '/etc/machine-id' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 673 | |||||||||||||
| 674 | # uncoverable statement count:2 | ||||||||||||
| 675 | 0 | my $data_uuid = Data::UUID->new; | |||||||||||
| 676 | 0 | my $uuid = $data_uuid->create_str(); | |||||||||||
| 677 | |||||||||||||
| 678 | # uncoverable statement count:3 | ||||||||||||
| 679 | 0 | ▶ | open my $fh, ">/etc/machine-id" | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 680 | or confess "failed to open /etc/machine-id: $!"; | ||||||||||||
| 681 | 0 | print $fh "$uuid\n"; | |||||||||||
| 682 | 0 | close($fh); | |||||||||||
| 683 | } | ||||||||||||
| 684 | |||||||||||||
| 685 | 3 | my $uuid = slurp("/etc/machine-id"); | |||||||||||
| 686 | 3 | chomp $uuid; | |||||||||||
| 687 | |||||||||||||
| 688 | 3 | $hostname = "$hostname-$uuid"; | |||||||||||
| 689 | } | ||||||||||||
| 690 | |||||||||||||
| 691 | 47 | return sprintf( "%s/%s", $self->{conf}->{dest_dir}, $hostname ); | |||||||||||
| 692 | } | ||||||||||||
| 693 | |||||||||||||
| 694 | =head2 get_list_of_backups | ||||||||||||
| 695 | |||||||||||||
| 696 | Returns an array of backups. They are in the format of "YYYY-MM-DD_HH:MM:SS". | ||||||||||||
| 697 | =cut | ||||||||||||
| 698 | |||||||||||||
| 699 | sub get_list_of_backups { | ||||||||||||
| 700 | 9 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
get_list_of_backups: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||
| 701 | |||||||||||||
| 702 | 9 | my @backups; | |||||||||||
| 703 | |||||||||||||
| 704 | 9 | my @list = $self->_ssh( sprintf( "ls %s", $self->get_dest_dir ) ); | |||||||||||
| 705 | |||||||||||||
| 706 | 9 | foreach my $e (@list) { | |||||||||||
| 707 | 0 | chomp $e; | |||||||||||
| 708 | |||||||||||||
| 709 | 0 | ▶ | if ( $e =~ /^\d\d\d\d-\d\d-\d\d_\d\d:\d\d:\d\d$/ ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 710 | 0 | push( @backups, $e ); | |||||||||||
| 711 | } | ||||||||||||
| 712 | } | ||||||||||||
| 713 | |||||||||||||
| 714 | 9 | return @backups; | |||||||||||
| 715 | } | ||||||||||||
| 716 | |||||||||||||
| 717 | 11 | 1; | |||||||||||