home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 March / PCWorld_2001-03_cd.bin / KOMUNIK / progweb / progweb.exe / mysql / DATA1.CAB / Examples / scripts / mysqlhotcopy.pl < prev    next >
Encoding:
Perl Script  |  2001-01-22  |  20.9 KB  |  783 lines

  1. #!/my/gnu/bin/perl -w
  2.  
  3. use strict;
  4. use Getopt::Long;
  5. use Data::Dumper;
  6. use File::Basename;
  7. use File::Path;
  8. use DBI;
  9.  
  10. =head1 NAME
  11.  
  12. mysqlhotcopy - fast on-line hot-backup utility for local MySQL databases
  13.  
  14. =head1 SYNOPSIS
  15.  
  16.   mysqlhotcopy db_name
  17.  
  18.   mysqlhotcopy --suffix=_copy db_name_1 ... db_name_n
  19.  
  20.   mysqlhotcopy db_name_1 ... db_name_n /path/to/new_directory
  21.  
  22.   mysqlhotcopy db_name./regex/
  23.  
  24.   mysqlhotcopy db_name./^\(foo\|bar\)/
  25.  
  26.   mysqlhotcopy db_name./~regex/
  27.  
  28.   mysqlhotcopy db_name_1./regex_1/ db_name_1./regex_2/ ... db_name_n./regex_n/ /path/to/new_directory
  29.  
  30.   mysqlhotcopy --method='scp -Bq -i /usr/home/foo/.ssh/identity' --user=root --password=secretpassword \
  31.          db_1./^nice_table/ user@some.system.dom:~/path/to/new_directory
  32.  
  33. WARNING: THIS IS VERY MUCH A FIRST-CUT ALPHA. Comments/patches welcome.
  34.  
  35. =cut
  36.  
  37. # Documentation continued at end of file
  38.  
  39. my $VERSION = "1.10";
  40.  
  41. my $opt_tmpdir = $ENV{TMPDIR} || "/tmp";
  42.  
  43. my $OPTIONS = <<"_OPTIONS";
  44.  
  45. $0 Ver $VERSION
  46.  
  47. Usage: $0 db_name [new_db_name | directory]
  48.  
  49.   -?, --help           display this helpscreen and exit
  50.   -u, --user=#         user for database login if not current user
  51.   -p, --password=#     password to use when connecting to server
  52.   -P, --port=#         port to use when connecting to local server
  53.   -S, --socket=#       socket to use when connecting to local server
  54.  
  55.   --allowold           don't abort if target already exists (rename it _old)
  56.   --keepold            don't delete previous (now renamed) target when done
  57.   --noindices          don't include full index files in copy
  58.   --method=#           method for copy (only "cp" currently supported)
  59.  
  60.   -q, --quiet          be silent except for errors
  61.   --debug              enable debug
  62.   -n, --dryrun         report actions without doing them
  63.  
  64.   --regexp=#           copy all databases with names matching regexp
  65.   --suffix=#           suffix for names of copied databases
  66.   --checkpoint=#       insert checkpoint entry into specified db.table
  67.   --flushlog           flush logs once all tables are locked 
  68.   --tmpdir=#           temporary directory (instead of $opt_tmpdir)
  69.  
  70.   Try 'perldoc $0 for more complete documentation'
  71. _OPTIONS
  72.  
  73. sub usage {
  74.     die @_, $OPTIONS;
  75. }
  76.  
  77. my %opt = (
  78.     user    => scalar getpwuid($>),
  79.     noindices    => 0,
  80.     allowold    => 0,    # for safety
  81.     keepold    => 0,
  82.     method    => "cp",
  83.     flushlog    => 0,
  84. );
  85. Getopt::Long::Configure(qw(no_ignore_case)); # disambuguate -p and -P
  86. GetOptions( \%opt,
  87.     "help",
  88.     "user|u=s",
  89.     "password|p=s",
  90.     "port|P=s",
  91.     "socket|S=s",
  92.     "allowold!",
  93.     "keepold!",
  94.     "noindices!",
  95.     "method=s",
  96.     "debug",
  97.     "quiet|q",
  98.     "mv!",
  99.     "regexp=s",
  100.     "suffix=s",
  101.     "checkpoint=s",
  102.     "flushlog",
  103.     "tmpdir|t=s",
  104.     "dryrun|n",
  105. ) or usage("Invalid option");
  106.  
  107. # @db_desc
  108. # ==========
  109. # a list of hash-refs containing:
  110. #
  111. #   'src'     - name of the db to copy
  112. #   't_regex' - regex describing tables in src
  113. #   'target'  - destination directory of the copy
  114. #   'tables'  - array-ref to list of tables in the db
  115. #   'files'   - array-ref to list of files to be copied
  116. #
  117.  
  118. my @db_desc = ();
  119. my $tgt_name = undef;
  120.  
  121. usage("") if ($opt{help});
  122.  
  123. if ( $opt{regexp} || $opt{suffix} || @ARGV > 2 ) {
  124.     $tgt_name   = pop @ARGV unless ( exists $opt{suffix} );
  125.     @db_desc = map { s{^([^\.]+)\./(.+)/$}{$1}; { 'src' => $_, 't_regex' => ( $2 ? $2 : '.*' ) } } @ARGV;
  126. }
  127. else {
  128.     usage("Database name to hotcopy not specified") unless ( @ARGV );
  129.  
  130.     $ARGV[0] =~ s{^([^\.]+)\./(.+)/$}{$1};
  131.     @db_desc = ( { 'src' => $ARGV[0], 't_regex' => ( $2 ? $2 : '.*' ) } );
  132.  
  133.     if ( @ARGV == 2 ) {
  134.     $tgt_name   = $ARGV[1];
  135.     }
  136.     else {
  137.     $opt{suffix} = "_copy";
  138.     }
  139. }
  140.  
  141. my %mysqld_vars;
  142. my $start_time = time;
  143. $opt_tmpdir= $opt{tmpdir} if $opt{tmpdir};
  144. $0 = $1 if $0 =~ m:/([^/]+)$:;
  145. $opt{quiet} = 0 if $opt{debug};
  146. $opt{allowold} = 1 if $opt{keepold};
  147.  
  148. # --- connect to the database ---
  149. my $dsn = ";host=localhost";
  150. $dsn .= ";port=$opt{port}" if $opt{port};
  151. $dsn .= ";mysql_socket=$opt{socket}" if $opt{socket};
  152.  
  153. my $dbh = DBI->connect("dbi:mysql:$dsn;mysql_read_default_group=mysqlhotcopy",
  154.                         $opt{user}, $opt{password},
  155. {
  156.     RaiseError => 1,
  157.     PrintError => 0,
  158.     AutoCommit => 1,
  159. });
  160.  
  161. # --- check that checkpoint table exists if specified ---
  162. if ( $opt{checkpoint} ) {
  163.     eval { $dbh->do( qq{ select time_stamp, src, dest, msg 
  164.              from $opt{checkpoint} where 1 != 1} );
  165.        };
  166.  
  167.     die "Error accessing Checkpoint table ($opt{checkpoint}): $@"
  168.       if ( $@ );
  169. }
  170.  
  171. # --- get variables from database ---
  172. my $sth_vars = $dbh->prepare("show variables like 'datadir'");
  173. $sth_vars->execute;
  174. while ( my ($var,$value) = $sth_vars->fetchrow_array ) {
  175.     $mysqld_vars{ $var } = $value;
  176. }
  177. my $datadir = $mysqld_vars{'datadir'}
  178.     || die "datadir not in mysqld variables";
  179. $datadir =~ s:/$::;
  180.  
  181.  
  182. # --- get target path ---
  183. my ($tgt_dirname, $to_other_database);
  184. $to_other_database=0;
  185. if ($tgt_name =~ m:^\w+$: && @db_desc <= 1)
  186. {
  187.     $tgt_dirname = "$datadir/$tgt_name";
  188.     $to_other_database=1;
  189. }
  190. elsif ($tgt_name =~ m:/: || $tgt_name eq '.') {
  191.     $tgt_dirname = $tgt_name;
  192. }
  193. elsif ( $opt{suffix} ) {
  194.     print "copy suffix $opt{suffix}\n" unless $opt{quiet};
  195. }
  196. else {
  197.     die "Target '$tgt_name' doesn't look like a database name or directory path.\n";
  198. }
  199.  
  200. # --- resolve database names from regexp ---
  201. if ( defined $opt{regexp} ) {
  202.     my $sth_dbs = $dbh->prepare("show databases");
  203.     $sth_dbs->execute;
  204.     while ( my ($db_name) = $sth_dbs->fetchrow_array ) {
  205.     push @db_desc, { 'src' => $db_name } if ( $db_name =~ m/$opt{regexp}/o );
  206.     }
  207. }
  208.  
  209. # --- get list of tables to hotcopy ---
  210.  
  211. my $hc_locks = "";
  212. my $hc_tables = "";
  213. my $num_tables = 0;
  214. my $num_files = 0;
  215.  
  216. foreach my $rdb ( @db_desc ) {
  217.     my $db = $rdb->{src};
  218.     eval { $dbh->do( "use $db" ); };
  219.     die "Database '$db' not accessible: $@"  if ( $@ );
  220.     my @dbh_tables = $dbh->func( '_ListTables' );
  221.  
  222.     ## generate regex for tables/files
  223.     my $t_regex = $rdb->{t_regex};        ## assign temporary regex
  224.     my $negated = $t_regex =~ tr/~//d;    ## remove and count negation operator: we don't allow ~ in table names
  225.     $t_regex = qr/$t_regex/;              ## make regex string from user regex
  226.  
  227.     ## filter (out) tables specified in t_regex
  228.     print "Filtering tables with '$t_regex'\n" if $opt{debug};
  229.     @dbh_tables = ( $negated 
  230.             ? grep { $_ !~ $t_regex } @dbh_tables 
  231.             : grep { $_ =~ $t_regex } @dbh_tables );
  232.  
  233.     ## get list of files to copy
  234.     my $db_dir = "$datadir/$db";
  235.     opendir(DBDIR, $db_dir ) 
  236.       or die "Cannot open dir '$db_dir': $!";
  237.  
  238.     my %db_files;
  239.     map { ( /(.+)\.\w+$/ ? ( $db_files{$_} = $1 ) : () ) } readdir(DBDIR);
  240.     unless( keys %db_files ) {
  241.     warn "'$db' is an empty database\n";
  242.     }
  243.     closedir( DBDIR );
  244.  
  245.     ## filter (out) files specified in t_regex
  246.     my @db_files = ( $negated 
  247.               ? grep { $db_files{$_} !~ $t_regex } keys %db_files
  248.               : grep { $db_files{$_} =~ $t_regex } keys %db_files );
  249.     @db_files = sort @db_files;
  250.     my @index_files=();
  251.  
  252.     ## remove indices unless we're told to keep them
  253.     if ($opt{noindices}) {
  254.         @index_files= grep { /\.(ISM|MYI)$/ } @db_files;
  255.     @db_files = grep { not /\.(ISM|MYI)$/ } @db_files;
  256.     }
  257.  
  258.     $rdb->{files}  = [ @db_files ];
  259.     $rdb->{index}  = [ @index_files ];
  260.     my @hc_tables = map { "$db.$_" } @dbh_tables;
  261.     $rdb->{tables} = [ @hc_tables ];
  262.  
  263.     $hc_locks .= ", "  if ( length $hc_locks && @hc_tables );
  264.     $hc_locks .= join ", ", map { "$_ READ" } @hc_tables;
  265.     $hc_tables .= ", "  if ( length $hc_tables && @hc_tables );
  266.     $hc_tables .= join ", ", @hc_tables;
  267.  
  268.     $num_tables += scalar @hc_tables;
  269.     $num_files  += scalar @{$rdb->{files}};
  270. }
  271.  
  272. # --- resolve targets for copies ---
  273.  
  274. my @targets = ();
  275.  
  276. if (length $tgt_name ) {
  277.     # explicit destination directory specified
  278.  
  279.     # GNU `cp -r` error message
  280.     die "copying multiple databases, but last argument ($tgt_dirname) is not a directory\n"
  281.       if ( @db_desc > 1 && !(-e $tgt_dirname && -d $tgt_dirname ) );
  282.  
  283.     if ($to_other_database)
  284.     {
  285.       foreach my $rdb ( @db_desc ) {
  286.     $rdb->{target} = "$tgt_dirname";
  287.       }
  288.     }
  289.     elsif ($opt{method} =~ /^scp\b/) 
  290.     {   # we have to trust scp to hit the target
  291.     foreach my $rdb ( @db_desc ) {
  292.         $rdb->{target} = "$tgt_dirname/$rdb->{src}";
  293.     }
  294.     }
  295.     else
  296.     {
  297.       die "Last argument ($tgt_dirname) is not a directory\n"
  298.     if (!(-e $tgt_dirname && -d $tgt_dirname ) );
  299.       foreach my $rdb ( @db_desc ) {
  300.     $rdb->{target} = "$tgt_dirname/$rdb->{src}";
  301.       }
  302.     }
  303.   }
  304. else {
  305.   die "Error: expected \$opt{suffix} to exist" unless ( exists $opt{suffix} );
  306.  
  307.   foreach my $rdb ( @db_desc ) {
  308.     $rdb->{target} = "$datadir/$rdb->{src}$opt{suffix}";
  309.   }
  310. }
  311.  
  312. print Dumper( \@db_desc ) if ( $opt{debug} );
  313.  
  314. # --- bail out if all specified databases are empty ---
  315.  
  316. die "No tables to hot-copy" unless ( length $hc_locks );
  317.  
  318. # --- create target directories if we are using 'cp' ---
  319.  
  320. my @existing = ();
  321.  
  322. if ($opt{method} =~ /^cp\b/)
  323. {
  324.   foreach my $rdb ( @db_desc ) {
  325.     push @existing, $rdb->{target} if ( -d  $rdb->{target} );
  326.   }
  327.  
  328.   die "Can't hotcopy to '", join( "','", @existing ), "' because already exist and --allowold option not given.\n"
  329.     if ( @existing && !$opt{allowold} );
  330. }
  331.  
  332. retire_directory( @existing ) if ( @existing );
  333.  
  334. foreach my $rdb ( @db_desc ) {
  335.     my $tgt_dirpath = $rdb->{target};
  336.     if ( $opt{dryrun} ) {
  337.     print "mkdir $tgt_dirpath, 0750\n";
  338.     }
  339.     elsif ($opt{method} =~ /^scp\b/) {
  340.     ## assume it's there?
  341.     ## ...
  342.     }
  343.     else {
  344.     mkdir($tgt_dirpath, 0750)
  345.       or die "Can't create '$tgt_dirpath': $!\n";
  346.     }
  347. }
  348.  
  349. ##############################
  350. # --- PERFORM THE HOT-COPY ---
  351. #
  352. # Note that we try to keep the time between the LOCK and the UNLOCK
  353. # as short as possible, and only start when we know that we should
  354. # be able to complete without error.
  355.  
  356. # read lock all the tables we'll be copying
  357. # in order to get a consistent snapshot of the database
  358.  
  359. if ( $opt{checkpoint} ) {
  360.     # convert existing READ lock on checkpoint table into WRITE lock
  361.     unless ( $hc_locks =~ s/$opt{checkpoint}\s+READ/$opt{checkpoint} WRITE/ ) {
  362.     $hc_locks .= ", $opt{checkpoint} WRITE";
  363.     }
  364. }
  365.  
  366. my $hc_started = time;    # count from time lock is granted
  367.  
  368. if ( $opt{dryrun} ) {
  369.     print "LOCK TABLES $hc_locks\n";
  370.     print "FLUSH TABLES /*!32323 $hc_tables */\n";
  371.     print "FLUSH LOGS\n" if ( $opt{flushlog} );
  372. }
  373. else {
  374.     my $start = time;
  375.     $dbh->do("LOCK TABLES $hc_locks");
  376.     printf "Locked $num_tables tables in %d seconds.\n", time-$start unless $opt{quiet};
  377.     $hc_started = time;    # count from time lock is granted
  378.  
  379.     # flush tables to make on-disk copy uptodate
  380.     $start = time;
  381.     $dbh->do("FLUSH TABLES /*!32323 $hc_tables */");
  382.     printf "Flushed tables ($hc_tables) in %d seconds.\n", time-$start unless $opt{quiet};
  383.     $dbh->do( "FLUSH LOGS" ) if ( $opt{flushlog} );
  384. }
  385.  
  386. my @failed = ();
  387.  
  388. foreach my $rdb ( @db_desc )
  389. {
  390.   my @files = map { "$datadir/$rdb->{src}/$_" } @{$rdb->{files}};
  391.   next unless @files;
  392.   
  393.   eval { copy_files($opt{method}, \@files, $rdb->{target} ); };
  394.   push @failed, "$rdb->{src} -> $rdb->{target} failed: $@"
  395.     if ( $@ );
  396.   
  397.   @files = @{$rdb->{index}};
  398.   if ($rdb->{index})
  399.   {
  400.     copy_index($opt{method}, \@files,
  401.            "$datadir/$rdb->{src}", $rdb->{target} );
  402.   }
  403.   
  404.   if ( $opt{checkpoint} ) {
  405.     my $msg = ( $@ ) ? "Failed: $@" : "Succeeded";
  406.     
  407.     eval {
  408.       $dbh->do( qq{ insert into $opt{checkpoint} (src, dest, msg) 
  409.               VALUES ( '$rdb->{src}', '$rdb->{target}', '$msg' )
  410.             } ); 
  411.     };
  412.     
  413.     if ( $@ ) {
  414.       warn "Failed to update checkpoint table: $@\n";
  415.     }
  416.   }
  417. }
  418.  
  419. if ( $opt{dryrun} ) {
  420.     print "UNLOCK TABLES\n";
  421.     if ( @existing && !$opt{keepold} ) {
  422.     my @oldies = map { $_ . '_old' } @existing;
  423.     print "rm -rf @oldies\n" 
  424.     }
  425.     $dbh->disconnect();
  426.     exit(0);
  427. }
  428. else {
  429.     $dbh->do("UNLOCK TABLES");
  430. }
  431.  
  432. my $hc_dur = time - $hc_started;
  433. printf "Unlocked tables.\n" unless $opt{quiet};
  434.  
  435. #
  436. # --- HOT-COPY COMPLETE ---
  437. ###########################
  438.  
  439. $dbh->disconnect;
  440.  
  441. if ( @failed ) {
  442.     # hotcopy failed - cleanup
  443.     # delete any @targets 
  444.     # rename _old copy back to original
  445.  
  446.     print "Deleting @targets \n" if $opt{debug};
  447.     rmtree([@targets]);
  448.     if (@existing) {
  449.     print "Restoring @existing from back-up\n" if $opt{debug};
  450.         foreach my $dir ( @existing ) {
  451.         rename("${dir}_old", $dir )
  452.           or warn "Can't rename ${dir}_old to $dir: $!\n";
  453.     }
  454.     }
  455.  
  456.     die join( "\n", @failed );
  457. }
  458. else {
  459.     # hotcopy worked
  460.     # delete _old unless $opt{keepold}
  461.  
  462.     if ( @existing && !$opt{keepold} ) {
  463.     my @oldies = map { $_ . '_old' } @existing;
  464.     print "Deleting previous copy in @oldies\n" if $opt{debug};
  465.     rmtree([@oldies]);
  466.     }
  467.  
  468.     printf "$0 copied %d tables (%d files) in %d second%s (%d seconds overall).\n",
  469.         $num_tables, $num_files,
  470.         $hc_dur, ($hc_dur==1)?"":"s", time - $start_time
  471.     unless $opt{quiet};
  472. }
  473.  
  474. exit 0;
  475.  
  476.  
  477. # ---
  478.  
  479. sub copy_files {
  480.     my ($method, $files, $target) = @_;
  481.     my @cmd;
  482.     print "Copying ".@$files." files...\n" unless $opt{quiet};
  483.  
  484.     if ($method =~ /^s?cp\b/) { # cp or scp with optional flags
  485.     @cmd = ($method);
  486.     # add option to preserve mod time etc of copied files
  487.     # not critical, but nice to have
  488.     push @cmd, "-p" if $^O =~ m/^(solaris|linux|freebsd)$/;
  489.  
  490.     # add recursive option for scp
  491.     push @cmd, "-r" if $^O =~ /m^(solaris|linux|freebsd)$/ && $method =~ /^scp\b/;
  492.  
  493.     # add files to copy and the destination directory
  494.     push @cmd, @$files, $target;
  495.     }
  496.     else
  497.     {
  498.     die "Can't use unsupported method '$method'\n";
  499.     }
  500.     safe_system (@cmd);
  501. }
  502.  
  503. #
  504. # Copy only the header of the index file
  505. #
  506.  
  507. sub copy_index
  508. {
  509.   my ($method, $files, $source, $target) = @_;
  510.   my $tmpfile="$opt_tmpdir/mysqlhotcopy$$";
  511.   
  512.   print "Copying indices for ".@$files." files...\n" unless $opt{quiet};  
  513.   foreach my $file (@$files)
  514.   {
  515.     my $from="$source/$file";
  516.     my $to="$target/$file";
  517.     my $buff;
  518.     open(INPUT, "<$from") || die "Can't open file $from: $!\n";
  519.     my $length=read INPUT, $buff, 2048;
  520.     die "Can't read index header from $from\n" if ($length < 1024);
  521.     close INPUT;
  522.     
  523.     if ( $opt{dryrun} )
  524.     {
  525.       print "$opt{method}-header $from $to\n";
  526.     }
  527.     elsif ($opt{method} eq 'cp')
  528.     {
  529.       open(OUTPUT,">$to")   || die "Can\'t create file $to: $!\n";
  530.       if (syswrite(OUTPUT,$buff) != length($buff))
  531.       {
  532.     die "Error when writing data to $to: $!\n";
  533.       }
  534.       close OUTPUT       || die "Error on close of $to: $!\n";
  535.     }
  536.     elsif ($opt{method} eq 'scp')
  537.     {
  538.       my $tmp=$tmpfile;
  539.       open(OUTPUT,">$tmp") || die "Can\'t create file $tmp: $!\n";
  540.       if (syswrite(OUTPUT,$buff) != length($buff))
  541.       {
  542.     die "Error when writing data to $tmp: $!\n";
  543.       }
  544.       close OUTPUT         || die "Error on close of $tmp: $!\n";
  545.       safe_system("scp $tmp $to");
  546.     }
  547.     else
  548.     {
  549.       die "Can't use unsupported method '$opt{method}'\n";
  550.     }
  551.   }
  552.   unlink "$tmpfile" if  ($opt{method} eq 'scp');
  553. }
  554.  
  555.  
  556. sub safe_system
  557. {
  558.   my @cmd= @_;
  559.  
  560.   if ( $opt{dryrun} )
  561.   {
  562.     print "@cmd\n";
  563.     return;
  564.   }
  565.  
  566.   ## for some reason system fails but backticks works ok for scp...
  567.   print "Executing '@cmd'\n" if $opt{debug};
  568.   my $cp_status = system "@cmd > /dev/null";
  569.   if ($cp_status != 0) {
  570.     warn "Burp ('scuse me). Trying backtick execution...\n" if $opt{debug}; #'
  571.     ## try something else
  572.     `@cmd` && die "Error: @cmd failed ($cp_status) while copying files.\n";
  573.   }
  574. }
  575.  
  576.  
  577. sub retire_directory {
  578.     my ( @dir ) = @_;
  579.  
  580.     foreach my $dir ( @dir ) {
  581.     my $tgt_oldpath = $dir . '_old';
  582.     if ( $opt{dryrun} ) {
  583.         print "rmtree $tgt_oldpath\n" if ( -d $tgt_oldpath );
  584.         print "rename $dir, $tgt_oldpath\n";
  585.         next;
  586.     }
  587.  
  588.     if ( -d $tgt_oldpath ) {
  589.         print "Deleting previous 'old' hotcopy directory ('$tgt_oldpath')\n" unless $opt{quiet};
  590.         rmtree([$tgt_oldpath])
  591.     }
  592.     rename($dir, $tgt_oldpath)
  593.       or die "Can't rename $dir=>$tgt_oldpath: $!\n";
  594.     print "Existing hotcopy directory renamed to '$tgt_oldpath'\n" unless $opt{quiet};
  595.     }
  596. }
  597.  
  598. __END__
  599.  
  600. =head1 DESCRIPTION
  601.  
  602. mysqlhotcopy is designed to make stable copies of live MySQL databases.
  603.  
  604. Here "live" means that the database server is running and the database
  605. may be in active use. And "stable" means that the copy will not have
  606. any corruptions that could occur if the table files were simply copied
  607. without first being locked and flushed from within the server.
  608.  
  609. =head1 OPTIONS
  610.  
  611. =over 4
  612.  
  613. =item --checkpoint checkpoint-table
  614.  
  615. As each database is copied, an entry is written to the specified
  616. checkpoint-table.  This has the happy side-effect of updating the
  617. MySQL update-log (if it is switched on) giving a good indication of
  618. where roll-forward should begin for backup+rollforward schemes.
  619.  
  620. The name of the checkpoint table should be supplied in database.table format.
  621. The checkpoint-table must contain at least the following fields:
  622.  
  623. =over 4
  624.  
  625.   time_stamp timestamp not null
  626.   src varchar(32)
  627.   dest varchar(60)
  628.   msg varchar(255)
  629.  
  630. =back
  631.  
  632. =item --suffix suffix
  633.  
  634. Each database is copied back into the originating datadir under
  635. a new name. The new name is the original name with the suffix
  636. appended. 
  637.  
  638. If only a single db_name is supplied and the --suffix flag is not
  639. supplied, then "--suffix=_copy" is assumed.
  640.  
  641. =item --allowold
  642.  
  643. Move any existing version of the destination to a backup directory for
  644. the duration of the copy. If the copy successfully completes, the backup 
  645. directory is deleted - unless the --keepold flag is set.  If the copy fails,
  646. the backup directory is restored.
  647.  
  648. The backup directory name is the original name with "_old" appended.
  649. Any existing versions of the backup directory are deleted.
  650.  
  651. =item --keepold
  652.  
  653. Behaves as for the --allowold, with the additional feature 
  654. of keeping the backup directory after the copy successfully completes.
  655.  
  656. =item --flushlog
  657.  
  658. Rotate the log files by executing "FLUSH LOGS" after all tables are
  659. locked, and before they are copied.
  660.  
  661. =item --regexp pattern
  662.  
  663. Copy all databases with names matching the pattern
  664.  
  665. =item db_name./pattern/
  666.  
  667. Copy only tables matching pattern. Shell metacharacters ( (, ), |, !,
  668. etc.) have to be escaped (e.g. \). For example, to select all tables
  669. in database db1 whose names begin with 'foo' or 'bar':
  670.  
  671.     mysqlhotcopy --indices --method=cp db1./^\(foo\|bar\)/
  672.  
  673. =item db_name./~pattern/
  674.  
  675. Copy only tables not matching pattern. For example, to copy tables
  676. that do not begin with foo nor bar:
  677.  
  678.     mysqlhotcopy --indices --method=cp db1./~^\(foo\|bar\)/
  679.  
  680. =item -?, --help
  681.  
  682. Display helpscreen and exit
  683.  
  684. =item -u, --user=#         
  685.  
  686. user for database login if not current user
  687.  
  688. =item -p, --password=#     
  689.  
  690. password to use when connecting to server
  691.  
  692. =item -P, --port=#         
  693.  
  694. port to use when connecting to local server
  695.  
  696. =item -S, --socket=#         
  697.  
  698. UNIX domain socket to use when connecting to local server
  699.  
  700. =item  --noindices          
  701.  
  702. Don\'t include index files in copy. Only up to the first 2048 bytes
  703. are copied;  You can restore the indexes with isamchk -r or myisamchk -r
  704. on the backup.
  705.  
  706. =item  --method=#           
  707.  
  708. method for copy (only "cp" currently supported). Alpha support for
  709. "scp" was added in November 2000. Your experience with the scp method
  710. will vary with your ability to understand how scp works. 'man scp'
  711. and 'man ssh' are your friends.
  712.  
  713. The destination directory _must exist_ on the target machine using the
  714. scp method. --keepold and --allowold are meeningless with scp.
  715. Liberal use of the --debug option will help you figure out what\'s
  716. really going on when you do an scp.
  717.  
  718. Note that using scp will lock your tables for a _long_ time unless
  719. your network connection is _fast_. If this is unacceptable to you,
  720. use the 'cp' method to copy the tables to some temporary area and then
  721. scp or rsync the files at your leisure.
  722.  
  723. =item -q, --quiet              
  724.  
  725. be silent except for errors
  726.  
  727. =item  --debug
  728.  
  729. Debug messages are displayed 
  730.  
  731. =item -n, --dryrun
  732.  
  733. Display commands without actually doing them
  734.  
  735. =back
  736.  
  737. =head1 WARRANTY
  738.  
  739. This software is free and comes without warranty of any kind. You
  740. should never trust backup software without studying the code yourself.
  741. Study the code inside this script and only rely on it if I<you> believe
  742. that it does the right thing for you.
  743.  
  744. Patches adding bug fixes, documentation and new features are welcome.
  745.  
  746. =head1 TO DO
  747.  
  748. Extend the individual table copy to allow multiple subsets of tables
  749. to be specified on the command line:
  750.  
  751.   mysqlhotcopy db newdb  t1 t2 /^foo_/ : t3 /^bar_/ : +
  752.  
  753. where ":" delimits the subsets, the /^foo_/ indicates all tables
  754. with names begining with "foo_" and the "+" indicates all tables
  755. not copied by the previous subsets.
  756.  
  757. newdb is either another not existing database or a full path to a directory
  758. where we can create a directory 'db'
  759.  
  760. Add option to lock each table in turn for people who don't need
  761. cross-table integrity.
  762.  
  763. Add option to FLUSH STATUS just before UNLOCK TABLES.
  764.  
  765. Add support for other copy methods (eg tar to single file?).
  766.  
  767. Add support for forthcoming MySQL ``RAID'' table subdirectory layouts.
  768.  
  769. =head1 AUTHOR
  770.  
  771. Tim Bunce
  772.  
  773. Martin Waite - added checkpoint, flushlog, regexp and dryrun options
  774.  
  775. Ralph Corderoy - added synonyms for commands
  776.  
  777. Scott Wiersdorf - added table regex and scp support
  778.  
  779. Monty - working --noindex (copy only first 2048 bytes of index file)
  780.         Fixes for --method=scp
  781.  
  782. Ask Bjoern Hansen - Cleanup code to fix a few bugs and enable -w again.
  783.