--- scripts/mysqlhotcopy Sat Mar 31 19:52:44 2001 +++ scripts/mysqlhotcopy-new Mon Apr 2 16:55:05 2001 @@ -1,4 +1,4 @@ -#!/usr/local/bin/perl -w +#!/usr/bin/perl -w use strict; use Getopt::Long; @@ -27,6 +27,8 @@ mysqlhotcopy db_name_1./regex_1/ db_name_1./regex_2/ ... db_name_n./regex_n/ /path/to/new_directory + mysqlhotcopy --method=rsync [arguments as above] + mysqlhotcopy --method='scp -Bq -i /usr/home/foo/.ssh/identity' --user=root --password=secretpassword \ db_1./^nice_table/ user@some.system.dom:~/path/to/new_directory @@ -55,7 +57,11 @@ --allowold don't abort if target already exists (rename it _old) --keepold don't delete previous (now renamed) target when done --noindices don't include full index files in copy - --method=# method for copy (only "cp" currently supported) + --method=cp copy of data files will use standard OS cp + --method=rsync copy of data files will use local rsync + to overwrite potentially existing files; + allowold and keepold become irrelevant. + added by mengwong@pobox.com 20010402 -q, --quiet be silent except for errors --debug enable debug @@ -147,6 +153,7 @@ $opt_tmpdir= $opt{tmpdir} if $opt{tmpdir}; $0 = $1 if $0 =~ m:/([^/]+)$:; $opt{quiet} = 0 if $opt{debug}; +$opt{keepold} = 1 if $opt{method} eq "rsync"; $opt{allowold} = 1 if $opt{keepold}; # --- connect to the database --- @@ -302,6 +309,9 @@ if (!(-e $tgt_dirname && -d $tgt_dirname ) ); foreach my $rdb ( @db_desc ) { $rdb->{target} = "$tgt_dirname/$rdb->{src}"; + + # man rsync for significance of trailing / --- mengwong 20010402 + for ($rdb->{target}) { $_ .= "/" if not /\/$/ and $opt{method} eq "rsync" } } } } @@ -323,30 +333,34 @@ my @existing = (); -if ($opt{method} =~ /^cp\b/) -{ - foreach my $rdb ( @db_desc ) { - push @existing, $rdb->{target} if ( -d $rdb->{target} ); - } - - die "Can't hotcopy to '", join( "','", @existing ), "' because already exist and --allowold option not given.\n" - if ( @existing && !$opt{allowold} ); -} +# method=rsync overwrites existing files, so no need to rmdir / mkdir. +unless ($opt{method} eq "rsync") { -retire_directory( @existing ) if ( @existing ); + if ($opt{method} =~ /^cp\b/) + { + foreach my $rdb ( @db_desc ) { + push @existing, $rdb->{target} if ( -d $rdb->{target} ); + } -foreach my $rdb ( @db_desc ) { - my $tgt_dirpath = $rdb->{target}; - if ( $opt{dryrun} ) { - print "mkdir $tgt_dirpath, 0750\n"; - } - elsif ($opt{method} =~ /^scp\b/) { - ## assume it's there? - ## ... + die "Can't hotcopy to '", join( "','", @existing ), "' because already exist and --allowold option not given.\n" + if ( @existing && !$opt{allowold} ); } - else { - mkdir($tgt_dirpath, 0750) - or die "Can't create '$tgt_dirpath': $!\n"; + + retire_directory( @existing ) if ( @existing ); + + foreach my $rdb ( @db_desc ) { + my $tgt_dirpath = $rdb->{target}; + if ( $opt{dryrun} ) { + print "mkdir $tgt_dirpath, 0750\n"; + } + elsif ($opt{method} =~ /^scp\b/) { + ## assume it's there? + ## ... + } + else { + mkdir($tgt_dirpath, 0750) + or die "Can't create '$tgt_dirpath': $!\n"; + } } } @@ -501,6 +515,14 @@ # add files to copy and the destination directory push @cmd, @$files, $target; } + elsif ($method eq "rsync") { # rsync --- but locally + @cmd = ($method); + # add option to preserve mod time etc of copied files + push @cmd, "-a"; + + # add files to copy and the destination directory + push @cmd, @$files, $target; + } else { die "Can't use unsupported method '$method'\n"; @@ -510,7 +532,7 @@ # # Copy only the header of the index file -# +# sub copy_index { @@ -528,9 +550,11 @@ die "Can't read index header from $from\n" if ($length < 1024); close INPUT; + my $filesize = -s $from; + if ( $opt{dryrun} ) { - print "$opt{method}-header $from $to\n"; + print "$opt{method}-header $from $to (will copy only $length bytes of $filesize)\n"; } elsif ($opt{method} eq 'cp') { @@ -803,4 +827,9 @@ Ask Bjoern Hansen - Cleanup code to fix a few bugs and enable -w again. Emil S. Hansen - Added resetslave and resetmaster. + +20010402 Meng Weng Wong + + added --method=rsync for when you want to update a previously + made hotcopy.