| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::Command::db::backup; | ||
| 5 | $Footprintless::Plugin::Database::Command::db::backup::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: creates a backup of the database | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::Command::db::backup | ||
| 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 backup...'); | ||
| 21 | eval { | ||
| 22 | $self->{db}->connect(); | ||
| 23 | $self->{db}->backup( $self->{file}, %{ $self->{options} } ); | ||
| 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 | [ 'file=s', 'the output file' ], | ||
| 34 | [ 'ignore-all-views', 'will ignore all views' ], | ||
| 35 | [ 'ignore-table=s@', 'will ignore the specified table' ], | ||
| 36 | [ 'only-table=s@', 'will only backup the specified table' ], | ||
| 37 | [ 'live', 'will backup live' ], | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | sub usage_desc { | ||
| 42 | return 'fpl db DB_COORD backup %o'; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub validate_args { | ||
| 46 | my ( $self, $opts, $args ) = @_; | ||
| 47 | |||
| 48 | eval { $self->{db} = $self->{footprintless}->db( $self->{coordinate} ); }; | ||
| 49 | croak("invalid coordinate [$self->{coordinate}]: $@") if ($@); | ||
| 50 | |||
| 51 | $self->{file} = $opts->{file} || \*STDOUT; | ||
| 52 | |||
| 53 | $self->{options} = { | ||
| 54 | ignore_all_views => $opts->{ignore_all_views}, | ||
| 55 | ( $opts->{ignore_table} ? ( ignore_tables => $opts->{ignore_table} ) : () ), | ||
| 56 | live => $opts->{live}, | ||
| 57 | ( $opts->{only_table} ? ( only_tables => $opts->{only_table} ) : () ), | ||
| 58 | }; | ||
| 59 | } | ||
| 60 | |||
| 61 | 1; | ||
| 62 | |||
| 63 | __END__ |