~akopytov/percona-xtrabackup/bug950334-2.1

342 by Alexey Kopytov
Cherrypick of fix for bug #892393.
1
#!/usr/bin/env perl
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2
#
3
# A script for making backups of InnoDB and MyISAM tables, indexes and .frm
4
# files.
5
#
242.62.3 by Alexey Kopytov
s/Percona Inc/Percona Ireland Ltd/g
6
# Copyright 2003, 2009 Innobase Oy and Percona Ireland Ltd 2009-2012.
7
# All Rights Reserved.
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
8
#
9
10
use strict;
11
use Getopt::Long;
12
use File::Spec;
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
13
use Pod::Usage qw(pod2usage);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
14
use POSIX "strftime";
15
use POSIX ":sys_wait_h";
16
use FileHandle;
109.1.1 by Aleksandr Kuzminsky
Fixed Bug #434486: Using dirname(), basename() instead of manual parsing of filenames
17
use File::Basename;
340 by Alexey Kopytov
Cherrypick of fix for bug #687544.
18
use File::Temp;
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
19
use File::Find;
20
use File::Copy;
121.1.1 by Daniel Nichter
Get ibbackup/xtrabackup exit code. Exit with same.
21
use English qw(-no_match_vars);
391.76.1 by Sergei Glushchenko
Bug 1095551
22
use Time::HiRes qw(usleep);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
23
24
# version of this script
25
my $innobackup_version = '1.5.1-xtrabackup';
109.3.1 by Aleksandr Kuzminsky
Fixed Bug #463709: The script outputs its own name
26
my $innobackup_script = basename($0);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
27
28
# copyright notice
356 by Alexey Kopytov
Cherrypick of (C) string update due to 2012.
29
my $copyright_notice =
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
30
"InnoDB Backup Utility v${innobackup_version}; Copyright 2003, 2009 Innobase Oy
242.62.3 by Alexey Kopytov
s/Percona Inc/Percona Ireland Ltd/g
31
and Percona Ireland Ltd 2009-2012.  All Rights Reserved.
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
32
33
This software is published under
34
the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
35
36
";
37
38
# required Perl version (5.005)
39
my @required_perl_version = (5, 0, 5);
40
my $required_perl_version_old_style = 5.005;
41
42
# force flush after every write and print
43
$| = 1;
44
391.1.43 by Alexey Kopytov
Bug #1003518: innobackupex does not copy back table files in subfolders
45
# disable nlink count optimization, see File::Find documentation for details
46
$File::Find::dont_use_nlink=1;
47
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
48
######################################################################
49
# modifiable parameters
50
######################################################################
51
52
# maximum number of files in a database directory which are
53
# separately printed when a backup is made
54
my $backup_file_print_limit = 9;
55
56
# timeout in seconds for a reply from mysql
57
my $mysql_response_timeout = 900;
58
59
# default compression level (this is an argument to ibbackup)
60
my $default_compression_level = 1;
61
62
# time in seconds after which a dummy query is sent to mysql server
63
# in order to keep the database connection alive
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
64
my $mysql_keep_alive = 5;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
65
66
######################################################################
67
# end of modifiable parameters
68
######################################################################
69
70
71
# command line options
72
my $option_help = '';
73
my $option_version = '';
74
my $option_apply_log = '';
181 by root
added --redo-only option to innobackupex
75
my $option_redo_only = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
76
my $option_copy_back = '';
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
77
my $option_move_back = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
78
my $option_include = '';
79
my $option_databases = '';
166.1.1 by Daniel Nichter
Add --tables-file to innobackupex.
80
my $option_tables_file = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
81
my $option_throttle = '';
82
my $option_sleep = '';
83
my $option_compress = 999;
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
84
my $option_compress_threads = 1;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
85
my $option_uncompress = '';
61 by kinoyasu
innobackupex can pass --export option
86
my $option_export = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
87
my $option_use_memory = '';
88
my $option_mysql_password = '';
89
my $option_mysql_user = '';
90
my $option_mysql_port = '';
91
my $option_mysql_socket = '';
116.1.1 by Daniel Nichter
Bug 510965: add --host, remove --host=127.0.0.1 from --port.
92
my $option_mysql_host = '';
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
93
my $option_defaults_group = 'mysqld';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
94
my $option_no_timestamp = '';
95
my $option_slave_info = '';
353 by Alexey Kopytov
Cherrypick of Galera support.
96
my $option_galera_info = '';
100.1.1 by Aleksandr Kuzminsky
--no-lock option is added
97
my $option_no_lock = '';
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
98
my $option_ibbackup_binary = 'autodetect';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
99
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
100
my $option_defaults_file = '';
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
101
my $option_defaults_extra_file = '';
14 by kinoyasu
using rsync for incremental backup
102
my $option_incremental = '';
130 by Vadim Tkachenko
bugfix 546876
103
my $option_incremental_basedir = '';
218 by Vadim Tkachenko
Added option --incremental-dir to innobackupex script
104
my $option_incremental_dir = '';
219 by Vadim Tkachenko
Added option --incremental-lsn to innobackupex script.
105
my $option_incremental_lsn = '';
220 by Vadim Tkachenko
Bugfix #680936
106
my $option_extra_lsndir = '';
14 by kinoyasu
using rsync for incremental backup
107
my $option_remote_host = '';
242.25.4 by Alexey Kopytov
Implementation of
108
my $option_rsync = '';
22 by kinoyasu
"innobackup --stream=tar" is supported
109
my $option_stream = '';
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
110
my $stream_cmd = '';
22 by kinoyasu
"innobackup --stream=tar" is supported
111
my $option_tmpdir = '';
14 by kinoyasu
using rsync for incremental backup
112
68 by kinoyasu
new mode of innobackupex --stream=tar4ibd; new command tar4ibd based on libtar-1.2.11
113
my $option_tar4ibd = '';
70.1.1 by Aleksandr Kuzminsky
Allow configuration of scp options
114
my $option_scp_opt = '-Cp -c arcfour';
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
115
my $option_ssh_opt = '';
70.1.1 by Aleksandr Kuzminsky
Allow configuration of scp options
116
195 by Alexey Kopytov
Implementation of parallel data files transfer for xtrabackup.
117
my $option_parallel = '';
118
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
119
my $option_safe_slave_backup = '';
120
my $option_safe_slave_backup_timeout = 300;
121
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
122
# name of the my.cnf configuration file
123
#my $config_file = '';
124
125
# root of the backup directory
126
my $backup_root = '';
127
128
# backup directory pathname
129
my $backup_dir = '';
130
131
# name of the ibbackup suspend-at-end file
132
my $suspend_file = '';
133
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
134
# name of the temporary transaction log file during the backup
135
my $tmp_logfile = '';
136
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
137
# home directory of innoDB log files
138
my $innodb_log_group_home_dir = '';
139
140
# backup my.cnf file
14 by kinoyasu
using rsync for incremental backup
141
my $backup_config_file = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
142
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
143
# whether slave SQL thread is running when wait_for_safe_slave() is called
144
my $sql_thread_started = 0;
242.24.1 by Lachlan Mulcahy
Fixed bug 860879 - innobackupex fails if mysql is not a slave, but --safe-slave-backup is specified.
145
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
146
# options from the options file
147
my %config;
148
149
# options from the backup options file
150
#my %backup_config;
151
152
# list of databases to be included in a backup
153
my %databases_list;
154
171 by Daniel Nichter
Filter files copied according to --tables-file.
155
# list of tables to be included in a backup
156
my %table_list;
157
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
158
# prefix for output lines
109.3.1 by Aleksandr Kuzminsky
Fixed Bug #463709: The script outputs its own name
159
my $prefix = "$innobackup_script:";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
160
161
# process id of mysql client program (runs as a child process of this script)
162
my $mysql_pid = '';
163
164
# mysql server version string
165
my $mysql_server_version = '';
166
167
# name of the file where stderr of mysql process is directed
340 by Alexey Kopytov
Cherrypick of fix for bug #687544.
168
my $mysql_stderr_fh = File::Temp->new();
169
my $mysql_stderr = $mysql_stderr_fh->filename;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
170
171
# name of the file where stdout of mysql process is directed
340 by Alexey Kopytov
Cherrypick of fix for bug #687544.
172
my $mysql_stdout_fh = File::Temp->new();
173
my $mysql_stdout = $mysql_stdout_fh->filename;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
174
175
# name of the file where binlog position info is written
176
my $binlog_info;
177
353 by Alexey Kopytov
Cherrypick of Galera support.
178
# name of the file where galera position info is written
179
my $galera_info;
180
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
181
# name of the file where slave info is written
182
my $slave_info;
183
184
# mysql binlog position as given by "SHOW MASTER STATUS" command
185
my $mysql_binlog_position = '';
186
187
# mysql master's binlog position as given by "SHOW SLAVE STATUS" command
188
# run on a slave server
189
my $mysql_slave_position = '';
190
191
# process id of ibbackup program (runs as a child process of this script)
192
my $ibbackup_pid = '';
193
194
# a counter for numbering mysql connection checks
195
my $hello_id = 0;
196
197
# the request which has been sent to mysqld, but to which
198
# mysqld has not yet replied. Empty string denotes that no
199
# request has been sent to mysqld or that mysqld has replied
200
# to all requests.
201
my $current_mysql_request = '';
202
203
# escape sequences for options files
204
my %option_value_escapes = ('b' => "\b",
205
                            't' => "\t",
206
                            'n' => "\n",
207
                            'r' => "\r",
208
                            "\\" => "\\",
209
                            's' => ' ');
