| 1 | package Backup::Duplicity::YADW; | ||
| 2 | $Backup::Duplicity::YADW::VERSION = '0.12'; | ||
| 3 | $Backup::Duplicity::YADW::VERSION = '0.11'; | ||
| 4 | 1 | ▶ | use Modern::Perl; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Smart::Args; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Config::ApacheFormat; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 1 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 1 | ▶ | use String::Util 'crunch', 'trim'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | use IPC::Run3; | ||
| 14 | use File::Path; | ||
| 15 | use Data::Dumper; | ||
| 16 | use Sys::Syslog; | ||
| 17 | use PID::File; | ||
| 18 | |||
| 19 | use constant CONF_DIR => '/etc/yadw'; | ||
| 20 | use constant CONF_FILE => 'default.conf'; | ||
| 21 | |||
| 22 | use constant PID_EXISTS => 10; | ||
| 23 | |||
| 24 | use vars qw($ErrCode $ErrStr); | ||
| 25 | |||
| 26 | # ABSTRACT: Yet Another Duplicity Wrapper | ||
| 27 | |||
| 28 | |||
| 29 | has conf_dir => ( is => 'rw', isa => 'Str', default => CONF_DIR ); | ||
| 30 | |||
| 31 | |||
| 32 | has conf_file => ( is => 'rw', isa => 'Str', default => CONF_FILE ); | ||
| 33 | |||
| 34 | |||
| 35 | has dry_run => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 36 | |||
| 37 | |||
| 38 | has use_syslog => ( is => 'rw', isa => 'Bool' ); | ||
| 39 | |||
| 40 | |||
| 41 | has verbose => ( is => 'rw', isa => 'Bool', default => 0 ); | ||
| 42 | |||
| 43 | has _conf => ( is => 'rw', isa => 'Config::ApacheFormat' ); | ||
| 44 | |||
| 45 | has _pid => ( is => 'rw', isa => 'PID::File' ); | ||
| 46 | |||
| 47 | |||
| 48 | sub BUILD { | ||
| 49 | my $self = shift; | ||
| 50 | |||
| 51 | $ErrCode = 0; | ||
| 52 | |||
| 53 | my $conf = | ||
| 54 | Config::ApacheFormat->new( fix_booleans => 1, | ||
| 55 | autoload_support => 0 ); | ||
| 56 | |||
| 57 | $conf->read( $self->conf_dir . "/" . $self->conf_file ); | ||
| 58 | $self->_conf($conf); | ||
| 59 | $self->_init_logs; | ||
| 60 | $self->_write_pidfile; | ||
| 61 | } | ||
| 62 | |||
| 63 | sub backup { | ||
| 64 | |||
| 65 | args_pos | ||
| 66 | my $self, | ||
| 67 | my $type => 'Str'; | ||
| 68 | |||
| 69 | $type = $type eq 'inc' ? 'incremental' : $type; | ||
| 70 | |||
| 71 | confess "invalid type: $type" | ||
| 72 | if $type ne 'full' and $type ne 'incremental'; | ||
| 73 | |||
| 74 | my @cmd = ( 'duplicity', $type ); | ||
| 75 | |||
| 76 | $self->_get_verbosity( \@cmd ); | ||
| 77 | $self->_get_exclude_device_files( \@cmd ); | ||
| 78 | $self->_get_incl_excl_list( \@cmd ); | ||
| 79 | $self->_get_encrypt_key( \@cmd ); | ||
| 80 | $self->_get_log_file( \@cmd ); | ||
| 81 | $self->_get_async_upload( \@cmd ); | ||
| 82 | $self->_get_s3_new( \@cmd ); | ||
| 83 | $self->_get_sourcedir( \@cmd ); | ||
| 84 | $self->_get_targetdir( \@cmd ); | ||
| 85 | |||
| 86 | $self->_system(@cmd); | ||
| 87 | |||
| 88 | return 1; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _get_sourcedir { | ||
| 92 | |||
| 93 | args_pos | ||
| 94 | my $self, | ||
| 95 | my $cmds; | ||
| 96 | |||
| 97 | push @$cmds, $self->_conf->get('sourcedir'); | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _get_targetdir { | ||
| 101 | |||
| 102 | args_pos | ||
| 103 | |||
| 104 | # required | ||
| 105 | my $self, my $cmds, | ||
| 106 | |||
| 107 | # optional | ||
| 108 | my $locaction => { isa => 'Str', optional => 1 }; | ||
| 109 | |||
| 110 | my $str = $self->_conf->get('targeturl'); | ||
| 111 | $str .= "/$locaction" if $locaction; | ||
| 112 | |||
| 113 | push( @$cmds, $str ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub _get_async_upload { | ||
| 117 | |||
| 118 | args_pos | ||
| 119 | my $self, | ||
| 120 | my $cmds; | ||
| 121 | |||
| 122 | if ( $self->_conf()->get('asyncupload') ) { | ||
| 123 | push @$cmds, '--asynchronous-upload'; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | sub _get_incl_excl_list { | ||
| 128 | |||
| 129 | args_pos | ||
| 130 | my $self, | ||
| 131 | my $cmds; | ||
| 132 | |||
| 133 | my $conf = $self->_conf; | ||
| 134 | my $block = $conf->block('inclexcl'); | ||
| 135 | my @list = $block->get('list'); | ||
| 136 | |||
| 137 | for ( my $i = 0; $i < @list; $i += 2 ) { | ||
| 138 | |||
| 139 | my $key = trim $list[$i]; | ||
| 140 | my $val = trim $list[ $i + 1 ]; | ||
| 141 | |||
| 142 | if ( $key eq '-' ) { | ||
| 143 | $key = '--exclude'; | ||
| 144 | } | ||
| 145 | elsif ( $key eq '+' ) { | ||
| 146 | $key = '--include'; | ||
| 147 | } | ||
| 148 | else { | ||
| 149 | confess "malformed InclExcl section"; | ||
| 150 | } | ||
| 151 | |||
| 152 | push @$cmds, $key, $val; | ||
| 153 | } | ||
| 154 | } | ||
| 155 | |||
| 156 | sub _write_pidfile { | ||
| 157 | |||
| 158 | args_pos my $self; | ||
| 159 | |||
| 160 | my $conf = $self->_conf; | ||
| 161 | my $pidfile = $conf->get('pidfile'); | ||
| 162 | |||
| 163 | $self->_log( 'info', "pidfile=$pidfile" ); | ||
| 164 | |||
| 165 | my $pid = PID::File->new( file => $pidfile); | ||
| 166 | |||
| 167 | if ( -e $pid->file ) { | ||
| 168 | if ( $pid->running ) { | ||
| 169 | $ErrCode = PID_EXISTS; | ||
| 170 | $ErrStr = "yadw is already running"; | ||
| 171 | confess $ErrStr; | ||
| 172 | } | ||
| 173 | else { | ||
| 174 | $self->_log( 'notice', "removing stale pidfile $pidfile" ); | ||
| 175 | unlink $pid->file or confess "failed to remove pidfile: $!"; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | $pid->create or confess "failed to write pidfile: $!"; | ||
| 180 | $pid->guard; # remove pidfile automatically when it goes out of scope | ||
| 181 | $self->_pid($pid); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _get_expire_days { | ||
| 185 | |||
| 186 | args_pos | ||
| 187 | my $self, | ||
| 188 | my $cmds; | ||
| 189 | |||
| 190 | my $days = $self->_conf->get('days2keepbackups'); | ||
| 191 | |||
| 192 | if ( !defined $days ) { | ||
| 193 | confess "missing configuration days2keepbackups"; | ||
| 194 | } | ||
| 195 | elsif ( !$days ) { | ||
| 196 | |||
| 197 | # confess "days2keepbackups must be greater than 0"; | ||
| 198 | } | ||
| 199 | |||
| 200 | push @$cmds, $days . 'D'; | ||
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | sub expire { | ||
| 205 | |||
| 206 | args_pos my $self; | ||
| 207 | |||
| 208 | $self->_log( 'info', "removing old backups" ); | ||
| 209 | |||
| 210 | my @cmd = ( 'duplicity', 'remove-older-than' ); | ||
| 211 | |||
| 212 | $self->_get_expire_days( \@cmd ); | ||
| 213 | push @cmd, '--force'; | ||
| 214 | push @cmd, '--extra-clean'; | ||
| 215 | $self->_get_targetdir( \@cmd ); | ||
| 216 | |||
| 217 | $self->_system(@cmd); | ||
| 218 | |||
| 219 | return 1; | ||
| 220 | } | ||
| 221 | |||
| 222 | |||
| 223 | sub status { | ||
| 224 | args_pos my $self; | ||
| 225 | |||
| 226 | my @cmd = ( 'duplicity', 'collection-status' ); | ||
| 227 | |||
| 228 | $self->_get_encrypt_key( \@cmd ); | ||
| 229 | $self->_get_s3_new( \@cmd ); | ||
| 230 | $self->_get_targetdir( \@cmd ); | ||
| 231 | |||
| 232 | $self->_system(@cmd); | ||
| 233 | |||
| 234 | return 1; | ||
| 235 | } | ||
| 236 | |||
| 237 | |||
| 238 | sub verify { | ||
| 239 | |||
| 240 | args_pos my $self; | ||
| 241 | |||
| 242 | $self->_log( 'info', "verifying backups" ); | ||
| 243 | |||
| 244 | my @cmd = ( 'duplicity', 'verify' ); | ||
| 245 | |||
| 246 | $self->_get_verbosity( \@cmd ); | ||
| 247 | $self->_get_exclude_device_files( \@cmd ); | ||
| 248 | $self->_get_incl_excl_list( \@cmd ); | ||
| 249 | $self->_get_encrypt_key( \@cmd ); | ||
| 250 | $self->_get_log_file( \@cmd ); | ||
| 251 | $self->_get_s3_new( \@cmd ); | ||
| 252 | $self->_get_targetdir( \@cmd ); | ||
| 253 | $self->_get_sourcedir( \@cmd ); | ||
| 254 | |||
| 255 | $self->_system(@cmd); | ||
| 256 | |||
| 257 | return 1; | ||
| 258 | } | ||
| 259 | |||
| 260 | sub _system { | ||
| 261 | |||
| 262 | my $self = shift; | ||
| 263 | |||
| 264 | $self->_log( 'info', "@_" ); | ||
| 265 | |||
| 266 | my @stderr; | ||
| 267 | run3( [@_], undef, undef, \@stderr ); | ||
| 268 | my $exit = $? >> 8; | ||
| 269 | if ($exit) { | ||
| 270 | $self->_log( 'err', "@stderr" ); | ||
| 271 | confess "duplicity exited with $exit"; | ||
| 272 | } | ||
| 273 | |||
| 274 | $self->_log( 'info', "done" ); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _log { | ||
| 278 | |||
| 279 | args_pos | ||
| 280 | my $self, | ||
| 281 | my $level, | ||
| 282 | my $msg; | ||
| 283 | |||
| 284 | if ( $self->use_syslog ) { | ||
| 285 | syslog( $level, $msg ); | ||
| 286 | } | ||
| 287 | |||
| 288 | $self->_verbose($msg); | ||
| 289 | } | ||
| 290 | |||
| 291 | sub _get_exclude_device_files { | ||
| 292 | |||
| 293 | args_pos | ||
| 294 | my $self, | ||
| 295 | my $cmds; | ||
| 296 | |||
| 297 | if ( $self->_conf->get('excludedevicefiles') ) { | ||
| 298 | push @$cmds, '--exclude-device-files'; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | sub _get_verbosity { | ||
| 303 | |||
| 304 | args_pos | ||
| 305 | my $self, | ||
| 306 | my $cmds; | ||
| 307 | |||
| 308 | my $level = $self->_conf->get('verbosity'); | ||
| 309 | |||
| 310 | push @$cmds, "-v$level"; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub _get_log_file { | ||
| 314 | |||
| 315 | args_pos | ||
| 316 | my $self, | ||
| 317 | my $cmds; | ||
| 318 | |||
| 319 | my $fullpath = $self->_conf->get('logfile'); | ||
| 320 | |||
| 321 | mkpath( dirname $fullpath); | ||
| 322 | |||
| 323 | push @$cmds, "--log-file", $fullpath; | ||
| 324 | } | ||
| 325 | |||
| 326 | sub _get_syslog { | ||
| 327 | args_pos my $self; | ||
| 328 | |||
| 329 | my $toggle = $self->_conf->get('syslog'); | ||
| 330 | |||
| 331 | if ($toggle) { | ||
| 332 | $self->use_syslog(1); | ||
| 333 | } | ||
| 334 | else { | ||
| 335 | $self->use_syslog(0); | ||
| 336 | } | ||
| 337 | } | ||
| 338 | |||
| 339 | sub _get_s3_new { | ||
| 340 | |||
| 341 | args_pos | ||
| 342 | my $self, | ||
| 343 | my $cmds; | ||
| 344 | |||
| 345 | if ( $self->_conf->get('s3usenewstyle') ) { | ||
| 346 | push @$cmds, '--s3-use-new-style'; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | |||
| 350 | sub _get_encrypt_key { | ||
| 351 | |||
| 352 | args_pos | ||
| 353 | my $self, | ||
| 354 | my $cmds; | ||
| 355 | |||
| 356 | my $key = $self->_conf->get('encryptkey'); | ||
| 357 | |||
| 358 | if ( !$key ) { | ||
| 359 | push @$cmds, '--no-encrypt'; | ||
| 360 | } | ||
| 361 | else { | ||
| 362 | |||
| 363 | push @$cmds, "--encrypt-key", $key; | ||
| 364 | } | ||
| 365 | } | ||
| 366 | |||
| 367 | |||
| 368 | sub restore { | ||
| 369 | args | ||
| 370 | |||
| 371 | # required | ||
| 372 | my $self => __PACKAGE__, | ||
| 373 | my $location => 'Str', | ||
| 374 | |||
| 375 | # optional | ||
| 376 | my $days => { isa => 'Int', optional => 1 }; | ||
| 377 | |||
| 378 | $self->_log( 'info', "restoring $location" ); | ||
| 379 | |||
| 380 | my @cmd = ( 'duplicity', 'restore' ); | ||
| 381 | |||
| 382 | $self->_get_verbosity( \@cmd ); | ||
| 383 | $self->_get_encrypt_key( \@cmd ); | ||
| 384 | $self->_get_log_file( \@cmd ); | ||
| 385 | $self->_get_s3_new( \@cmd ); | ||
| 386 | $self->_get_targetdir( \@cmd ); | ||
| 387 | push( @cmd, $location ); | ||
| 388 | |||
| 389 | $self->_system(@cmd); | ||
| 390 | |||
| 391 | return 1; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub _get_dry_run { | ||
| 395 | |||
| 396 | # TODO | ||
| 397 | } | ||
| 398 | |||
| 399 | sub _init_logs { | ||
| 400 | |||
| 401 | args_pos my $self; | ||
| 402 | |||
| 403 | $self->_get_syslog; | ||
| 404 | |||
| 405 | if ( $self->use_syslog ) { | ||
| 406 | openlog( 'backups', $$, 'user' ); | ||
| 407 | } | ||
| 408 | |||
| 409 | $self->_log( 'info', "$0 @ARGV" ); | ||
| 410 | } | ||
| 411 | |||
| 412 | sub _verbose { | ||
| 413 | |||
| 414 | my $self = shift; | ||
| 415 | |||
| 416 | print STDERR "[VERBOSE] @_\n" if $self->verbose; | ||
| 417 | } | ||
| 418 | |||
| 419 | __PACKAGE__->meta->make_immutable; | ||
| 420 | |||
| 421 | 1; # End of Backup::Duplicity::YADW | ||
| 422 | |||
| 423 | __END__ | ||