| 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::NodeConst; |
| 26 |
use MHA::NodeUtil; |
| 27 |
use File::Basename; |
| 28 |
use Getopt::Long; |
| 29 |
use Pod::Usage; |
| 30 |
|
| 31 |
GetOptions( |
| 32 |
\my %opt, qw/ |
| 33 |
help |
| 34 |
version |
| 35 |
command=s |
| 36 |
start_file=s |
| 37 |
start_pos=i |
| 38 |
binlog_prefix=s |
| 39 |
binlog_dir=s |
| 40 |
output_file=s |
| 41 |
datadir=s |
| 42 |
relay_log_info=s |
| 43 |
oldest_version=s |
| 44 |
handle_raw_binlog=i |
| 45 |
skip_filter=i |
| 46 |
disable_log_bin=i |
| 47 |
manager_version=s |
| 48 |
client_bindir=s |
| 49 |
client_libdir=s |
| 50 |
debug |
| 51 |
/, |
| 52 |
) or pod2usage(1); |
| 53 |
|
| 54 |
# for compatibility |
| 55 |
unless ( $opt{datadir} ) { |
| 56 |
$opt{datadir} = $opt{binlog_dir}; |
| 57 |
} |
| 58 |
$opt{binlog_dir} ||= "/var/lib/mysql"; |
| 59 |
$opt{handle_raw_binlog} = 1 if ( !defined( $opt{handle_raw_binlog} ) ); |
| 60 |
$opt{disable_log_bin} = 0 if ( !defined( $opt{disable_log_bin} ) ); |
| 61 |
$opt{skip_filter} = 0 if ( !defined( $opt{skip_filter} ) ); |
| 62 |
|
| 63 |
$| = 1; |
| 64 |
my $_binlog_manager; |
| 65 |
my $EVENT_NOT_EXIST = 10; |
| 66 |
exit &main(); |
| 67 |
|
| 68 |
sub main { |
| 69 |
my $exit_code = 1; |
| 70 |
eval { |
| 71 |
if ( $opt{help} ) { |
| 72 |
pod2usage(0); |
| 73 |
} |
| 74 |
if ( $opt{version} ) { |
| 75 |
print "save_binary_logs version $MHA::NodeConst::VERSION.\n"; |
| 76 |
exit 0; |
| 77 |
} |
| 78 |
MHA::NodeUtil::check_manager_version( $opt{manager_version} ) |
| 79 |
if ( $opt{manager_version} ); |
| 80 |
unless ( $opt{command} ) { |
| 81 |
croak "Set --command=[test|save]\n"; |
| 82 |
} |
| 83 |
if ( $opt{command} eq "test" ) { |
| 84 |
if ( !$opt{start_file} && !$opt{binlog_prefix} ) { |
| 85 |
croak |
| 86 |
"Set --start_file=<starting_binlog_filename> or --binlog_prefix=<binlog_file_prefix>\n"; |
| 87 |
} |
| 88 |
} |
| 89 |
elsif ( !$opt{start_file} ) { |
| 90 |
croak "Set --start_file=<starting_binlog_filename>\n"; |
| 91 |
} |
| 92 |
if ( !defined( $opt{start_pos} ) ) { |
| 93 |
croak "Set --start_pos=<starting_binlog_pos>\n"; |
| 94 |
} |
| 95 |
$_binlog_manager = new MHA::BinlogManager( |
| 96 |
handle_raw_binlog => $opt{handle_raw_binlog}, |
| 97 |
skip_filter => $opt{skip_filter}, |
| 98 |
disable_log_bin => $opt{disable_log_bin}, |
| 99 |
mysql_version => $opt{oldest_version}, |
| 100 |
client_bindir => $opt{client_bindir}, |
| 101 |
client_libdir => $opt{client_libdir}, |
| 102 |
debug => $opt{debug}, |
| 103 |
); |
| 104 |
$_binlog_manager->init_mysqlbinlog() unless ( $opt{handle_raw_binlog} ); |
| 105 |
|
| 106 |
# Finding target binary/relay log directory |
| 107 |
if ( $opt{relay_log_info} ) { |
| 108 |
$_binlog_manager->init_from_relay_log_info( $opt{relay_log_info}, |
| 109 |
$opt{datadir} ); |
| 110 |
} |
| 111 |
else { |
| 112 |
my $binlog_dir; |
| 113 |
if ( $opt{start_file} ) { |
| 114 |
$binlog_dir = |
| 115 |
MHA::BinlogManager::find_correct_binlog_dir( $opt{start_file}, |
| 116 |
$opt{binlog_dir} ); |
| 117 |
} |
| 118 |
else { |
| 119 |
( $binlog_dir, $opt{start_file} ) = |
| 120 |
MHA::BinlogManager::find_correct_binlog_dir_file_from_prefix( |
| 121 |
$opt{binlog_prefix}, $opt{binlog_dir} ); |
| 122 |
} |
| 123 |
croak |
| 124 |
"Binlog not found from $opt{binlog_dir}! If you got this error at MHA Manager, please set \"master_binlog_dir=/path/to/binlog_directory_of_the_master\" correctly in the MHA Manager's configuration file and try again.\n" |
| 125 |
unless ($binlog_dir); |
| 126 |
$_binlog_manager->init_from_dir_file( $binlog_dir, $opt{start_file} ); |
| 127 |
} |
| 128 |
unless ( $opt{output_file} ) { |
| 129 |
croak "Set --output_file=<output_file_path>\n"; |
| 130 |
} |
| 131 |
printf( " Creating %s if not exists.. ", dirname( $opt{output_file} ) ); |
| 132 |
MHA::NodeUtil::create_dir_if( dirname( $opt{output_file} ) ); |
| 133 |
print " ok.\n"; |
| 134 |
|
| 135 |
if ( $opt{command} eq "test" ) { |
| 136 |
print " Checking output directory is accessible or not..\n"; |
| 137 |
my $fh; |
| 138 |
open( $fh, ">", $opt{output_file} ) or croak "$!:$opt{output_file}\n"; |
| 139 |
unlink $opt{output_file}; |
| 140 |
print " ok.\n"; |
| 141 |
print |
| 142 |
" Binlog found at $_binlog_manager->{dir}, up to $_binlog_manager->{end_log}\n"; |
| 143 |
exit 0; |
| 144 |
} |
| 145 |
elsif ( $opt{command} eq "save" ) { |
| 146 |
my $rc = generate_diff_binary_log(); |
| 147 |
if ( $rc == $EVENT_NOT_EXIST ) { |
| 148 |
print "Event not exists.\n"; |
| 149 |
exit $rc; |
| 150 |
} |
| 151 |
elsif ($rc) { |
| 152 |
croak "Failed!\n"; |
| 153 |
} |
| 154 |
else { |
| 155 |
exit 0; |
| 156 |
} |
| 157 |
} |
| 158 |
else { |
| 159 |
pod2usage(1); |
| 160 |
} |
| 161 |
$exit_code = 0; |
| 162 |
}; |
| 163 |
if ($@) { |
| 164 |
warn "Failed to save binary log: $@"; |
| 165 |
} |
| 166 |
return $exit_code; |
| 167 |
} |
| 168 |
|
| 169 |
sub generate_diff_binary_log() { |
| 170 |
my $start_binlog_file = $opt{start_file}; |
| 171 |
my $start_binlog_pos = $opt{start_pos}; |
| 172 |
my $out_diff_file = $opt{output_file}; |
| 173 |
|
| 174 |
MHA::NodeUtil::drop_file_if($out_diff_file); |
| 175 |
|
| 176 |
if ( |
| 177 |
$_binlog_manager->concat_all_binlogs_from( |
| 178 |
$start_binlog_file, $start_binlog_pos, $out_diff_file |
| 179 |
) |
| 180 |
) |
| 181 |
{ |
| 182 |
return $EVENT_NOT_EXIST; |
| 183 |
} |
| 184 |
return 0; |
| 185 |
} |
| 186 |
|
| 187 |
# ############################################################################ |
| 188 |
# Documentation |
| 189 |
# ############################################################################ |
| 190 |
|
| 191 |
=pod |
| 192 |
|
| 193 |
=head1 NAME |
| 194 |
|
| 195 |
save_binary_logs - Concatenating binary or relay logs from the specified |
| 196 |
file/position to the end of the log. This command is automatically executed from MHA Manager on failover, and manual execution should not be needed normally. |
| 197 |
|
| 198 |
=head1 SYNOPSIS |
| 199 |
|
| 200 |
# Test |
| 201 |
|
| 202 |
$ save_binary_logs --command=test --binlog_dir=/var/lib/mysql --start_file=mysqld-bin.000002 |
| 203 |
|
| 204 |
# Saving binary logs |
| 205 |
|
| 206 |
$ save_binary_logs --command=save --binlog_dir=/var/lib/mysql --start_file=mysqld-bin.000002 --start_pos=312 --output_file=/var/tmp/aggregate.binlog |
| 207 |
|
| 208 |
# Saving relay logs |
| 209 |
|
| 210 |
$ save_binary_logs --command=save --start_file=mysqld-relay-bin.000002 --start_pos=312 --relay_log_info=/var/lib/mysql/relay-log.info --output_file=/var/tmp/aggregate.binlog |
| 211 |
|
| 212 |
save_binary_logs concatenates binary or relay logs from the specified log |
| 213 |
file/position to the end of the log. This tool is intended to |
| 214 |
be invoked from the master failover script(MHA Manager), and |
| 215 |
manual execution is normally not needed. |
| 216 |
|
| 217 |
|
| 218 |
=head1 DESCRIPTION |
| 219 |
|
| 220 |
Suppose that master is crashed and the latest slave server has received |
| 221 |
binary logs up to mysqld-bin.000002:312. It is likely that master has |
| 222 |
more binary logs. If it is not sent to the slave, slaves will lose all |
| 223 |
binlogs from mysqld-bin.000002:312. The purpose of the save_binary_logs is |
| 224 |
to save binary logs that are not replicated to slaves. If master is reachable |
| 225 |
through SSH and binary logs are readable, saving binary logs is possible. |
| 226 |
|
| 227 |
Here is an example: |
| 228 |
|
| 229 |
$ save_binary_logs --command=save --start_file=mysqld-bin.000002 --start_pos=312 --output_file=/var/tmp/aggregate.binlog |
| 230 |
|
| 231 |
Then all binary logs starting from mysqld-bin.000002:312 are concatenated |
| 232 |
and stored into /var/tmp/aggregate.binlog. If you have binary logs |
| 233 |
up to mysqld-bin.000004, the following mysqlbinlog outputs are written. |
| 234 |
|
| 235 |
mysqld-bin.000002:Format Description Event(FDE), plus from 312 to the tail |
| 236 |
mysqld-bin.000003:from 0 to the tail, excluding FDE |
| 237 |
mysqld-bin.000004:from 0 to the tail, excluding FDE |
| 238 |
|