210
211
# signal that is sent to child processes when they are killed
212
my $kill_signal = 15;
213
214
# current local time
215
my $now;
216
66 by Vadim Tkachenko
Added incremental option to innobackupex-1.5.1 script
217
# incremental backup base directory
218
my $incremental_basedir = '';
219
108.1.4 by Aleksandr Kuzminsky
escape_path() function is added.
220
my $src_name;
221
my $dst_name;
222
my $win = ($^O eq 'MSWin32' ? 1 : 0);
223
my $CP_CMD = ($win eq 1 ? "copy /Y" : "cp -p");
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
224
my $xtrabackup_binary_file = 'xtrabackup_binary';
353 by Alexey Kopytov
Cherrypick of Galera support.
225
my $xtrabackup_pid_file = 'xtrabackup_pid';
242.25.4 by Alexey Kopytov
Implementation of
226
my %rsync_files_hash;
227
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
228
my $copy_dir_src;
229
my $copy_dir_dst;
230
my $copy_dir_exclude_regexp;
231
my $copy_dir_overwrite;
232
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
233
######################################################################
234
# program execution begins here
235
######################################################################
236
237
# check command-line args
238
check_args();
239
240
# print program version and copyright
241
print_version();
242
243
# initialize global variables and perform some checks
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
244
if ($option_copy_back || $option_move_back) {
242.11.8 by Alexey Kopytov
Bug #817132: innobackupex copy-back doesn't work without ibbackup
245
    $option_ibbackup_binary = 'xtrabackup' if ($option_ibbackup_binary eq 'autodetect');
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
246
} elsif ($option_apply_log) {
247
	# Read XtraBackup version from backup dir
248
	if (-e "$backup_dir/$xtrabackup_binary_file") {
249
		# Read XtraBackup version from file
250
		open XTRABACKUP_BINARY, "$backup_dir/$xtrabackup_binary_file"
251
			or die "Cannot open file $backup_dir/$xtrabackup_binary_file: $!\n";
252
		$option_ibbackup_binary = <XTRABACKUP_BINARY>;
253
		close XTRABACKUP_BINARY;
254
		}
255
256
	else {
257
		if( $option_ibbackup_binary eq "autodetect" ){
258
			# Try to connect MySQL and get the version
242.32.1 by Alexey Kopytov
Bug #514068: Output to STDOUT and STDERR is not conventional
259
			print STDERR "option_ibbackup_binary is autodetect, trying to connect to MySQL\n";
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
260
			my $options = get_mysql_options();
261
			$mysql_pid = open(*MYSQL_WRITER, "| mysql $options >$mysql_stdout 2>$mysql_stderr ");
242.32.1 by Alexey Kopytov
Bug #514068: Output to STDOUT and STDERR is not conventional
262
			print STDERR "Connected to MySQL with pid $mysql_pid\n";
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
263
			sleep 1;
264
			if ($mysql_pid && $mysql_pid == waitpid($mysql_pid, &WNOHANG)) {
265
				my $reason = `cat $mysql_stderr`;
266
				$mysql_pid = '';
267
				# Failed to connect to MySQL
391.54.1 by Hrvoje Matijakovic
* bugfixes for bugs: Bug #1066836, Bug #1059945, Bug #1065761
268
				die "Failed to connect to MySQL server to detect version.\nYou must set xtrabackup version to use with --ibbackup option.\nPossible values are xtrabackup_51 (for MySQL 5.0 and 5.1), xtrabackup_55 (for MySQL 5.5) or xtrabackup (for MySQL 5.1 with InnoDB plugin or Percona Server)\n";
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
269
				}
270
			else{
271
				mysql_close();
242.32.1 by Alexey Kopytov
Bug #514068: Output to STDOUT and STDERR is not conventional
272
				print STDERR "Connected successfully\n";
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
273
				$option_ibbackup_binary = set_xtrabackup_version();
274
				}
275
			}
276
		}
255.2.1 by Stewart Smith
innobackupex ignores --ibbackup and calls set_xtrabackup_version() anyway.
277
} elsif ($option_ibbackup_binary eq 'autodetect') {
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
278
    $option_ibbackup_binary = set_xtrabackup_version();
279
}
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
280
init();
281
121.1.1 by Daniel Nichter
Get ibbackup/xtrabackup exit code. Exit with same.
282
my $ibbackup_exit_code = 0;
283
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
284
if ($option_copy_back) {
285
    # copy files from backup directory back to their original locations
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
286
    copy_back(0);
287
} elsif ($option_move_back) {
288
    # move files from backup directory back to their original locations
289
    copy_back(1);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
290
} elsif ($option_apply_log) {
291
    # expand data files in backup directory by applying log files to them
292
    apply_log();
293
} else {
294
    # make a backup of InnoDB and MyISAM tables, indexes and .frm files.
121.1.1 by Daniel Nichter
Get ibbackup/xtrabackup exit code. Exit with same.
295
    $ibbackup_exit_code = backup();
259 by Alexey Kopytov
Bug #787988: innobackupex fails with --remote-host when trying to create
296
    if ($option_remote_host) {
297
      open(XTRABACKUP_BINARY,
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
298
	   "| ssh $option_ssh_opt $option_remote_host 'cat > $backup_dir/$xtrabackup_binary_file'")
259 by Alexey Kopytov
Bug #787988: innobackupex fails with --remote-host when trying to create
299
	|| die "Failed to open file '$option_remote_host:$backup_dir/$xtrabackup_binary_file': $!";
300
    } elsif ($option_stream) {
301
      open XTRABACKUP_BINARY, "> $option_tmpdir/$xtrabackup_binary_file"
302
    	|| die "Cannot open file $option_tmpdir/$xtrabackup_binary_file: $!\n";
303
    } else {
304
      open XTRABACKUP_BINARY, "> $backup_dir/$xtrabackup_binary_file"
305
    	|| die "Cannot open file $backup_dir/$xtrabackup_binary_file: $!\n";
306
    }
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
307
    print XTRABACKUP_BINARY $option_ibbackup_binary;
308
    close XTRABACKUP_BINARY;
259 by Alexey Kopytov
Bug #787988: innobackupex fails with --remote-host when trying to create
309
310
    if ($option_stream) {
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
311
      system("cd $option_tmpdir; $stream_cmd $xtrabackup_binary_file")
259 by Alexey Kopytov
Bug #787988: innobackupex fails with --remote-host when trying to create
312
	&& die "Failed to stream $xtrabackup_binary_file: $!";
313
      unlink "$option_tmpdir/$xtrabackup_binary_file";
314
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
315
}
316
317
$now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
318
319
if ($option_stream eq 'tar') {
121.1.1 by Daniel Nichter
Get ibbackup/xtrabackup exit code. Exit with same.
320
   print STDERR "$prefix You must use -i (--ignore-zeros) option for extraction of the tar stream.\n";
321
}
322
323
if ( $ibbackup_exit_code == 0 ) {
324
   # program has completed successfully
325
   print STDERR "$now  $prefix completed OK!\n";
326
}
327
else {
328
   print STDERR "$now  $prefix $option_ibbackup_binary failed! (exit code $ibbackup_exit_code)  The backup may not be complete.\n";
329
}
330
331
exit $ibbackup_exit_code;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
332
333
######################################################################
334
# end of program execution
335
######################################################################
336
337
338
#
339
# print_version subroutine prints program version and copyright.
340
#
341
sub print_version {
22 by kinoyasu
"innobackup --stream=tar" is supported
342
    printf(STDERR $copyright_notice);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
343
}
344
345
346
#
347
# usage subroutine prints instructions of how to use this program to stdout.
348
#
349
sub usage {
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
350
   my $msg = shift || '';
351
   pod2usage({ -msg => $msg, -verbose => 1});
352
   return 0;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
353
}
354
355
356
#
357
# return current local time as string in form "070816 12:23:15"
358
#
359
sub current_time {
360
    return strftime("%y%m%d %H:%M:%S", localtime());
361
}
362
363
364
#
365
# Die subroutine kills all child processes and exits this process.
366
# This subroutine takes the same argument as the built-in die function.
367
#    Parameters:
368
#       message   string which is printed to stdout
369
#
370
sub Die {
371
    my $message = shift;
372
    my $extra_info = '';
373
374
    # kill all child processes of this process
375
    kill_child_processes();
376
377
    if ($current_mysql_request) {
378
        $extra_info = " while waiting for reply to MySQL request:" .
379
            " '$current_mysql_request'";
380
    }
381
    die "$prefix Error: $message$extra_info";
382
}
383
    
384
385
#
386
# backup subroutine makes a backup of InnoDB and MyISAM tables, indexes and 
387
# .frm files. It connects to the database server and runs ibbackup as a child
388
# process.
389
#
390
sub backup {
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
391
    my $orig_datadir = get_option(\%config, $option_defaults_group, 'datadir');
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
392
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
393
    # check that we can connect to the database. This done by
394
    # connecting, issuing a query, and closing the connection.
395
    mysql_open();
396
    mysql_close();
397
398
    # start ibbackup as a child process
399
    start_ibbackup();
400
401
    # wait for ibbackup to suspend itself
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
402
    if (!$option_remote_host) {
14 by kinoyasu
using rsync for incremental backup
403
        wait_for_ibbackup_suspend();
404
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
405
242.22.1 by Lachlan Mulcahy
Fixed bug 857788
406
    # connect to database
407
    mysql_open();
408
242.14.1 by Alexey Kopytov
Bug #834657: --slave-info does not work with --no-lock
409
    if ($option_safe_slave_backup) {
410
      wait_for_safe_slave();
411
    }
412
255.1.1 by Vadim Tkachenko
During incremental backup
413
    # flush tables with read lock
330.1.1 by Alexey Kopytov
Manual merge from 1.6 (conflict in innobackupex).
414
    if (!$option_no_lock) {
242.25.4 by Alexey Kopytov
Implementation of
415
        # make a prep copy before locking tables, if using rsync
416
        backup_files(1);
417
66 by Vadim Tkachenko
Added incremental option to innobackupex-1.5.1 script
418
        # flush tables with read lock
242.25.4 by Alexey Kopytov
Implementation of
419
        mysql_lockall();
66 by Vadim Tkachenko
Added incremental option to innobackupex-1.5.1 script
420
    }
255.1.1 by Vadim Tkachenko
During incremental backup
421
242.22.1 by Lachlan Mulcahy
Fixed bug 857788
422
    if ($option_slave_info) {
423
        write_slave_info();
424
    }
425
242.14.1 by Alexey Kopytov
Bug #834657: --slave-info does not work with --no-lock
426
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
427
    # backup non-InnoDB files and tables
242.25.4 by Alexey Kopytov
Implementation of
428
    # (or finalize the backup by syncing changes if using rsync)
429
    backup_files(0);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
430
391.53.7 by Alexey Kopytov
Bug #1055989: innobackupex waits for streaming temporary log file to
431
    # resume ibbackup and wait till log copying is finished
432
    resume_ibbackup();
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
433
242.61.1 by Alexey Kopytov
Bug #771981: myisam tables are not locked during incremental backup
434
    # release read locks on all tables
435
    mysql_unlockall() if !$option_no_lock;
255.1.1 by Vadim Tkachenko
During incremental backup
436
391.53.7 by Alexey Kopytov
Bug #1055989: innobackupex waits for streaming temporary log file to
437
    my $ibbackup_exit_code = wait_for_ibbackup_finish();
438
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
439
    if ( $option_safe_slave_backup && $sql_thread_started) {
242.14.1 by Alexey Kopytov
Bug #834657: --slave-info does not work with --no-lock
440
      print STDERR "$prefix: Starting slave SQL thread\n";
441
      mysql_send('START SLAVE SQL_THREAD;');
442
    }
443
242.22.1 by Lachlan Mulcahy
Fixed bug 857788
444
    # Close the DB connection
445
    mysql_close();
446
380.2.1 by Sergei Glushchenko
Bug 543134.
447
    # copy ib_lru_dump
448
    if (-e "$orig_datadir/ib_lru_dump") {
449
        if ($option_remote_host) {
450
            print STDERR "$prefix Backing up file 'ib_lru_dump'\n";
451
            system("scp $option_scp_opt '$orig_datadir/ib_lru_dump' '$option_remote_host:$backup_dir/ib_lru_dump'")
452
                and Die "Failed to scp file 'ib_lru_dump': $!";
453
        } elsif ($option_stream) {
454
            print STDERR "$prefix Backing up as tar stream 'ib_lru_dump'\n";
391.21.1 by Sergei Glushchenko
Bug #983720: ib_lru_dump and --galera-info fail with --stream=xbstream
455
            system("cd $orig_datadir; $stream_cmd ib_lru_dump")
380.2.1 by Sergei Glushchenko
Bug 543134.
456
                and Die "Failed to stream 'ib_lru_dump': $!";
457
        } elsif (!$option_rsync) {
458
            my $src_name = escape_path("$orig_datadir/ib_lru_dump");
459
            my $dst_name = escape_path("$backup_dir/ib_lru_dump");
460
            system("$CP_CMD \"$src_name\" \"$dst_name\"")
461
                and Die "Failed to copy file 'ib_lru_dump': $!";
462
        }
463
    }
464
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
465
    if ($option_remote_host) {
70.1.1 by Aleksandr Kuzminsky
Allow configuration of scp options
466
        system("scp $option_scp_opt '$tmp_logfile' '$option_remote_host:$backup_dir/xtrabackup_logfile'")
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
467
            and Die "Failed to scp file '$option_remote_host:$backup_dir/xtrabackup_logfile': $!";
20 by kinoyasu
innobackup --include=REGEXP with --remote
468
        unlink $tmp_logfile || Die "Failed to delete '$tmp_logfile': $!";
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
469
374 by Alexey Kopytov
Cherrypick of fix for placement of xtrabackup_suspended and xtrabackup_checkpoint.
470
        system("scp $option_scp_opt '$option_tmpdir/xtrabackup_checkpoints' '$option_remote_host:$backup_dir/xtrabackup_checkpoints'")
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
471
            and Die "Failed to scp file '$option_remote_host:$backup_dir/xtrabackup_checkpoints': $!";
374 by Alexey Kopytov
Cherrypick of fix for placement of xtrabackup_suspended and xtrabackup_checkpoint.
472
        unlink "$option_tmpdir/xtrabackup_checkpoints" || Die "Failed to delete '$option_tmpdir/xtrabackup_checkpoints': $!";
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
473
    }
474
22 by kinoyasu
"innobackup --stream=tar" is supported
475
    print STDERR "\n$prefix Backup created in directory '$backup_dir'\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
476
    if ($mysql_binlog_position) {
22 by kinoyasu
"innobackup --stream=tar" is supported
477
        print STDERR "$prefix MySQL binlog position: $mysql_binlog_position\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
478
    }
479
    if ($mysql_slave_position && $option_slave_info) {
22 by kinoyasu
"innobackup --stream=tar" is supported
480
        print STDERR "$prefix MySQL slave binlog position: $mysql_slave_position\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
481
    }
121.1.1 by Daniel Nichter
Get ibbackup/xtrabackup exit code. Exit with same.
482
483
    return $ibbackup_exit_code;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
484
}
485
486
487
#
488
# are_equal_innodb_data_file_paths subroutine checks if the given
489
# InnoDB data file option values are equal.
490
#   Parameters:
491
#     str1    InnoDB data file path option value
492
#     str2    InnoDB data file path option value
493
#   Return value:
494
#     1  if values are equal
495
#     0  otherwise
496
#
497
sub are_equal_innodb_data_file_paths {
498
    my $str1 = shift;
499
    my $str2 = shift;
500
    my @array1 = split(/;/, $str1);
501
    my @array2 = split(/;/, $str2);
502
    
503
    if ($#array1 != $#array2) { return 0; }
504
505
    for (my $i = 0; $i <= $#array1; $i++) {
506
        my @def1 = split(/:/, $array1[$i]);
507
        my @def2 = split(/:/, $array2[$i]);
508
        
509
        if ($#def1 != $#def2) { return 0; }
510
511
        for (my $j = 0; $j <= $#def1; $j++) {
512
            if ($def1[$j] ne $def2[$j]) { return 0; }
513
        }
514
    }
515
    return 1;
516
}        
517
518
519
#
242.26.20 by Alexey Kopytov
Fix for bug #924026: innobackupex fails during backup if table is
520
# copy_if_exists subroutin attempt to copy a file from $src to $dst
521
# If the copy fails due to the file not existing, the error is ignored
522
#  Parameters:
523
#    $src    The source file to copy
524
#    $dst    The destination to copy to
525
#  Return value:
526
#    1  if copy was successful, or unsuccessful but the source file didn't exist
527
#    0  otherwise
528
#
529
sub copy_if_exists {
530
    my $src = shift;
531
    my $dst = shift;
532
533
    # Copy the file
534
    if( system("$CP_CMD \"$src\" \"$dst\"") != 0 ) {
535
        # if the copy failed, check if the file exists
536
        if( -e "$src" ) {
537
            # if the file exists, we had a real error
538
            return 0;
539
        }
540
    }
541
    # Success otherwise
542
    return 1;
543
}
544
545
546
#
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
547
# is_in_array subroutine checks if the given string is in the array.
548
#   Parameters:
549
#     str       a string
550
#     array_ref a reference to an array of strings
551
#   Return value:
552
#     1  if string is in the array
553
#     0  otherwise
554
# 
555
sub is_in_array {
556
    my $str = shift;
557
    my $array_ref = shift;
558
559
    if (grep { $str eq $_ } @{$array_ref}) {
560
        return 1;
561
    }
562
    return 0;
563
}
564
242.29.1 by Valentine Gostev
Fix for bug https://bugs.launchpad.net/percona-xtrabackup/+bug/737569
565
#
566
# if_directory_exists_and_empty accepts two arguments:
567
# variable with directory name and comment.
568
# Sub checks that directory exists and is empty
569
# usage: is_directory_exists_and_empty($directory,"Comment");
570
#
571
572
sub if_directory_exists_and_empty {
573
    my $empty_dir = shift;
574
    my $is_directory_empty_comment = shift;
575
    if (! -d $empty_dir) {
391.53.17 by Alexey Kopytov
Backport of the fix for bug #1089375 to 2.0:
576
        die "$is_directory_empty_comment directory '$empty_dir' does not exist!";
242.29.1 by Valentine Gostev
Fix for bug https://bugs.launchpad.net/percona-xtrabackup/+bug/737569
577
    }
391.53.17 by Alexey Kopytov
Backport of the fix for bug #1089375 to 2.0:
578
    opendir (my $dh, $empty_dir) or die "$is_directory_empty_comment directory '$empty_dir': Not a directory";
391.25.1 by Igor Tverdovskiy
my.cnf and master.info are excluded from empty destination dir validator now.
579
    if ( ! scalar( grep { $_ ne "." && $_ ne ".." && $_ ne "my.cnf" && $_ ne "master.info"} readdir($dh)) == 0) {
391.53.17 by Alexey Kopytov
Backport of the fix for bug #1089375 to 2.0:
580
        die "$is_directory_empty_comment directory '$empty_dir' is not empty!";
242.29.1 by Valentine Gostev
Fix for bug https://bugs.launchpad.net/percona-xtrabackup/+bug/737569
581
    }
582
    closedir($dh);
583
}
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
584
585
#
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
586
# copy() wrapper with error handling
587
#
588
sub copy_file {
589
    my $src_path = shift;
590
    my $dst_path = shift;
591
592
    print STDERR "$prefix Copying '$src_path' to '$dst_path'\n";
593
    copy($src_path, $dst_path) or Die "copy failed: $!";
594
}
595
596
#
597
# move() wrapper with error handling
598
#
599
sub move_file {
600
    my $src_path = shift;
601
    my $dst_path = shift;
602
603
    print STDERR "$prefix Moving '$src_path' to '$dst_path'\n";
604
    move($src_path, $dst_path) or Die "move failed: $!";
605
}
606
607
608
#
609
# Auxiliary function called from find() callbacks to copy or move files and create directories
610
# when necessary.
611
#
612
# SYNOPSIS
613
#
614
#     process_file(\&process_func);
615
#
616
#       &process_func is a code reference that does the actual file operation
617
#
618
sub process_file {
619
    my $process_func = shift;
620
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
621
    if (/$copy_dir_exclude_regexp/) {
622
	return;
623
    }
624
    (my $dst_path = $File::Find::name) =~ s/^$copy_dir_src/$copy_dir_dst/;
625
    if (-d "$File::Find::name") {
626
	# Create the directory in the destination if necessary
627
	if (! -e "$dst_path") {
628
	    print STDERR "$prefix Creating directory '$dst_path'\n";
629
	    mkdir "$dst_path" or Die "mkdir failed: $!";
630
	} elsif (! -d "$dst_path") {
631
	    Die "$dst_path exists, but is not a directory";
632
	}
633
    } else {
634
	# Don't overwrite files unless $copy_dir_overwrite is 1
635
	if (!$copy_dir_overwrite && -e "$copy_dir_dst/$_") {
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
636
	    Die "Failed to process file $File::Find::name: " .
637
		"not overwriting file $copy_dir_dst/$_";
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
638
	}
639
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
640
	&$process_func($File::Find::name, $dst_path);
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
641
    }
642
}
643
644
#
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
645
# find() callback to copy files
646
#
647
sub copy_file_callback {
648
    process_file(\&copy_file);
649
}
650
651
#
652
# find() callback to move files
653
#
654
sub move_file_callback {
655
    process_file(\&move_file);
656
}
657
658
#
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
659
# copy_dir_recursively subroutine does a recursive copy of a specified
660
# directory excluding files matching a specifies regexp. If $overwrite
661
# is 1, it overwrites the existing files.
662
#
663
# SYNOPSIS 
664
#
665
#     copy_dir_recursively($src_dir, $dst_dir, $exclude_regexp,
666
#                          $overwrite);
667
#
668
# TODO
669
#
670
#     use rsync when --rsync is specified
671
#
672
sub copy_dir_recursively {
242.55.1 by Alexey Kopytov
Bug #1002688: innobackupex incremental apply-log copies to wrong
673
    # Clean paths and remove trailing slashes if any
674
    $copy_dir_src = File::Spec->canonpath(shift);
675
    $copy_dir_dst = File::Spec->canonpath(shift);
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
676
    $copy_dir_exclude_regexp = shift;
677
    $copy_dir_overwrite = shift;
678
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
679
    find(\&copy_file_callback, $copy_dir_src);
680
}
681
682
#
683
# Similar to copy_dir_recursively, but moves files instead.
684
#
685
# SYNOPSIS
686
#
687
#     move_dir_recursively($src_dir, $dst_dir, $exclude_regexp,
688
#                          $overwrite);
689
#
690
# TODO
691
#
692
#     use rsync when --rsync is specified
693
#
694
sub move_dir_recursively {
695
    # Clean paths and remove trailing slashes if any
696
    $copy_dir_src = File::Spec->canonpath(shift);
697
    $copy_dir_dst = File::Spec->canonpath(shift);
698
    $copy_dir_exclude_regexp = shift;
699
    $copy_dir_overwrite = shift;
700
701
    find(\&move_file_callback, $copy_dir_src);
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
702
}
703
704
#
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
705
# copy_back subroutine copies data and index files from backup directory 
706
# back to their original locations.
707
#
708
sub copy_back {
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
709
    my $move_flag = shift;
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
710
    my $orig_datadir = get_option(\%config, $option_defaults_group, 'datadir');
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
711
    my $orig_ibdata_dir = 
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
712
        get_option(\%config, $option_defaults_group, 'innodb_data_home_dir');
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
713
    my $orig_innodb_data_file_path = 
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
714
        get_option(\%config, $option_defaults_group, 'innodb_data_file_path');
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
715
    my $orig_iblog_dir =
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
716
        get_option(\%config, $option_defaults_group, 'innodb_log_group_home_dir');
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
717
    my $iblog_files = 'ib_logfile.*';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
718
    my $excluded_files = 
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
719
        '\.\.?|backup-my\.cnf|xtrabackup_logfile|' .
391.1.50 by Alexey Kopytov
Bug #983695: --copy-back should ignore *.qp files
720
	'xtrabackup_binary|xtrabackup_binlog_info|xtrabackup_checkpoints|' .
721
        '.*\.qp|' .
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
722
	$iblog_files;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
723
    my $compressed_data_file = '.*\.ibz$';
724
    my $file;
725
    my $backup_innodb_data_file_path;
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
726
391.59.1 by Sergei Glushchenko
Bug #1066843: Fix for bug #932623 does not take separate doublewrite
727
    if (has_option(\%config, $option_defaults_group, 'innodb_doublewrite_file')) {
728
        my $doublewrite_file =
729
                get_option(\%config, $option_defaults_group,
730
                            'innodb_doublewrite_file');
731
        $excluded_files = $excluded_files . '|' . $doublewrite_file;
732
    }
733
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
734
    # check whether files should be copied or moved to dest directory
735
    my $move_or_copy_file = $move_flag ? \&move_file : \&copy_file;
736
    my $move_or_copy_dir = $move_flag ?
737
        \&move_dir_recursively : \&copy_dir_recursively;
738
    my $operation = $move_flag ? "move" : "copy";
739
740
242.29.1 by Valentine Gostev
Fix for bug https://bugs.launchpad.net/percona-xtrabackup/+bug/737569
741
    # check that original data directories exist and they are empty
742
    if_directory_exists_and_empty($orig_datadir, "Original data");
743
    if_directory_exists_and_empty($orig_ibdata_dir, "Original InnoDB data");
744
    if_directory_exists_and_empty($orig_iblog_dir, "Original InnoDB log");
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
745
746
    # check that the original options file and the backup options file have
747
    # the same value for "innodb_data_file_path" option
748
    #$backup_innodb_data_file_path = 
749
    #    get_option(\%backup_config, 'mysqld', 'innodb_data_file_path');
750
    #if (!are_equal_innodb_data_file_paths($orig_innodb_data_file_path, 
751
    #                                      $backup_innodb_data_file_path)
752
    #) {
753
    #    Die "The value of 'innodb_data_file_path' option in the original "
754
    #      . "my.cnf file '$config_file' is different from the value "
755
    #      . "in the backup my.cnf file '$backup_config_file'.\n(original: "
756
    #      . "'$orig_innodb_data_file_path')\n"
757
    #      . "(backup:   '$backup_innodb_data_file_path')";
758
    #}
759
760
    # make a list of all ibdata files in the backup directory and all
761
    # directories in the backup directory under which there are ibdata files
762
    foreach my $a (split(/;/, $orig_innodb_data_file_path)) {
763
        my $path = (split(/:/,$a))[0];
42 by kinoyasu
fix bug 357653; remove innobackup-1.5.1.patch
764
        my $filename = (split(/\/+/, $path))[-1];
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
765
766
        # check that the backup data file exists
42 by kinoyasu
fix bug 357653; remove innobackup-1.5.1.patch
767
        if (! -e "$backup_dir/$filename") {
768
            if (-e "$backup_dir/${filename}.ibz") {
769
                Die "Backup data file '$backup_dir/$filename' does not exist, but "
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
770
                  . "its compressed copy '${path}.ibz' exists. Check "
109.3.1 by Aleksandr Kuzminsky
Fixed Bug #463709: The script outputs its own name
771
                  . "that you have run '$innobackup_script --apply-log --uncompress "
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
772
                  . "...' before attempting '$innobackup_script --copy-back ...'  "
773
                  . "or '$innobackup_script --move-back ...' !";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
774
            } else {
42 by kinoyasu
fix bug 357653; remove innobackup-1.5.1.patch
775
                Die "Backup data file '$backup_dir/$filename' does not exist.";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
776
            }
777
        }
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
778
	
779
        $excluded_files .= "|\Q$filename\E";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
780
    }
781
782
    # copy files from backup dir to their original locations
783
784
    # copy files to original data directory
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
785
    my $excluded_regexp = '^(' . $excluded_files . ')$';
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
786
    print STDERR "$prefix Starting to $operation files in '$backup_dir'\n"; 
22 by kinoyasu
"innobackup --stream=tar" is supported
787
    print STDERR "$prefix back to original data directory '$orig_datadir'\n";
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
788
    &$move_or_copy_dir($backup_dir, $orig_datadir, $excluded_regexp, 0);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
789
790
    # copy InnoDB data files to original InnoDB data directory
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
791
    print STDERR "\n$prefix Starting to $operation InnoDB system tablespace\n";
22 by kinoyasu
"innobackup --stream=tar" is supported
792
    print STDERR "$prefix in '$backup_dir'\n";
793
    print STDERR "$prefix back to original InnoDB data directory '$orig_ibdata_dir'\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
794
    foreach my $a (split(/;/, $orig_innodb_data_file_path)) {
795
        # get the relative pathname of a data file
796
        my $path = (split(/:/,$a))[0];
42 by kinoyasu
fix bug 357653; remove innobackup-1.5.1.patch
797
        my $filename = (split(/\/+/, $path))[-1];
108.1.4 by Aleksandr Kuzminsky
escape_path() function is added.
798
        $src_name = escape_path("$backup_dir/$filename");
799
        $dst_name = escape_path("$orig_ibdata_dir/$path");
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
800
        &$move_or_copy_file($src_name, $dst_name);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
801
    }
802
803
    # copy InnoDB log files to original InnoDB log directory
804
    opendir(DIR, $backup_dir) 
805
        || Die "Can't open directory '$backup_dir': $!\n";
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
806
    print STDERR "\n$prefix Starting to $operation InnoDB log files\n";
22 by kinoyasu
"innobackup --stream=tar" is supported
807
    print STDERR "$prefix in '$backup_dir'\n";
808
    print STDERR "$prefix back to original InnoDB log directory '$orig_iblog_dir'\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
809
    while (defined($file = readdir(DIR))) {
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
810
        if ($file =~ /'^' . $iblog_files . '$'/ && -f "$backup_dir/$file") {
108.1.4 by Aleksandr Kuzminsky
escape_path() function is added.
811
            $src_name = escape_path("$backup_dir/$file");
812
            $dst_name = escape_path("$orig_iblog_dir");
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
813
            &$move_or_copy_file($src_name, $dst_name);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
814
        }
815
    }
816
    closedir(DIR);
817
22 by kinoyasu
"innobackup --stream=tar" is supported
818
    print STDERR "$prefix Finished copying back files.\n\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
819
}
820
821
822
#
823
# apply_log subroutine prepares a backup for starting database server
824
# on the backup. It applies InnoDB log files to the InnoDB data files.
825
#
826
sub apply_log {
827
    my $rcode;
828
    my $cmdline = '';
43 by kinoyasu
fix bug 358266, bug 359341
829
    my $options = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
830
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
831
    if ($option_defaults_file) {
108.1.5 by Aleksandr Kuzminsky
Fixed issue in quoting defaults-file when specified
832
        $options = $options . " --defaults-file=\"$option_defaults_file\" ";
242.47.10 by Sergei Glushchenko
Bug 996493: innobackupex --apply-log doesn't read config from backup-my.cnf
833
    } else {
834
        $options = $options . " --defaults-file=\"${backup_dir}/backup-my.cnf\" ";
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
835
    }
43 by kinoyasu
fix bug 358266, bug 359341
836
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
837
    if ($option_defaults_extra_file) {
838
        $options = $options . " --defaults-extra-file=\"$option_defaults_extra_file\" ";
839
    }
840
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
841
    if ($option_defaults_group) {
842
        $options = $options . " --defaults-group=\"$option_defaults_group\" ";
843
    }
844
43 by kinoyasu
fix bug 358266, bug 359341
845
    $options = $options . "--prepare --target-dir=$backup_dir";
846
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
847
    if ($option_uncompress) {
848
        $options = $options . ' --uncompress';
849
    }
61 by kinoyasu
innobackupex can pass --export option
850
    if ($option_export) {
851
        $options = $options . ' --export';
852
    }
181 by root
added --redo-only option to innobackupex
853
    if ($option_redo_only) {
854
        $options = $options . ' --apply-log-only';
855
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
856
    if ($option_use_memory) {
857
        $options = $options . " --use-memory=$option_use_memory";
858
    }
859
218 by Vadim Tkachenko
Added option --incremental-dir to innobackupex script
860
    if ($option_incremental_dir) {
861
        $options = $options . " --incremental-dir=$option_incremental_dir";
862
    }
863
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
864
    # run ibbackup as a child process
865
    $cmdline = "$option_ibbackup_binary $options";
866
    $now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
867
    print STDERR "\n$now  $prefix Starting ibbackup with command: $cmdline\n\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
868
    $rcode = system("$cmdline");
869
    if ($rcode) {
870
        # failure
871
        Die "\n$prefix ibbackup failed";
872
    }
873
222 by Vadim Tkachenko
Disabled auto-creating of ib_logfile* when
874
    # We should not create ib_logfile files if we prepare for following incremental applies
875
    # Also we do not prepare ib_logfile if we applied incremental changes
876
    if (!( ($option_redo_only) or ($option_incremental_dir))) { 
345 by Alexey Kopytov
Cherrypick of fix for bug #03984 - When applying deltas from an incremental over a full backup to "roll forward" is completed, update the xtrabackup_slave_info file in the target dir if there is an updated xtraback.
877
        $now = current_time();
878
        print STDERR "\n$now  $prefix Restarting xtrabackup with command: $cmdline\nfor creating ib_logfile*\n\n";
879
        $rcode = system("$cmdline");
880
        if ($rcode) {
881
            # failure
882
            Die "\n$prefix xtrabackup (2nd execution) failed";
883
        }
884
    }
885
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
886
    # If we were applying an incremental change set, we need to make
887
    # sure non-InnoDB files and xtrabackup_* metainfo files are copied
888
    # to the full backup directory.
345 by Alexey Kopytov
Cherrypick of fix for bug #03984 - When applying deltas from an incremental over a full backup to "roll forward" is completed, update the xtrabackup_slave_info file in the target dir if there is an updated xtraback.
889
    if ( $option_incremental_dir ) {
242.42.1 by Alexey Kopytov
Bug #759701: innobackupex does not copy non-InnoDB files when applying
890
	print STDERR "$prefix Starting to copy non-InnoDB files in '$option_incremental_dir'\n"; 
891
	print STDERR "$prefix to the full backup directory '$backup_dir'\n";
892
	copy_dir_recursively($option_incremental_dir, $backup_dir,
893
			     '^(\.\.?|backup-my\.cnf|xtrabackup_logfile|' .
894
			     'xtrabackup_binary|xtrabackup_checkpoints|' .
895
			     '.*\.delta|.*\.meta|ib_logfile.*)$', 1);
896
        # If the latest backup has no file, we need to remove the old
897
        # xtrabackup_slave_info file, because it is out of date
898
        # TODO: this will not be needed when bug #856400 is fixed.
899
        if ( -e "$backup_dir/xtrabackup_slave_info" ) {
345 by Alexey Kopytov
Cherrypick of fix for bug #03984 - When applying deltas from an incremental over a full backup to "roll forward" is completed, update the xtrabackup_slave_info file in the target dir if there is an updated xtraback.
900
            print STDERR "\n$now  $prefix No updated xtrabackup_slave_info found in incremental dir, removing stale xtrabackup_slave_info file from target dir.";
901
            $cmdline = "rm $backup_dir/xtrabackup_slave_info";
902
            $rcode = system("$cmdline");
903
            if ($rcode) {
904
                # failure
905
                Die "\n$prefix Failed to remove stale xtrabackup_slave_info in $backup_dir";
906
            }
907
908
        }
909
    }
910
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
911
}
912
913
914
#
915
# wait_for_ibbackup_suspend subroutine waits until ibbackup has suspended 
916
# itself.
917
#
918
sub wait_for_ibbackup_suspend {
22 by kinoyasu
"innobackup --stream=tar" is supported
919
    print STDERR "$prefix Waiting for ibbackup (pid=$ibbackup_pid) to suspend\n";
920
    print STDERR "$prefix Suspend file '$suspend_file'\n\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
921
    for (;;) {
391.76.1 by Sergei Glushchenko
Bug 1095551
922
        usleep(100000);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
923
        last if -e $suspend_file;
924
925
        # check that ibbackup child process is still alive
926
        if ($ibbackup_pid == waitpid($ibbackup_pid, &WNOHANG)) {
927
            $ibbackup_pid = '';
928
            Die "ibbackup child process has died";
929
        }
930
    }
931
    $now = current_time();
353 by Alexey Kopytov
Cherrypick of Galera support.
932
    open XTRABACKUP_PID, "> $option_tmpdir/$xtrabackup_pid_file";
933
    print XTRABACKUP_PID $ibbackup_pid;
934
    close XTRABACKUP_PID;
22 by kinoyasu
"innobackup --stream=tar" is supported
935
    print STDERR "\n$now  $prefix Continuing after ibbackup has suspended\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
936
}
937
938
939
#
391.53.7 by Alexey Kopytov
Bug #1055989: innobackupex waits for streaming temporary log file to
940
# resume_ibbackup subroutine signals ibbackup to finish log copying by deleting
941
# the 'xtrabackup_suspended' file and waits until log copying is stopped,
942
# i.e. until 'xtrabackup_suspended' is created again.
943
#
944
# If this functions detects that the xtrabackup process has terminated, it also
945
# sets ibbackup_exit_code and resets ibbackup_pid to avoid trying to reap a
946
# non-existing process later
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
947
#
948
sub resume_ibbackup {
391.53.7 by Alexey Kopytov
Bug #1055989: innobackupex waits for streaming temporary log file to
949
    my $pid = -1;
950
951
    $now = current_time();
952
    print STDERR "$now  $prefix Waiting for log copying to finish\n\n";
953
    unlink $suspend_file || Die "Failed to delete '$suspend_file': $!";
954
955
    for (;;) {
956
        # check if the xtrabackup process is still alive _before_ checking if
957
        # the file exists to avoid a race condition when the file is created and
958
        # the process terminates right after we do the file check
959
        $pid = waitpid($ibbackup_pid, &WNOHANG);
960
961
        last if -e $suspend_file;
962
963
        if ($ibbackup_pid == $pid) {
964
            # The file doesn't exist, but the process has terminated
965
            Die "ibbackup child process has died";
966
        }
967
968
        sleep 1;
969
    }
970
971
    if ($pid == $ibbackup_pid) {
972
        $ibbackup_exit_code = $CHILD_ERROR >> 8;
973
        $ibbackup_pid = '';
974
    }
975
976
    unlink $suspend_file || Die "Failed to delete '$suspend_file': $!";
977
}
978
979
#
980
# wait for ibbackup to finish and return its exit code
981
#
982
sub wait_for_ibbackup_finish {
983
    if (!$ibbackup_pid) {
984
        # The process has already been reaped.
391.80.1 by Alexey Kopytov
Bug #1114955: xtrabackup_pid remains existed after execution
985
        unlink "$option_tmpdir/$xtrabackup_pid_file";
391.53.7 by Alexey Kopytov
Bug #1055989: innobackupex waits for streaming temporary log file to
986
        return $ibbackup_exit_code;
987
    }
988
989
    $now = current_time();
990
    print STDERR "$now  $prefix Waiting for ibbackup (pid=$ibbackup_pid) to finish\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
991
992
    # wait for ibbackup to finish
993
    waitpid($ibbackup_pid, 0);
353 by Alexey Kopytov
Cherrypick of Galera support.
994
    unlink "$option_tmpdir/$xtrabackup_pid_file";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
995
    $ibbackup_pid = '';
121.1.1 by Daniel Nichter
Get ibbackup/xtrabackup exit code. Exit with same.
996
    return $CHILD_ERROR >> 8;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
997
}
998
    
999
1000
#
1001
# start_ibbackup subroutine spawns a child process running ibbackup
1002
# program for backing up InnoDB tables and indexes.
1003
#
1004
sub start_ibbackup {
43 by kinoyasu
fix bug 358266, bug 359341
1005
    my $options = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1006
    my $cmdline = '';
1007
    my $pid = undef;
1008
43 by kinoyasu
fix bug 358266, bug 359341
1009
    if ($option_defaults_file) {
108.1.5 by Aleksandr Kuzminsky
Fixed issue in quoting defaults-file when specified
1010
        $options = $options . " --defaults-file=\"$option_defaults_file\" ";
43 by kinoyasu
fix bug 358266, bug 359341
1011
    }
1012
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
1013
    if ($option_defaults_extra_file) {
1014
        $options = $options . " --defaults-extra-file=\"$option_defaults_extra_file\" ";
1015
    }
1016
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1017
    if ($option_defaults_group) {
1018
        $options = $options . " --defaults-group=\"$option_defaults_group\" ";
1019
    }
1020
43 by kinoyasu
fix bug 358266, bug 359341
1021
    $options = $options . "--backup --suspend-at-end";
1022
22 by kinoyasu
"innobackup --stream=tar" is supported
1023
    if (!$option_remote_host && !$option_stream) {
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1024
        $options = $options . " --target-dir=$backup_dir";
1025
    } else {
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
1026
        #(datadir) for 'xtrabackup_suspended' and 'xtrabackup_checkpoints'
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1027
        $options = $options . " --target-dir=" . $option_tmpdir;
1028
	if ($option_remote_host) {
1029
	  $options = $options . " --log-stream";
1030
	}
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1031
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1032
1033
    # prepare command line for running ibbackup
1034
    if ($option_throttle) {
1035
        $options = $options . " --throttle=$option_throttle";
1036
    }
1037
    if ($option_sleep) {
1038
        $options = $options . " --sleep=$option_sleep";
1039
    }
1040
    if ($option_compress) {
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1041
        $options = $options . " --compress";
1042
        $options = $options . " --compress-threads=$option_compress_threads";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1043
    }
1044
    if ($option_use_memory) {
1045
        $options = $options . " --use-memory=$option_use_memory";
1046
    }
1047
    if ($option_include) {
19 by kinoyasu
xtrabackup --tables=REGEXP, innobackup --index=REGEXP
1048
        $options = $options . " --tables='$option_include'";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1049
    }
220 by Vadim Tkachenko
Bugfix #680936
1050
    if ($option_extra_lsndir) {
1051
        $options = $options . " --extra-lsndir='$option_extra_lsndir'";
1052
    }
1053
66 by Vadim Tkachenko
Added incremental option to innobackupex-1.5.1 script
1054
    if ($option_incremental) {
219 by Vadim Tkachenko
Added option --incremental-lsn to innobackupex script.
1055
        if($option_incremental_lsn) {
1056
          $options = $options . " --incremental-lsn='$option_incremental_lsn'";
242.23.1 by Lachlan Mulcahy
Fixed bug 860133
1057
	    } else {
219 by Vadim Tkachenko
Added option --incremental-lsn to innobackupex script.
1058
          $options = $options . " --incremental-basedir='$incremental_basedir'";
242.23.1 by Lachlan Mulcahy
Fixed bug 860133
1059
	    }
66 by Vadim Tkachenko
Added incremental option to innobackupex-1.5.1 script
1060
    }
220 by Vadim Tkachenko
Bugfix #680936
1061
166.1.1 by Daniel Nichter
Add --tables-file to innobackupex.
1062
    if ($option_tables_file) {
1063
        $options = $options . " --tables_file='$option_tables_file'";
1064
    }
195 by Alexey Kopytov
Implementation of parallel data files transfer for xtrabackup.
1065
    if ($option_parallel) {
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1066
        $options = $options . " --parallel=$option_parallel";
1067
    }
1068
    if ($option_stream) {
1069
	$options = $options . " --stream=$option_stream";
195 by Alexey Kopytov
Implementation of parallel data files transfer for xtrabackup.
1070
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1071
    $cmdline = "$option_ibbackup_binary $options";
1072
1073
    # run ibbackup as a child process
1074
    $now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
1075
    print STDERR "\n$now  $prefix Starting ibbackup with command: $cmdline\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1076
    if (defined($pid = fork)) {
1077
        if ($pid) {
1078
            # parent process
1079
            $ibbackup_pid = $pid;
14 by kinoyasu
using rsync for incremental backup
1080
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1081
            if($option_remote_host) {
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
1082
                #direct copy to remote
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1083
                my $orig_datadir = get_option(\%config, $option_defaults_group, 'datadir');
14 by kinoyasu
using rsync for incremental backup
1084
                my $orig_ibdata_dir =
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1085
                    get_option(\%config, $option_defaults_group, 'innodb_data_home_dir');
14 by kinoyasu
using rsync for incremental backup
1086
                my $orig_innodb_data_file_path =
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1087
                    get_option(\%config, $option_defaults_group, 'innodb_data_file_path');
224 by Alexey Kopytov
Bug #606981: linux + o_direct + stream backup = broken?
1088
                my $innodb_flush_method = 
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1089
                    get_option(\%config, $option_defaults_group, 'innodb_flush_method');
224 by Alexey Kopytov
Bug #606981: linux + o_direct + stream backup = broken?
1090
                my $innodb_use_odirect;
242.11.13 by Alexey Kopytov
Bug #759225: xtrabackup does not support ALL_O_DIRECT for
1091
                $innodb_use_odirect = 1 if $innodb_flush_method =~ m/^(ALL_)?O_DIRECT$/i;
224 by Alexey Kopytov
Bug #606981: linux + o_direct + stream backup = broken?
1092
14 by kinoyasu
using rsync for incremental backup
1093
                my $subdir;
1094
                my @list;
1095
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1096
                if (system("ssh $option_ssh_opt $option_remote_host test -e $backup_dir/ib_logfile0")
1097
                    == 0) {
1098
                    print STDERR "$prefix Remove $option_remote_host:$backup_dir/ib_logfile*\n";
1099
                    system("ssh $option_ssh_opt $option_remote_host rm $backup_dir/ib_logfile\*")
1100
                        and Die "Failed to rm file '$backup_dir/ib_logfile*': $!";
14 by kinoyasu
using rsync for incremental backup
1101
                }
1102
1103
                wait_for_ibbackup_suspend();
1104
1105
                #InnoDB data files from original InnoDB data directory
22 by kinoyasu
"innobackup --stream=tar" is supported
1106
                print STDERR "\n$prefix Starting to backup InnoDB tables and indexes\n";
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1107
                print STDERR "$prefix to '$backup_dir'\n";
22 by kinoyasu
"innobackup --stream=tar" is supported
1108
                print STDERR "$prefix from original InnoDB data directory '$orig_ibdata_dir'\n";
14 by kinoyasu
using rsync for incremental backup
1109
                foreach my $a (split(/;/, $orig_innodb_data_file_path)) {
1110
                    my $path = (split(/:/,$a))[0];
86.1.1 by akuzminsky
Fixed bug#417178
1111
                    $path=~s/([\$\\\" ])/\\$1/g;
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1112
                    print STDERR "$prefix Backing up file '$orig_ibdata_dir/$path'\n";
1113
                    system("scp $option_scp_opt '$orig_ibdata_dir/$path' '$option_remote_host:$backup_dir/$path'")
1114
                      and Die "Failed to scp file '$path': $!";
14 by kinoyasu
using rsync for incremental backup
1115
                }
1116
1117
                #copy *.ibd files
1118
                opendir(DIR, $orig_datadir)
1119
                    || Die "Can't open directory '$orig_datadir': $!\n";
1120
                while (defined($subdir = readdir(DIR))) {
1121
                    my $print_each_file = 0;
1122
                    my $file_c;
1123
                    my $file;
1124
                    if ($subdir eq '.' || $subdir eq '..') { next; }
1125
                    next unless -d "$orig_datadir/$subdir";
1126
                    next unless check_if_required($subdir);
1127
1128
                    @list = glob("$orig_datadir/$subdir/" . '*.ibd');
1129
1130
                    $file_c = @list;
1131
                    if ($file_c <= $backup_file_print_limit) {
1132
                        $print_each_file = 1;
1133
                    } else {
22 by kinoyasu
"innobackup --stream=tar" is supported
1134
                        print STDERR "$prefix Backing up files " .
14 by kinoyasu
using rsync for incremental backup
1135
                            "'$orig_datadir/$subdir/*.ibd' ($file_c files)\n";
1136
                    }
1137
                    foreach $file (@list) {
174 by Vadim Tkachenko
tables_file option is applied to copy via stream also
1138
			next unless check_if_required($subdir, $file);
20 by kinoyasu
innobackup --include=REGEXP with --remote
1139
                        if($option_include) {
1140
                            my $table_name;
1141
391.67.1 by Sergei Glushchenko
Bug 711166
1142
                            $table_name = get_table_name($file);
20 by kinoyasu
innobackup --include=REGEXP with --remote
1143
391.67.1 by Sergei Glushchenko
Bug 711166
1144
                            if (!("$subdir.$table_name" =~ /$option_include/)) {
22 by kinoyasu
"innobackup --stream=tar" is supported
1145
                                print STDERR "'$file' is skipped.\n";
20 by kinoyasu
innobackup --include=REGEXP with --remote
1146
                                next;
1147
                            }
1148
                        }
1149
14 by kinoyasu
using rsync for incremental backup
1150
                        if ($print_each_file) {
22 by kinoyasu
"innobackup --stream=tar" is supported
1151
                            print STDERR "$prefix Backing up file '$file'\n";
1152
                        }
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1153
                        if (system("ssh $option_ssh_opt $option_remote_host test -e $backup_dir/$subdir")
1154
                            != 0) {
1155
                          system("ssh $option_ssh_opt $option_remote_host mkdir $backup_dir/$subdir");
22 by kinoyasu
"innobackup --stream=tar" is supported
1156
                        }
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1157
                        system("scp $option_scp_opt '$file' '$option_remote_host:$backup_dir/$subdir/'")
1158
                          and Die "Failed to scp file '$file': $!";
1159
		      }
14 by kinoyasu
using rsync for incremental backup
1160
                }
1161
                closedir(DIR);
1162
            }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1163
        } else {
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1164
            if($option_remote_host) {
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
1165
                open(STDOUT, "> $tmp_logfile")
1166
                || Die "Failed to open file '$tmp_logfile': $!"
14 by kinoyasu
using rsync for incremental backup
1167
            }
1168
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1169
            # child process
1170
            exec($cmdline) || Die "Failed to exec ibbackup: $!";
1171
        }
1172
    } else {
1173
        Die "failed to fork ibbackup child process: $!";
1174
    }
1175
}
1176
                  
1177
1178
#
1179
# get_mysql_options subroutine returns the options to mysql client program
1180
# as a string. The options are determined from the options given by the
1181
# user to innobackup.
1182
#
1183
sub get_mysql_options {
114.2.1 by Daniel Nichter
Fix bug 510964: innobackupex doesn't pass --defaults-files to mysql child proc
1184
    my $options = '';
1185
1186
    # this option has to be first
1187
    if ($option_defaults_file) {
211.1.2 by Valentine Gostev
Added fix for bug 688211 (additional single quotes)
1188
        $options = "$options --defaults-file='$option_defaults_file'";
114.2.1 by Daniel Nichter
Fix bug 510964: innobackupex doesn't pass --defaults-files to mysql child proc
1189
    }
1190
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
1191
    if ($option_defaults_extra_file) {
1192
        $options = $options . " --defaults-extra-file=\"$option_defaults_extra_file\" ";
1193
    }
1194
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1195
    if ($option_mysql_password) {
211.1.2 by Valentine Gostev
Added fix for bug 688211 (additional single quotes)
1196
        $options = "$options --password='$option_mysql_password'";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1197
    }
1198
    if ($option_mysql_user) {
211.1.2 by Valentine Gostev
Added fix for bug 688211 (additional single quotes)
1199
        $options = "$options --user='$option_mysql_user'";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1200
    }
116.1.1 by Daniel Nichter
Bug 510965: add --host, remove --host=127.0.0.1 from --port.
1201
    if ($option_mysql_host) {
211.1.2 by Valentine Gostev
Added fix for bug 688211 (additional single quotes)
1202
        $options = "$options --host='$option_mysql_host'";
116.1.1 by Daniel Nichter
Bug 510965: add --host, remove --host=127.0.0.1 from --port.
1203
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1204
    if ($option_mysql_port) {
211.1.2 by Valentine Gostev
Added fix for bug 688211 (additional single quotes)
1205
        $options = "$options --port='$option_mysql_port'";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1206
    }
1207
    if ($option_mysql_socket) {
211.1.2 by Valentine Gostev
Added fix for bug 688211 (additional single quotes)
1208
        $options = "$options --socket='$option_mysql_socket'";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1209
    }
114.2.1 by Daniel Nichter
Fix bug 510964: innobackupex doesn't pass --defaults-files to mysql child proc
1210
    $options = "$options --unbuffered --";
1211
    return $options;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1212
}
1213
1214
1215
#
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1216
# Check that the server is responding to queries
1217
#
1218
sub mysql_ping {
242.32.3 by Alexey Kopytov
Don't do file io inside a signal handler as that breaks system() calls.
1219
    my $alarm_handler = shift;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1220
    my $mysql_pid_copy = $mysql_pid;
1221
1222
    # send a dummy query to mysql child process
1223
    $hello_id++;
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1224
1225
    my $hello_message = "xtrabackup ping $hello_id";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1226
    print MYSQL_WRITER "select '$hello_message';\n" 
1227
        or Die "Connection to mysql child process failed: $!";
1228
242.32.3 by Alexey Kopytov
Don't do file io inside a signal handler as that breaks system() calls.
1229
    # Don't check server's response if running in a signal handler
1230
    # because that breaks system() calls
1231
    if ($alarm_handler)
1232
    {
1233
	return 0;
1234
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1235
    # wait for reply
1236
    eval {
1237
        local $SIG{ALRM} = sub { die "alarm clock restart" };
1238
        my $stdout = '';
1239
        my $stderr = '';
1240
        alarm $mysql_response_timeout;
1241
        while (index($stdout, $hello_message) < 0) {
1242
            sleep 2;
1243
            if ($mysql_pid && $mysql_pid == waitpid($mysql_pid, &WNOHANG)) {
1244
                my $reason = `cat $mysql_stderr`;
1245
                $mysql_pid = '';
1246
                die "mysql child process has died: $reason";
1247
            }
1248
            $stdout = `cat $mysql_stdout`;
163 by Aleksandr Kuzminsky
suppress warnings in error detecting function
1249
            $stderr = `cat $mysql_stderr | grep -v ^Warning`;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1250
            if ($stderr) {
1251
                # mysql has reported an error, do exit
1252
                die "mysql error: $stderr";
1253
            }
1254
        }
1255
        alarm 0;
1256
    };
1257
    if ($@ =~ /alarm clock restart/) {
1258
        Die "Connection to mysql child process (pid=$mysql_pid_copy) timedout."
1259
            . " (Time limit of $mysql_response_timeout seconds exceeded."
1260
            . " You may adjust time limit by editing the value of parameter"
1261
            . " \"\$mysql_response_timeout\" in this script.)";
1262
    } elsif ($@) { Die $@; }
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1263
}
1264
1265
1266
#
1267
# SIGALRM handler which sends a keepalive query to the server
1268
#
1269
sub catch_sigalrm {
242.32.3 by Alexey Kopytov
Don't do file io inside a signal handler as that breaks system() calls.
1270
  mysql_ping(1);
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1271
  # Reschedule SIGALRM
1272
  alarm $mysql_keep_alive;
1273
}
1274
1275
1276
#
1277
# Schedule periodic server pings
1278
#
1279
sub start_keepalives {
1280
  $SIG{ALRM} = \&catch_sigalrm;
1281
  alarm $mysql_keep_alive;
1282
}
1283
1284
#
1285
# Cancel periodic server pings
1286
#
1287
sub stop_keepalives {
1288
  alarm 0;
1289
  $SIG{ALRM} = "DEFAULT";
1290
}
1291
1292
#
1293
# mysql_open subroutine starts mysql as a child process with
1294
# a pipe connection.
1295
#
1296
sub mysql_open {
1297
    my $options = get_mysql_options();
1298
    # run mysql as a child process with a pipe connection
1299
    $now = current_time();
330 by Alexey Kopytov
Manual merge from 1.6.
1300
    (my $prt_options = $options) =~ s/--password=[^ ]+ /--password=xxxxxxxx /g;
1301
    print STDERR "$now  $prefix Starting mysql with options: $prt_options\n";
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1302
    $mysql_pid = open(*MYSQL_WRITER, "| mysql $options >$mysql_stdout 2>$mysql_stderr ") or Die "Failed to spawn mysql child process: $!";
1303
    MYSQL_WRITER->autoflush(1);
1304
    $now = current_time();
1305
    print STDERR "$now  $prefix Connected to database with mysql child process (pid=$mysql_pid)\n";
330 by Alexey Kopytov
Manual merge from 1.6.
1306
    print MYSQL_WRITER "SET SESSION wait_timeout = 2147000;\n" or die "Connection to mysql child process failed: $!";
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1307
242.32.3 by Alexey Kopytov
Don't do file io inside a signal handler as that breaks system() calls.
1308
    mysql_ping(0);
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1309
1310
    start_keepalives();
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1311
}
1312
1313
1314
#
1315
# mysql_send subroutine send a request string to mysql child process.
1316
# This subroutine appends a newline character to the request and checks 
1317
# that mysqld receives the query.
1318
# Parameters:
1319
#    request  request string
1320
#
1321
sub mysql_send {
1322
    my $request = shift;
1323
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1324
    stop_keepalives();
1325
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1326
    $current_mysql_request = $request;
1327
    print MYSQL_WRITER "$request\n";
242.32.3 by Alexey Kopytov
Don't do file io inside a signal handler as that breaks system() calls.
1328
    mysql_ping(0);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1329
    $current_mysql_request = '';
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1330
1331
    start_keepalives();
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1332
}
1333
    
1334
1335
#
1336
# mysql_close subroutine terminates mysql child process gracefully.
1337
# 
1338
sub mysql_close {
242.25.3 by Alexey Kopytov
Bug #408803: innodbbackupex reaches Servers wait_timeout
1339
    stop_keepalives();
1340
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1341
    print MYSQL_WRITER "quit\n";
1342
    $now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
1343
    print STDERR "$now  $prefix Connection to database server closed\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1344
    $mysql_pid = '';
1345
}
1346
1347
1348
#
1349
# write_binlog_info subroutine retrieves MySQL binlog position and
1350
# saves it in a file. It also prints it to stdout.
1351
#
1352
sub write_binlog_info {
1353
    my @lines;
1354
    my $position = '';
1355
    my $filename = '';
1356
1357
    # get binlog position
242.55.4 by Alexey Kopytov
Bug #977101: --safe-slave-backup results in incorrect binlog info
1358
    mysql_send 'SHOW MASTER STATUS\G';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1359
242.55.4 by Alexey Kopytov
Bug #977101: --safe-slave-backup results in incorrect binlog info
1360
    # get the name of the last binlog file and position in it
1361
    # from the SHOW MASTER STATUS output
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1362
    file_to_array($mysql_stdout, \@lines);
242.55.4 by Alexey Kopytov
Bug #977101: --safe-slave-backup results in incorrect binlog info
1363
    foreach (@lines) {
1364
        $filename = $1 if /^\s+File:\s(\S+)\s*$/;
1365
        $position = $1 if /^\s+Position:\s(\d+)\s*$/;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1366
    }
1367
1368
    # write binlog info file
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1369
    if (!$option_remote_host) {
1370
        open(FILE, ">$binlog_info") || 
1371
            Die "Failed to open file '$binlog_info': $!";
1372
    } else {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
1373
        open(FILE, "| ssh $option_ssh_opt $option_remote_host 'cat > $binlog_info'") ||
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1374
            Die "Failed to open file '$option_remote_host:$binlog_info': $!";
1375
    }
242.55.4 by Alexey Kopytov
Bug #977101: --safe-slave-backup results in incorrect binlog info
1376
    print FILE  "$filename\t$position\t\t\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1377
    close(FILE);
1378
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1379
    if ($option_stream) {
1380
        system("cd $option_tmpdir; $stream_cmd xtrabackup_binlog_info")
22 by kinoyasu
"innobackup --stream=tar" is supported
1381
            and Die "Failed to stream 'xtrabackup_binlog_info': $!";
1382
        unlink $binlog_info || Die "Failed to delete '$binlog_info': $!";
1383
    }
1384
242.55.4 by Alexey Kopytov
Bug #977101: --safe-slave-backup results in incorrect binlog info
1385
    $mysql_binlog_position = "filename '$filename', position $position";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1386
}
1387
353 by Alexey Kopytov
Cherrypick of Galera support.
1388
#
1389
# write_galera_info subroutine retrieves MySQL Galera and
1390
# saves it in a file. It also prints it to stdout.
1391
#
1392
sub write_galera_info {
1393
    my @lines;
1394
    my @info_lines = ();
1395
    my $position = '';
1396
    my $filename = '';
1397
1398
    # get binlog position
1399
    mysql_send "SHOW STATUS LIKE 'wsrep_local_state_uuid';";
1400
    mysql_send "SHOW STATUS LIKE 'wsrep_last_committed';";
1401
1402
    # get "show master status" output lines (2) from mysql output
1403
    file_to_array($mysql_stdout, \@lines);
1404
1405
    open(FILE, ">$galera_info") || 
1406
         Die "Failed to open file '$galera_info': $!";
1407
1408
    foreach my $line (@lines) {
1409
        if ($line =~ m/wsrep_local_state_uuid/) {
1410
	    $line =~ s/wsrep_local_state_uuid\s*//g;
1411
            print FILE  "$line:";
1412
        }
1413
    }
1414
1415
    foreach my $line (@lines) {
1416
        if ($line =~ m/wsrep_last_committed/) {
1417
	    $line =~ s/wsrep_last_committed\s*//g;
1418
            print FILE  "$line\n";
1419
        }
1420
    }
1421
1422
    close(FILE);
1423
391.21.1 by Sergei Glushchenko
Bug #983720: ib_lru_dump and --galera-info fail with --stream=xbstream
1424
    if ($option_stream) {
1425
        system("cd $option_tmpdir; $stream_cmd xtrabackup_galera_info")
353 by Alexey Kopytov
Cherrypick of Galera support.
1426
            and Die "Failed to stream 'xtrabackup_galera_info': $!";
1427
        unlink $galera_info || Die "Failed to delete '$galera_info': $!";
1428
    }
1429
1430
}
1431
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1432
1433
# 
1434
# write_slave_info subroutine retrieves MySQL binlog position of the
1435
# master server in a replication setup and saves it in a file. It
1436
# also saves it in $msql_slave_position variable.
1437
#
1438
sub write_slave_info {
1439
    my @lines;
1440
    my @info_lines;
1441
    my $position = '';
1442
    my $filename = '';
1443
    my $master= '';
1444
	
1445
    # get slave status. Use single quotes here, otherwise
1446
    # \G is evaluated as a control character.
242.55.4 by Alexey Kopytov
Bug #977101: --safe-slave-backup results in incorrect binlog info
1447
    mysql_send 'SHOW SLAVE STATUS\G';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1448
1449
    # get output of the "show slave status" command from mysql output
1450
    # and extract binlog position of the master server
1451
    file_to_array($mysql_stdout, \@lines);
1452
    for (@lines) {
1453
        $master = $1 if /Master_Host:\s*(\S*)\s*$/;
1454
        $filename = $1 if /Master_Log_File:\s*(\S*)\s*$/;
1455
        $position = $1 if /Master_Log_Pos:\s*(\S*)\s*$/;
1456
    }
1457
1458
    # print slave status to a file
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1459
    if (!$option_remote_host) {
1460
        open(FILE, ">$slave_info") || 
1461
            Die "Failed to open file '$slave_info': $!";
1462
    } else {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
1463
        open(FILE, "| ssh $option_ssh_opt $option_remote_host 'cat > $slave_info'") ||
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1464
            Die "Failed to open file '$option_remote_host:$slave_info': $!";
1465
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1466
    print FILE  "CHANGE MASTER TO MASTER_LOG_FILE='$filename', MASTER_LOG_POS=$position\n";
1467
    close(FILE);
1468
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1469
    if ($option_stream) {
1470
        system("cd $option_tmpdir; $stream_cmd xtrabackup_slave_info")
22 by kinoyasu
"innobackup --stream=tar" is supported
1471
            and Die "Failed to stream 'xtrabackup_slave_info': $!";
1472
        unlink $slave_info || Die "Failed to delete '$slave_info': $!";
1473
    }
1474
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1475
    $mysql_slave_position = "master host '$master', filename '$filename', position $position";
1476
}
1477
1478
1479
#
1480
# mysql_lockall subroutine puts a read lock on all tables in all databases.
1481
# 
1482
sub mysql_lockall {
1483
    $now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
1484
    print STDERR "$now  $prefix Starting to lock all tables...\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1485
1486
    mysql_send "USE mysql;";
89 by kinoyasu
fix bug 386535, bug 402884, probability to crash --prepare
1487
#    mysql_send "DROP TABLE IF EXISTS ibbackup_binlog_marker;";
1488
#    if (compare_versions($mysql_server_version, '4.1.0') == -1) {
1489
#        # MySQL server version is 4.0 or older, ENGINE keyword not supported
1490
#        mysql_send "CREATE TABLE ibbackup_binlog_marker(a INT) TYPE=INNODB;";
1491
#    } else {
1492
#        # MySQL server version is 4.1 or newer, use ENGINE keyword
1493
#        mysql_send "CREATE TABLE ibbackup_binlog_marker(a INT) ENGINE=INNODB;";
1494
#    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1495
    mysql_send "SET AUTOCOMMIT=0;";
89 by kinoyasu
fix bug 386535, bug 402884, probability to crash --prepare
1496
#    mysql_send "INSERT INTO ibbackup_binlog_marker VALUES (1);";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1497
    if (compare_versions($mysql_server_version, '4.0.22') == 0
1498
        || compare_versions($mysql_server_version, '4.1.7') == 0) {
1499
        # MySQL server version is 4.0.22 or 4.1.7
1500
        mysql_send "COMMIT;";
1501
        mysql_send "FLUSH TABLES WITH READ LOCK;";
1502
    } else {
1503
        # MySQL server version is other than 4.0.22 or 4.1.7
1504
        mysql_send "FLUSH TABLES WITH READ LOCK;";
1505
        mysql_send "COMMIT;";
1506
    }
1507
    write_binlog_info;
353 by Alexey Kopytov
Cherrypick of Galera support.
1508
    write_galera_info if $option_galera_info;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1509
	
1510
    $now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
1511
    print STDERR "$now  $prefix All tables locked and flushed to disk\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1512
}
1513
1514
1515
#
1516
# mysql_unlockall subroutine releases read locks on all tables in all 
1517
# databases.
1518
# 
1519
sub mysql_unlockall {
1520
    mysql_send "UNLOCK TABLES;";
89 by kinoyasu
fix bug 386535, bug 402884, probability to crash --prepare
1521
#    mysql_send "DROP TABLE IF EXISTS ibbackup_binlog_marker;";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1522
1523
    $now = current_time();
22 by kinoyasu
"innobackup --stream=tar" is supported
1524
    print STDERR "$now  $prefix All tables unlocked\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1525
}
1526
1527
1528
#
1529
# catch_sigpipe subroutine is a signal handler for SIGPIPE.
1530
#
1531
sub catch_sigpipe {
1532
    my $rcode;
1533
1534
    if ($mysql_pid && (-1 == ($rcode = waitpid($mysql_pid, &WNOHANG))
1535
                       || $rcode == $mysql_pid)) {
1536
        my $reason = `cat $mysql_stderr`;
22 by kinoyasu
"innobackup --stream=tar" is supported
1537
        print STDERR "Pipe to mysql child process broken: $reason at";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1538
        system("date +'%H:%M:%S'");
1539
        exit(1);
1540
    } else {
1541
        Die "Broken pipe";
1542
    }
1543
}
1544
1545
1546
#
1547
# kill_child_processes subroutine kills all child processes of this process.
1548
#
1549
sub kill_child_processes {
1550
    if ($ibbackup_pid) {
1551
        kill($kill_signal, $ibbackup_pid);
1552
        $ibbackup_pid = '';
1553
    }
1554
    
1555
    if ($mysql_pid) {
1556
        kill($kill_signal, $mysql_pid);
1557
        $mysql_pid = '';
1558
    }
1559
}
1560
1561
1562
#
1563
# require_external subroutine checks that an external program is runnable
1564
# via the shell. This is tested by calling the program with the
1565
# given arguments. It is checked that the program returns 0 and does 
1566
# not print anything to stderr. If this check fails, this subroutine exits.
1567
#    Parameters:
1568
#       program      pathname of the program file
1569
#       args         arguments to the program
1570
#       pattern      a string containing a regular expression for finding 
1571
#                    the program version.
1572
#                    this pattern should contain a subpattern enclosed
1573
#                    in parentheses which is matched with the version.
1574
#       version_ref  a reference to a variable where the program version
1575
#                    string is returned. Example "2.0-beta2".
1576
#
1577
sub require_external {
1578
    my $program = shift;
1579
    my $args = shift;
1580
    my $pattern = shift;
1581
    my $version_ref = shift;
1582
    my @lines;
1583
    my $tmp_stdout = tmpnam();
1584
    my $tmp_stderr = tmpnam();
1585
    my $rcode;
1586
    my $error;
1587
1588
    $rcode = system("$program $args >$tmp_stdout 2>$tmp_stderr");
1589
    if ($rcode) {
1590
        $error = $!;
1591
    }
163 by Aleksandr Kuzminsky
suppress warnings in error detecting function
1592
    my $stderr = `cat $tmp_stderr | grep -v ^Warning`;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1593
    unlink $tmp_stderr;
1594
    if ($stderr ne '') {
1595
        # failure
1596
        unlink $tmp_stdout;
1597
        Die "Couldn't run $program: $stderr";
1598
    } elsif ($rcode) {
1599
        # failure
1600
        unlink $tmp_stdout;
1601
        Die "Couldn't run $program: $error";
1602
    }
1603
1604
    # success
1605
    my $stdout = `cat $tmp_stdout`;
1606
    unlink $tmp_stdout;
1607
    @lines = split(/\n|;/,$stdout);
22 by kinoyasu
"innobackup --stream=tar" is supported
1608
    print STDERR "$prefix Using $lines[0]\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1609
1610
    # get version string from the first output line of the program
1611
    ${$version_ref} = '';
1612
    if ($lines[0] =~ /$pattern/) {
1613
        ${$version_ref} = $1;
1614
    }
1615
}
1616
1617
1618
# compare_versions subroutine compares two GNU-style version strings.
1619
# A GNU-style version string consists of three decimal numbers delimitted 
1620
# by dots, and optionally followed by extra attributes.
1621
# Examples: "4.0.1", "4.1.1-alpha-debug". 
1622
#    Parameters:
1623
#       str1   a GNU-style version string
1624
#       str2   a GNU-style version string
1625
#    Return value:
1626
#       -1   if str1 < str2
1627
#        0   if str1 == str2
1628
#        1   is str1 > str2
1629
sub compare_versions {
1630
    my $str1 = shift;
1631
    my $str2 = shift;
1632
    my $extra1 = '';
1633
    my $extra2 = '';
1634
    my @array1 = ();
1635
    my @array2 = ();
1636
    my $i;
1637
1638
    # remove possible extra attributes
1639
    ($str1, $extra1) = $str1 =~ /^([0-9.]*)(.*)/;
1640
    ($str2, $extra2) = $str2 =~ /^([0-9.]*)(.*)/;
1641
1642
    # split "dotted" decimal number string into an array
1643
    @array1 = split('\.', $str1);
1644
    @array2 = split('\.', $str2);
1645
1646
    # compare in lexicographic order
1647
    for ($i = 0; $i <= $#array1 && $i <= $#array2; $i++) {
1648
        if ($array1[$i] < $array2[$i]) {
1649
            return -1;
1650
        } elsif ($array1[$i] > $array2[$i]) {
1651
            return 1;
1652
        }
1653
    }
1654
    if ($#array1 < $#array2) {
1655
        return -1;
1656
    } elsif ($#array1 > $#array2) {
1657
        return 1;
1658
    } else {
1659
        return 0;
1660
    }
1661
}
1662
1663
1664
#
1665
# init subroutine initializes global variables and performs some checks on the
1666
# system we are running on.
1667
#
1668
sub init {
1669
    my $mysql_version = '';
1670
    my $ibbackup_version = '';
1671
    my $run = '';
1672
1673
    # print some instructions to the user
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1674
    if (!$option_apply_log && !$option_copy_back && !$option_move_back) {
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1675
        $run = 'backup';
1676
    } elsif ($option_copy_back) {
1677
        $run = 'copy-back';
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1678
    } elsif ($option_move_back) {
1679
        $run = 'move-back';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1680
    } else {
1681
        $run = 'apply-log';
1682
    }
22 by kinoyasu
"innobackup --stream=tar" is supported
1683
    print STDERR "IMPORTANT: Please check that the $run run completes successfully.\n";
109.3.1 by Aleksandr Kuzminsky
Fixed Bug #463709: The script outputs its own name
1684
    print STDERR "           At the end of a successful $run run $innobackup_script\n";
1685
    print STDERR "           prints \"completed OK!\".\n\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1686
1687
    # check that MySQL client program and InnoDB Hot Backup program
1688
    # are runnable via shell
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1689
    if (!$option_copy_back && !$option_move_back) {
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1690
        # we are making a backup or applying log to backup
1691
        if (!$option_apply_log) {
1692
            # we are making a backup, we need mysql server
1693
            my $output = '';
1694
            my @lines = ();
1695
1696
            # check that we have mysql client program
1697
            require_external('mysql', '--version', 'Ver ([^,]+)', 
1698
                             \$mysql_version);
1699
            
1700
            # get mysql server version
1701
            my $options = get_mysql_options();
1702
            @lines = split('\n', 
108.1.1 by Aleksandr Kuzminsky
Fixed number of issues in innobackupex-1.5.1 on windows platform
1703
                           `mysql $options -e "select \@\@version"`);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1704
            $mysql_server_version = $lines[1];
22 by kinoyasu
"innobackup --stream=tar" is supported
1705
            print STDERR "$prefix Using mysql server version $mysql_server_version\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1706
        }
1707
        #require_external($option_ibbackup_binary, '--license', 
1708
        #                 'version (\S+)', \$ibbackup_version);
22 by kinoyasu
"innobackup --stream=tar" is supported
1709
        print STDERR "\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1710
        
1711
        if ($option_include 
1712
            && $ibbackup_version 
1713
            && $ibbackup_version le "2.0") {
1714
            # --include option was given, but ibbackup is too
1715
            # old to support it
1716
            Die "--include option was given, but ibbackup is too old"
1717
                . " to support it. You must upgrade to InnoDB Hot Backup"
1718
                . " v2.0 in order to use --include option.\n";
1719
        }
1720
    }
1721
    
1722
    # set signal handlers
1723
    $SIG{PIPE} = \&catch_sigpipe;
1724
1725
    # read MySQL options file
1726
    #read_config_file($config_file, \%config);
1727
    read_config_file(\%config);
1728
22 by kinoyasu
"innobackup --stream=tar" is supported
1729
    if(!$option_tmpdir) {
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1730
        $option_tmpdir = get_option(\%config, $option_defaults_group, 'tmpdir');
22 by kinoyasu
"innobackup --stream=tar" is supported
1731
    }
1732
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1733
    # get innodb log home directory from options file
1734
    #$innodb_log_group_home_dir = 
1735
    #    get_option(\%config, 'mysqld', 'innodb_log_group_home_dir');
1736
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1737
    if (!$option_apply_log && !$option_copy_back && !$option_move_back) {
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1738
        # we are making a backup, create a new backup directory
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
1739
        if (!$option_remote_host) {
14 by kinoyasu
using rsync for incremental backup
1740
            $backup_dir = File::Spec->rel2abs(make_backup_dir());
1741
        } else {
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
1742
            $backup_dir = make_backup_dir();
14 by kinoyasu
using rsync for incremental backup
1743
        }
22 by kinoyasu
"innobackup --stream=tar" is supported
1744
        print STDERR "$prefix Created backup directory $backup_dir\n";
1745
        if (!$option_remote_host && !$option_stream) {
14 by kinoyasu
using rsync for incremental backup
1746
        $backup_config_file = $backup_dir . '/backup-my.cnf';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1747
        $suspend_file = $backup_dir . '/xtrabackup_suspended';
1748
        $binlog_info = $backup_dir . '/xtrabackup_binlog_info';
353 by Alexey Kopytov
Cherrypick of Galera support.
1749
        $galera_info = $backup_dir . '/xtrabackup_galera_info';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1750
        $slave_info = $backup_dir . '/xtrabackup_slave_info';
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1751
        } else {
374 by Alexey Kopytov
Cherrypick of fix for placement of xtrabackup_suspended and xtrabackup_checkpoint.
1752
        $suspend_file = $option_tmpdir . '/xtrabackup_suspended';
22 by kinoyasu
"innobackup --stream=tar" is supported
1753
        $tmp_logfile = $option_tmpdir . '/xtrabackup_logfile';
1754
        if ($option_stream) {
1755
            $backup_config_file = $option_tmpdir . '/backup-my.cnf';
1756
            $binlog_info = $option_tmpdir . '/xtrabackup_binlog_info';
353 by Alexey Kopytov
Cherrypick of Galera support.
1757
            $galera_info = $option_tmpdir . '/xtrabackup_galera_info';
22 by kinoyasu
"innobackup --stream=tar" is supported
1758
            $slave_info = $option_tmpdir . '/xtrabackup_slave_info';
1759
        } else {
1760
            $backup_config_file = $backup_dir . '/backup-my.cnf';
1761
            $binlog_info = $backup_dir . '/xtrabackup_binlog_info';
353 by Alexey Kopytov
Cherrypick of Galera support.
1762
            $galera_info = $backup_dir . '/xtrabackup_galera_info';
22 by kinoyasu
"innobackup --stream=tar" is supported
1763
            $slave_info = $backup_dir . '/xtrabackup_slave_info';
1764
        }
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1765
        }
1766
        write_backup_config_file($backup_config_file);
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1767
    } elsif ($option_copy_back || $option_move_back) {
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1768
        #$backup_config_file = $backup_dir . '/backup-my.cnf';
1769
        #read_config_file($backup_config_file, \%backup_config);
391.66.1 by George O. Lorch III
Fixed https://bugs.launchpad.net/percona-xtrabackup/+bug/1007446
1770
    }
1771
1772
    if ( -e "$suspend_file" ) {
1773
        print STDERR "WARNING : A left over instance of suspend file '$suspend_file' was found.\n";
1774
        unlink $suspend_file || Die "Failed to delete '$suspend_file': $!";
1775
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1776
}
1777
1778
1779
#
1780
# write_backup_config_file subroutine creates a backup options file for
1781
# ibbackup program. It writes to the file only those options that
1782
# are required by ibbackup.
1783
#    Parameters:
1784
#       filename  name for the created options file
1785
#
1786
sub write_backup_config_file {
1787
    my $filename = shift;
42 by kinoyasu
fix bug 357653; remove innobackup-1.5.1.patch
1788
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1789
    if (!$option_remote_host) {
1790
        open(FILE, "> $filename") || Die "Failed to open file '$filename': $!";
1791
    } else {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
1792
        open(FILE, "| ssh $option_ssh_opt $option_remote_host 'cat > $filename'")
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
1793
            || Die "Failed to open file '$option_remote_host:$filename': $!";
1794
    }
1795
242.44.1 by Sergei Glushchenko
Bug #733651
1796
    my @option_names = (
242.53.1 by Sergei Glushchenko
Bug 983685: innodb_data_file_path is not written to backup-my.cnf
1797
        "innodb_data_file_path",
242.44.1 by Sergei Glushchenko
Bug #733651
1798
        "innodb_log_files_in_group",
1799
        "innodb_log_file_size",
1800
        "innodb_fast_checksum",
1801
        "innodb_page_size",
391.59.1 by Sergei Glushchenko
Bug #1066843: Fix for bug #932623 does not take separate doublewrite
1802
        "innodb_log_block_size"
242.44.1 by Sergei Glushchenko
Bug #733651
1803
        );
1804
1805
    my $options_dump = "# This MySQL options file was generated by $innobackup_script.\n\n" .
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1806
          "# The MySQL server\n" .
242.47.10 by Sergei Glushchenko
Bug 996493: innobackupex --apply-log doesn't read config from backup-my.cnf
1807
          "[mysqld]\n";
242.44.1 by Sergei Glushchenko
Bug #733651
1808
1809
    my $option_name;
1810
    foreach $option_name (@option_names) {
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1811
        if (has_option(\%config, $option_defaults_group, $option_name)) {
1812
            my $option_value = get_option(\%config, $option_defaults_group, $option_name);
242.44.1 by Sergei Glushchenko
Bug #733651
1813
            $options_dump .= "$option_name=$option_value\n";
1814
        }
1815
    }
391.59.1 by Sergei Glushchenko
Bug #1066843: Fix for bug #932623 does not take separate doublewrite
1816
    if (has_option(\%config,
1817
        $option_defaults_group, "innodb_doublewrite_file")) {
1818
        $options_dump .= "innodb_doublewrite_file=" . (split(/\/+/,
1819
                get_option(\%config, $option_defaults_group,
1820
                            'innodb_doublewrite_file')))[-1] . "\n";
1821
    }
242.44.1 by Sergei Glushchenko
Bug #733651
1822
1823
    print FILE $options_dump;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1824
    close(FILE);
22 by kinoyasu
"innobackup --stream=tar" is supported
1825
1826
    if ($option_stream) {
109.1.1 by Aleksandr Kuzminsky
Fixed Bug #434486: Using dirname(), basename() instead of manual parsing of filenames
1827
        my $filename_dir = dirname($filename);
1828
        my $filename_name = basename($filename);
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1829
	system("cd $filename_dir; $stream_cmd $filename_name")
1830
	  and Die "Failed to stream '$filename_name': $!";
1831
22 by kinoyasu
"innobackup --stream=tar" is supported
1832
        unlink $filename || Die "Failed to delete '$filename': $!";
1833
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1834
}
1835
1836
1837
#
1838
# check_args subroutine checks command line arguments. If there is a problem,
1839
# this subroutine prints error message and exits.
1840
#
1841
sub check_args {
1842
    my $i;
1843
    my $rcode;
1844
    my $buf;
1845
    my $perl_version;
1846
1847
    # check the version of the perl we are running
1848
    if (!defined $^V) {
1849
        # this perl is prior to 5.6.0 and uses old style version string
1850
        my $required_version = $required_perl_version_old_style;
1851
        if ($] lt $required_version) {
22 by kinoyasu
"innobackup --stream=tar" is supported
1852
            print STDERR "$prefix Warning: " . 
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1853
                "Your perl is too old! Innobackup requires\n";
22 by kinoyasu
"innobackup --stream=tar" is supported
1854
            print STDERR "$prefix Warning: perl $required_version or newer!\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1855
        }
1856
    }
1857
1858
    if (@ARGV == 0) {
1859
        # no command line arguments
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
1860
        print STDERR "$prefix You must specify the backup directory.\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1861
        exit(1);
1862
    }
1863
1864
    # read command line options
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1865
    $rcode = GetOptions('compress' => \$option_compress,
1866
	    		'compress-threads=i' => \$option_compress_threads,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1867
                        'help' => \$option_help,
1868
                        'version' => \$option_version,
1869
                        'throttle=i' => \$option_throttle,
1870
                        'sleep=i' => \$option_sleep,
1871
                        'apply-log' => \$option_apply_log,
181 by root
added --redo-only option to innobackupex
1872
                        'redo-only' => \$option_redo_only,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1873
                        'copy-back' => \$option_copy_back,
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1874
                        'move-back' => \$option_move_back,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1875
                        'include=s' => \$option_include,
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
1876
                        'databases=s' => \$option_databases,
1877
                        'tables-file=s', => \$option_tables_file,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1878
                        'use-memory=s' => \$option_use_memory,
1879
                        'uncompress' => \$option_uncompress,
61 by kinoyasu
innobackupex can pass --export option
1880
                        'export' => \$option_export,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1881
                        'password=s' => \$option_mysql_password,
1882
                        'user=s' => \$option_mysql_user,
116.1.1 by Daniel Nichter
Bug 510965: add --host, remove --host=127.0.0.1 from --port.
1883
                        'host=s' => \$option_mysql_host,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1884
                        'port=s' => \$option_mysql_port,
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
1885
                        'defaults-group=s' => \$option_defaults_group,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1886
                        'slave-info' => \$option_slave_info,
353 by Alexey Kopytov
Cherrypick of Galera support.
1887
                        'galera-info' => \$option_galera_info,
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1888
                        'socket=s' => \$option_mysql_socket,
1889
                        'no-timestamp' => \$option_no_timestamp,
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
1890
                        'defaults-file=s' => \$option_defaults_file,
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
1891
                        'defaults-extra-file=s' => \$option_defaults_extra_file,
14 by kinoyasu
using rsync for incremental backup
1892
                        'incremental' => \$option_incremental,
130 by Vadim Tkachenko
bugfix 546876
1893
                        'incremental-basedir=s' => \$option_incremental_basedir,
219 by Vadim Tkachenko
Added option --incremental-lsn to innobackupex script.
1894
                        'incremental-lsn=s' => \$option_incremental_lsn,
218 by Vadim Tkachenko
Added option --incremental-dir to innobackupex script
1895
                        'incremental-dir=s' => \$option_incremental_dir,
220 by Vadim Tkachenko
Bugfix #680936
1896
                        'extra-lsndir=s' => \$option_extra_lsndir,
14 by kinoyasu
using rsync for incremental backup
1897
                        'remote-host=s' => \$option_remote_host,
22 by kinoyasu
"innobackup --stream=tar" is supported
1898
                        'stream=s' => \$option_stream,
242.25.4 by Alexey Kopytov
Implementation of
1899
			'rsync' => \$option_rsync,
22 by kinoyasu
"innobackup --stream=tar" is supported
1900
                        'tmpdir=s' => \$option_tmpdir,
100.1.1 by Aleksandr Kuzminsky
--no-lock option is added
1901
                        'no-lock' => \$option_no_lock,
70.1.1 by Aleksandr Kuzminsky
Allow configuration of scp options
1902
                        'ibbackup=s' => \$option_ibbackup_binary,
119.1.1 by Daniel Nichter
Make --stream=tar use tar4ibd by default unless hidden --force-tar is specified.
1903
                        'scpopt=s' => \$option_scp_opt,
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
1904
                        'sshopt=s' => \$option_ssh_opt,
211 by Alexey Kopytov
Manual merge from lp:~percona-dev/percona-xtrabackup/safe-slave-backup
1905
                        'parallel=i' => \$option_parallel,
1906
                        'safe-slave-backup' => \$option_safe_slave_backup,
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
1907
                        'safe-slave-backup-timeout=i' => \$option_safe_slave_backup_timeout,
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
1908
    );
1909
                        
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1910
    if (!$rcode) {
1911
        # failed to read options
22 by kinoyasu
"innobackup --stream=tar" is supported
1912
        print STDERR "$prefix Bad command line arguments\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1913
        exit(1);
1914
    }
1915
    if ($option_help) {
1916
        # print help text and exit
1917
        usage();
1918
        exit(0);
1919
    }
1920
    if ($option_version) {
1921
        # print program version and copyright
1922
        print_version();
1923
        exit(0);
1924
    }
1925
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
1926
    if ($option_defaults_file && $option_defaults_extra_file) {
1927
        print STDERR "$prefix --defaults-file and --defaults-extra-file " .
1928
            "options are mutually exclusive";
1929
        exit(1);
1930
    }
1931
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1932
    if ($option_copy_back && $option_move_back) {
1933
        print STDERR "$prefix --copy-back and --move-back options are " .
1934
            "mutually exclusive";
1935
    }
1936
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1937
    if ($option_compress == 0) {
1938
        # compression level no specified, use default level
1939
        $option_compress = $default_compression_level;
1940
    } 
1941
1942
    if ($option_compress == 999) {
1943
        # compress option not given in the command line
1944
	$option_compress = 0;
1945
    }
1946
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
1947
    if ($option_stream eq 'tar') {
1948
      $stream_cmd = 'tar chf -';
1949
    } elsif ($option_stream eq 'xbstream') {
1950
      $stream_cmd = 'xbstream -c'
1951
    }
1952
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1953
    if (@ARGV < 1) {
22 by kinoyasu
"innobackup --stream=tar" is supported
1954
        print STDERR "$prefix Missing command line argument\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1955
        exit(1);
1956
    } elsif (@ARGV > 1) {
22 by kinoyasu
"innobackup --stream=tar" is supported
1957
        print STDERR "$prefix Too many command line arguments\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1958
        exit(1);
1959
    }
1960
1961
    # get options file name
1962
    #$config_file = $ARGV[0];
1963
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
1964
    if (!$option_apply_log && !$option_copy_back && !$option_move_back) {
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1965
        # we are making a backup, get backup root directory
1966
        $backup_root = $ARGV[0];
242.23.1 by Lachlan Mulcahy
Fixed bug 860133
1967
        if ($option_incremental && !$option_incremental_lsn) {
66 by Vadim Tkachenko
Added incremental option to innobackupex-1.5.1 script
1968
            my @dirs = `ls -1 -t $backup_root`;
1969
            my $inc_dir = $dirs[0];
1970
            chomp($inc_dir);
130 by Vadim Tkachenko
bugfix 546876
1971
            if ($option_incremental_basedir) {
1972
                $incremental_basedir = $option_incremental_basedir;
1973
            } else {
1974
                $incremental_basedir = File::Spec->catfile($backup_root, $inc_dir);
1975
            }
14 by kinoyasu
using rsync for incremental backup
1976
        }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
1977
    } else {
1978
        # get backup directory
1979
        $backup_dir = File::Spec->rel2abs($ARGV[0]);
1980
    }        
1981
242.14.1 by Alexey Kopytov
Bug #834657: --slave-info does not work with --no-lock
1982
    if ($option_slave_info) {
301.2.1 by Alexey Kopytov
Manual merge of the fix for bug #834657 from 1.6.
1983
        if ($option_no_lock and !$option_safe_slave_backup) {
1984
	  print STDERR "--slave-info is used with --no-lock but without --safe-slave-backup. The binlog position cannot be consistent with the backup data.\n";
242.14.1 by Alexey Kopytov
Bug #834657: --slave-info does not work with --no-lock
1985
	    exit(1);
1986
	}
1987
    }
1988
242.25.4 by Alexey Kopytov
Implementation of
1989
    if ($option_rsync && ($option_remote_host || $option_stream)) {
1990
        print STDERR "--rsync doesn't work with --remote-host or --stream\n";
1991
	exit(1);
1992
    }
1993
242.48.1 by Stewart Smith
Deprecate --remote-host in favor of streaming backups. We will remove --remote-host in 2.0
1994
    if ($option_remote_host) {
1995
	print STDERR "\nWARNING: --remote-host is DEPRECATED and will be ",
242.47.3 by Stewart Smith
merge deprecation warning for --remote-host, updating version it to be removed in to 2.1
1996
	"removed in Percona XtraBackup 2.1 in favor of streaming backups.\n";
242.48.1 by Stewart Smith
Deprecate --remote-host in favor of streaming backups. We will remove --remote-host in 2.0
1997
    }
1998
22 by kinoyasu
"innobackup --stream=tar" is supported
1999
    print STDERR "\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2000
2001
    parse_databases_option_value();
171 by Daniel Nichter
Filter files copied according to --tables-file.
2002
    parse_tables_file_option_value($option_tables_file);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2003
}
2004
2005
2006
#
2007
# make_backup_dir subroutine creates a new backup directory and returns
2008
# its name.
2009
#
2010
sub make_backup_dir {
2011
    my $dir;
2012
    my $innodb_data_file_path = 
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
2013
        get_option(\%config, $option_defaults_group, 'innodb_data_file_path');
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2014
2015
    # create backup directory
2016
    $dir = $backup_root;
22 by kinoyasu
"innobackup --stream=tar" is supported
2017
    if ($option_stream) {
2018
        return $dir;
2019
    }
2020
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2021
    $dir .= '/' . strftime("%Y-%m-%d_%H-%M-%S", localtime())
2022
       unless $option_no_timestamp;
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
2023
    if (!$option_remote_host) {
2024
        mkdir($dir, 0777) || Die "Failed to create backup directory $dir: $!";
2025
    } else {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
2026
        system("ssh $option_ssh_opt $option_remote_host mkdir $dir");
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
2027
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2028
2029
    # create subdirectories for ibdata files if needed
42 by kinoyasu
fix bug 357653; remove innobackup-1.5.1.patch
2030
#    foreach my $a (split(/;/, $innodb_data_file_path)) {
2031
#        my $path = (split(/:/,$a))[0];
2032
#        my @relative_path = split(/\/+/, $path);
2033
#        pop @relative_path;
2034
#        if (@relative_path) {
2035
#            # there is a non-trivial path from the backup directory
2036
#            # to the directory of this backup ibdata file, check
2037
#            # that all the directories in the path exist.
2038
#            create_path_if_needed($dir, \@relative_path);
2039
#        }
2040
#    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2041
2042
    return $dir;
2043
}
2044
2045
2046
#
2047
# create_path_if_needed subroutine checks that all components
2048
# in the given relative path are directories. If the
2049
# directories do not exist, they are created.
2050
#    Parameters:
2051
#       root           a path to the root directory of the relative pathname
2052
#       relative_path  a relative pathname (a reference to an array of 
2053
#                      pathname components) 
2054
#
2055
sub create_path_if_needed {
2056
    my $root = shift;
2057
    my $relative_path = shift;
2058
    my $path;
2059
2060
    $path = $root;
2061
    foreach $a (@{$relative_path}) {
2062
        $path = $path . "/" . $a;
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
2063
        if (!$option_remote_host) {
2064
            if (! -d $path) {
2065
                # this directory does not exist, create it !
2066
                mkdir($path, 0777) || Die "Failed to create backup directory: $!";
2067
            }
2068
        } else {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
2069
            if (system("ssh $option_ssh_opt $option_remote_host test -d $path") != 0) {
2070
                system("ssh $option_ssh_opt $option_remote_host mkdir $path");
17 by kinoyasu
--remote-host uses scp/ssh, remove using rsync
2071
            }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2072
        }
2073
    }
2074
}
2075
2076
2077
#
2078
# remove_from_array subroutine removes excluded element from the array.
2079
#    Parameters:
2080
#       array_ref   a reference to an array of strings
2081
#       excluded   a string to be excluded from the copy
2082
#  
2083
sub remove_from_array {
2084
    my $array_ref = shift;
2085
    my $excluded = shift;
2086
    my @copy = ();
2087
    my $size = 0;
2088
2089
    foreach my $str (@{$array_ref}) {
2090
        if ($str ne $excluded) {
2091
            $copy[$size] = $str;
2092
            $size = $size + 1;
2093
        }
2094
    }
2095
    @{$array_ref} = @copy;
2096
}
2097
2098
2099
#
2100
# backup_files subroutine copies .frm, .MRG, .MYD and .MYI files to 
2101
# backup directory.
2102
#
2103
sub backup_files {
242.25.4 by Alexey Kopytov
Implementation of
2104
    my $prep_mode = shift;
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
2105
    my $source_dir = get_option(\%config, $option_defaults_group, 'datadir');
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2106
    my @list;
2107
    my $file;
2108
    my $database;
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2109
    my $wildcard = '*.{frm,MYD,MYI,MAD,MAI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}';
242.25.4 by Alexey Kopytov
Implementation of
2110
    my $rsync_file_list;
2111
    my $operation;
2112
    my $rsync_tmpfile_pass1 = "$option_tmpdir/xtrabackup_rsyncfiles_pass1";
2113
    my $rsync_tmpfile_pass2 = "$option_tmpdir/xtrabackup_rsyncfiles_pass2";
2114
2115
    # prep_mode will pre-copy the data, so that rsync is faster the 2nd time
2116
    # saving time while all tables are locked.
2117
    # currently only rsync mode is supported for prep.
2118
    if ($prep_mode and !$option_rsync) {
2119
        return;
2120
    }
2121
2122
    if ($option_rsync) {
2123
	if ($prep_mode) {
2124
	    $rsync_file_list = $rsync_tmpfile_pass1;
2125
	} else {
2126
	    $rsync_file_list = $rsync_tmpfile_pass2;
2127
	}
2128
	open(RSYNC, ">$rsync_file_list")
2129
	    || Die "Can't open $rsync_file_list for writing: $!\n";
2130
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2131
2132
    opendir(DIR, $source_dir) 
2133
        || Die "Can't open directory '$source_dir': $!\n";
2134
    $now = current_time();
242.25.4 by Alexey Kopytov
Implementation of
2135
    if ($prep_mode) {
2136
	$operation = "a prep copy of";
2137
    } else {
2138
	$operation = "to backup";
2139
    }
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2140
    print STDERR "\n$now  $prefix Starting $operation non-InnoDB tables and files\n";
2141
    print STDERR "$prefix in subdirectories of '$source_dir'\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2142
    # loop through all database directories
2143
    while (defined($database = readdir(DIR))) {
2144
        my $print_each_file = 0;
2145
        my $file_c;
83 by Aleksandr Kuzminsky
Fixed bug394464
2146
        my @scp_files;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2147
        # skip files that are not database directories
2148
        if ($database eq '.' || $database eq '..') { next; }
2149
        next unless -d "$source_dir/$database";
171 by Daniel Nichter
Filter files copied according to --tables-file.
2150
	     next unless check_if_required($database);
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2151
        
22 by kinoyasu
"innobackup --stream=tar" is supported
2152
        if (!$option_remote_host && !$option_stream) {
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
2153
            if (! -e "$backup_dir/$database") {
2154
                # create database directory for the backup
2155
                mkdir("$backup_dir/$database", 0777)
2156
                    || Die "Couldn't create directory '$backup_dir/$database': $!";
2157
            }
22 by kinoyasu
"innobackup --stream=tar" is supported
2158
        } elsif ($option_remote_host) {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
2159
            if (system("ssh $option_ssh_opt $option_remote_host test -e $backup_dir/$database")
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
2160
                    != 0) {
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
2161
                system("ssh $option_ssh_opt $option_remote_host mkdir $backup_dir/$database");
15 by kinoyasu
trial implementation of --remote-host when --incremental (innobackup)
2162
            }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2163
        }
2164
2165
        # copy files of this database
108.1.1 by Aleksandr Kuzminsky
Fixed number of issues in innobackupex-1.5.1 on windows platform
2166
	opendir(DBDIR, "$source_dir/$database");
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2167
	@list = grep(/\.(frm|MYD|MYI|MAD|MAI|MRG|TRG|TRN|ARM|ARZ|CSM|CSV|opt|par)$/, readdir(DBDIR));
108.1.1 by Aleksandr Kuzminsky
Fixed number of issues in innobackupex-1.5.1 on windows platform
2168
	closedir DBDIR;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2169
        $file_c = @list;
2170
        if ($file_c <= $backup_file_print_limit) {
2171
            $print_each_file = 1;
2172
        } else {
22 by kinoyasu
"innobackup --stream=tar" is supported
2173
            print STDERR "$prefix Backing up files " . 
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2174
                "'$source_dir/$database/$wildcard' ($file_c files)\n";
2175
        }
2176
        foreach $file (@list) {
2177
            next unless check_if_required($database, $file);
217 by Vadim Tkachenko
Bugfix #597384
2178
2179
	    if($option_include) {
391.67.1 by Sergei Glushchenko
Bug 711166
2180
	      my $table_name = get_table_name($file);
2181
	      if (!("$database.$table_name" =~ /$option_include/)) {
217 by Vadim Tkachenko
Bugfix #597384
2182
	        print STDERR "$database.$file is skipped because it does not match $option_include.\n";
2183
	        next;
2184
	      }
2185
	    }
2186
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2187
               
2188
            if ($print_each_file) {
108.1.1 by Aleksandr Kuzminsky
Fixed number of issues in innobackupex-1.5.1 on windows platform
2189
                print STDERR "$prefix Backing up file '$source_dir/$database/$file'\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2190
            }
242.25.4 by Alexey Kopytov
Implementation of
2191
2192
	    if ($option_rsync) {
2193
		print RSYNC "$database/$file\n";
2194
		if (!$prep_mode) {
2195
		    $rsync_files_hash{"$database/$file"} = 1;
2196
		}
2197
            } elsif (!$option_remote_host && !$option_stream) {
108.1.4 by Aleksandr Kuzminsky
escape_path() function is added.
2198
                $src_name = escape_path("$source_dir/$database/$file");
2199
                $dst_name = escape_path("$backup_dir/$database");
242.26.20 by Alexey Kopytov
Fix for bug #924026: innobackupex fails during backup if table is
2200
                # Copy the file - If we get an error and the file actually exists, die with error msg
2201
                copy_if_exists("$src_name", "$dst_name")
2202
                  or Die "Failed to copy file '$file': $!";
22 by kinoyasu
"innobackup --stream=tar" is supported
2203
            } elsif ($option_remote_host) {
83 by Aleksandr Kuzminsky
Fixed bug394464
2204
                # Queue up files for one single scp per database.
2205
                push(@scp_files, "'$file'");
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2206
            } elsif($option_stream) {
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
2207
                my $ret = 0;
22 by kinoyasu
"innobackup --stream=tar" is supported
2208
                my $file_name = substr($file, rindex($file, '/') + 1);
86.1.1 by akuzminsky
Fixed bug#417178
2209
                $file_name=~s/([\$\\\" ])/\\$1/g;
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2210
                $ret = system("cd $source_dir; $stream_cmd $database/$file_name") >> 8;
2211
                if ($ret == 1 && $option_stream eq 'tar') {
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
2212
                    print STDERR "$prefix If you use GNU tar, this warning can be ignored.\n";
242.21.2 by Lachlan Mulcahy
Changed handling of non-existant files to use that same style as other places in innobackupex
2213
                # Check for non-zero exit code
2214
                } elsif ($ret != 0) {
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2215
                    print STDERR "$prefix $stream_cmd returned with exit code $ret.\n";
242.21.2 by Lachlan Mulcahy
Changed handling of non-existant files to use that same style as other places in innobackupex
2216
                    # Only treat as fatal cases where the file exists
2217
                    if ( -e "$database/$file_name" ) {
2218
                        Die "Failed to stream '$database/$file_name': $!";
2219
                    } else {
2220
                        print STDERR "$prefix Ignoring nonexistent file '$database/$file_name'.\n";
2221
                    }
2222
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
2223
                }
14 by kinoyasu
using rsync for incremental backup
2224
            }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2225
        }
131 by Vadim Tkachenko
bugfix 552111
2226
        if ($option_remote_host and @scp_files) {
114.1.1 by Daniel Nichter
Fix bug 510960: innobackupex --remote-host scp doesn't copy MyISAM files
2227
          my $scp_file_list = join(" ", map { "$source_dir/$database/$_" } @scp_files);
83 by Aleksandr Kuzminsky
Fixed bug394464
2228
          system("scp $option_scp_opt $scp_file_list '$option_remote_host:$backup_dir/$database/'")
102 by kinoyasu
change error message of scp .frm files
2229
              and Die "Failed to execute \"scp $option_scp_opt $scp_file_list '$option_remote_host:$backup_dir/$database/'\": $!";
83 by Aleksandr Kuzminsky
Fixed bug394464
2230
        }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2231
    }
2232
    closedir(DIR);
2233
242.25.4 by Alexey Kopytov
Implementation of
2234
    if ($option_rsync) {
380.2.1 by Sergei Glushchenko
Bug 543134.
2235
        if (-e "$source_dir/ib_lru_dump") {
2236
            print RSYNC "ib_lru_dump\n";
2237
            if (!$prep_mode) {
2238
                $rsync_files_hash{"ib_lru_dump"} = 1;
2239
            }
2240
        }
242.25.4 by Alexey Kopytov
Implementation of
2241
	close(RSYNC);
2242
2243
	# do the actual rsync now
2244
	$now = current_time();
2245
	my $rsync_cmd = "rsync -t \"$source_dir\" --files-from=\"$rsync_file_list\" \"$backup_dir\"";
2246
	print STDERR "$now Starting rsync as: $rsync_cmd\n";
2247
2248
	# ignore errors in the prep mode, since we are running without lock,
2249
	# so some files may have disappeared.
2250
	if (system("$rsync_cmd") && !$prep_mode) {
2251
	    Die "rsync failed: $!\n";
2252
	}
2253
2254
	$now = current_time();
2255
	print STDERR "$now rsync finished successfully.\n";
2256
2257
	# Remove from $backup_dir files that have been removed between first and
2258
	# second passes. Cannot use "rsync --delete" because it does not work
2259
	# with --files-from.
2260
	if (!$prep_mode) {
2261
	    open(RSYNC, "<$rsync_tmpfile_pass1")
2262
		|| Die "Can't open $rsync_tmpfile_pass1 for reading: $!\n";
2263
2264
	    while (<RSYNC>) {
2265
		chomp;
2266
		if (!exists $rsync_files_hash{$_}) {
2267
		    print STDERR "Removing '$backup_dir/$_'\n";
2268
		    unlink "$backup_dir/$_";
2269
		}
2270
	    }
2271
2272
	    close(RSYNC);
2273
	    unlink "$rsync_tmpfile_pass1" || \
2274
		Die "Failed to delete $rsync_tmpfile_pass1: $!";
2275
	    unlink "$rsync_tmpfile_pass2" || \
2276
		Die "Failed to delete $rsync_tmpfile_pass2: $!";
2277
	}
2278
    }
2279
2280
    if ($prep_mode) {
2281
	$operation = "a prep copy of";
2282
    } else {
2283
	$operation = "backing up";
2284
    }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2285
    $now = current_time();
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2286
    print STDERR "$now  $prefix Finished $operation non-InnoDB tables and files\n\n";
242.25.4 by Alexey Kopytov
Implementation of
2287
 }
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2288
2289
2290
#
2291
# file_to_array subroutine reads the given text file into an array and
2292
# stores each line as an element of the array. The end-of-line
2293
# character(s) are removed from the lines stored in the array.
2294
#    Parameters:
2295
#       filename   name of a text file
2296
#       lines_ref  a reference to an array
2297
#
2298
sub file_to_array {
2299
    my $filename = shift;
2300
    my $lines_ref = shift;
2301
    
2302
    open(FILE, $filename) || Die "can't open file '$filename': $!";
2303
    @{$lines_ref} = <FILE>;
2304
    close(FILE) || Die "can't close file '$filename': $!";
2305
2306
    foreach my $a (@{$lines_ref}) {
2307
        chomp($a);
2308
    }
2309
}
2310
2311
2312
#
2313
# unescape_string subroutine expands escape sequences found in the string and
2314
# returns the expanded string. It also removes possible single or double quotes
2315
# around the value.
2316
#    Parameters:
2317
#       value   a string
2318
#    Return value:
2319
#       a string with expanded escape sequences
2320
# 
2321
sub unescape_string {
2322
    my $value = shift;
2323
    my $result = '';
2324
    my $offset = 0;
2325
2326
    # remove quotes around the value if they exist
2327
    if (length($value) >= 2) {
2328
        if ((substr($value, 0, 1) eq "'" && substr($value, -1, 1) eq "'")
2329
            || (substr($value, 0, 1) eq '"' && substr($value, -1, 1) eq '"')) {
2330
            $value = substr($value, 1, -1);
2331
        }
2332
    }
2333
    
2334
    # expand escape sequences
2335
    while ($offset < length($value)) {
2336
        my $pos = index($value, "\\", $offset);
2337
        if ($pos < 0) {
2338
            $pos = length($value);
2339
            $result = $result . substr($value, $offset, $pos - $offset);
2340
            $offset = $pos;
2341
        } else {
2342
            my $replacement = substr($value, $pos, 2);
2343
            my $escape_code = substr($value, $pos + 1, 1);
2344
            if (exists $option_value_escapes{$escape_code}) {
2345
                $replacement = $option_value_escapes{$escape_code};
2346
            }
2347
            $result = $result 
2348
                . substr($value, $offset, $pos - $offset)
2349
                . $replacement;
2350
            $offset = $pos + 2;
2351
        }
2352
    }
2353
2354
    return $result;
2355
}
2356
2357
2358
#
2359
# read_config_file subroutine reads MySQL options file and
2360
# returns the options in a hash containing one hash per group.
2361
#    Parameters:
2362
#       filename    name of a MySQL options file
2363
#       groups_ref  a reference to hash variable where the read
2364
#                   options are returned
2365
#
2366
sub read_config_file {
2367
    #my $filename = shift;
2368
    my $groups_ref = shift;
2369
    my @lines ;
2370
    my $i;
2371
    my $group;
2372
    my $group_hash_ref;
2373
2374
    my $cmdline = '';
43 by kinoyasu
fix bug 358266, bug 359341
2375
    my $options = '';
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2376
242.47.10 by Sergei Glushchenko
Bug 996493: innobackupex --apply-log doesn't read config from backup-my.cnf
2377
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
2378
    if ($option_defaults_file) {
108.1.5 by Aleksandr Kuzminsky
Fixed issue in quoting defaults-file when specified
2379
        $options = $options . " --defaults-file=\"$option_defaults_file\" ";
242.47.10 by Sergei Glushchenko
Bug 996493: innobackupex --apply-log doesn't read config from backup-my.cnf
2380
    } elsif ($option_apply_log) {
2381
        $options = $options . " --defaults-file=\"${backup_dir}/backup-my.cnf\" ";
36 by kinoyasu
some small fixes of innobackupex: bug 355214, bug 356776, etc
2382
    }
2383
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
2384
    if ($option_defaults_extra_file) {
2385
        $options = $options . " --defaults-extra-file=\"$option_defaults_extra_file\" ";
2386
    }
2387
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
2388
    if ($option_defaults_group) {
2389
        $options = $options . " --defaults-group=\"$option_defaults_group\" ";
2390
    }
2391
43 by kinoyasu
fix bug 358266, bug 359341
2392
    $options = $options . "--print-param";
2393
108.1.5 by Aleksandr Kuzminsky
Fixed issue in quoting defaults-file when specified
2394
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2395
    # read file to an array, one line per element
2396
    #file_to_array($filename, \@lines);
2397
    $cmdline = "$option_ibbackup_binary $options";
2398
    @lines = `$cmdline`;
2399
2400
    # classify lines and save option values
2401
    $group = 'default';
2402
    $group_hash_ref = {}; 
2403
    ${$groups_ref}{$group} = $group_hash_ref;
2404
    # this pattern described an option value which may be
2405
    # quoted with single or double quotes. This pattern
2406
    # does not work by its own. It assumes that the first
2407
    # opening parenthesis in this string is the second opening
2408
    # parenthesis in the full pattern. 
108.1.1 by Aleksandr Kuzminsky
Fixed number of issues in innobackupex-1.5.1 on windows platform
2409
    my $value_pattern = q/((["'])([^\\\4]|(\\[^\4]))*\4)|([^\s]+)/;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2410
    for ($i = 0; $i < @lines; $i++) {
2411
      SWITCH: for ($lines[$i]) {
2412
          # comment
2413
          /^\s*(#|;)/
2414
             && do { last; };
2415
2416
          # group      
2417
          /^\s*\[(.*)\]/ 
2418
                && do { 
2419
                    $group = $1; 
2420
                    if (!exists ${$groups_ref}{$group}) {
2421
                        $group_hash_ref = {}; 
2422
                        ${$groups_ref}{$group} = $group_hash_ref;
2423
                    } else {
2424
                        $group_hash_ref = ${$groups_ref}{$group};
2425
                    }
2426
                    last; 
2427
                };
2428
2429
          # option
2430
          /^\s*([^\s=]+)\s*(#.*)?$/
2431
              && do { 
2432
                  ${$group_hash_ref}{$1} = '';
2433
                  last; 
2434
              };
2435
2436
          # set-variable = option = value
2437
          /^\s*set-variable\s*=\s*([^\s=]+)\s*=\s*($value_pattern)\s*(#.*)?$/
2438
              && do { ${$group_hash_ref}{$1} = unescape_string($2); last; };
2439
2440
          # option = value
2441
          /^\s*([^\s=]+)\s*=\s*($value_pattern)\s*(#.*)?$/
2442
              && do { ${$group_hash_ref}{$1} = unescape_string($2); last; };
2443
2444
          # empty line
2445
          /^\s*$/
2446
              && do { last; };
2447
2448
          # unknown
2449
          print("$prefix: Warning: Ignored unrecognized line ",
2450
                $i + 1,
2451
                " in options : '${lines[$i]}'\n"
2452
                );
2453
      }
2454
   }
2455
}
2456
    
2457
242.44.1 by Sergei Glushchenko
Bug #733651
2458
# has_option return whether the config has an option with the given name
2459
#    Parameters:
2460
#       config_ref   a reference to a config data
2461
#       group        option group name
2462
#       option_name  name of the option
2463
#    Return value:
2464
#       true if option exists, otherwise false
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2465
#
242.44.1 by Sergei Glushchenko
Bug #733651
2466
sub has_option {
2467
    my $config_ref = shift;
2468
    my $group = shift;
2469
    my $option_name = shift;
2470
    my $group_hash_ref;
2471
2472
    if (!exists ${$config_ref}{$group}) {
2473
        # no group
2474
        print STDERR "$prefix fatal error: no '$group' group in MySQL options\n";
2475
        exit(1);
2476
    }
2477
2478
    $group_hash_ref = ${$config_ref}{$group};
2479
    
2480
    return exists ${$group_hash_ref}{$option_name};
2481
}
2482
    
2483
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2484
# get_option subroutine returns the value of given option in the config
2485
# structure. If option is missing, this subroutine calls exit.
2486
#    Parameters:
2487
#       config_ref   a reference to a config data
2488
#       group        option group name
2489
#       option_name  name of the option
2490
#    Return value:
2491
#       option value as a string
2492
#
2493
sub get_option {
2494
    my $config_ref = shift;
2495
    my $group = shift;
2496
    my $option_name = shift;
2497
    my $group_hash_ref;
2498
2499
    if (!exists $config{$group}) {
2500
        # no group
22 by kinoyasu
"innobackup --stream=tar" is supported
2501
        print STDERR "$prefix fatal error: no '$group' group in MySQL options\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2502
        exit(1);
2503
    }
2504
    
2505
    $group_hash_ref = ${$config_ref}{$group};
2506
    if (!exists ${$group_hash_ref}{$option_name}) {
2507
        # no option
22 by kinoyasu
"innobackup --stream=tar" is supported
2508
        print STDERR "$prefix fatal error: no '$option_name' option in group '$group' in MySQL options\n";
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2509
        exit(1);
2510
    }
2511
2512
    return ${$group_hash_ref}{$option_name};
2513
}
2514
391.67.1 by Sergei Glushchenko
Bug 711166
2515
# get_table_name subroutine returns table name of specified file.
2516
#    Parameters:
2517
#       $_[0]        table path
2518
#    Return value:
2519
#       1 table name
2520
#
2521
sub get_table_name {
2522
   my $table_path = shift;
2523
   my $filename;
2524
   my $table;
2525
2526
   # get the last component in the table pathname 
2527
   $filename = (reverse(split(/\//, $table_path)))[0];
2528
   # get name of the table by removing file suffix
2529
   $table = (split(/\./, $filename))[0];
2530
   # and partition suffix
2531
   $table = (split('#P#', $table))[0];
2532
2533
   return $table;
2534
}
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2535
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2536
# check_if_required subroutine returns 1 if the specified database and
2537
# table needs to be backed up.
2538
#    Parameters:
2539
#       $_[0]        name of database to be checked 
2540
#       $_[1]        full path of table file (This argument is optional)
2541
#    Return value:
2542
#       1 if backup should be done and 0 if not
2543
#
2544
sub check_if_required {
171 by Daniel Nichter
Filter files copied according to --tables-file.
2545
   my ( $db, $table_path ) = @_;
2546
   my $db_count  = scalar keys %databases_list;
2547
   my $tbl_count = scalar keys %table_list;
2548
   my $table;
2549
2550
   if ( $db_count == 0 && $tbl_count == 0 ) {
2551
      # No databases defined with --databases option, include all databases,
2552
      # and no tables defined with --tables-file option, include all tables.
2553
      return 1;
2554
   }
2555
   else {
2556
      if ( $table_path ) {
391.67.1 by Sergei Glushchenko
Bug 711166
2557
         $table = get_table_name($table_path);
171 by Daniel Nichter
Filter files copied according to --tables-file.
2558
      }
2559
   }
2560
2561
   # Filter for --databases.
2562
   if ( $db_count ) {
2563
      if (defined $databases_list{$db}) {
2564
         if (defined $table_path) {
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2565
            my $db_hash = $databases_list{$db};
2566
            $db_count = keys %$db_hash;
2567
            if ($db_count > 0 && ! defined $databases_list{$db}->{$table}) {
171 by Daniel Nichter
Filter files copied according to --tables-file.
2568
               # --databases option specified, but table is not included
2569
               return 0;
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2570
            }
171 by Daniel Nichter
Filter files copied according to --tables-file.
2571
         }
2572
         # include this database and table
2573
         return 1;
2574
      }
2575
      else {
2576
         # --databases option given, but database is not included
2577
         return 0;
2578
      }
2579
   }
2580
2581
   # Filter for --tables-file.
2582
   if ( $tbl_count ) {
2583
      return 0 unless exists $table_list{$db};
2584
      return 0 if $table && !$table_list{$db}->{$table};
2585
   }
2586
2587
   return 1;  # backup the table
7 by Vadim Tkachenko
patche innobackup-1.5.1 added
2588
}
2589
2590
2591
# parse_databases_option_value subroutine parses the value of 
2592
# --databases option. If the option value begins with a slash
2593
# it is considered a pathname and the option value is read
2594
# from the file.
2595
# 
2596
# This subroutine sets the global "databases_list" variable.
2597
#
2598
sub parse_databases_option_value {
2599
    my $item;
2600
2601
    if ($option_databases =~ /^\//) {
2602
        # the value of the --databases option begins with a slash,
2603
        # the option value is pathname of the file containing
2604
        # list of databases
2605
        if (! -f $option_databases) {
2606
            Die "can't find file '$option_databases'";
2607
        }
2608
2609
        # read from file the value of --databases option
2610
        my @lines;
2611
    	file_to_array($option_databases, \@lines);
2612
	$option_databases = join(" ", @lines);
2613
    }
2614
2615
    # mark each database or database.table definition in the
2616
    # global databases_list.
2617
    foreach $item (split(/\s/, $option_databases)) {
2618
        my $db = "";
2619
        my $table = "";
2620
        my %hash;
2621
2622
        if ($item eq "") {
2623
            # ignore empty strings
2624
            next;
2625
        }
2626
2627
        # get database and table names
2628
        if ($item =~ /(\S*)\.(\S*)/) {
2629
            # item is of the form DATABASE.TABLE
2630
            $db = $1;
2631
            $table = $2;
2632
        } else {
2633
            # item is database name, table is undefined
2634
            $db = $item;
2635
        }
2636
2637
        if (! defined $databases_list{$db}) {
2638
            # create empty hash for the database
2639
            $databases_list{$db} = \%hash;
2640
        }
2641
        if ($table ne "") {
2642
            # add mapping table --> 1 to the database hash
2643
            my $h = $databases_list{$db};
2644
            $h->{$table} = 1;
2645
        }
2646
    }
2647
}
108.1.4 by Aleksandr Kuzminsky
escape_path() function is added.
2648
171 by Daniel Nichter
Filter files copied according to --tables-file.
2649
# Parse the --tables-file file to determine which InnoDB tables
2650
# are backedup up.  Only backedup tables have their .frm, etc.
2651
# files copied.
2652
sub parse_tables_file_option_value {
2653
   my ( $filename ) = @_;
2654
2655
   return unless $filename;
2656
2657
   open my $fh, '<', $filename;
2658
   if ( $fh ) {
2659
      while ( my $line = <$fh> ) {
172 by Daniel Nichter
Parse tables file as simple list of databases.tables.
2660
         chomp $line;
2661
         my ( $db, $tbl ) = $line =~ m/\s*([^\.]+)\.([^\.]+)\s*/;
171 by Daniel Nichter
Filter files copied according to --tables-file.
2662
         if ( $db && $tbl ) {
2663
            $table_list{$db}->{$tbl} = 1;
2664
            print STDERR "$prefix $db.$tbl will be registerd to the list\n";
2665
         }
2666
         else {
2667
            warn "$prefix Invalid line in $filename: $line";
2668
         }
2669
      }
2670
   }
2671
   else {
2672
      warn "$prefix Cannot read --tables-file $filename: $OS_ERROR";
2673
   }
2674
2675
   return;
2676
}
2677
108.1.4 by Aleksandr Kuzminsky
escape_path() function is added.
2678
sub escape_path {
2679
  my $str = shift;
2680
  if ($win eq 1) {
2681
    $str =~ s/\//\\/g;
2682
    $str =~ s/\\\\/\\/g;
2683
    }
2684
  else{
2685
    $str =~ s/\/\//\//g;
2686
    }
2687
  return $str;
2688
2689
}
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2690
2691
sub set_xtrabackup_version {
2692
# Based on MySQL version choose correct binary
178 by root
support for 5.5 in scripts
2693
#  MySQL 5.0.* - xtrabackup_51
2694
#  MySQL 5.1.* - xtrabackup_51
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
2695
#  MySQL 5.1.* with InnoDB plugin - xtrabackup
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2696
#  MariaDB 5.1.* - xtrabackup
2697
#  MariaDB 5.2.* - xtrabackup
2698
#  MariaDB 5.3.* - xtrabackup
391.64.13 by Hrvoje Matijakovic
- Bug fixes for: Bug #1100008, Bug #1095925, Bug #1092235, Bug #1088967,
2699
#  Percona Server 5.0 - xtrabackup_51
2700
#  Percona Server 5.1 - xtrabackup
2701
#  Percona Server 5.5 - xtrabackup_55
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2702
#  MySQL 5.5.* - xtrabackup_55
2703
#  MariaDB 5.5.* - xtrabackup_55
178 by root
support for 5.5 in scripts
2704
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2705
my @lines;
2706
my $var_version = '';
2707
my $var_innodb_version = '';
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
2708
my $ibbackup_binary;
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2709
mysql_open();
2710
mysql_send "SHOW VARIABLES LIKE 'version'\\G";
2711
file_to_array($mysql_stdout, \@lines);
2712
for (@lines) {
2713
	$var_version = $1 if /Value:\s+(\S+)/;
2714
	}
2715
mysql_send "SHOW VARIABLES LIKE 'innodb_version'\\G";
2716
file_to_array($mysql_stdout, \@lines);
2717
for (@lines) {
2718
	$var_innodb_version = $1 if /Value:\s+(\S+)/;
2719
	}
2720
if($var_version =~ m/5\.0\.\d/){
160 by Aleksandr Kuzminsky
Use new patch for MySQL 5.1
2721
	$ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup_51');
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2722
}
373 by Alexey Kopytov
Cherrypick of innodb version detection failure fix.
2723
if($var_version =~ m/5\.1\.\d/ and $var_innodb_version =~ m/.*/){
160 by Aleksandr Kuzminsky
Use new patch for MySQL 5.1
2724
	$ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup_51');
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2725
}
2726
if($var_version =~ m/5\.1\.\d/ and $var_innodb_version =~ m/1\.0\.\d+$/){
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
2727
	$ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup');
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2728
}
373 by Alexey Kopytov
Cherrypick of innodb version detection failure fix.
2729
if($var_version =~ m/5\.1\.\d/ and $var_innodb_version =~ m/1\.0\.\d+-(rel)?\d/){
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
2730
	$ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup');
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2731
}
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2732
if($var_version =~ m/5\.2\.\d/){
2733
    $ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup');
2734
}
2735
if($var_version =~ m/5\.3\.\d/){
2736
    $ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup');
2737
}
178 by root
support for 5.5 in scripts
2738
if($var_version =~ m/5\.5\.\d/){
2739
	$ibbackup_binary = ($win eq 1 ? 'xtrabackup.exe' : 'xtrabackup_55');
2740
}
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2741
mysql_close();
152 by Aleksandr Kuzminsky
1. innobackupex saves the xtrabackup binary name in a file xtrabackup_binary.
2742
return $ibbackup_binary;
136.1.2 by Aleksandr Kuzminsky
innobackupex checks MySQL version.
2743
}
211 by Alexey Kopytov
Manual merge from lp:~percona-dev/percona-xtrabackup/safe-slave-backup
2744
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
2745
# Wait until it's safe to backup a slave.  Returns immediately if
2746
# the host isn't a slave.  Currently there's only one check:
2747
# Slave_open_temp_tables has to be zero.  Dies on timeout.
2748
sub wait_for_safe_slave {
2749
   my @lines;
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
2750
   # whether host is detected as slave in safe slave backup mode
2751
   my $host_is_slave = 0;
2752
2753
   $sql_thread_started = 0;
2754
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
2755
   mysql_send 'SHOW SLAVE STATUS\G;';
2756
   file_to_array($mysql_stdout, \@lines);
2757
   foreach my $line ( @lines ) {
2758
      if ( $line =~ m/Read_Master_Log_Pos/ ) {
2759
         $host_is_slave = 1;
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
2760
     } elsif ( $line =~ m/Slave_SQL_Running:.*Yes/ ) {
2761
         $sql_thread_started = 1;
2762
     }
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
2763
   }
2764
   if ( !$host_is_slave ) {
2765
      print STDERR "$prefix: Not checking slave open temp tables for --safe-slave-backup because host is not a slave\n";
2766
      return;
2767
   }
2768
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
2769
   if ($sql_thread_started) {
2770
       mysql_send 'STOP SLAVE SQL_THREAD;';
2771
   }
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
2772
2773
   my $open_temp_tables = get_slave_open_temp_tables();
2774
   print STDERR "$prefix: Slave open temp tables: $open_temp_tables\n";
2775
2776
   return if $open_temp_tables == 0;
2777
2778
   my $sleep_time = 3;
2779
   my $n_attempts = int($option_safe_slave_backup_timeout / $sleep_time) || 1;
2780
   while ( $n_attempts-- ) {
2781
      print STDERR "$prefix: Starting slave SQL thread, waiting $sleep_time seconds, then checking Slave_open_temp_tables again ($n_attempts attempts remaining)...\n";
2782
      
2783
      mysql_send 'START SLAVE SQL_THREAD;';
2784
      sleep $sleep_time;
2785
      mysql_send 'STOP SLAVE SQL_THREAD;';
2786
2787
      $open_temp_tables = get_slave_open_temp_tables();
2788
      print STDERR "$prefix: Slave open temp tables: $open_temp_tables\n";
2789
      if ( !$open_temp_tables ) {
2790
         print STDERR "$prefix: Slave is safe to backup\n";
2791
         return;
2792
      }
2793
   } 
2794
391.52.2 by Alexey Kopytov
Bug #1037379: SQL_THREAD left in stopped state of
2795
   # Restart the slave if it was running at start
2796
   if ($sql_thread_started) {
2797
       print STDERR "Restarting slave SQL thread.\n";
2798
       mysql_send 'START SLAVE SQL_THREAD;';
2799
   }
2800
127.1.6 by Daniel Nichter
Add and implement --safe-slave-backup and --safe-slave-backup-timeout.
2801
   Die "Slave_open_temp_tables did not become zero after waiting $option_safe_slave_backup_timeout seconds";
2802
}
2803
2804
sub get_slave_open_temp_tables {
2805
   my @lines;
2806
   mysql_send 'SHOW STATUS LIKE "slave_open_temp_tables"\G;';
2807
   file_to_array($mysql_stdout, \@lines);
2808
   my $last_value;
2809
   for my $i ( 0..$#lines ) {
2810
      $last_value = $i + 1
2811
         if $lines[$i] =~ m/Variable_name: Slave_open_temp_tables/i;
2812
   }
2813
   Die "SHOW STATUS LIKE 'slave_open_temp_tables' did not return anything"
2814
      unless $last_value;
2815
2816
   Die "Failed to get Slave_open_temp_tables from SHOW STATUS"
2817
      unless defined $lines[$last_value];
2818
2819
   my ($n) = $lines[$last_value] =~ m/(\d+)/;
2820
   return $n;
2821
}
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2822
2823
=pod
2824
2825
=head1 NAME
2826
2827
innobackupex - Non-blocking backup tool for InnoDB, XtraDB and HailDB databases
2828
2829
=head1 SYNOPOSIS
2830
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2831
innobackupex [--compress] [--compress-threads=NUMBER-OF-THREADS]
2832
             [--include=REGEXP] [--user=NAME]
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2833
             [--password=WORD] [--port=PORT] [--socket=SOCKET]
242.17.19 by Alexey Kopytov
Bug #809073: ibbackup-binary option in innobackupex does not work
2834
             [--no-timestamp] [--ibbackup=IBBACKUP-BINARY]
391.45.1 by Hrvoje Matijakovic
* bugfix for Bug #1032090
2835
             [--slave-info] [--galera-info] [--stream=tar|xbstream]
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2836
             [--scpopt=OPTIONS-FOR-SCP]  [--sshopt=OPTIONS-FOR-SSH]
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
2837
             [--defaults-file=MY.CNF] [--defaults-group=GROUP-NAME]
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2838
             [--databases=LIST] [--remote-host=HOSTNAME] [--no-lock] 
2839
             [--tmpdir=DIRECTORY] [--tables-file=FILE]
2840
             [--incremental] [--incremental-basedir]
2841
             [--incremental-dir] [--incremental-lsn]
2842
             BACKUP-ROOT-DIR
2843
254.3.1 by Rodrigo Gadea
Fix Bug #764936: Documentation's unit in ''--use-memory'' option is not correct.
2844
innobackupex --apply-log [--use-memory=B] [--uncompress]
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2845
             [--defaults-file=MY.CNF]
2846
             [--export] [--redo-only] [--ibbackup=IBBACKUP-BINARY]
2847
             BACKUP-DIR
2848
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
2849
innobackupex --copy-back [--defaults-file=MY.CNF] [--defaults-group=GROUP-NAME] BACKUP-DIR
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2850
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
2851
innobackupex --move-back [--defaults-file=MY.CNF] [--defaults-group=GROUP-NAME] BACKUP-DIR
2852
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2853
=head1 DESCRIPTION
2854
2855
The first command line above makes a hot backup of a MySQL database.
2856
By default it creates a backup directory (named by the current date
2857
and time) in the given backup root directory.  With the --no-timestamp
2858
option it does not create a time-stamped backup directory, but it puts
2859
the backup in the given directory (which must not exist).  This
2860
command makes a complete backup of all MyISAM and InnoDB tables and
2861
indexes in all databases or in all of the databases specified with the
2862
--databases option.  The created backup contains .frm, .MRG, .MYD,
391.65.1 by Alexey Kopytov
Bug #733665: innobackupex not supporting Maria 5.2
2863
.MYI, .MAD, .MAI, .TRG, .TRN, .ARM, .ARZ, .CSM, CSV, .opt, .par, and
2864
InnoDB data and log files.  The MY.CNF options file defines the
2865
location of the database.  This command connects to the MySQL server
2866
using the mysql client program, and runs xtrabackup as a child
2867
process.
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2868
2869
The --apply-log command prepares a backup for starting a MySQL
2870
server on the backup. This command recovers InnoDB data files as specified
2871
in BACKUP-DIR/backup-my.cnf using BACKUP-DIR/xtrabackup_logfile,
2872
and creates new InnoDB log files as specified in BACKUP-DIR/backup-my.cnf.
2873
The BACKUP-DIR should be the path to a backup directory created by
2874
xtrabackup. This command runs xtrabackup as a child process, but it does not 
2875
connect to the database server.
2876
2877
The --copy-back command copies data, index, and log files
2878
from the backup directory back to their original locations.
2879
The MY.CNF options file defines the original location of the database.
2880
The BACKUP-DIR is the path to a backup directory created by xtrabackup.
2881
391.46.2 by Alexey Kopytov
Bug #803636: "moves files" option needed with --copy-back
2882
The --move-back command is similar to --copy-back with the only difference that
2883
it moves files to their original locations rather than copies them. As this
2884
option removes backup files, it must be used with caution. It may be useful in
2885
cases when there is not enough free disk space to copy files.
2886
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2887
On success the exit code innobackupex is 0. A non-zero exit code 
2888
indicates an error.
2889
2890
2891
=head1 OPTIONS
2892
2893
=over
2894
2895
=item --apply-log
2896
2897
Prepare a backup in BACKUP-DIR by applying the transaction log file named "xtrabackup_logfile" located in the same directory. Also, create new transaction logs. The InnoDB configuration is read from the file "backup-my.cnf".
2898
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
2899
=item --compress
2900
2901
This option instructs xtrabackup to compress backup copies of InnoDB
2902
data files. It is passed directly to the xtrabackup child process. Try
2903
'xtrabackup --help' for more details.
2904
2905
=item --compress-threads
2906
2907
This option specifies the number of worker threads that will be used
2908
for parallel compression. It is passed directly to the xtrabackup
2909
child process. Try 'xtrabackup --help' for more details.
2910
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2911
=item --copy-back
2912
2913
Copy all the files in a previously made backup from the backup directory to their original locations.
2914
2915
=item --databases=LIST
2916
391.46.4 by Hrvoje Matijakovic
* updated the supported releases in apt_repo
2917
This option specifies the list of databases that innobackupex should back up. The option accepts a string argument or path to file that contains the list of databases to back up. The list is of the form "databasename1[.table_name1] databasename2[.table_name2] . . .". If this option is not specified, all databases containing MyISAM and InnoDB tables will be backed up.  Please make sure that --databases contains all of the InnoDB databases and tables, so that all of the innodb.frm files are also backed up. In case the list is very long, this can be specified in a file, and the full path of the file can be specified instead of the list. (See option --tables-file.)
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2918
2919
=item --defaults-file=[MY.CNF]
2920
2921
This option specifies what file to read the default MySQL options from.  The option accepts a string argument. It is also passed directly to xtrabackup's --defaults-file option. See the xtrabackup documentation for details.
2922
391.77.1 by George O. Lorch III
Incorporated patch for Bug #740489 : Add --defaults-extra-file param to innobackupex
2923
=item --defaults-extra-file=[MY.CNF]
2924
2925
This option specifies what extra file to read the default MySQL options from before the standard defaults-file.  The option accepts a string argument. It is also passed directly to xtrabackup's --defaults-extra-file option. See the xtrabackup documentation for details.
2926
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2927
=item --export
2928
2929
This option is passed directly to xtrabackup's --export option. It enables exporting individual tables for import into another server. See the xtrabackup documentation for details.
2930
2931
=item --extra-lsndir=DIRECTORY
2932
2933
This option specifies the directory in which to save an extra copy of the "xtrabackup_checkpoints" file.  The option accepts a string argument. It is passed directly to xtrabackup's --extra-lsndir option. See the xtrabackup documentation for details.
2934
391.45.1 by Hrvoje Matijakovic
* bugfix for Bug #1032090
2935
=item --galera-info
2936
2937
This options creates the xtrabackup_galera_info file which contians the local node state at the time of the backup. Option should be used when performing the backup of Percona-XtraDB-Cluster.
2938
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2939
=item --help
2940
2941
This option displays a help screen and exits.
2942
2943
=item --host=HOST
2944
2945
This option specifies the host to use when connecting to the database server with TCP/IP.  The option accepts a string argument. It is passed to the mysql child process without alteration. See mysql --help for details.
2946
242.17.19 by Alexey Kopytov
Bug #809073: ibbackup-binary option in innobackupex does not work
2947
=item --ibbackup=IBBACKUP-BINARY
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2948
2949
This option specifies which xtrabackup binary should be used.  The option accepts a string argument. IBBACKUP-BINARY should be the command used to run XtraBackup. The option can be useful if the xtrabackup binary is not in your search path or working directory. If this option is not specified, innobackupex attempts to determine the binary to use automatically. By default, "xtrabackup" is the command used. However, when option --copy-back is specified, "xtrabackup_51" is the command used. And when option --apply-log is specified, the binary is used whose name is in the file "xtrabackup_binary" in the backup directory, if that file exists.
2950
2951
=item --include=REGEXP
2952
2953
This option is a regular expression to be matched against table names in databasename.tablename format. It is passed directly to xtrabackup's --tables option. See the xtrabackup documentation for details.
2954
2955
=item --incremental
2956
2957
This option tells xtrabackup to create an incremental backup, rather than a full one. It is passed to the xtrabackup child process. When this option is specified, either --incremental-lsn or --incremental-basedir can also be given. If neither option is given, option --incremental-basedir is passed to xtrabackup by default, set to the first timestamped backup directory in the backup base directory.
2958
2959
=item --incremental-basedir=DIRECTORY
2960
2961
This option specifies the directory containing the full backup that is the base dataset for the incremental backup.  The option accepts a string argument. It is used with the --incremental option.
2962
2963
=item --incremental-dir=DIRECTORY
2964
2965
This option specifies the directory where the incremental backup will be combined with the full backup to make a new full backup.  The option accepts a string argument. It is used with the --incremental option.
2966
2967
=item --incremental-lsn
2968
2969
This option specifies the log sequence number (LSN) to use for the incremental backup.  The option accepts a string argument. It is used with the --incremental option. It is used instead of specifying --incremental-basedir. For databases created by MySQL and Percona Server 5.0-series versions, specify the LSN as two 32-bit integers in high:low format. For databases created in 5.1 and later, specify the LSN as a single 64-bit integer.
2970
391.51.1 by Hrvoje Matijakovic
* release notes for 2.0.3
2971
=item --move-back
2972
2973
Move all the files in a previously made backup from the backup directory to the actual datadir location. Use with caution, as it removes backup files.
2974
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2975
=item --no-lock
2976
391.64.13 by Hrvoje Matijakovic
- Bug fixes for: Bug #1100008, Bug #1095925, Bug #1092235, Bug #1088967,
2977
Use this option to disable table lock with "FLUSH TABLES WITH READ LOCK". Use it only if ALL your tables are InnoDB and you DO NOT CARE about the binary log position of the backup. This option shouldn't be used if there are any DDL statements being executed or if any updates are happening on non-InnoDB tables (this includes the system MyISAM tables in the mysql database), otherwise it could lead to an inconsistent backup. If you are considering to use --no-lock because your backups are failing to acquire the lock, this could be because of incoming replication events preventing the lock from succeeding. Please try using --safe-slave-backup to momentarily stop the replication slave thread, this may help the backup to succeed and you then don't need to resort to using this option.
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2978
2979
=item --no-timestamp
2980
2981
This option prevents creation of a time-stamped subdirectory of the BACKUP-ROOT-DIR given on the command line. When it is specified, the backup is done in BACKUP-ROOT-DIR instead.
2982
2983
=item --parallel=NUMBER-OF-THREADS
2984
2985
This option specifies the number of threads the xtrabackup child process should use to back up files concurrently.  The option accepts an integer argument. It is passed directly to xtrabackup's --parallel option. See the xtrabackup documentation for details.
2986
2987
2988
=item --password=WORD
2989
2990
This option specifies the password to use when connecting to the database. It accepts a string argument.  It is passed to the mysql child process without alteration. See mysql --help for details.
2991
2992
=item --port=PORT
2993
2994
This option specifies the port to use when connecting to the database server with TCP/IP.  The option accepts a string argument. It is passed to the mysql child process. It is passed to the mysql child process without alteration. See mysql --help for details.
2995
2996
=item --redo-only
2997
391.64.13 by Hrvoje Matijakovic
- Bug fixes for: Bug #1100008, Bug #1095925, Bug #1092235, Bug #1088967,
2998
This option should be used when preparing the base full backup and when merging all incrementals except the last one. This option is passed directly to xtrabackup's --apply-log-only option. This forces xtrabackup to skip the "rollback" phase and do a "redo" only. This is necessary if the backup will have incremental changes applied to it later. See the xtrabackup documentation for details. 
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
2999
3000
=item --remote-host=HOSTNAME 
3001
242.47.3 by Stewart Smith
merge deprecation warning for --remote-host, updating version it to be removed in to 2.1
3002
This option is DEPRECATED and will be removed in Percona XtraBackup 2.1. In Percona XtraBackup 2.0 and later, you should use streaming backups instead. This option specifies the remote host on which the backup files will be created, by using an ssh connection.  The option accepts a string argument.
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3003
391.62.1 by Hrvoje Matijakovic
Bug fixes for bug: Bug #1073502, Bug #1075269 and Bug #1066978
3004
=item --rsync
3005
3006
Uses the rsync utility to optimize local file transfers. When this option is specified, innobackupex uses rsync to copy all non-InnoDB files instead of spawning a separate cp for each file, which can be much faster for servers with a large number of databases or tables.  This option cannot be used together with --remote-host or --stream.
3007
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3008
=item --safe-slave-backup
3009
3010
Stop slave SQL thread and wait to start backup until Slave_open_temp_tables in "SHOW STATUS" is zero. If there are no open temporary tables, the backup will take place, otherwise the SQL thread will be started and stopped until there are no open temporary tables. The backup will fail if Slave_open_temp_tables does not become zero after --safe-slave-backup-timeout seconds. The slave SQL thread will be restarted when the backup finishes.
3011
3012
=item --safe-slave-backup-timeout
3013
3014
How many seconds --safe-slave-backup should wait for Slave_open_temp_tables to become zero. (default 300)
3015
3016
=item --scpopt=SCP-OPTIONS
3017
3018
This option specifies the command line options to pass to scp when the option --remost-host is specified.  The option accepts a string argument. If the option is not specified, the default options are "-Cp -c arcfour".
3019
371 by Alexey Kopytov
Cherrypick of fix for bug 733658: pass options to both ssh and scp.
3020
=item --sshopt=SSH-OPTIONS
3021
3022
This option specifies the command line options to pass to ssh when the option --remost-host is specified.  The option accepts a string argument.
3023
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3024
=item --slave-info
3025
3026
This option is useful when backing up a replication slave server. It prints the binary log position and name of the master server. It also writes this information to the "xtrabackup_slave_info" file as a "CHANGE MASTER" command. A new slave for this master can be set up by starting a slave server on this backup and issuing a "CHANGE MASTER" command with the binary log position saved in the "xtrabackup_slave_info" file.
3027
3028
=item --socket=SOCKET
3029
3030
This option specifies the socket to use when connecting to the local database server with a UNIX domain socket.  The option accepts a string argument. It is passed to the mysql child process without alteration. See mysql --help for details.
3031
391.62.1 by Hrvoje Matijakovic
Bug fixes for bug: Bug #1073502, Bug #1075269 and Bug #1066978
3032
=item --stream=STREAMNAME
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3033
385.1.1 by Alexey Kopytov
Rebase of the parallel compression patch on new trunk + post-review
3034
This option specifies the format in which to do the streamed backup.  The option accepts a string argument. The backup will be done to STDOUT in the specified format. Currently, the only supported formats are tar and xbstream. This option is passed directly to xtrabackup's --stream option.
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3035
3036
=item --tables-file=FILE
3037
3038
This option specifies the file in which there are a list of names of the form database.  The option accepts a string argument.table, one per line. The option is passed directly to xtrabackup's --tables-file option.
3039
3040
=item --throttle=IOS
3041
3042
This option specifies a number of I/O operations (pairs of read+write) per second.  It accepts an integer argument.  It is passed directly to xtrabackup's --throttle option.
3043
3044
=item --tmpdir=DIRECTORY
3045
3046
This option specifies the location where a temporary file will be stored.  The option accepts a string argument. It should be used when --remote-host or --stream is specified. For these options, the transaction log will first be stored to a temporary file, before streaming or copying to a remote host. This option specifies the location where that temporary file will be stored. If the option is not specifed, the default is to use the value of tmpdir read from the server configuration.
3047
254.3.1 by Rodrigo Gadea
Fix Bug #764936: Documentation's unit in ''--use-memory'' option is not correct.
3048
=item --use-memory=B
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3049
254.3.1 by Rodrigo Gadea
Fix Bug #764936: Documentation's unit in ''--use-memory'' option is not correct.
3050
This option accepts a string argument that specifies the amount of memory in bytes for xtrabackup to use for crash recovery while preparing a backup. Multiples are supported providing the unit (e.g. 1MB, 1GB). It is used only with the option --apply-log. It is passed directly to xtrabackup's --use-memory option. See the xtrabackup documentation for details.
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3051
3052
=item --user=NAME
3053
3054
This option specifies the MySQL username used when connecting to the server, if that's not the current user. The option accepts a string argument.  It is passed to the mysql child process without alteration. See mysql --help for details.
3055
391.26.2 by Sergei Glushchenko
Bug #483827. Support for mysqld_multi.
3056
=item --defaults-group=GROUP-NAME
3057
3058
This option specifies the group name in my.cnf which should be used. This is needed for mysqld_multi deployments.
3059
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3060
=item --version
3061
3062
This option displays the xtrabackup version and copyright notice and then exits.
3063
3064
=back
3065
3066
=head1 BUGS
3067
3068
Bugs can be reported on Launchpad: https://bugs.launchpad.net/percona-xtrabackup/+filebug
3069
3070
=head1 COPYRIGHT
3071
356 by Alexey Kopytov
Cherrypick of (C) string update due to 2012.
3072
InnoDB Backup Utility Copyright 2003, 2009 Innobase Oy and Percona, Inc 2009-2012. All Rights Reserved.
240 by Alexey Kopytov
Updates to built-in innobackupex usage docs.
3073
3074
This software is published under the GNU GENERAL PUBLIC LICENSE Version 2, June 1991.
3075
3076
=cut