| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::Command::db::restore; | ||
| 5 | $Footprintless::Plugin::Database::Command::db::restore::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: restore a database from a backup | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::Command::db::restore | ||
| 8 | |||
| 9 | use parent qw(Footprintless::App::Action); | ||
| 10 | |||
| 11 | use Carp; | ||
| 12 | use Footprintless::App -ignore; | ||
| 13 | use Log::Any; | ||
| 14 | |||
| 15 | my $logger = Log::Any->get_logger(); | ||
| 16 | |||
| 17 | sub execute { | ||
| 18 | my ( $self, $opts, $args ) = @_; | ||
| 19 | |||
| 20 | $logger->info('Performing restore...'); | ||
| 21 | eval { | ||
| 22 | $self->{db}->connect(); | ||
| 23 | $self->{db}->restore( ( delete( $opts->{file} ) || \*STDIN ), %$opts ); | ||
| 24 | }; | ||
| 25 | my $error = $@; | ||
| 26 | $self->{db}->disconnect(); | ||
| 27 | die($error) if ($error); | ||
| 28 | $logger->info('Done...'); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub opt_spec { | ||
| 32 | return ( | ||
| 33 | [ 'clean', 'drop all data on the target before restoring' ], | ||
| 34 | [ 'file=s', 'the input file' ], | ||
| 35 | [ 'ignore-deny', 'will allow running on denied coordinates' ], | ||
| 36 | [ 'post-restore=s', 'a sql script to run after the restore' ], | ||
| 37 | ); | ||
| 38 | } | ||
| 39 | |||
| 40 | sub usage_desc { | ||
| 41 | return 'fpl db DB_COORD restore %o'; | ||
| 42 | } | ||
| 43 | |||
| 44 | sub validate_args { | ||
| 45 | my ( $self, $opts, $args ) = @_; | ||
| 46 | |||
| 47 | my $command_helper = $self->{footprintless}->db_command_helper(); | ||
| 48 | croak("destination [$self->{coordinate}] not allowed") | ||
| 49 | unless $opts->{ignore_deny} | ||
| 50 | || $command_helper->allowed_destination( $self->{coordinate} ); | ||
| 51 | |||
| 52 | eval { $self->{db} = $self->{footprintless}->db( $self->{coordinate} ); }; | ||
| 53 | croak("invalid coordinate [$self->{coordinate}]: $@") if ($@); | ||
| 54 | } | ||
| 55 | |||
| 56 | 1; | ||
| 57 | |||
| 58 | __END__ |