| 1 | use strict; | ||
| 2 | use warnings; | ||
| 3 | |||
| 4 | package Footprintless::Plugin::Database::MySqlProvider; | ||
| 5 | $Footprintless::Plugin::Database::MySqlProvider::VERSION = '1.06'; | ||
| 6 | # ABSTRACT: A MySql provider implementation | ||
| 7 | # PODNAME: Footprintless::Plugin::Database::MySqlProvider | ||
| 8 | |||
| 9 | use parent qw(Footprintless::Plugin::Database::AbstractProvider); | ||
| 10 | |||
| 11 | use overload q{""} => 'to_string', fallback => 1; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use File::Temp; | ||
| 15 | use Footprintless::Command qw( | ||
| 16 | command | ||
| 17 | pipe_command | ||
| 18 | sed_command | ||
| 19 | ); | ||
| 20 | use Footprintless::Mixins qw( | ||
| 21 | _run_or_die | ||
| 22 | ); | ||
| 23 | use Log::Any; | ||
| 24 | |||
| 25 | my $logger = Log::Any->get_logger(); | ||
| 26 | |||
| 27 | sub backup { | ||
| 28 | my ( $self, $to, %options ) = @_; | ||
| 29 | |||
| 30 | croak("not connected backup") | ||
| 31 | unless ( $self->{connection} | ||
| 32 | || ( !$options{live} && defined( $self->{backup} ) ) ); | ||
| 33 | |||
| 34 | $logger->info('backing up database'); | ||
| 35 | if ( $options{ignore_all_views} ) { | ||
| 36 | my @views = (); | ||
| 37 | $self->query( | ||
| 38 | { sql => 'select table_name ' | ||
| 39 | . 'from information_schema.tables ' | ||
| 40 | . 'where table_type like \'VIEW\' and table_schema = ?', | ||
| 41 | parameters => [ $self->{schema} ] | ||
| 42 | }, | ||
| 43 | sub { | ||
| 44 | push( @views, $_[0] ); | ||
| 45 | } | ||
| 46 | ); | ||
| 47 | |||
| 48 | if (@views) { | ||
| 49 | if ( !$options{ignore_tables} ) { | ||
| 50 | $options{ignore_tables} = \@views; | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | push( @{ $options{ignore_tables} }, @views ); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | my $command = $self->_dump_command(%options); | ||
| 59 | |||
| 60 | if ( eval { $to->isa('Footprintless::Plugin::Database::MySqlProvider') } ) { | ||
| 61 | $to->restore( | ||
| 62 | $self, | ||
| 63 | 'clean' => $options{clean}, | ||
| 64 | 'backup' => { | ||
| 65 | command => $command, | ||
| 66 | options => \%options | ||
| 67 | } | ||
| 68 | ); | ||
| 69 | } | ||
| 70 | elsif ( ref($to) eq 'CODE' ) { | ||
| 71 | $self->_run_or_die( | ||
| 72 | $command, | ||
| 73 | { out_callback => $to, | ||
| 74 | err_handle => \*STDERR | ||
| 75 | } | ||
| 76 | ); | ||
| 77 | } | ||
| 78 | elsif ( ref($to) eq 'GLOB' ) { | ||
| 79 | $logger->debug('writing to GLOB'); | ||
| 80 | $self->_run_or_die( | ||
| 81 | $command, | ||
| 82 | { out_handle => $to, | ||
| 83 | err_handle => \*STDERR | ||
| 84 | } | ||
| 85 | ); | ||
| 86 | } | ||
| 87 | else { | ||
| 88 | $logger->debugf( 'writing to file %s', $to ); | ||
| 89 | open( my $filehandle, '>', $to ); | ||
| 90 | $self->_run_or_die( | ||
| 91 | $command, | ||
| 92 | { out_handle => $filehandle, | ||
| 93 | err_handle => \*STDERR | ||
| 94 | } | ||
| 95 | ); | ||
| 96 | close($filehandle); | ||
| 97 | } | ||
| 98 | |||
| 99 | $logger->info('finished backing up database'); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _client_command { | ||
| 103 | my ( $self, $command, @additional_options ) = @_; | ||
| 104 | |||
| 105 | my $cnf = $self->_cnf(); | ||
| 106 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 107 | |||
| 108 | return join( ' ', | ||
| 109 | $command, "--defaults-file=$cnf", '--default-character-set=utf8', | ||
| 110 | '--max_allowed_packet=512M', "-h $hostname", "-P $port", @additional_options, | ||
| 111 | $self->{schema} ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub client { | ||
| 115 | my ( $self, %options ) = @_; | ||
| 116 | |||
| 117 | my $in_file; | ||
| 118 | eval { | ||
| 119 | my $in_handle = delete( $options{in_handle} ); | ||
| 120 | if ( $options{in_file} ) { | ||
| 121 | open( $in_file, '<', delete( $options{in_file} ) ) | ||
| 122 | || croak("invalid in_file: $!"); | ||
| 123 | } | ||
| 124 | if ( $options{in_string} ) { | ||
| 125 | my $string = delete( $options{in_string} ); | ||
| 126 | open( $in_file, '<', \$string ) | ||
| 127 | || croak("invalid in_string: $!"); | ||
| 128 | } | ||
| 129 | $self->_connect_tunnel(); | ||
| 130 | |||
| 131 | my $command = $self->_client_command( 'mysql', @{ $options{client_options} } ); | ||
| 132 | $self->_run_or_die( | ||
| 133 | $command, | ||
| 134 | { in_handle => $in_file || $in_handle || \*STDIN, | ||
| 135 | out_handle => \*STDOUT, | ||
| 136 | err_handle => \*STDERR | ||
| 137 | } | ||
| 138 | ); | ||
| 139 | }; | ||
| 140 | my $error = $@; | ||
| 141 | $self->disconnect(); | ||
| 142 | if ($in_file) { | ||
| 143 | close($in_file); | ||
| 144 | } | ||
| 145 | |||
| 146 | croak($error) if ($error); | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _cnf { | ||
| 150 | my ($self) = @_; | ||
| 151 | |||
| 152 | if ( !$self->{cnf} ) { | ||
| 153 | File::Temp->safe_level(File::Temp::HIGH); | ||
| 154 | my $cnf = File::Temp->new(); | ||
| 155 | if ( !chmod( 0600, $cnf ) ) { | ||
| 156 | croak("unable to create secure temp file"); | ||
| 157 | } | ||
| 158 | print( $cnf | ||
| 159 | join( "\n", '[client]', "user=$self->{username}", "password=$self->{password}" ) | ||
| 160 | ); | ||
| 161 | close($cnf); | ||
| 162 | |||
| 163 | $self->{cnf} = $cnf; | ||
| 164 | } | ||
| 165 | |||
| 166 | return $self->{cnf}; | ||
| 167 | } | ||
| 168 | |||
| 169 | sub _connection_string { | ||
| 170 | my ($self) = @_; | ||
| 171 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 172 | return join( '', | ||
| 173 | 'DBI:mysql:', 'database=', $self->{schema}, ';', 'host=', | ||
| 174 | $hostname, ';', 'port=', $port ); | ||
| 175 | } | ||
| 176 | |||
| 177 | sub _dump_command { | ||
| 178 | my ( $self, %options ) = @_; | ||
| 179 | |||
| 180 | my $dump_command; | ||
| 181 | if ( $options{live} || !defined( $self->{backup} ) ) { | ||
| 182 | my @only_tables = (); | ||
| 183 | my $and_in_tables = ''; | ||
| 184 | my @ignore_tables = (); | ||
| 185 | my $and_not_in_tables = ''; | ||
| 186 | my @parameters; | ||
| 187 | if ( defined( $options{only_tables} ) && scalar( @{ $options{only_tables} } ) ) { | ||
| 188 | my @in_placeholders = (); | ||
| 189 | foreach my $table ( @{ $options{only_tables} } ) { | ||
| 190 | push( @in_placeholders, '?' ); | ||
| 191 | push( @only_tables, $table ); | ||
| 192 | push( @parameters, $table ); | ||
| 193 | } | ||
| 194 | $and_in_tables = ' AND table_name IN (' . join( ',', @in_placeholders ) . ')'; | ||
| 195 | } | ||
| 196 | elsif ( defined( $options{ignore_tables} ) && scalar( @{ $options{ignore_tables} } ) ) { | ||
| 197 | my @not_in_placeholders = (); | ||
| 198 | foreach my $table ( @{ $options{ignore_tables} } ) { | ||
| 199 | push( @not_in_placeholders, '?' ); | ||
| 200 | push( @ignore_tables, "--ignore-table=$self->{schema}.$table" ); | ||
| 201 | push( @parameters, $table ); | ||
| 202 | } | ||
| 203 | $and_not_in_tables = | ||
| 204 | ' AND table_name NOT IN (' . join( ',', @not_in_placeholders ) . ')'; | ||
| 205 | } | ||
| 206 | |||
| 207 | my $cnf = $self->_cnf(); | ||
| 208 | my ( $hostname, $port ) = $self->_hostname_port(); | ||
| 209 | |||
| 210 | my $size = | ||
| 211 | $options{where} | ||
| 212 | ? 0 | ||
| 213 | : $self->query_for_scalar( | ||
| 214 | { sql => 'SELECT SUM(data_length + index_length) AS bytes ' | ||
| 215 | . 'FROM information_schema.TABLES ' | ||
| 216 | . 'WHERE table_schema = ? ' | ||
| 217 | . $and_in_tables | ||
| 218 | . $and_not_in_tables, | ||
| 219 | parameters => [ $self->{schema}, @parameters ] | ||
| 220 | } | ||
| 221 | ); | ||
| 222 | |||
| 223 | my $client_command = $self->_client_command( | ||
| 224 | 'mysqldump', | ||
| 225 | ( $options{where} ? "--where=\"$options{where}\"" : () ), | ||
| 226 | ( $options{single_transaction} ? "--single-transaction" : () ), | ||
| 227 | @ignore_tables | ||
| 228 | ); | ||
| 229 | $dump_command = pipe_command( | ||
| 230 | join( ' ', $client_command, @only_tables ), | ||
| 231 | "pv -f " . ( $size ? "-s $size" : "-b" ) | ||
| 232 | ); | ||
| 233 | } | ||
| 234 | else { | ||
| 235 | my $backup_base = $self->{backup}{base}; | ||
| 236 | my $command_options = | ||
| 237 | $self->{backup}{command_options} | ||
| 238 | ? $self->{factory}->command_options( %{ $self->{backup}{command_options} } ) | ||
| 239 | : $self->{factory}->command_options( hostname => $self->{hostname} ); | ||
| 240 | |||
| 241 | # find backup file | ||
| 242 | require Date::Parse; | ||
| 243 | my $listing = $self->_run_or_die( command( "ls $backup_base", $command_options ), | ||
| 244 | { out_buffer => 1, timeout => 10 } ); | ||
| 245 | my @folders = split( /\s+/, $listing ); | ||
| 246 | my $most_recent; | ||
| 247 | my $most_recent_time = 0; | ||
| 248 | foreach my $folder (@folders) { | ||
| 249 | my $folder_time = Date::Parse::str2time($folder); | ||
| 250 | if ( $folder_time > $most_recent_time ) { | ||
| 251 | $most_recent = $folder; | ||
| 252 | $most_recent_time = $folder_time; | ||
| 253 | } | ||
| 254 | } | ||
| 255 | my $backup_file = "$backup_base/$most_recent/$self->{schema}.sql.gz"; | ||
| 256 | |||
| 257 | # get size of file for progress meter | ||
| 258 | my $size = int( | ||
| 259 | $self->_run_or_die( | ||
| 260 | command( "stat -c \"\%s\" $backup_file", $command_options ), | ||
| 261 | { out_buffer => 1, timeout => 10 } | ||
| 262 | ) | ||
| 263 | ); | ||
| 264 | |||
| 265 | # build the command | ||
| 266 | $dump_command = pipe_command( command( "cat $backup_file", $command_options ), | ||
| 267 | "pv -f -s $size", "gunzip" ); | ||
| 268 | } | ||
| 269 | |||
| 270 | return pipe_command( | ||
| 271 | $dump_command, | ||
| 272 | sed_command( | ||
| 273 | { replace_map => { | ||
| 274 | 'VIEW `[^`]`' => 'VIEW ', | ||
| 275 | 'DEFINER=[^*]*\\*' => '\\*' | ||
| 276 | } | ||
| 277 | } | ||
| 278 | ), | ||
| 279 | ( $options{pipeline} ? @{ $options{pipeline} } : () ) | ||
| 280 | ); | ||
| 281 | } | ||
| 282 | |||
| 283 | sub _init { | ||
| 284 | my ( $self, %options ) = @_; | ||
| 285 | $self->Footprintless::Plugin::Database::AbstractProvider::_init(%options); | ||
| 286 | |||
| 287 | $self->{port} = 3306 unless ( $self->{port} ); | ||
| 288 | |||
| 289 | return $self; | ||
| 290 | } | ||
| 291 | |||
| 292 | sub restore { | ||
| 293 | my ( $self, $from, %options ) = @_; | ||
| 294 | |||
| 295 | croak('not connected backup') unless ( $self->{connection} ); | ||
| 296 | |||
| 297 | $logger->infof( 'restoring database on %s', $self->{hostname} ); | ||
| 298 | |||
| 299 | if ( $options{clean} ) { | ||
| 300 | my @tables = (); | ||
| 301 | my @views = (); | ||
| 302 | $self->query( | ||
| 303 | { sql => 'select table_name, table_type ' | ||
| 304 | . 'from information_schema.tables ' | ||
| 305 | . 'where table_schema = ?', | ||
| 306 | parameters => [ $self->{schema} ] | ||
| 307 | }, | ||
| 308 | sub { | ||
| 309 | my ( $table_name, $table_type ) = @_; | ||
| 310 | if ( uc($table_type) eq 'VIEW' ) { | ||
| 311 | push( @views, $table_name ); | ||
| 312 | } | ||
| 313 | else { | ||
| 314 | push( @tables, $table_name ); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | ); | ||
| 318 | |||
| 319 | if (@views) { | ||
| 320 | my $drop_views_sql = 'drop view `' . join( '`,`', @views ) . '`'; | ||
| 321 | $logger->infof( 'dropping views: %s', $drop_views_sql ); | ||
| 322 | $self->execute( { sql => $drop_views_sql } ); | ||
| 323 | } | ||
| 324 | |||
| 325 | if (@tables) { | ||
| 326 | my $drop_tables_sql = 'drop table `' . join( '`,`', @tables ) . '`'; | ||
| 327 | $logger->infof( 'dropping tables: %s', $drop_tables_sql ); | ||
| 328 | $self->execute( { sql => $drop_tables_sql } ); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | my $command = $self->_client_command('mysql'); | ||
| 333 | |||
| 334 | if ( eval { $from->isa('Footprintless::Plugin::Database::MySqlProvider') } ) { | ||
| 335 | $logger->debug('Restoring from another mysql instance'); | ||
| 336 | $self->_run_or_die( pipe_command( $options{backup}{command}, $command ), | ||
| 337 | { err_handle => \*STDERR } ); | ||
| 338 | |||
| 339 | $options{post_restore} = $options{backup}{options}{post_restore} | ||
| 340 | if ( $options{backup}{options} ); | ||
| 341 | } | ||
| 342 | elsif ( ref($from) eq 'HASH' ) { | ||
| 343 | $logger->debug("Restoring from HASH"); | ||
| 344 | $self->_run_or_die( pipe_command( $from->{command}, $command ), | ||
| 345 | { err_handle => \*STDERR } ); | ||
| 346 | |||
| 347 | $options{post_restore} = $from->{options}{post_restore} | ||
| 348 | if ( $from->{options} ); | ||
| 349 | } | ||
| 350 | elsif ( ref($from) eq 'GLOB' ) { | ||
| 351 | $logger->debug('Restoring from GLOB'); | ||
| 352 | $self->_run_or_die( | ||
| 353 | pipe_command( "pv -b", $command ), | ||
| 354 | { in_handle => $from, | ||
| 355 | err_handle => \*STDERR | ||
| 356 | } | ||
| 357 | ); | ||
| 358 | } | ||
| 359 | else { | ||
| 360 | $logger->debugf( 'Restoring from %s', $from ); | ||
| 361 | $self->_run_or_die( pipe_command( "pv $from", $command ), { err_handle => \*STDERR } ); | ||
| 362 | } | ||
| 363 | |||
| 364 | if ( $options{post_restore} ) { | ||
| 365 | $self->_run_or_die( pipe_command( "cat \"$options{post_restore}\"", $command ) ); | ||
| 366 | } | ||
| 367 | |||
| 368 | $logger->info("finished restoring database"); | ||
| 369 | } | ||
| 370 | |||
| 371 | 1; | ||
| 372 | |||
| 373 | __END__ |