1/******************************************************
2hot backup tool for InnoDB
3(c) 2009-2015 Percona LLC and/or its affiliates
4Originally Created 3/3/2009 Yasufumi Kinoshita
5Written by Alexey Kopytov, Aleksandr Kuzminsky, Stewart Smith, Vadim Tkachenko,
6Yasufumi Kinoshita, Ignacio Nin and Baron Schwartz.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; version 2 of the License.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20
21*******************************************************
22
23This file incorporates work covered by the following copyright and
24permission notice:
25
26Copyright (c) 2000, 2011, MySQL AB & Innobase Oy. All Rights Reserved.
27
28This program is free software; you can redistribute it and/or modify it under
29the terms of the GNU General Public License as published by the Free Software
30Foundation; version 2 of the License.
31
32This program is distributed in the hope that it will be useful, but WITHOUT
33ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
35
36You should have received a copy of the GNU General Public License along with
37this program; if not, write to the Free Software Foundation, Inc., 59 Temple
38Place, Suite 330, Boston, MA 02111-1307 USA
39
40*******************************************************/
41
42#include <my_global.h>
43#include <stdio.h>
44#include <string.h>
45#include <mysql.h>
46#include <my_dir.h>
47#include <ut0mem.h>
48#include <os0file.h>
49#include <srv0start.h>
50#include <algorithm>
51#include <mysqld.h>
52#include <my_default.h>
53#include <my_getopt.h>
54#include <string>
55#include <sstream>
56#include <set>
57#include "common.h"
58#include "innobackupex.h"
59#include "xtrabackup.h"
60#include "xbstream.h"
61#include "fil_cur.h"
62#include "write_filt.h"
63#include "backup_copy.h"
64
65using std::min;
66using std::max;
67
68/* options */
69my_bool opt_ibx_version = FALSE;
70my_bool opt_ibx_help = FALSE;
71my_bool opt_ibx_apply_log = FALSE;
72my_bool opt_ibx_incremental = FALSE;
73my_bool opt_ibx_notimestamp = FALSE;
74
75my_bool opt_ibx_copy_back = FALSE;
76my_bool opt_ibx_move_back = FALSE;
77my_bool opt_ibx_galera_info = FALSE;
78my_bool opt_ibx_slave_info = FALSE;
79my_bool opt_ibx_no_lock = FALSE;
80my_bool opt_ibx_safe_slave_backup = FALSE;
81my_bool opt_ibx_rsync = FALSE;
82my_bool opt_ibx_force_non_empty_dirs = FALSE;
83my_bool opt_ibx_noversioncheck = FALSE;
84my_bool opt_ibx_no_backup_locks = FALSE;
85my_bool opt_ibx_decompress = FALSE;
86
87char *opt_ibx_incremental_history_name = NULL;
88char *opt_ibx_incremental_history_uuid = NULL;
89
90char *opt_ibx_user = NULL;
91char *opt_ibx_password = NULL;
92char *opt_ibx_host = NULL;
93char *opt_ibx_defaults_group = NULL;
94char *opt_ibx_socket = NULL;
95uint opt_ibx_port = 0;
96
97ulong opt_ibx_lock_wait_query_type;
98ulong opt_ibx_kill_long_query_type;
99
100uint opt_ibx_kill_long_queries_timeout = 0;
101uint opt_ibx_lock_wait_timeout = 0;
102uint opt_ibx_lock_wait_threshold = 0;
103uint opt_ibx_debug_sleep_before_unlock = 0;
104uint opt_ibx_safe_slave_backup_timeout = 0;
105
106const char *opt_ibx_history = NULL;
107
108char *opt_ibx_include = NULL;
109char *opt_ibx_databases = NULL;
110bool ibx_partial_backup = false;
111
112char *ibx_position_arg = NULL;
113char *ibx_backup_directory = NULL;
114
115/* copy of proxied xtrabackup options */
116my_bool ibx_xb_close_files;
117const char *ibx_xtrabackup_compress_alg;
118uint ibx_xtrabackup_compress_threads;
119ulonglong ibx_xtrabackup_compress_chunk_size;
120my_bool ibx_xtrabackup_export;
121char *ibx_xtrabackup_extra_lsndir;
122char *ibx_xtrabackup_incremental_basedir;
123char *ibx_xtrabackup_incremental_dir;
124my_bool ibx_xtrabackup_incremental_force_scan;
125ulint ibx_xtrabackup_log_copy_interval;
126char *ibx_xtrabackup_incremental;
127int ibx_xtrabackup_parallel;
128char *ibx_xtrabackup_stream_str;
129char *ibx_xtrabackup_tables_file;
130long ibx_xtrabackup_throttle;
131char *ibx_opt_mysql_tmpdir;
132longlong ibx_xtrabackup_use_memory;
133
134
135static inline int ibx_msg(const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 1, 2);
136static inline int ibx_msg(const char *fmt, ...)
137{
138 int result;
139 time_t t = time(NULL);
140 char date[100];
141 char *line;
142 va_list args;
143
144 strftime(date, sizeof(date), "%y%m%d %H:%M:%S", localtime(&t));
145
146 va_start(args, fmt);
147
148 result = vasprintf(&line, fmt, args);
149
150 va_end(args);
151
152 if (result != -1) {
153 result = fprintf(stderr, "%s %s: %s",
154 date, INNOBACKUPEX_BIN_NAME, line);
155 free(line);
156 }
157
158 return result;
159}
160
161enum innobackupex_options
162{
163 OPT_APPLY_LOG = 256,
164 OPT_COPY_BACK,
165 OPT_MOVE_BACK,
166 OPT_REDO_ONLY,
167 OPT_GALERA_INFO,
168 OPT_SLAVE_INFO,
169 OPT_INCREMENTAL,
170 OPT_INCREMENTAL_HISTORY_NAME,
171 OPT_INCREMENTAL_HISTORY_UUID,
172 OPT_LOCK_WAIT_QUERY_TYPE,
173 OPT_KILL_LONG_QUERY_TYPE,
174 OPT_KILL_LONG_QUERIES_TIMEOUT,
175 OPT_LOCK_WAIT_TIMEOUT,
176 OPT_LOCK_WAIT_THRESHOLD,
177 OPT_DEBUG_SLEEP_BEFORE_UNLOCK,
178 OPT_NO_LOCK,
179 OPT_SAFE_SLAVE_BACKUP,
180 OPT_SAFE_SLAVE_BACKUP_TIMEOUT,
181 OPT_RSYNC,
182 OPT_HISTORY,
183 OPT_INCLUDE,
184 OPT_FORCE_NON_EMPTY_DIRS,
185 OPT_NO_TIMESTAMP,
186 OPT_NO_VERSION_CHECK,
187 OPT_NO_BACKUP_LOCKS,
188 OPT_DATABASES,
189 OPT_DECOMPRESS,
190
191 /* options wich are passed directly to xtrabackup */
192 OPT_CLOSE_FILES,
193 OPT_COMPACT,
194 OPT_COMPRESS,
195 OPT_COMPRESS_THREADS,
196 OPT_COMPRESS_CHUNK_SIZE,
197 OPT_EXPORT,
198 OPT_EXTRA_LSNDIR,
199 OPT_INCREMENTAL_BASEDIR,
200 OPT_INCREMENTAL_DIR,
201 OPT_INCREMENTAL_FORCE_SCAN,
202 OPT_LOG_COPY_INTERVAL,
203 OPT_PARALLEL,
204 OPT_REBUILD_INDEXES,
205 OPT_REBUILD_THREADS,
206 OPT_STREAM,
207 OPT_TABLES_FILE,
208 OPT_THROTTLE,
209 OPT_USE_MEMORY
210};
211
212ibx_mode_t ibx_mode = IBX_MODE_BACKUP;
213
214static struct my_option ibx_long_options[] =
215{
216 {"version", 'v', "print xtrabackup version information",
217 (uchar *) &opt_ibx_version, (uchar *) &opt_ibx_version, 0,
218 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
219
220 {"help", '?', "This option displays a help screen and exits.",
221 (uchar *) &opt_ibx_help, (uchar *) &opt_ibx_help, 0,
222 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
223
224 {"apply-log", OPT_APPLY_LOG, "Prepare a backup in BACKUP-DIR by "
225 "applying the redo log 'ib_logfile0' and creating new redo log. "
226 "The InnoDB configuration is read from the file \"backup-my.cnf\".",
227 (uchar*) &opt_ibx_apply_log, (uchar*) &opt_ibx_apply_log,
228 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
229
230 {"copy-back", OPT_COPY_BACK, "Copy all the files in a previously made "
231 "backup from the backup directory to their original locations.",
232 (uchar *) &opt_ibx_copy_back, (uchar *) &opt_ibx_copy_back, 0,
233 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
234
235 {"move-back", OPT_MOVE_BACK, "Move all the files in a previously made "
236 "backup from the backup directory to the actual datadir location. "
237 "Use with caution, as it removes backup files.",
238 (uchar *) &opt_ibx_move_back, (uchar *) &opt_ibx_move_back, 0,
239 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
240
241 {"galera-info", OPT_GALERA_INFO, "This options creates the "
242 "xtrabackup_galera_info file which contains the local node state at "
243 "the time of the backup. Option should be used when performing the "
244 "backup of MariaDB Galera Cluster. Has no effect when backup locks "
245 "are used to create the backup.",
246 (uchar *) &opt_ibx_galera_info, (uchar *) &opt_ibx_galera_info, 0,
247 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
248
249 {"slave-info", OPT_SLAVE_INFO, "This option is useful when backing "
250 "up a replication slave server. It prints the binary log position "
251 "and name of the master server. It also writes this information to "
252 "the \"xtrabackup_slave_info\" file as a \"CHANGE MASTER\" command. "
253 "A new slave for this master can be set up by starting a slave server "
254 "on this backup and issuing a \"CHANGE MASTER\" command with the "
255 "binary log position saved in the \"xtrabackup_slave_info\" file.",
256 (uchar *) &opt_ibx_slave_info, (uchar *) &opt_ibx_slave_info, 0,
257 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
258
259 {"incremental", OPT_INCREMENTAL, "This option tells xtrabackup to "
260 "create an incremental backup, rather than a full one. It is passed "
261 "to the xtrabackup child process. When this option is specified, "
262 "either --incremental-lsn or --incremental-basedir can also be given. "
263 "If neither option is given, option --incremental-basedir is passed "
264 "to xtrabackup by default, set to the first timestamped backup "
265 "directory in the backup base directory.",
266 (uchar *) &opt_ibx_incremental, (uchar *) &opt_ibx_incremental, 0,
267 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
268
269 {"no-lock", OPT_NO_LOCK, "Use this option to disable table lock "
270 "with \"FLUSH TABLES WITH READ LOCK\". Use it only if ALL your "
271 "tables are InnoDB and you DO NOT CARE about the binary log "
272 "position of the backup. This option shouldn't be used if there "
273 "are any DDL statements being executed or if any updates are "
274 "happening on non-InnoDB tables (this includes the system MyISAM "
275 "tables in the mysql database), otherwise it could lead to an "
276 "inconsistent backup. If you are considering to use --no-lock "
277 "because your backups are failing to acquire the lock, this could "
278 "be because of incoming replication events preventing the lock "
279 "from succeeding. Please try using --safe-slave-backup to "
280 "momentarily stop the replication slave thread, this may help "
281 "the backup to succeed and you then don't need to resort to "
282 "using this option.",
283 (uchar *) &opt_ibx_no_lock, (uchar *) &opt_ibx_no_lock, 0,
284 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
285
286 {"safe-slave-backup", OPT_SAFE_SLAVE_BACKUP, "Stop slave SQL thread "
287 "and wait to start backup until Slave_open_temp_tables in "
288 "\"SHOW STATUS\" is zero. If there are no open temporary tables, "
289 "the backup will take place, otherwise the SQL thread will be "
290 "started and stopped until there are no open temporary tables. "
291 "The backup will fail if Slave_open_temp_tables does not become "
292 "zero after --safe-slave-backup-timeout seconds. The slave SQL "
293 "thread will be restarted when the backup finishes.",
294 (uchar *) &opt_ibx_safe_slave_backup,
295 (uchar *) &opt_ibx_safe_slave_backup,
296 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
297
298 {"rsync", OPT_RSYNC, "Uses the rsync utility to optimize local file "
299 "transfers. When this option is specified, innobackupex uses rsync "
300 "to copy all non-InnoDB files instead of spawning a separate cp for "
301 "each file, which can be much faster for servers with a large number "
302 "of databases or tables. This option cannot be used together with "
303 "--stream.",
304 (uchar *) &opt_ibx_rsync, (uchar *) &opt_ibx_rsync,
305 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
306
307 {"force-non-empty-directories", OPT_FORCE_NON_EMPTY_DIRS, "This "
308 "option, when specified, makes --copy-back or --move-back transfer "
309 "files to non-empty directories. Note that no existing files will be "
310 "overwritten. If --copy-back or --nove-back has to copy a file from "
311 "the backup directory which already exists in the destination "
312 "directory, it will still fail with an error.",
313 (uchar *) &opt_ibx_force_non_empty_dirs,
314 (uchar *) &opt_ibx_force_non_empty_dirs,
315 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
316
317 {"no-timestamp", OPT_NO_TIMESTAMP, "This option prevents creation of a "
318 "time-stamped subdirectory of the BACKUP-ROOT-DIR given on the "
319 "command line. When it is specified, the backup is done in "
320 "BACKUP-ROOT-DIR instead.",
321 (uchar *) &opt_ibx_notimestamp,
322 (uchar *) &opt_ibx_notimestamp,
323 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
324
325 {"no-version-check", OPT_NO_VERSION_CHECK, "This option disables the "
326 "version check which is enabled by the --version-check option.",
327 (uchar *) &opt_ibx_noversioncheck,
328 (uchar *) &opt_ibx_noversioncheck,
329 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
330
331 {"no-backup-locks", OPT_NO_BACKUP_LOCKS, "This option controls if "
332 "backup locks should be used instead of FLUSH TABLES WITH READ LOCK "
333 "on the backup stage. The option has no effect when backup locks are "
334 "not supported by the server. This option is enabled by default, "
335 "disable with --no-backup-locks.",
336 (uchar *) &opt_ibx_no_backup_locks,
337 (uchar *) &opt_ibx_no_backup_locks,
338 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
339
340 {"decompress", OPT_DECOMPRESS, "Decompresses all files with the .qp "
341 "extension in a backup previously made with the --compress option.",
342 (uchar *) &opt_ibx_decompress,
343 (uchar *) &opt_ibx_decompress,
344 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
345
346 {"user", 'u', "This option specifies the MySQL username used "
347 "when connecting to the server, if that's not the current user. "
348 "The option accepts a string argument. See mysql --help for details.",
349 (uchar*) &opt_ibx_user, (uchar*) &opt_ibx_user, 0, GET_STR,
350 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
351
352 {"host", 'H', "This option specifies the host to use when "
353 "connecting to the database server with TCP/IP. The option accepts "
354 "a string argument. See mysql --help for details.",
355 (uchar*) &opt_ibx_host, (uchar*) &opt_ibx_host, 0, GET_STR,
356 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
357
358 {"port", 'P', "This option specifies the port to use when "
359 "connecting to the database server with TCP/IP. The option accepts "
360 "a string argument. See mysql --help for details.",
361 &opt_ibx_port, &opt_ibx_port, 0, GET_UINT, REQUIRED_ARG,
362 0, 0, 0, 0, 0, 0},
363
364 {"password", 'p', "This option specifies the password to use "
365 "when connecting to the database. It accepts a string argument. "
366 "See mysql --help for details.",
367 0, 0, 0, GET_STR,
368 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
369
370 {"socket", 'S', "This option specifies the socket to use when "
371 "connecting to the local database server with a UNIX domain socket. "
372 "The option accepts a string argument. See mysql --help for details.",
373 (uchar*) &opt_ibx_socket, (uchar*) &opt_ibx_socket, 0, GET_STR,
374 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
375
376 {"incremental-history-name", OPT_INCREMENTAL_HISTORY_NAME,
377 "This option specifies the name of the backup series stored in the "
378 "PERCONA_SCHEMA.xtrabackup_history history record to base an "
379 "incremental backup on. Xtrabackup will search the history table "
380 "looking for the most recent (highest innodb_to_lsn), successful "
381 "backup in the series and take the to_lsn value to use as the "
382 "starting lsn for the incremental backup. This will be mutually "
383 "exclusive with --incremental-history-uuid, --incremental-basedir "
384 "and --incremental-lsn. If no valid lsn can be found (no series by "
385 "that name, no successful backups by that name) xtrabackup will "
386 "return with an error. It is used with the --incremental option.",
387 (uchar*) &opt_ibx_incremental_history_name,
388 (uchar*) &opt_ibx_incremental_history_name, 0, GET_STR,
389 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
390
391 {"incremental-history-uuid", OPT_INCREMENTAL_HISTORY_UUID,
392 "This option specifies the UUID of the specific history record "
393 "stored in the PERCONA_SCHEMA.xtrabackup_history to base an "
394 "incremental backup on. --incremental-history-name, "
395 "--incremental-basedir and --incremental-lsn. If no valid lsn can be "
396 "found (no success record with that uuid) xtrabackup will return "
397 "with an error. It is used with the --incremental option.",
398 (uchar*) &opt_ibx_incremental_history_uuid,
399 (uchar*) &opt_ibx_incremental_history_uuid, 0, GET_STR,
400 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
401
402 {"ftwrl-wait-query-type", OPT_LOCK_WAIT_QUERY_TYPE,
403 "This option specifies which types of queries are allowed to complete "
404 "before innobackupex will issue the global lock. Default is all.",
405 (uchar*) &opt_ibx_lock_wait_query_type,
406 (uchar*) &opt_ibx_lock_wait_query_type, &query_type_typelib,
407 GET_ENUM, REQUIRED_ARG, QUERY_TYPE_ALL, 0, 0, 0, 0, 0},
408
409 {"kill-long-query-type", OPT_KILL_LONG_QUERY_TYPE,
410 "This option specifies which types of queries should be killed to "
411 "unblock the global lock. Default is \"all\".",
412 (uchar*) &opt_ibx_kill_long_query_type,
413 (uchar*) &opt_ibx_kill_long_query_type, &query_type_typelib,
414 GET_ENUM, REQUIRED_ARG, QUERY_TYPE_SELECT, 0, 0, 0, 0, 0},
415
416 {"history", OPT_HISTORY,
417 "This option enables the tracking of backup history in the "
418 "PERCONA_SCHEMA.xtrabackup_history table. An optional history "
419 "series name may be specified that will be placed with the history "
420 "record for the current backup being taken.",
421 NULL, NULL, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
422
423 {"include", OPT_INCLUDE,
424 "This option is a regular expression to be matched against table "
425 "names in databasename.tablename format. It is passed directly to "
426 "xtrabackup's --tables option. See the xtrabackup documentation for "
427 "details.",
428 (uchar*) &opt_ibx_include,
429 (uchar*) &opt_ibx_include, 0, GET_STR,
430 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
431
432 {"databases", OPT_DATABASES,
433 "This option specifies the list of databases that innobackupex should "
434 "back up. The option accepts a string argument or path to file that "
435 "contains the list of databases to back up. The list is of the form "
436 "\"databasename1[.table_name1] databasename2[.table_name2] . . .\". "
437 "If this option is not specified, all databases containing MyISAM and "
438 "InnoDB tables will be backed up. Please make sure that --databases "
439 "contains all of the InnoDB databases and tables, so that all of the "
440 "innodb.frm files are also backed up. In case the list is very long, "
441 "this can be specified in a file, and the full path of the file can "
442 "be specified instead of the list. (See option --tables-file.)",
443 (uchar*) &opt_ibx_databases,
444 (uchar*) &opt_ibx_databases, 0, GET_STR,
445 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
446
447 {"kill-long-queries-timeout", OPT_KILL_LONG_QUERIES_TIMEOUT,
448 "This option specifies the number of seconds innobackupex waits "
449 "between starting FLUSH TABLES WITH READ LOCK and killing those "
450 "queries that block it. Default is 0 seconds, which means "
451 "innobackupex will not attempt to kill any queries.",
452 (uchar*) &opt_ibx_kill_long_queries_timeout,
453 (uchar*) &opt_ibx_kill_long_queries_timeout, 0, GET_UINT,
454 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
455
456 {"ftwrl-wait-timeout", OPT_LOCK_WAIT_TIMEOUT,
457 "This option specifies time in seconds that innobackupex should wait "
458 "for queries that would block FTWRL before running it. If there are "
459 "still such queries when the timeout expires, innobackupex terminates "
460 "with an error. Default is 0, in which case innobackupex does not "
461 "wait for queries to complete and starts FTWRL immediately.",
462 (uchar*) &opt_ibx_lock_wait_timeout,
463 (uchar*) &opt_ibx_lock_wait_timeout, 0, GET_UINT,
464 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
465
466 {"ftwrl-wait-threshold", OPT_LOCK_WAIT_THRESHOLD,
467 "This option specifies the query run time threshold which is used by "
468 "innobackupex to detect long-running queries with a non-zero value "
469 "of --ftwrl-wait-timeout. FTWRL is not started until such "
470 "long-running queries exist. This option has no effect if "
471 "--ftwrl-wait-timeout is 0. Default value is 60 seconds.",
472 (uchar*) &opt_ibx_lock_wait_threshold,
473 (uchar*) &opt_ibx_lock_wait_threshold, 0, GET_UINT,
474 REQUIRED_ARG, 60, 0, 0, 0, 0, 0},
475
476 {"debug-sleep-before-unlock", OPT_DEBUG_SLEEP_BEFORE_UNLOCK,
477 "This is a debug-only option used by the XtraBackup test suite.",
478 (uchar*) &opt_ibx_debug_sleep_before_unlock,
479 (uchar*) &opt_ibx_debug_sleep_before_unlock, 0, GET_UINT,
480 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
481
482 {"safe-slave-backup-timeout", OPT_SAFE_SLAVE_BACKUP_TIMEOUT,
483 "How many seconds --safe-slave-backup should wait for "
484 "Slave_open_temp_tables to become zero. (default 300)",
485 (uchar*) &opt_ibx_safe_slave_backup_timeout,
486 (uchar*) &opt_ibx_safe_slave_backup_timeout, 0, GET_UINT,
487 REQUIRED_ARG, 300, 0, 0, 0, 0, 0},
488
489
490 /* Following command-line options are actually handled by xtrabackup.
491 We put them here with only purpose for them to showup in
492 innobackupex --help output */
493
494 {"close_files", OPT_CLOSE_FILES, "Do not keep files opened. This "
495 "option is passed directly to xtrabackup. Use at your own risk.",
496 (uchar*) &ibx_xb_close_files, (uchar*) &ibx_xb_close_files, 0,
497 GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
498
499 {"compress", OPT_COMPRESS, "This option instructs xtrabackup to "
500 "compress backup copies of InnoDB data files. It is passed directly "
501 "to the xtrabackup child process. Try 'xtrabackup --help' for more "
502 "details.", (uchar*) &ibx_xtrabackup_compress_alg,
503 (uchar*) &ibx_xtrabackup_compress_alg, 0,
504 GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
505
506 {"compress-threads", OPT_COMPRESS_THREADS,
507 "This option specifies the number of worker threads that will be used "
508 "for parallel compression. It is passed directly to the xtrabackup "
509 "child process. Try 'xtrabackup --help' for more details.",
510 (uchar*) &ibx_xtrabackup_compress_threads,
511 (uchar*) &ibx_xtrabackup_compress_threads,
512 0, GET_UINT, REQUIRED_ARG, 1, 1, UINT_MAX, 0, 0, 0},
513
514 {"compress-chunk-size", OPT_COMPRESS_CHUNK_SIZE, "Size of working "
515 "buffer(s) for compression threads in bytes. The default value "
516 "is 64K.", (uchar*) &ibx_xtrabackup_compress_chunk_size,
517 (uchar*) &ibx_xtrabackup_compress_chunk_size,
518 0, GET_ULL, REQUIRED_ARG, (1 << 16), 1024, ULONGLONG_MAX, 0, 0, 0},
519
520 {"export", OPT_EXPORT, "This option is passed directly to xtrabackup's "
521 "--export option. It enables exporting individual tables for import "
522 "into another server. See the xtrabackup documentation for details.",
523 (uchar*) &ibx_xtrabackup_export, (uchar*) &ibx_xtrabackup_export,
524 0, GET_BOOL, NO_ARG, 0, 0, 0, 0, 0, 0},
525
526 {"extra-lsndir", OPT_EXTRA_LSNDIR, "This option specifies the "
527 "directory in which to save an extra copy of the "
528 "\"xtrabackup_checkpoints\" file. The option accepts a string "
529 "argument. It is passed directly to xtrabackup's --extra-lsndir "
530 "option. See the xtrabackup documentation for details.",
531 (uchar*) &ibx_xtrabackup_extra_lsndir,
532 (uchar*) &ibx_xtrabackup_extra_lsndir,
533 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
534
535 {"incremental-basedir", OPT_INCREMENTAL_BASEDIR, "This option "
536 "specifies the directory containing the full backup that is the base "
537 "dataset for the incremental backup. The option accepts a string "
538 "argument. It is used with the --incremental option.",
539 (uchar*) &ibx_xtrabackup_incremental_basedir,
540 (uchar*) &ibx_xtrabackup_incremental_basedir,
541 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
542
543 {"incremental-dir", OPT_INCREMENTAL_DIR, "This option specifies the "
544 "directory where the incremental backup will be combined with the "
545 "full backup to make a new full backup. The option accepts a string "
546 "argument. It is used with the --incremental option.",
547 (uchar*) &ibx_xtrabackup_incremental_dir,
548 (uchar*) &ibx_xtrabackup_incremental_dir,
549 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
550
551 {"incremental-force-scan", OPT_INCREMENTAL_FORCE_SCAN,
552 "This options tells xtrabackup to perform full scan of data files "
553 "for taking an incremental backup even if full changed page bitmap "
554 "data is available to enable the backup without the full scan.",
555 (uchar*)&ibx_xtrabackup_incremental_force_scan,
556 (uchar*)&ibx_xtrabackup_incremental_force_scan, 0, GET_BOOL, NO_ARG,
557 0, 0, 0, 0, 0, 0},
558
559 {"log-copy-interval", OPT_LOG_COPY_INTERVAL, "This option specifies "
560 "time interval between checks done by log copying thread in "
561 "milliseconds.", (uchar*) &ibx_xtrabackup_log_copy_interval,
562 (uchar*) &ibx_xtrabackup_log_copy_interval,
563 0, GET_LONG, REQUIRED_ARG, 1000, 0, LONG_MAX, 0, 1, 0},
564
565 {"incremental-lsn", OPT_INCREMENTAL, "This option specifies the log "
566 "sequence number (LSN) to use for the incremental backup. The option "
567 "accepts a string argument. It is used with the --incremental option. "
568 "It is used instead of specifying --incremental-basedir. For "
569 "databases created by MySQL and Percona Server 5.0-series versions, "
570 "specify the LSN as two 32-bit integers in high:low format. For "
571 "databases created in 5.1 and later, specify the LSN as a single "
572 "64-bit integer.",
573 (uchar*) &ibx_xtrabackup_incremental,
574 (uchar*) &ibx_xtrabackup_incremental,
575 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
576
577 {"parallel", OPT_PARALLEL, "On backup, this option specifies the "
578 "number of threads the xtrabackup child process should use to back "
579 "up files concurrently. The option accepts an integer argument. It "
580 "is passed directly to xtrabackup's --parallel option. See the "
581 "xtrabackup documentation for details.",
582 (uchar*) &ibx_xtrabackup_parallel, (uchar*) &ibx_xtrabackup_parallel,
583 0, GET_INT, REQUIRED_ARG, 1, 1, INT_MAX, 0, 0, 0},
584
585
586 {"stream", OPT_STREAM, "This option specifies the format in which to "
587 "do the streamed backup. The option accepts a string argument. The "
588 "backup will be done to STDOUT in the specified format. Currently, "
589 "the only supported formats are tar and xbstream. This option is "
590 "passed directly to xtrabackup's --stream option.",
591 (uchar*) &ibx_xtrabackup_stream_str,
592 (uchar*) &ibx_xtrabackup_stream_str, 0, GET_STR,
593 REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
594
595 {"tables-file", OPT_TABLES_FILE, "This option specifies the file in "
596 "which there are a list of names of the form database. The option "
597 "accepts a string argument.table, one per line. The option is passed "
598 "directly to xtrabackup's --tables-file option.",
599 (uchar*) &ibx_xtrabackup_tables_file,
600 (uchar*) &ibx_xtrabackup_tables_file,
601 0, GET_STR, REQUIRED_ARG, 0, 0, 0, 0, 0, 0},
602
603 {"throttle", OPT_THROTTLE, "This option specifies a number of I/O "
604 "operations (pairs of read+write) per second. It accepts an integer "
605 "argument. It is passed directly to xtrabackup's --throttle option.",
606 (uchar*) &ibx_xtrabackup_throttle, (uchar*) &ibx_xtrabackup_throttle,
607 0, GET_LONG, REQUIRED_ARG, 0, 0, LONG_MAX, 0, 1, 0},
608
609 {"tmpdir", 't', "This option specifies the location where a temporary "
610 "files will be stored. If the option is not specified, the default is "
611 "to use the value of tmpdir read from the server configuration.",
612 (uchar*) &ibx_opt_mysql_tmpdir,
613 (uchar*) &ibx_opt_mysql_tmpdir, 0, GET_STR, REQUIRED_ARG,
614 0, 0, 0, 0, 0, 0},
615
616 {"use-memory", OPT_USE_MEMORY, "This option accepts a string argument "
617 "that specifies the amount of memory in bytes for xtrabackup to use "
618 "for crash recovery while preparing a backup. Multiples are supported "
619 "providing the unit (e.g. 1MB, 1GB). It is used only with the option "
620 "--apply-log. It is passed directly to xtrabackup's --use-memory "
621 "option. See the xtrabackup documentation for details.",
622 (uchar*) &ibx_xtrabackup_use_memory,
623 (uchar*) &ibx_xtrabackup_use_memory,
624 0, GET_LL, REQUIRED_ARG, 100*1024*1024L, 1024*1024L, LONGLONG_MAX, 0,
625 1024*1024L, 0},
626
627 { 0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
628};
629
630
631static void usage(void)
632{
633 puts("Open source backup tool for InnoDB and XtraDB\n\
634\n\
635Copyright (C) 2009-2015 Percona LLC and/or its affiliates.\n\
636Portions Copyright (C) 2000, 2011, MySQL AB & Innobase Oy. All Rights Reserved.\n\
637\n\
638This program is free software; you can redistribute it and/or\n\
639modify it under the terms of the GNU General Public License\n\
640as published by the Free Software Foundation version 2\n\
641of the License.\n\
642\n\
643This program is distributed in the hope that it will be useful,\n\
644but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
645MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
646GNU General Public License for more details.\n\
647\n\
648You can download full text of the license on http://www.gnu.org/licenses/gpl-2.0.txt\n\n");
649
650 puts("innobackupex - Non-blocking backup tool for InnoDB, XtraDB and HailDB databases\n\
651\n\
652SYNOPOSIS\n\
653\n\
654innobackupex [--compress] [--compress-threads=NUMBER-OF-THREADS] [--compress-chunk-size=CHUNK-SIZE]\n\
655 [--include=REGEXP] [--user=NAME]\n\
656 [--password=WORD] [--port=PORT] [--socket=SOCKET]\n\
657 [--no-timestamp] [--ibbackup=IBBACKUP-BINARY]\n\
658 [--slave-info] [--galera-info] [--stream=tar|xbstream]\n\
659 [--defaults-file=MY.CNF] [--defaults-group=GROUP-NAME]\n\
660 [--databases=LIST] [--no-lock] \n\
661 [--tmpdir=DIRECTORY] [--tables-file=FILE]\n\
662 [--history=NAME]\n\
663 [--incremental] [--incremental-basedir]\n\
664 [--incremental-dir] [--incremental-force-scan] [--incremental-lsn]\n\
665 [--incremental-history-name=NAME] [--incremental-history-uuid=UUID]\n\
666 [--close-files]\n\
667 BACKUP-ROOT-DIR\n\
668\n\
669innobackupex --apply-log [--use-memory=B]\n\
670 [--defaults-file=MY.CNF]\n\
671 [--export] [--ibbackup=IBBACKUP-BINARY]\n\
672 BACKUP-DIR\n\
673\n\
674innobackupex --copy-back [--defaults-file=MY.CNF] [--defaults-group=GROUP-NAME] BACKUP-DIR\n\
675\n\
676innobackupex --move-back [--defaults-file=MY.CNF] [--defaults-group=GROUP-NAME] BACKUP-DIR\n\
677\n\
678innobackupex [--decompress]\n\
679 [--parallel=NUMBER-OF-FORKS] BACKUP-DIR\n\
680\n\
681DESCRIPTION\n\
682\n\
683The first command line above makes a hot backup of a MySQL database.\n\
684By default it creates a backup directory (named by the current date\n\
685 and time) in the given backup root directory. With the --no-timestamp\n\
686option it does not create a time-stamped backup directory, but it puts\n\
687the backup in the given directory (which must not exist). This\n\
688command makes a complete backup of all MyISAM and InnoDB tables and\n\
689indexes in all databases or in all of the databases specified with the\n\
690--databases option. The created backup contains .frm, .MRG, .MYD,\n\
691.MYI, .MAD, .MAI, .TRG, .TRN, .ARM, .ARZ, .CSM, CSV, .opt, .par, and\n\
692InnoDB data and log files. The MY.CNF options file defines the\n\
693location of the database. This command connects to the MySQL server\n\
694using the mysql client program, and runs xtrabackup as a child\n\
695process.\n\
696\n\
697The --apply-log command prepares a backup for starting a MySQL\n\
698server on the backup. This command recovers InnoDB data files as specified\n\
699in BACKUP-DIR/backup-my.cnf using BACKUP-DIR/ib_logfile0,\n\
700and creates new InnoDB log files as specified in BACKUP-DIR/backup-my.cnf.\n\
701The BACKUP-DIR should be the path to a backup directory created by\n\
702xtrabackup. This command runs xtrabackup as a child process, but it does not \n\
703connect to the database server.\n\
704\n\
705The --copy-back command copies data, index, and log files\n\
706from the backup directory back to their original locations.\n\
707The MY.CNF options file defines the original location of the database.\n\
708The BACKUP-DIR is the path to a backup directory created by xtrabackup.\n\
709\n\
710The --move-back command is similar to --copy-back with the only difference that\n\
711it moves files to their original locations rather than copies them. As this\n\
712option removes backup files, it must be used with caution. It may be useful in\n\
713cases when there is not enough free disk space to copy files.\n\
714\n\
715The --decompress command will decompress a backup made\n\
716with the --compress option. The\n\
717--parallel option will allow multiple files to be decompressed\n\
718simultaneously. In order to decompress, the qpress utility MUST be installed\n\
719and accessable within the path. This process will remove the original\n\
720compressed files and leave the results in the same location.\n\
721\n\
722On success the exit code innobackupex is 0. A non-zero exit code \n\
723indicates an error.\n");
724 printf("Usage: [%s [--defaults-file=#] --backup | %s [--defaults-file=#] --prepare] [OPTIONS]\n", my_progname, my_progname);
725 my_print_help(ibx_long_options);
726}
727
728
729static
730my_bool
731ibx_get_one_option(int optid,
732 const struct my_option *opt __attribute__((unused)),
733 char *argument)
734{
735 switch(optid) {
736 case '?':
737 usage();
738 exit(0);
739 break;
740 case 'v':
741 msg("innobackupex version %s %s (%s)\n",
742 MYSQL_SERVER_VERSION,
743 SYSTEM_TYPE, MACHINE_TYPE);
744 exit(0);
745 break;
746 case OPT_HISTORY:
747 if (argument) {
748 opt_ibx_history = argument;
749 } else {
750 opt_ibx_history = "";
751 }
752 break;
753 case OPT_STREAM:
754 if (!strcasecmp(argument, "xbstream"))
755 xtrabackup_stream_fmt = XB_STREAM_FMT_XBSTREAM;
756 else {
757 ibx_msg("Invalid --stream argument: %s\n", argument);
758 return 1;
759 }
760 xtrabackup_stream = TRUE;
761 break;
762 case OPT_COMPRESS:
763 if (argument == NULL)
764 xtrabackup_compress_alg = "quicklz";
765 else if (strcasecmp(argument, "quicklz"))
766 {
767 ibx_msg("Invalid --compress argument: %s\n", argument);
768 return 1;
769 }
770 xtrabackup_compress = TRUE;
771 break;
772 case 'p':
773 if (argument)
774 {
775 char *start = argument;
776 my_free(opt_ibx_password);
777 opt_ibx_password= my_strdup(argument, MYF(MY_FAE));
778 /* Destroy argument */
779 while (*argument)
780 *argument++= 'x';
781 if (*start)
782 start[1]=0 ;
783 }
784 break;
785 }
786 return(0);
787}
788
789bool
790make_backup_dir()
791{
792 time_t t = time(NULL);
793 char buf[100];
794
795 if (!opt_ibx_notimestamp && !ibx_xtrabackup_stream_str) {
796 strftime(buf, sizeof(buf), "%Y-%m-%d_%H-%M-%S", localtime(&t));
797 ut_a(asprintf(&ibx_backup_directory, "%s/%s",
798 ibx_position_arg, buf) != -1);
799 } else {
800 ibx_backup_directory = strdup(ibx_position_arg);
801 }
802
803 if (!directory_exists(ibx_backup_directory, true)) {
804 return(false);
805 }
806
807 return(true);
808}
809
810bool
811ibx_handle_options(int *argc, char ***argv)
812{
813 int i, n_arguments;
814
815 if (handle_options(argc, argv, ibx_long_options, ibx_get_one_option)) {
816 return(false);
817 }
818
819 if (opt_ibx_apply_log) {
820 ibx_mode = IBX_MODE_APPLY_LOG;
821 } else if (opt_ibx_copy_back) {
822 ibx_mode = IBX_MODE_COPY_BACK;
823 } else if (opt_ibx_move_back) {
824 ibx_mode = IBX_MODE_MOVE_BACK;
825 } else if (opt_ibx_decompress) {
826 ibx_mode = IBX_MODE_DECRYPT_DECOMPRESS;
827 } else {
828 ibx_mode = IBX_MODE_BACKUP;
829 }
830
831 /* find and save position argument */
832 i = 0;
833 n_arguments = 0;
834 while (i < *argc) {
835 char *opt = (*argv)[i];
836
837 if (strncmp(opt, "--", 2) != 0
838 && !(strlen(opt) == 2 && opt[0] == '-')) {
839 if (ibx_position_arg != NULL
840 && ibx_position_arg != opt) {
841 ibx_msg("Error: extra argument found %s\n",
842 opt);
843 }
844 ibx_position_arg = opt;
845 ++n_arguments;
846 }
847 ++i;
848 }
849
850 *argc -= n_arguments;
851 if (n_arguments > 1) {
852 return(false);
853 }
854
855 if (ibx_position_arg == NULL) {
856 ibx_msg("Missing argument\n");
857 return(false);
858 }
859
860 /* set argv[0] to be the program name */
861 --(*argv);
862 ++(*argc);
863
864 return(true);
865}
866
867/*********************************************************************//**
868Parse command-line options, connect to MySQL server,
869detect server capabilities, etc.
870@return true on success. */
871bool
872ibx_init()
873{
874 const char *run;
875
876 /*=====================*/
877 xtrabackup_copy_back = opt_ibx_copy_back;
878 xtrabackup_move_back = opt_ibx_move_back;
879 opt_galera_info = opt_ibx_galera_info;
880 opt_slave_info = opt_ibx_slave_info;
881 opt_no_lock = opt_ibx_no_lock;
882 opt_safe_slave_backup = opt_ibx_safe_slave_backup;
883 opt_rsync = opt_ibx_rsync;
884 opt_force_non_empty_dirs = opt_ibx_force_non_empty_dirs;
885 opt_noversioncheck = opt_ibx_noversioncheck;
886 opt_no_backup_locks = opt_ibx_no_backup_locks;
887 opt_decompress = opt_ibx_decompress;
888
889 opt_incremental_history_name = opt_ibx_incremental_history_name;
890 opt_incremental_history_uuid = opt_ibx_incremental_history_uuid;
891
892 opt_user = opt_ibx_user;
893 opt_password = opt_ibx_password;
894 opt_host = opt_ibx_host;
895 opt_defaults_group = opt_ibx_defaults_group;
896 opt_socket = opt_ibx_socket;
897 opt_port = opt_ibx_port;
898
899 opt_lock_wait_query_type = opt_ibx_lock_wait_query_type;
900 opt_kill_long_query_type = opt_ibx_kill_long_query_type;
901
902 opt_kill_long_queries_timeout = opt_ibx_kill_long_queries_timeout;
903 opt_lock_wait_timeout = opt_ibx_lock_wait_timeout;
904 opt_lock_wait_threshold = opt_ibx_lock_wait_threshold;
905 opt_debug_sleep_before_unlock = opt_ibx_debug_sleep_before_unlock;
906 opt_safe_slave_backup_timeout = opt_ibx_safe_slave_backup_timeout;
907
908 opt_history = opt_ibx_history;
909
910 /* setup xtrabackup options */
911 xb_close_files = ibx_xb_close_files;
912 xtrabackup_compress_alg = ibx_xtrabackup_compress_alg;
913 xtrabackup_compress_threads = ibx_xtrabackup_compress_threads;
914 xtrabackup_compress_chunk_size = ibx_xtrabackup_compress_chunk_size;
915 xtrabackup_export = ibx_xtrabackup_export;
916 xtrabackup_extra_lsndir = ibx_xtrabackup_extra_lsndir;
917 xtrabackup_incremental_basedir = ibx_xtrabackup_incremental_basedir;
918 xtrabackup_incremental_dir = ibx_xtrabackup_incremental_dir;
919 xtrabackup_incremental_force_scan =
920 ibx_xtrabackup_incremental_force_scan;
921 xtrabackup_log_copy_interval = ibx_xtrabackup_log_copy_interval;
922 xtrabackup_incremental = ibx_xtrabackup_incremental;
923 xtrabackup_parallel = ibx_xtrabackup_parallel;
924 xtrabackup_stream_str = ibx_xtrabackup_stream_str;
925 xtrabackup_tables_file = ibx_xtrabackup_tables_file;
926 xtrabackup_throttle = ibx_xtrabackup_throttle;
927 opt_mysql_tmpdir = ibx_opt_mysql_tmpdir;
928 xtrabackup_use_memory = ibx_xtrabackup_use_memory;
929
930 if (!opt_ibx_incremental
931 && (xtrabackup_incremental
932 || xtrabackup_incremental_basedir
933 || opt_ibx_incremental_history_name
934 || opt_ibx_incremental_history_uuid)) {
935 ibx_msg("Error: --incremental-lsn, --incremental-basedir, "
936 "--incremental-history-name and "
937 "--incremental-history-uuid require the "
938 "--incremental option.\n");
939 return(false);
940 }
941
942 if (opt_ibx_databases != NULL) {
943 if (is_path_separator(*opt_ibx_databases)) {
944 xtrabackup_databases_file = opt_ibx_databases;
945 } else {
946 xtrabackup_databases = opt_ibx_databases;
947 }
948 }
949
950 /* --tables and --tables-file options are xtrabackup only */
951 ibx_partial_backup = (opt_ibx_include || opt_ibx_databases);
952
953 if (ibx_mode == IBX_MODE_BACKUP) {
954
955 if (!make_backup_dir()) {
956 return(false);
957 }
958 }
959
960 /* --binlog-info is xtrabackup only, so force
961 --binlog-info=ON. i.e. behavior before the feature had been
962 implemented */
963 opt_binlog_info = BINLOG_INFO_ON;
964
965 switch (ibx_mode) {
966 case IBX_MODE_APPLY_LOG:
967 xtrabackup_prepare = TRUE;
968 xtrabackup_target_dir = ibx_position_arg;
969 run = "apply-log";
970 break;
971 case IBX_MODE_BACKUP:
972 xtrabackup_backup = TRUE;
973 xtrabackup_target_dir = ibx_backup_directory;
974 if (opt_ibx_include != NULL) {
975 xtrabackup_tables = opt_ibx_include;
976 }
977 run = "backup";
978 break;
979 case IBX_MODE_COPY_BACK:
980 xtrabackup_copy_back = TRUE;
981 xtrabackup_target_dir = ibx_position_arg;
982 run = "copy-back";
983 break;
984 case IBX_MODE_MOVE_BACK:
985 xtrabackup_move_back = TRUE;
986 xtrabackup_target_dir = ibx_position_arg;
987 run = "move-back";
988 break;
989 case IBX_MODE_DECRYPT_DECOMPRESS:
990 xtrabackup_decrypt_decompress = TRUE;
991 xtrabackup_target_dir = ibx_position_arg;
992 run = "decompress";
993 break;
994 default:
995 ut_error;
996 }
997
998 ibx_msg("Starting the %s operation\n\n"
999 "IMPORTANT: Please check that the %s run completes "
1000 "successfully.\n"
1001 " At the end of a successful %s run innobackupex\n"
1002 " prints \"completed OK!\".\n\n", run, run, run);
1003
1004
1005 return(true);
1006}
1007
1008void
1009ibx_cleanup()
1010{
1011 free(ibx_backup_directory);
1012}
1013