| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Copyright (C) 2011 DeNA Co.,Ltd. |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 2 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# This program is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with this program; if not, write to the Free Software |
| 17 |
# Foundation, Inc., |
| 18 |
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 19 |
|
| 20 |
use strict; |
| 21 |
use warnings FATAL => 'all'; |
| 22 |
|
| 23 |
use Carp qw(croak); |
| 24 |
use MHA::BinlogManager; |
| 25 |
use MHA::BinlogPosFindManager; |
| 26 |
use MHA::BinlogPosFinder; |
| 27 |
use MHA::BinlogPosFinderXid; |
| 28 |
use MHA::BinlogPosFinderElp; |
| 29 |
use MHA::NodeConst; |
| 30 |
use MHA::NodeUtil; |
| 31 |
use MHA::SlaveUtil; |
| 32 |
use Getopt::Long; |
| 33 |
use Pod::Usage; |
| 34 |
use File::Basename; |
| 35 |
use File::Copy; |
| 36 |
use Sys::Hostname; |
| 37 |
|
| 38 |
GetOptions( |
| 39 |
\my %opt, qw/ |
| 40 |
help |
| 41 |
version |
| 42 |
command=s |
| 43 |
datadir=s |
| 44 |
relay_log_info=s |
| 45 |
relay_dir=s |
| 46 |
current_relay_log=s |
| 47 |
scp_user=s |
| 48 |
scp_host=s |
| 49 |
scp_port=i |
| 50 |
ssh_options=s |
| 51 |
slave_user=s |
| 52 |
slave_pass=s |
| 53 |
slave_host=s |
| 54 |
slave_ip=s |
| 55 |
slave_port=i |
| 56 |
master_server_id=i |
| 57 |
server_id=i |
| 58 |
latest_mlf=s |
| 59 |
latest_rmlp=i |
| 60 |
target_mlf=s |
| 61 |
target_rmlp=i |
| 62 |
target_version=s |
| 63 |
workdir=s |
| 64 |
diff_file_readtolatest=s |
| 65 |
apply_files=s |
| 66 |
timestamp=s |
| 67 |
handle_raw_binlog=i |
| 68 |
disable_log_bin=i |
| 69 |
no_apply |
| 70 |
manager_version=s |
| 71 |
client_bindir=s |
| 72 |
client_libdir=s |
| 73 |
debug |
| 74 |
/, |
| 75 |
) or pod2usage(1); |
| 76 |
$opt{command} ||= ""; |
| 77 |
$opt{scp_user} ||= "root"; |
| 78 |
$opt{scp_port} ||= 22; |
| 79 |
$opt{slave_user} ||= "root"; |
| 80 |
$opt{slave_pass} ||= ""; |
| 81 |
|
| 82 |
# for printing and authentication |
| 83 |
$opt{slave_host} ||= "127.0.0.1"; |
| 84 |
$opt{slave_port} ||= 3306; |
| 85 |
$opt{target_rmlp} ||= 0; |
| 86 |
$opt{workdir} ||= "/var/tmp"; |
| 87 |
$opt{timestamp} ||= "0"; |
| 88 |
$opt{handle_raw_binlog} = 1 if ( !defined( $opt{handle_raw_binlog} ) ); |
| 89 |
$opt{disable_log_bin} = 0 if ( !defined( $opt{disable_log_bin} ) ); |
| 90 |
|
| 91 |
# for compatibility |
| 92 |
unless ( $opt{datadir} ) { |
| 93 |
$opt{datadir} = $opt{relay_dir}; |
| 94 |
} |
| 95 |
|
| 96 |
if ( $opt{ssh_options} ) { |
| 97 |
$MHA::NodeConst::SSH_OPT_ALIVE = $opt{ssh_options}; |
| 98 |
} |
| 99 |
|
| 100 |
my $_mysql = MHA::NodeUtil::client_cli_prefix( "mysql", $opt{client_bindir}, |
| 101 |
$opt{client_libdir} ); |
| 102 |
my $_mysqlbinlog = |
| 103 |
MHA::NodeUtil::client_cli_prefix( "mysqlbinlog", $opt{client_bindir}, |
| 104 |
$opt{client_libdir} ); |
| 105 |
|
| 106 |
my $_binlog_manager; |
| 107 |
my $_find_logname_only = 0; |
| 108 |
my $_escaped_slave_user; |
| 109 |
my $_escaped_slave_pass; |
| 110 |
|
| 111 |
$| = 1; |
| 112 |
|
| 113 |
if ( $opt{help} ) { |
| 114 |
pod2usage(0); |
| 115 |
} |
| 116 |
if ( $opt{version} ) { |
| 117 |
print "apply_diff_relay_logs version $MHA::NodeConst::VERSION.\n"; |
| 118 |
exit 0; |
| 119 |
} |
| 120 |
|
| 121 |
exit &main(); |
| 122 |
|
| 123 |
sub escape_for_mysql { |
| 124 |
my $str = shift; |
| 125 |
unless ($str) { |
| 126 |
return $str; |
| 127 |
} |
| 128 |
$str = MHA::NodeUtil::unescape_for_shell($str); |
| 129 |
$str = MHA::NodeUtil::escape_for_mysql_command($str); |
| 130 |
return $str; |
| 131 |
} |
| 132 |
|
| 133 |
sub escape_mysql_account { |
| 134 |
$_escaped_slave_user = escape_for_mysql( $opt{slave_user} ); |
| 135 |
$_escaped_slave_pass = escape_for_mysql( $opt{slave_pass} ); |
| 136 |
} |
| 137 |
|
| 138 |
sub fast_find_pos() { |
| 139 |
my %status = (); |
| 140 |
if ( $opt{target_mlf} eq $opt{latest_mlf} ) { |
| 141 |
my $offset = $opt{latest_rmlp} - $opt{target_rmlp}; |
| 142 |
my $filesize = -s "$_binlog_manager->{dir}/$_binlog_manager->{end_log}"; |
| 143 |
my $start_rlp = $filesize - $offset; |
| 144 |
return if ( $start_rlp <= 0 ); |
| 145 |
|
| 146 |
# Checking binlog is readable from this position |
| 147 |
unless ( |
| 148 |
system( |
| 149 |
"$_mysqlbinlog --start-position=$start_rlp --stop-position=$start_rlp $_binlog_manager->{dir}/$_binlog_manager->{end_log} > /dev/null" |
| 150 |
) |
| 151 |
) |
| 152 |
{ |
| 153 |
$status{status} = 0; |
| 154 |
$status{start_rlf} = $_binlog_manager->{end_log}; |
| 155 |
$status{start_rlp} = $start_rlp; |
| 156 |
return %status; |
| 157 |
} |
| 158 |
} |
| 159 |
else { |
| 160 |
return; |
| 161 |
} |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
sub generate_diff_relay_log($$$) { |
| 166 |
my $start_relay_log = shift; |
| 167 |
my $start_relay_pos = shift; |
| 168 |
my $out = shift; |
| 169 |
return $_binlog_manager->concat_all_binlogs_from( $start_relay_log, |
| 170 |
$start_relay_pos, $out ); |
| 171 |
} |
| 172 |
|
| 173 |
sub find_starting_relay_log { |
| 174 |
my %start; |
| 175 |
unless ( $opt{latest_mlf} ) { |
| 176 |
croak "--latest_mlf (my latest master log file name) must be set.\n"; |
| 177 |
} |
| 178 |
unless ( $opt{target_mlf} ) { |
| 179 |
croak "--target_mlf (target master log file name) must be set.\n"; |
| 180 |
} |
| 181 |
unless ( $opt{target_rmlp} ) { |
| 182 |
croak "--target_rmlp (target master read log pos) must be set.\n"; |
| 183 |
} |
| 184 |
unless ( $opt{latest_rmlp} ) { |
| 185 |
croak "--latest_rmlp (latest master read log pos) must be set.\n"; |
| 186 |
} |
| 187 |
unless ( $opt{server_id} ) { |
| 188 |
croak "--server_id (my server id) must be set.\n"; |
| 189 |
} |
| 190 |
|
| 191 |
if ( %start = fast_find_pos() ) { |
| 192 |
print " Fast relay log position search succeeded.\n"; |
| 193 |
} |
| 194 |
else { |
| 195 |
print |
| 196 |
" Fast relay log position search failed. Reading relay logs to find..\n"; |
| 197 |
%start = new MHA::BinlogPosFindManager( |
| 198 |
binlog_manager => $_binlog_manager, |
| 199 |
server_id => $opt{server_id}, |
| 200 |
target_mlf => $opt{target_mlf}, |
| 201 |
target_rmlp => $opt{target_rmlp}, |
| 202 |
find_logname_only => $_find_logname_only, |
| 203 |
debug => $opt{debug}, |
| 204 |
)->find_target_relay_log( $_binlog_manager->{end_log}, |
| 205 |
$opt{latest_mlf}, 0 ); |
| 206 |
|
| 207 |
if ( !defined( $start{status} ) ) { |
| 208 |
$start{status} = $MHA::NodeConst::Relay_Log_Not_Found; |
| 209 |
return %start; |
| 210 |
} |
| 211 |
return %start if ( $start{status} != 0 || $_find_logname_only ); |
| 212 |
|
| 213 |
if ( !$start{start_rlf} || !defined( $start{start_rlp} ) ) { |
| 214 |
print "FATAL: getting start relay file and/or position failed.\n"; |
| 215 |
$start{status} = $MHA::NodeConst::Relay_Log_Not_Found; |
| 216 |
return %start; |
| 217 |
} |
| 218 |
} |
| 219 |
print |
| 220 |
" Target relay log file/position found. start_file:$start{start_rlf}, start_pos:$start{start_rlp}.\n"; |
| 221 |
|
| 222 |
return %start; |
| 223 |
} |
| 224 |
|
| 225 |
sub do_generate($$$) { |
| 226 |
my $start_relay_log = shift; |
| 227 |
my $start_relay_pos = shift; |
| 228 |
my $out = shift; |
| 229 |
|
| 230 |
my $rc = generate_diff_relay_log( $start_relay_log, $start_relay_pos, $out ); |
| 231 |
print " Generating diff relay log succeeded. Saved at $out .\n" |
| 232 |
if ( $rc == 0 ); |
| 233 |
return $rc; |
| 234 |
} |
| 235 |
|
| 236 |
sub use_binary_mode() { |
| 237 |
my $v = `$_mysql --version`; |
| 238 |
my $mysqlclient_version; |
| 239 |
chomp($v); |
| 240 |
if ( $v =~ /Distrib (\d+\.\d+\.\d+)/ ) { |
| 241 |
$mysqlclient_version = $1; |
| 242 |
} |
| 243 |
if ( $mysqlclient_version |
| 244 |
&& MHA::NodeUtil::mysql_version_ge( $mysqlclient_version, "5.6.3" ) ) |
| 245 |
{ |
| 246 |
print |
| 247 |
"MySQL client version is $mysqlclient_version. Using --binary-mode.\n"; |
| 248 |
return "--binary-mode"; |
| 249 |
} |
| 250 |
return ""; |
| 251 |
} |
| 252 |
|
| 253 |
sub apply_diff($$) { |
| 254 |
my $apply_files = shift; |
| 255 |
my $err_file = shift; |
| 256 |
my $command = ""; |
| 257 |
|
| 258 |
my @diffs = split( /,/, $apply_files ); |
| 259 |
if ( $#diffs < 0 ) { |
| 260 |
croak "ERROR: --apply_files must be set.\n"; |
| 261 |
} |
| 262 |
foreach my $file (@diffs) { |
| 263 |
if ( !-f $file ) { |
| 264 |
croak "ERROR: $file not found!\n"; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
if ( $opt{handle_raw_binlog} ) { |
| 269 |
if ( $#diffs == 0 ) { |
| 270 |
$command = "$_mysqlbinlog $apply_files "; |
| 271 |
} |
| 272 |
else { |
| 273 |
my $out = |
| 274 |
"$opt{workdir}/total_binlog_for_$opt{slave_host}_$opt{slave_port}.$opt{timestamp}.binlog"; |
| 275 |
MHA::NodeUtil::drop_file_if($out); |
| 276 |
$_binlog_manager->concat_generated_binlogs( \@diffs, $out ); |
| 277 |
print "All apply target binary logs are concatinated at $out .\n"; |
| 278 |
$command = "$_mysqlbinlog $out "; |
| 279 |
} |
| 280 |
$command .= $_binlog_manager->get_apply_arg(); |
| 281 |
} |
| 282 |
else { |
| 283 |
$command = "cat "; |
| 284 |
foreach my $file (@diffs) { |
| 285 |
$command .= "$file "; |
| 286 |
} |
| 287 |
} |
| 288 |
return 0 if ( $opt{no_apply} ); |
| 289 |
|
| 290 |
my $binary_mode = use_binary_mode(); |
| 291 |
escape_mysql_account(); |
| 292 |
|
| 293 |
my ($super_read_only_enabled, $dbh) = |
| 294 |
MHA::SlaveUtil::check_if_super_read_only($opt{slave_host}, $opt{slave_ip}, $opt{slave_port}, $opt{slave_user}, $opt{slave_pass}); |
| 295 |
|
| 296 |
if ($super_read_only_enabled) { |
| 297 |
MHA::SlaveUtil::disable_super_read_only($dbh); |
| 298 |
} |
| 299 |
|
| 300 |
$command .= |
| 301 |
" | $_mysql $binary_mode --user=$_escaped_slave_user --password=$_escaped_slave_pass --host=$opt{slave_ip} --port=$opt{slave_port} -vvv --unbuffered > $err_file 2>&1"; |
| 302 |
|
| 303 |
# applying relay log |
| 304 |
print |
| 305 |
"Applying differential binary/relay log files $apply_files on $opt{slave_host}:$opt{slave_port}. This may take long time...\n"; |
| 306 |
|
| 307 |
# applying log failed |
| 308 |
if ( my $rc = system($command) ) { |
| 309 |
my ( $high, $low ) = MHA::NodeUtil::system_rc($rc); |
| 310 |
print "FATAL: applying log files failed with rc $high:$low!\n"; |
| 311 |
printf( "Error logs from %s:%s (the last 200 lines)..\n", |
| 312 |
hostname(), $err_file ); |
| 313 |
system("tail -200 $err_file"); |
| 314 |
|
| 315 |
if ($super_read_only_enabled) { |
| 316 |
MHA::SlaveUtil::enable_super_read_only($dbh); |
| 317 |
} |
| 318 |
return $MHA::NodeConst::Applying_SQL_File_Failed; |
| 319 |
} |
| 320 |
if ($super_read_only_enabled) { |
| 321 |
MHA::SlaveUtil::enable_super_read_only($dbh); |
| 322 |
} |
| 323 |
return 0; |
| 324 |
} |
| 325 |
|
| 326 |
sub apply { |
| 327 |
unless ( $opt{apply_files} ) { |
| 328 |
croak "ERROR: --apply_files must be set.\n"; |
| 329 |
} |
| 330 |
my $date = $opt{timestamp}; |
| 331 |
unless ( $opt{timestamp} ) { |
| 332 |
my ( $year, $mon, @time ) = reverse( (localtime)[ 0 .. 5 ] ); |
| 333 |
$date = sprintf '%04d%02d%02d%02d%02d%02d', $year + 1900, $mon + 1, @time; |
| 334 |
} |
| 335 |
return apply_diff( $opt{apply_files}, |
| 336 |
"$opt{workdir}/relay_log_apply_for_$opt{slave_host}_$opt{slave_port}_$date" |
| 337 |
. "_err.log" ); |
| 338 |
} |
| 339 |
|
| 340 |
sub check_set_relay_dir_endlog { |
| 341 |
if ( $opt{relay_dir} && $opt{current_relay_log} ) { |
| 342 |
$_binlog_manager->init_from_dir_file( $opt{relay_dir}, |
| 343 |
$opt{current_relay_log} ); |
| 344 |
} |
| 345 |
elsif ( $opt{relay_log_info} ) { |
| 346 |
print " Opening $opt{relay_log_info} ..."; |
| 347 |
$_binlog_manager->init_from_relay_log_info( $opt{relay_log_info}, |
| 348 |
$opt{datadir} ); |
| 349 |
print " ok.\n"; |
| 350 |
} |
| 351 |
else { |
| 352 |
croak |
| 353 |
"ERROR: Either (--relay_dir=s, --current_relay_log=s) or (--relay_log_info=s) must be set.\n"; |
| 354 |
} |
| 355 |
|
| 356 |
print |
| 357 |
" Relay log found at $_binlog_manager->{dir}, up to $_binlog_manager->{end_log}\n"; |
| 358 |
} |
| 359 |
|
| 360 |
sub check_slave_host() { |
| 361 |
croak "--slave_host must be set.\n" unless ( $opt{slave_host} ); |
| 362 |
unless ( $opt{slave_ip} ) { |
| 363 |
$opt{slave_ip} = MHA::NodeUtil::get_ip( $opt{slave_host} ); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
sub check() { |
| 368 |
my $rc; |
| 369 |
print " Checking slave recovery environment settings..\n"; |
| 370 |
|
| 371 |
if ( $rc = system("echo 100 | md5sum > /dev/null") ) { |
| 372 |
my ( $high, $low ) = MHA::NodeUtil::system_rc($rc); |
| 373 |
croak "md5sum failed with rc $high:$low!\n"; |
| 374 |
} |
| 375 |
|
| 376 |
check_set_relay_dir_endlog(); |
| 377 |
print |
| 378 |
" Temporary relay log file is $_binlog_manager->{dir}/$_binlog_manager->{end_log}\n"; |
| 379 |
|
| 380 |
print " Checking if super_read_only is defined and turned on.."; |
| 381 |
my ($super_read_only_enabled, $dbh) = |
| 382 |
MHA::SlaveUtil::check_if_super_read_only($opt{slave_host}, $opt{slave_ip}, $opt{slave_port}, $opt{slave_user}, $opt{slave_pass}); |
| 383 |
if ($super_read_only_enabled) { |
| 384 |
MHA::SlaveUtil::disable_super_read_only($dbh); |
| 385 |
} else { |
| 386 |
print " not present or turned off, ignoring.\n"; |
| 387 |
} |
| 388 |
|
| 389 |
print " Testing mysql connection and privileges..\n"; |
| 390 |
check_slave_host(); |
| 391 |
escape_mysql_account(); |
| 392 |
|
| 393 |
if ( |
| 394 |
$rc = system( |
| 395 |
"$_mysql --user=$_escaped_slave_user --password=$_escaped_slave_pass --host=$opt{slave_ip} --port=$opt{slave_port} -e \"set sql_log_bin=0; create table if not exists mysql.apply_diff_relay_logs_test(id int); insert into mysql.apply_diff_relay_logs_test values(1); update mysql.apply_diff_relay_logs_test set id=id+1 where id=1; delete from mysql.apply_diff_relay_logs_test; drop table mysql.apply_diff_relay_logs_test;\"" |
| 396 |
) |
| 397 |
) |
| 398 |
{ |
| 399 |
if ($super_read_only_enabled) { |
| 400 |
MHA::SlaveUtil::enable_super_read_only($dbh); |
| 401 |
} |
| 402 |
|
| 403 |
my ( $high, $low ) = MHA::NodeUtil::system_rc($rc); |
| 404 |
croak "mysql command failed with rc $high:$low!\n"; |
| 405 |
} |
| 406 |
|
| 407 |
print " done.\n"; |
| 408 |
|
| 409 |
if ($super_read_only_enabled) { |
| 410 |
print " Enabling super_read_only again.."; |
| 411 |
MHA::SlaveUtil::enable_super_read_only($dbh); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
print " Testing mysqlbinlog output.."; |
| 416 |
my $workfile = |
| 417 |
"$opt{workdir}/slavediff_tmp_$opt{slave_host}_$opt{slave_port}.log"; |
| 418 |
|
| 419 |
if ( |
| 420 |
$rc = system( |
| 421 |
"$_mysqlbinlog $_binlog_manager->{dir}/$_binlog_manager->{end_log} --start-position=4 --stop-position=4 > $workfile" |
| 422 |
) |
| 423 |
) |
| 424 |
{ |
| 425 |
my ( $high, $low ) = MHA::NodeUtil::system_rc($rc); |
| 426 |
croak "mysqlbinlog failed with rc $high:$low!\n"; |
| 427 |
} |
| 428 |
print " done.\n"; |
| 429 |
print " Cleaning up test file(s).."; |
| 430 |
MHA::NodeUtil::drop_file_if($workfile); |
| 431 |
print " done.\n"; |
| 432 |
|
| 433 |
if ( !$opt{scp_user} || !$opt{scp_host} ) { |
| 434 |
return 0; |
| 435 |
} |
| 436 |
print |
| 437 |
" Testing connecting to remote slave $opt{scp_host}($opt{scp_port}) via ssh.."; |
| 438 |
my $ssh_user_host = $opt{scp_user} . '@' . $opt{scp_host}; |
| 439 |
if ( |
| 440 |
$rc = system( |
| 441 |
"ssh $MHA::NodeConst::SSH_OPT_ALIVE -p $opt{scp_port} $ssh_user_host \"exit 0 \"" |
| 442 |
) |
| 443 |
) |
| 444 |
{ |
| 445 |
my ( $high, $low ) = MHA::NodeUtil::system_rc($rc); |
| 446 |
croak "ssh failed with rc $high:$low!\n"; |
| 447 |
} |
| 448 |
print " done.\n\n"; |
| 449 |
return 0; |
| 450 |
} |
| 451 |
|
| 452 |
sub generate_and_send { |
| 453 |
if ( $opt{command} eq "generate_and_send" ) { |
| 454 |
if ( !$opt{scp_user} || !$opt{scp_host} ) { |
| 455 |
croak "ERROR: --scp_user and --scp_host must be set.\n"; |
| 456 |
} |
| 457 |
} |
| 458 |
unless ( $opt{diff_file_readtolatest} ) { |
| 459 |
croak "ERROR: --diff_file_readtolatest=<Diff file name> must be set.\n"; |
| 460 |
} |
| 461 |
my $gen_file_dir = dirname( $opt{diff_file_readtolatest} ); |
| 462 |
MHA::NodeUtil::create_dir_if($gen_file_dir); |
| 463 |
MHA::NodeUtil::drop_file_if( $opt{diff_file_readtolatest} ); |
| 464 |
|
| 465 |
check_set_relay_dir_endlog(); |
| 466 |
my %status = find_starting_relay_log(); |
| 467 |
if ( $status{status} == $MHA::NodeConst::Target_Has_Received_All_Relay_Logs ) |
| 468 |
{ |
| 469 |
print "Target slave has received all relay logs.\n"; |
| 470 |
return 0; |
| 471 |
} |
| 472 |
croak if ( $status{status} != 0 || !$status{start_rlf} ); |
| 473 |
|
| 474 |
my $rc = |
| 475 |
do_generate( $status{start_rlf}, $status{start_rlp}, |
| 476 |
$opt{diff_file_readtolatest} ); |
| 477 |
return $rc if ( $rc || $opt{command} eq "generate" ); |
| 478 |
|
| 479 |
my $scp_user_host = $opt{scp_user} . '@' . $opt{scp_host}; |
| 480 |
if ( |
| 481 |
$rc = system( |
| 482 |
"ssh $MHA::NodeConst::SSH_OPT_ALIVE $scp_user_host -p $opt{scp_port} \"mkdir -p $gen_file_dir\"" |
| 483 |
) |
| 484 |
) |
| 485 |
{ |
| 486 |
my ( $high, $low ) = MHA::NodeUtil::system_rc($rc); |
| 487 |
croak |
| 488 |
"ssh $scp_user_host for mkdir $gen_file_dir failed with rc $high:$low!\n"; |
| 489 |
} |
| 490 |
if ( |
| 491 |
MHA::NodeUtil::file_copy( |
| 492 |
1, |
| 493 |
$opt{diff_file_readtolatest}, |
| 494 |
$opt{diff_file_readtolatest}, |
| 495 |
$opt{scp_user}, $opt{scp_host}, undef, $opt{scp_port} |
| 496 |
) |
| 497 |
) |
| 498 |
{ |
| 499 |
my $dsn_host = hostname() =~ m{:} ? '[' . hostname() . ']' : hostname(); |
| 500 |
croak "ERROR: scp %s:%s to %s(%d) failed!\n", $dsn_host, |
| 501 |
$opt{diff_file_readtolatest}, $scp_user_host, $opt{scp_port}; |
| 502 |
} |
| 503 |
else { |
| 504 |
my $dsn_host = hostname() =~ m{:} ? '[' . hostname() . ']' : hostname(); |
| 505 |
printf " scp %s:%s to %s(%d) succeeded.\n", $dsn_host, |
| 506 |
$opt{diff_file_readtolatest}, $scp_user_host, $opt{scp_port}; |
| 507 |
} |
| 508 |
return 0; |
| 509 |
|
| 510 |
} |
| 511 |
|
| 512 |
sub main() { |
| 513 |
my $exit_code = 1; |
| 514 |
eval { |
| 515 |
MHA::NodeUtil::check_manager_version( $opt{manager_version} ) |
| 516 |
if ( $opt{manager_version} ); |
| 517 |
|
| 518 |
$_binlog_manager = new MHA::BinlogManager( |
| 519 |
handle_raw_binlog => $opt{handle_raw_binlog}, |
| 520 |
disable_log_bin => $opt{disable_log_bin}, |
| 521 |
mysql_version => $opt{target_version}, |
| 522 |
client_bindir => $opt{client_bindir}, |
| 523 |
client_libdir => $opt{client_libdir}, |
| 524 |
debug => $opt{debug}, |
| 525 |
); |
| 526 |
if (!$opt{handle_raw_binlog} |
| 527 |
|| $opt{command} eq "test" |
| 528 |
|| $opt{command} eq "apply" ) |
| 529 |
{ |
| 530 |
croak "--target_version=<mysql_version> must be set.\n" |
| 531 |
unless ( $opt{target_version} ); |
| 532 |
$_binlog_manager->init_mysqlbinlog(); |
| 533 |
} |
| 534 |
MHA::NodeUtil::create_dir_if( $opt{workdir} ); |
| 535 |
|
| 536 |
if ( $opt{command} eq "test" ) { |
| 537 |
croak if ( check() ); |
| 538 |
} |
| 539 |
elsif ( $opt{command} eq "generate_and_send" |
| 540 |
|| $opt{command} eq "generate" ) |
| 541 |
{ |
| 542 |
croak if ( generate_and_send() ); |
| 543 |
} |
| 544 |
elsif ( $opt{command} eq "find" ) { |
| 545 |
$_find_logname_only = 1; |
| 546 |
check_set_relay_dir_endlog(); |
| 547 |
my %status = find_starting_relay_log(); |
| 548 |
croak if ( $status{status} != 0 || !$status{start_rlf} ); |
| 549 |
print "Target relay log FOUND!\n"; |
| 550 |
} |
| 551 |
elsif ( $opt{command} eq "apply" ) { |
| 552 |
check_slave_host(); |
| 553 |
$exit_code = apply(); |
| 554 |
croak if ($exit_code); |
| 555 |
print "Applying log files succeeded.\n"; |
| 556 |
} |
| 557 |
else { |
| 558 |
croak |
| 559 |
"Invalid command $opt{command}. Either [test|find|generate|generate_and_send|apply] command must be set.\n"; |
| 560 |
} |
| 561 |
$exit_code = 0; |
| 562 |
}; |
| 563 |
if ($@) { |
| 564 |
warn $@; |
| 565 |
} |
| 566 |
return $exit_code; |
| 567 |
} |
| 568 |
|
| 569 |
# ############################################################################ |
| 570 |
# Documentation |
| 571 |
# ############################################################################ |
| 572 |
|
| 573 |
=pod |
| 574 |
|
| 575 |
=head1 NAME |
| 576 |
|
| 577 |
apply_diff_relay_logs - Generating differential relay logs between the latest slave and target slave, and applying all binlog/relay log files. This command is automatically executed from MHA Manager on failover, and manual execution should not be needed normally. |
| 578 |
|
| 579 |
=head1 SYNOPSIS |
| 580 |
|
| 581 |
# For checking |
| 582 |
apply_diff_relay_logs --command=test --target_version=5.1.56 --relay_log_info=s --slave_user=s --slave_host=s --slave_ip=s --slave_port=i --workdir=s |
| 583 |
|
| 584 |
# For generating differential log events |
| 585 |
apply_diff_relay_logs --command=generate_and_send --target_version=5.1.56 --scp_user=s --scp_host=s --latest_mlf=s --target_mlf=s --target_rmlp=i --relay_log_info=s --server_id=i --diff_file_readtolatest=s --target_version=s --workdir=s --timestamp=s |
| 586 |
|
| 587 |
# For applying log files |
| 588 |
apply_diff_relay_logs --command=apply --target_version=5.1.56 --slave_user=s --slave_host=s --slave_ip=s --slave_port=i --apply_files=file1,file2.. --workdir=s --timestamp=s --slave_pass=xxx |
| 589 |
|
| 590 |
|