~akopytov/percona-xtrabackup/bug907147-1.6

« back to all changes in this revision

Viewing changes to innobackupex

  • Committer: Alexey Kopytov
  • Date: 2011-11-28 12:20:51 UTC
  • mfrom: (305.1.1 1.6)
  • Revision ID: akopytov@gmail.com-20111128122051-8g2t06tdxnxfol9e
Merge from the BP tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
93
93
my $option_incremental_lsn = '';
94
94
my $option_extra_lsndir = '';
95
95
my $option_remote_host = '';
 
96
my $option_rsync = '';
96
97
my $option_stream = '';
97
98
my $option_tmpdir = '';
98
99
 
203
204
my $win = ($^O eq 'MSWin32' ? 1 : 0);
204
205
my $CP_CMD = ($win eq 1 ? "copy /Y" : "cp -p");
205
206
my $xtrabackup_binary_file = 'xtrabackup_binary';
 
207
my %rsync_files_hash;
 
208
 
206
209
######################################################################
207
210
# program execution begins here
208
211
######################################################################
365
368
      wait_for_safe_slave();
366
369
    }
367
370
 
368
 
    if (!$option_incremental) {
 
371
    if (!$option_incremental && !$option_no_lock) {
 
372
        # make a prep copy before locking tables, if using rsync
 
373
        backup_files(1);
 
374
 
369
375
        # flush tables with read lock
370
 
        mysql_lockall() if !$option_no_lock;
371
 
 
 
376
        mysql_lockall();
372
377
    }
373
378
 
374
379
    if ($option_slave_info) {
377
382
 
378
383
 
379
384
    # backup .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files
380
 
    backup_files();
 
385
    # (or finalize the backup by syncing changes if using rsync)
 
386
    backup_files(0);
381
387
 
382
388
    # resume ibbackup and wait till it has finished
383
389
    my $ibbackup_exit_code = resume_ibbackup();
1603
1609
                        'extra-lsndir=s' => \$option_extra_lsndir,
1604
1610
                        'remote-host=s' => \$option_remote_host,
1605
1611
                        'stream=s' => \$option_stream,
 
1612
                        'rsync' => \$option_rsync,
1606
1613
                        'tmpdir=s' => \$option_tmpdir,
1607
1614
                        'no-lock' => \$option_no_lock,
1608
1615
                        'ibbackup=s' => \$option_ibbackup_binary,
1700
1707
        }
1701
1708
    }
1702
1709
 
 
1710
    if ($option_rsync && ($option_remote_host || $option_stream)) {
 
1711
        print STDERR "--rsync doesn't work with --remote-host or --stream\n";
 
1712
        exit(1);
 
1713
    }
 
1714
 
1703
1715
    print STDERR "\n";
1704
1716
 
1705
1717
    parse_databases_option_value();
