| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::Command::db::copy_to; | ||
| 5 | $Footprintless::Plugin::Database::Command::db::copy_to::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: copy's the database to the specified destination | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::Command::db::copy_to | ||
| 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 copy...'); | ||
| 21 | eval { | ||
| 22 | $self->{db}->connect(); | ||
| 23 | $self->{destination_db}->connect(); | ||
| 24 | |||
| 25 | $self->{db}->backup( | ||
| 26 | $self->{destination_db}, | ||
| 27 | %{ $self->{options} }, | ||
| 28 | post_restore => $self->{post_restore} | ||
| 29 | ); | ||
| 30 | }; | ||
| 31 | my $error = $@; | ||
| 32 | $self->{db}->disconnect(); | ||
| 33 | $self->{destination_db}->disconnect(); | ||
| 34 | die($error) if ($error); | ||
| 35 | $logger->info('Done!'); | ||
| 36 | } | ||
| 37 | |||
| 38 | sub opt_spec { | ||
| 39 | return ( | ||
| 40 | [ 'clean', 'drop all data on the target before restoring' ], | ||
| 41 | [ 'ignore-all-views', 'will ignore all views' ], | ||
| 42 | [ 'ignore-deny', 'will allow running on denied coordinates' ], | ||
| 43 | [ 'ignore-table=s@', 'will ignore the specified table' ], | ||
| 44 | [ 'live', 'will backup live' ], | ||
| 45 | [ 'only-table=s@', 'will only backup the specified table' ], | ||
| 46 | [ 'single-transaction', 'perform the restore in a single transaction' ], | ||
| 47 | [ 'where=s', 'a where clause' ], | ||
| 48 | ); | ||
| 49 | } | ||
| 50 | |||
| 51 | sub usage_desc { | ||
| 52 | return 'fpl db DB_COORD copy-to DB_COORD %o'; | ||
| 53 | } | ||
| 54 | |||
| 55 | sub validate_args { | ||
| 56 | my ( $self, $opts, $args ) = @_; | ||
| 57 | |||
| 58 | my $command_helper = $self->{footprintless}->db_command_helper(); | ||
| 59 | my ($destination_coordinate) = @$args; | ||
| 60 | $self->usage_error('destination coordinate required for copy') | ||
| 61 | unless ($destination_coordinate); | ||
| 62 | croak("destination [$destination_coordinate] not allowed") | ||
| 63 | unless $opts->{ignore_deny} | ||
| 64 | || $command_helper->allowed_destination($destination_coordinate); | ||
| 65 | |||
| 66 | eval { $self->{db} = $self->{footprintless}->db( $self->{coordinate} ); }; | ||
| 67 | croak("invalid coordinate [$self->{coordinate}]: $@") if ($@); | ||
| 68 | |||
| 69 | eval { $self->{destination_db} = $self->{footprintless}->db($destination_coordinate); }; | ||
| 70 | croak("invalid destination coordinate [$destination_coordinate]: $@") if ($@); | ||
| 71 | |||
| 72 | $self->{options} = { | ||
| 73 | clean => $opts->{clean}, | ||
| 74 | ignore_all_views => $opts->{ignore_all_views}, | ||
| 75 | ( $opts->{ignore_table} ? ( ignore_tables => $opts->{ignore_table} ) : () ), | ||
| 76 | live => $opts->{live}, | ||
| 77 | ( $opts->{only_table} ? ( only_tables => $opts->{only_table} ) : () ), | ||
| 78 | single_transaction => $opts->{single_transaction}, | ||
| 79 | ( $opts->{where} ? ( where => $opts->{where} ) : () ), | ||
| 80 | }; | ||
| 81 | |||
| 82 | $self->{post_restore} = | ||
| 83 | $command_helper->post_restore( $self->{coordinate}, $destination_coordinate ); | ||
| 84 | } | ||
| 85 | |||
| 86 | 1; | ||
| 87 | |||
| 88 | __END__ |