/usr/bin/mysqlbinlogmove is in mysql-utilities 1.6.1-2.
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | #!/usr/bin/python
#
# Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
#
# 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; version 2 of the License.
#
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
This file contains the binlog relocate utility. It is used to move binlog
files to a different location, updating the binlog index files accordingly.
"""
from mysql.utilities.common.tools import check_python_version
# Check Python version compatibility
check_python_version()
import os
import sys
import mysql.utilities.command.binlog_admin as binlog_admin
from mysql.utilities.common.binary_log_file import (LOG_TYPE_BIN,
LOG_TYPE_RELAY,
LOG_TYPES)
from mysql.utilities.common.ip_parser import parse_connection
from mysql.utilities.common.messages import (PARSE_ERR_OPTS_EXCLD,
PARSE_ERR_OPTS_REQ,
WARN_OPT_NOT_REQUIRED_FOR_TYPE,
WARN_OPT_ONLY_USED_WITH)
from mysql.utilities.common.options import (add_verbosity, check_date_time,
check_dir_option,
get_absolute_path,
get_value_intervals_list,
setup_common_options)
from mysql.utilities.common.tools import check_connector_python
from mysql.utilities.exception import FormatError, UtilError
# Check for connector/python
if not check_connector_python():
sys.exit(1)
# Constants
NAME = "MySQL Utilities - mysqlbinlogmove"
DESCRIPTION = "mysqlbinlogmove - binary log relocate utility"
USAGE = "%prog --server=user:pass@host:port <destination_directory>"
EXTENDED_HELP = """
Introduction
------------
The mysqlbinlogmove utility was designed to relocate binary log files to a new
location in a simple and easy way. The use of this utility is recommended when
you intend to change the base location for the binlog files (enabled with the
server option --log-bin) moving all binlog files to the target location and
updating all required index files. It is also useful to archive some binary
log files to a different location.
Note: In order to relocate all binary log files the mysql server must be
stopped. This requirement is not needed if only some of binary log files are
relocated.
The behaviour of the utility depends on the options specified. Use the
--binlog_dir option to relocate all binary logs. Use the --server option to
relocate all binary logs except the ones currently in use (with the higher
sequence number). The target destination directory must be specified as an
argument and other option can be used to restrict the binary log files that
will be moved, as shown in the following examples.
# Move all binlog files to a new location (from /old/location
# to /new/location).
$ mysqlbinlogmove --binlog-dir=/old/location /new/location
# Move all binlog files except the one currently in use to a new
# location (from the server log_bin_basename directory to /new/location).
$ mysqlbinlogmove --server=root:pass@host1:3306 /new/location
# Move all binlog files within a specific sequence range (10-100),
# except the one currently in use, to a new location (from the server
# log_bin_basename directory to /new/location).
$ mysqlbinlogmove --server=root:pass@host1:3306 --sequence=10-100 \\
/new/location
# Move all binlog files not modified in the last two days, except the one
# currently in use, to a new location (from the server log_bin_basename
# directory to /new/location).
$ mysqlbinlogmove --server=root:pass@host1:3306 --modified-before=2 \\
/new/location
# Move all binlog files older than a specific date (not modified),
# except the one currently in use, to a new location (from the server
# log_bin_basename directory to /new/location).
$ mysqlbinlogmove --server=root:pass@host1:3306 \\
--modified-before=2004-07-30 /new/location
Helpful Hints
-------------
- By default only binlog files are moved. To move relay log files or both
use the --log-type option with the desired value.
- By default the utility will try to automatically determine the base name
for the binary logs and index files by applying the default filename
formats and files location. If custom file names are used, you can specify
them using the options --bin-log-index, --bin-log-basename,
--relay-log-index, and --relay-log-basename, respectively for binlog and
relay log files.
- When the --server option is used by default binary logs are flushed at the
end of the relocate operation in order to update the server's info. Use
--skip-flush-binlogs to skip this step.
"""
if __name__ == '__main__':
# Setup the command parser (with common options including --server).
parser = setup_common_options(os.path.basename(sys.argv[0]),
DESCRIPTION, USAGE, server=True,
server_default=None,
extended_help=EXTENDED_HELP)
# Source bin-log base directory (where files to move are located).
parser.add_option("--binlog-dir", action="store", dest="binlog_dir",
type="string", default=None,
help="Source directory (full path) where the binary log "
"files to move are located.")
# Basename for binlogs (filename without the extension).
parser.add_option("--bin-log-basename", action="store",
dest="bin_log_basename", type="string", default=None,
help="Basename for the binlog files. "
"If not available it is assumed to be any name "
"ended with '-bin'.")
# Basename for relay-logs (filename without the extension).
parser.add_option("--relay-log-basename", action="store",
dest="relay_log_basename", type="string", default=None,
help="Basename for the relay log files. "
"If not available it is assumed to be any name "
"ended with '-relay-bin'.")
# Index file for binlogs (full path).
parser.add_option("--bin-log-index", action="store", dest="bin_log_index",
type="string", default=None,
help="Location (full path) of the binlogs index file. "
"If not specified it is assumed to be located in "
"the binary log directory.")
# Index file for relay-logs (full path).
parser.add_option("--relay-log-index", action="store",
dest="relay_log_index", type="string", default=None,
help="Location (full path) of the relay logs index "
"file. If not specified it is assumed to be "
"located in the binary log directory.")
# Add verbosity option (no --quite option).
add_verbosity(parser, False)
# Add option to specify the type of the binary log files to process.
parser.add_option("--log-type", action="store", dest="log_type",
type="choice", default=LOG_TYPE_BIN,
choices=LOG_TYPES,
help="type of the binary log files to relocate: "
"'bin' - bin-log files (default), "
"'relay' - relay-log files, "
"'all' - bin-log and relay-log files.")
# Add option to filter the files by sequence number.
parser.add_option("--sequence", action="store", dest="sequence",
type="string", default=None,
help="relocate files with the specified sequence "
"values. Accepts a comma-separated list of "
"non-negative integers (corresponding to the file "
"sequence number) or intervals marked with a dash. "
"For example: 3,5-12,16,21.")
# Add option to filter the files by modified date.
parser.add_option("--modified-before", action="store",
dest="modified_before", type="string", default=None,
help="relocate files with the modified date prior to "
"the specified date/time or number of days. "
"Accepts a date/time in the format: "
"yyyy-mm-ddThh:mm:ss or yyyy-mm-dd, or an integer "
"for the elapsed days.")
# Add option to skip the flush binary/relay logs operation.
parser.add_option("--skip-flush-binlogs", action="store_true",
dest="skip_flush_binlogs", default=False,
help="Skip the binary/relay flush operation to reload "
"server's cache after moving files.")
# Parse the options and arguments.
opt, args = parser.parse_args()
# The --server and --binlog-dir options cannot be used simultaneously
# (only one).
if opt.server and opt.binlog_dir:
parser.error(PARSE_ERR_OPTS_EXCLD.format(opt1='--server',
opt2='--binlog-dir'))
# Check mandatory options: --server or --binlog-dir.
if not opt.server and not opt.binlog_dir:
parser.error(PARSE_ERR_OPTS_REQ.format(
opt="--server' or '--binlog-dir"))
# Check specified server.
server_val = None
if opt.server:
# Parse server connection values
try:
server_val = parse_connection(opt.server, None, opt)
except FormatError:
_, err, _ = sys.exc_info()
parser.error("Server connection values invalid: %s." % err)
except UtilError:
_, err, _ = sys.exc_info()
parser.error("Server connection values invalid: %s." % err.errmsg)
# Check specified source binlog directory.
binlog_dir = None
if opt.binlog_dir:
# Check the access to the source binlog directory.
binlog_dir = check_dir_option(parser, opt.binlog_dir, '--binlog-dir',
check_access=True, read_only=True)
# Check destination directory.
num_args = len(args)
if num_args < 1:
parser.error("You must specify the destination directory as argument.")
elif num_args > 1:
parser.error("You can only specify one destination directory. "
"Multiple arguments found.")
else:
destination = get_absolute_path(args[0])
if not os.path.isdir(destination):
parser.error("The destination path specified as argument is not a "
"valid directory: {0}".format(args[0]))
if not os.access(destination, os.R_OK | os.W_OK):
parser.error("You do not have enough privileges to access the "
"specified destination directory: "
"{0}.".format(args[0]))
# Check specified path for the binlog index file.
bin_log_index_file = None
if opt.bin_log_index:
bin_log_index_file = get_absolute_path(opt.bin_log_index)
if not os.path.isfile(bin_log_index_file):
parser.error("The specified value for --bin-index is not a "
"file: {0}".format(opt.bin_log_index))
if not os.access(bin_log_index_file, os.R_OK | os.W_OK):
parser.error("You do not have enough privileges to access the "
"specified binlog index file: "
"{0}.".format(opt.bin_log_index))
# Check specified path for the relay log index file.
relay_log_index_file = None
if opt.relay_log_index:
relay_log_index_file = get_absolute_path(opt.relay_log_index)
if not os.path.isfile(relay_log_index_file):
parser.error("The specified value for --relay-index is not a "
"file: {0}".format(opt.relay_log_index))
if not os.access(relay_log_index_file, os.R_OK | os.W_OK):
parser.error("You do not have enough privileges to access the "
"specified relay log index file: "
"{0}.".format(opt.relay_log_index))
# Check values specified for the --sequence option.
sequence_list = []
if opt.sequence:
sequence_list = get_value_intervals_list(parser, opt.sequence,
'--sequence', 'sequence')
# Check values specified for the --modified-before option.
modified_before = None
if opt.modified_before:
modified_before = check_date_time(parser, opt.modified_before,
'modified', allow_days=True)
# Check options not required for specific log types.
if opt.log_type == LOG_TYPE_BIN:
if opt.relay_log_basename:
print(WARN_OPT_NOT_REQUIRED_FOR_TYPE.format(
opt='--relay-log-basename',
type='{0}log'.format(LOG_TYPE_BIN)))
if opt.relay_log_index:
print(WARN_OPT_NOT_REQUIRED_FOR_TYPE.format(
opt='--relay-log-index',
type='{0}log'.format(LOG_TYPE_BIN)))
if opt.log_type == LOG_TYPE_RELAY:
if opt.bin_log_basename:
print(WARN_OPT_NOT_REQUIRED_FOR_TYPE.format(
opt='--bin-log-basename',
type='{0} log'.format(LOG_TYPE_RELAY)))
if opt.bin_log_index:
print(WARN_OPT_NOT_REQUIRED_FOR_TYPE.format(
opt='--bin-log-index',
type='{0} log'.format(LOG_TYPE_RELAY)))
# Check use of the --skip-flush-binlogs option.
if not opt.server and opt.skip_flush_binlogs:
print(WARN_OPT_ONLY_USED_WITH.format(opt='--skip-flush-binlogs',
used_with='--server'))
# Create dictionary of options
options = {
'verbosity': 0 if opt.verbosity is None else opt.verbosity,
'log_type': opt.log_type,
'sequence': sequence_list,
'modified_before': modified_before,
'skip_flush_binlogs': opt.skip_flush_binlogs,
}
# Relocate binary log files.
try:
# Relocate binary log files based for specified server.
if server_val:
binlog_admin.move_binlogs_from_server(
server_val, destination, options,
bin_basename=opt.bin_log_basename,
bin_index=bin_log_index_file,
relay_basename=opt.relay_log_basename
)
# Relocate binary log files from given source binlog directory.
if binlog_dir:
binlog_admin.move_binlogs(
binlog_dir, destination, options,
bin_basename=opt.bin_log_basename,
bin_index=bin_log_index_file,
relay_basename=opt.relay_log_basename,
relay_index=relay_log_index_file
)
except UtilError:
_, err, _ = sys.exc_info()
sys.stderr.write("ERROR: {0}\n".format(err.errmsg))
sys.exit(1)
|