1805
1817
# backup directory.
1806
1818
#
1807
1819
sub backup_files {
 
1820
    my $prep_mode = shift;
1808
1821
    my $source_dir = get_option(\%config, 'mysqld', 'datadir');
1809
1822
    my @list;
1810
1823
    my $file;
1811
1824
    my $database;
1812
1825
    my $wildcard = '*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,CSM,CSV,opt,par}';
 
1826
    my $rsync_file_list;
 
1827
    my $operation;
 
1828
    my $rsync_tmpfile_pass1 = "$option_tmpdir/xtrabackup_rsyncfiles_pass1";
 
1829
    my $rsync_tmpfile_pass2 = "$option_tmpdir/xtrabackup_rsyncfiles_pass2";
 
1830
 
 
1831
    # prep_mode will pre-copy the data, so that rsync is faster the 2nd time
 
1832
    # saving time while all tables are locked.
 
1833
    # currently only rsync mode is supported for prep.
 
1834
    if ($prep_mode and !$option_rsync) {
 
1835
        return;
 
1836
    }
 
1837
 
 
1838
    if ($option_rsync) {
 
1839
        if ($prep_mode) {
 
1840
            $rsync_file_list = $rsync_tmpfile_pass1;
 
1841
        } else {
 
1842
            $rsync_file_list = $rsync_tmpfile_pass2;
 
1843
        }
 
1844
        open(RSYNC, ">$rsync_file_list")
 
1845
            || Die "Can't open $rsync_file_list for writing: $!\n";
 
1846
    }
1813
1847
 
1814
1848
    opendir(DIR, $source_dir) 
1815
1849
        || Die "Can't open directory '$source_dir': $!\n";
1816
1850
    $now = current_time();
1817
 
    print STDERR "\n$now  $prefix Starting to backup .frm, .MRG, .MYD, .MYI,\n";
 
1851
    if ($prep_mode) {
 
1852
        $operation = "a prep copy of";
 
1853
    } else {
 
1854
        $operation = "to backup";
 
1855
    }
 
1856
    print STDERR "\n$now  $prefix Starting $operation .frm, .MRG, .MYD, .MYI,\n";
1818
1857
    print STDERR "$prefix .TRG, .TRN, .ARM, .ARZ, .CSM, .CSV and .opt files in\n";
1819
1858
    print STDERR "$prefix subdirectories of '$source_dir'\n";
1820
1859
    # loop through all database directories
1865
1904
            if ($print_each_file) {
1866
1905
                print STDERR "$prefix Backing up file '$source_dir/$database/$file'\n";
1867
1906
            }
1868
 
            if (!$option_remote_host && !$option_stream) {
 
1907
 
 
1908
            if ($option_rsync) {
 
1909
                print RSYNC "$database/$file\n";
 
1910
                if (!$prep_mode) {
 
1911
                    $rsync_files_hash{"$database/$file"} = 1;
 
1912
                }
 
1913
            } elsif (!$option_remote_host && !$option_stream) {
1869
1914
                $src_name = escape_path("$source_dir/$database/$file");
1870
1915
                $dst_name = escape_path("$backup_dir/$database");
1871
1916
                system("$CP_CMD \"$src_name\" \"$dst_name\"")
1901
1946
    }
1902
1947
    closedir(DIR);
1903
1948
 
 
1949
    if ($option_rsync) {
 
1950
        close(RSYNC);
 
1951
 
 
1952
        # do the actual rsync now
 
1953
        $now = current_time();
 
1954
        my $rsync_cmd = "rsync -t \"$source_dir\" --files-from=\"$rsync_file_list\" \"$backup_dir\"";
 
1955
        print STDERR "$now Starting rsync as: $rsync_cmd\n";
 
1956
 
 
1957
        # ignore errors in the prep mode, since we are running without lock,
 
1958
        # so some files may have disappeared.
 
1959
        if (system("$rsync_cmd") && !$prep_mode) {
 
1960
            Die "rsync failed: $!\n";
 
1961
        }
 
1962
 
 
1963
        $now = current_time();
 
1964
        print STDERR "$now rsync finished successfully.\n";
 
1965
 
 
1966
        # Remove from $backup_dir files that have been removed between first and
 
1967
        # second passes. Cannot use "rsync --delete" because it does not work
 
1968
        # with --files-from.
 
1969
        if (!$prep_mode) {
 
1970
            open(RSYNC, "<$rsync_tmpfile_pass1")
 
1971
                || Die "Can't open $rsync_tmpfile_pass1 for reading: $!\n";
 
1972
 
 
1973
            while (<RSYNC>) {
 
1974
                chomp;
 
1975
                if (!exists $rsync_files_hash{$_}) {
 
1976
                    print STDERR "Removing '$backup_dir/$_'\n";
 
1977
                    unlink "$backup_dir/$_";
 
1978
                }
 
1979
            }
 
1980
 
 
1981
            close(RSYNC);
 
1982
            unlink "$rsync_tmpfile_pass1" || \
 
1983
                Die "Failed to delete $rsync_tmpfile_pass1: $!";
 
1984
            unlink "$rsync_tmpfile_pass2" || \
 
1985
                Die "Failed to delete $rsync_tmpfile_pass2: $!";
 
1986
        }
 
1987
    }
 
1988
 
 
1989
    if ($prep_mode) {
 
1990
        $operation = "a prep copy of";
 
1991
    } else {
 
1992
        $operation = "backing up";
 
1993
    }
1904
1994
    $now = current_time();
1905
 
    print STDERR "$now  $prefix Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files\n\n";
1906
 
}
 
1995
    print STDERR "$now  $prefix Finished $operation .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ, .CSV, .CSM and .opt files\n\n";
 
1996
 }
1907
1997
 
1908
1998
 
1909
1999
#