/usr/bin/save_binary_logs is in mha4mysql-node 0.54-1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | #!/usr/bin/env perl
# Copyright (C) 2011 DeNA Co.,Ltd.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
use strict;
use warnings FATAL => 'all';
use Carp qw(croak);
use MHA::BinlogManager;
use MHA::NodeConst;
use MHA::NodeUtil;
use File::Basename;
use Getopt::Long;
use Pod::Usage;
GetOptions(
\my %opt, qw/
help
version
command=s
start_file=s
start_pos=i
binlog_prefix=s
binlog_dir=s
output_file=s
datadir=s
relay_log_info=s
oldest_version=s
handle_raw_binlog=i
disable_log_bin=i
manager_version=s
debug
/,
) or pod2usage(1);
# for compatibility
unless ( $opt{datadir} ) {
$opt{datadir} = $opt{binlog_dir};
}
$opt{binlog_dir} ||= "/var/lib/mysql";
$opt{handle_raw_binlog} = 1 if ( !defined( $opt{handle_raw_binlog} ) );
$opt{disable_log_bin} = 0 if ( !defined( $opt{disable_log_bin} ) );
$| = 1;
my $_binlog_manager;
my $EVENT_NOT_EXIST = 10;
exit &main();
sub main {
my $exit_code = 1;
eval {
if ( $opt{help} )
{
pod2usage(0);
}
if ( $opt{version} ) {
print "save_binary_logs version $MHA::NodeConst::VERSION.\n";
exit 0;
}
MHA::NodeUtil::check_manager_version( $opt{manager_version} )
if ( $opt{manager_version} );
unless ( $opt{command} ) {
croak "Set --command=[test|save]\n";
}
if ( $opt{command} eq "test" ) {
if ( !$opt{start_file} && !$opt{binlog_prefix} ) {
croak
"Set --start_file=<starting_binlog_filename> or --binlog_prefix=<binlog_file_prefix>\n";
}
}
elsif ( !$opt{start_file} ) {
croak "Set --start_file=<starting_binlog_filename>\n";
}
if ( !defined( $opt{start_pos} ) ) {
croak "Set --start_pos=<starting_binlog_pos>\n";
}
$_binlog_manager = new MHA::BinlogManager(
handle_raw_binlog => $opt{handle_raw_binlog},
disable_log_bin => $opt{disable_log_bin},
mysql_version => $opt{oldest_version},
debug => $opt{debug},
);
$_binlog_manager->init_mysqlbinlog() unless ( $opt{handle_raw_binlog} );
# Finding target binary/relay log directory
if ( $opt{relay_log_info} ) {
$_binlog_manager->init_from_relay_log_info( $opt{relay_log_info},
$opt{datadir} );
}
else {
my $binlog_dir;
if ( $opt{start_file} ) {
$binlog_dir =
MHA::BinlogManager::find_correct_binlog_dir( $opt{start_file},
$opt{binlog_dir} );
}
else {
( $binlog_dir, $opt{start_file} ) =
MHA::BinlogManager::find_correct_binlog_dir_file_from_prefix(
$opt{binlog_prefix}, $opt{binlog_dir} );
}
croak
"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"
unless ($binlog_dir);
$_binlog_manager->init_from_dir_file( $binlog_dir, $opt{start_file} );
}
unless ( $opt{output_file} ) {
croak "Set --output_file=<output_file_path>\n";
}
printf( " Creating %s if not exists.. ", dirname( $opt{output_file} ) );
MHA::NodeUtil::create_dir_if( dirname( $opt{output_file} ) );
print " ok.\n";
if ( $opt{command} eq "test" ) {
print " Checking output directory is accessible or not..\n";
my $fh;
open( $fh, ">", $opt{output_file} ) or croak "$!:$opt{output_file}\n";
unlink $opt{output_file};
print " ok.\n";
print
" Binlog found at $_binlog_manager->{dir}, up to $_binlog_manager->{end_log}\n";
exit 0;
}
elsif ( $opt{command} eq "save" ) {
my $rc = generate_diff_binary_log();
if ( $rc == $EVENT_NOT_EXIST ) {
print "Event not exists.\n";
exit $rc;
}
elsif ($rc) {
croak "Failed!\n";
}
else {
exit 0;
}
}
else {
pod2usage(1);
}
$exit_code = 0;
};
if ($@) {
warn "Failed to save binary log: $@";
}
return $exit_code;
}
sub generate_diff_binary_log() {
my $start_binlog_file = $opt{start_file};
my $start_binlog_pos = $opt{start_pos};
my $out_diff_file = $opt{output_file};
MHA::NodeUtil::drop_file_if($out_diff_file);
if (
$_binlog_manager->concat_all_binlogs_from(
$start_binlog_file, $start_binlog_pos, $out_diff_file
)
)
{
return $EVENT_NOT_EXIST;
}
return 0;
}
# ############################################################################
# Documentation
# ############################################################################
=pod
=head1 NAME
save_binary_logs - Concatenating binary or relay logs from the specified
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.
=head1 SYNOPSIS
# Test
$ save_binary_logs --command=test --binlog_dir=/var/lib/mysql --start_file=mysqld-bin.000002
# Saving binary logs
$ save_binary_logs --command=save --binlog_dir=/var/lib/mysql --start_file=mysqld-bin.000002 --start_pos=312 --output_file=/var/tmp/aggregate.binlog
# Saving relay logs
$ 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
save_binary_logs concatenates binary or relay logs from the specified log
file/position to the end of the log. This tool is intended to
be invoked from the master failover script(MHA Manager), and
manual execution is normally not needed.
=head1 DESCRIPTION
Suppose that master is crashed and the latest slave server has received
binary logs up to mysqld-bin.000002:312. It is likely that master has
more binary logs. If it is not sent to the slave, slaves will lose all
binlogs from mysqld-bin.000002:312. The purpose of the save_binary_logs is
to save binary logs that are not replicated to slaves. If master is reachable
through SSH and binary logs are readable, saving binary logs is possible.
Here is an example:
$ save_binary_logs --command=save --start_file=mysqld-bin.000002 --start_pos=312 --output_file=/var/tmp/aggregate.binlog
Then all binary logs starting from mysqld-bin.000002:312 are concatenated
and stored into /var/tmp/aggregate.binlog. If you have binary logs
up to mysqld-bin.000004, the following mysqlbinlog outputs are written.
mysqld-bin.000002:Format Description Event(FDE), plus from 312 to the tail
mysqld-bin.000003:from 0 to the tail, excluding FDE
mysqld-bin.000004:from 0 to the tail, excluding FDE
|