📄 mysqlhotcopy
字号:
$dbh->do( qq{ replace into $table_name set host=?, log_file=?, log_pos=?, master_host=?, master_log_file=?, master_log_pos=? }, undef, $hostname, $file, $position, $master_host, $log_file, $log_pos ); }; if ( $@ ) { warn "Failed to store master position: $@\n"; }}sub get_row { my ( $dbh, $sql ) = @_; my $sth = $dbh->prepare($sql); $sth->execute; return $sth->fetchrow_array();}sub get_row_hash { my ( $dbh, $sql ) = @_; my $sth = $dbh->prepare($sql); $sth->execute; return $sth->fetchrow_hashref();}sub scan_raid_dir { my ( $r_db_files, $data_dir, @raid_dir ) = @_; local(*RAID_DIR); foreach my $rd ( @raid_dir ) { opendir(RAID_DIR, "$data_dir/$rd" ) or die "Cannot open dir '$data_dir/$rd': $!"; while ( defined( my $name = readdir RAID_DIR ) ) { $r_db_files->{"$rd/$name"} = $1 if ( $name =~ /(.+)\.\w+$/ ); } closedir( RAID_DIR ); }}sub get_raid_dirs { my ( $r_files ) = @_; my %dirs = (); foreach my $f ( @$r_files ) { if ( $f =~ m:^(\d\d)/: ) { $dirs{$1} = 1; } } return sort keys %dirs;}sub get_list_of_tables { my ( $db ) = @_; # "use database" cannot cope with database names containing spaces # so create a new connection my $dbh = DBI->connect("dbi:mysql:${db}${dsn};mysql_read_default_group=mysqlhotcopy", $opt{user}, $opt{password}, { RaiseError => 1, PrintError => 0, AutoCommit => 1, }); my @dbh_tables = eval { $dbh->tables() }; ## Remove quotes around table names my $quote = $dbh->get_info(29); # SQL_IDENTIFIER_QUOTE_CHAR if ($quote) { foreach (@dbh_tables) { s/^$quote(.*)$quote$/$1/; s/$quote$quote/$quote/g; } } $dbh->disconnect(); return @dbh_tables;}sub quote_names { my ( $name ) = @_; # given a db.table name, add quotes my ($db, $table, @cruft) = split( /\./, $name ); die "Invalid db.table name '$name'" if (@cruft || !defined $db || !defined $table ); # Earlier versions of DBD return table name non-quoted, # such as DBD-2.1012 and the newer ones, such as DBD-2.9002 # returns it quoted. Let's have a support for both. $table=~ s/\`//g; return "`$db`.`$table`";}__END__=head1 DESCRIPTIONmysqlhotcopy is designed to make stable copies of live MySQL databases.Here "live" means that the database server is running and the databasemay be in active use. And "stable" means that the copy will not haveany corruptions that could occur if the table files were simply copiedwithout first being locked and flushed from within the server.=head1 OPTIONS=over 4=item --checkpoint checkpoint-tableAs each database is copied, an entry is written to the specifiedcheckpoint-table. This has the happy side-effect of updating theMySQL update-log (if it is switched on) giving a good indication ofwhere roll-forward should begin for backup+rollforward schemes.The name of the checkpoint table should be supplied in database.table format.The checkpoint-table must contain at least the following fields:=over 4 time_stamp timestamp not null src varchar(32) dest varchar(60) msg varchar(255)=back=item --record_log_pos log-pos-tableJust before the database files are copied, update the record in thelog-pos-table from the values returned from "show master status" and"show slave status". The master status values are stored in thelog_file and log_pos columns, and establish the position in the binarylogs that any slaves of this host should adopt if initialised fromthis dump. The slave status values are stored in master_host,master_log_file, and master_log_pos, and these are useful if the hostperforming the dump is a slave and other sibling slaves are to beinitialised from this dump.The name of the log-pos table should be supplied in database.table format.A sample log-pos table definition:=over 4CREATE TABLE log_pos ( host varchar(60) NOT null, time_stamp timestamp(14) NOT NULL, log_file varchar(32) default NULL, log_pos int(11) default NULL, master_host varchar(60) NULL, master_log_file varchar(32) NULL, master_log_pos int NULL, PRIMARY KEY (host) );=back=item --suffix suffixEach database is copied back into the originating datadir undera new name. The new name is the original name with the suffixappended. If only a single db_name is supplied and the --suffix flag is notsupplied, then "--suffix=_copy" is assumed.=item --allowoldMove any existing version of the destination to a backup directory forthe duration of the copy. If the copy successfully completes, the backup directory is deleted - unless the --keepold flag is set. If the copy fails,the backup directory is restored.The backup directory name is the original name with "_old" appended.Any existing versions of the backup directory are deleted.=item --keepoldBehaves as for the --allowold, with the additional feature of keeping the backup directory after the copy successfully completes.=item --addtodestDon't rename target directory if it already exists, just add thecopied files into it.This is most useful when backing up a database with many largetables and you don't want to have all the tables locked for thewhole duration.In this situation, I<if> you are happy for groups of tables to bebacked up separately (and thus possibly not be logically consistantwith one another) then you can run mysqlhotcopy several times onthe same database each with different db_name./table_regex/.All but the first should use the --addtodest option so the tablesall end up in the same directory.=item --flushlogRotate the log files by executing "FLUSH LOGS" after all tables arelocked, and before they are copied.=item --resetmasterReset the bin-log by executing "RESET MASTER" after all tables arelocked, and before they are copied. Useful if you are recovering aslave in a replication setup.=item --resetslaveReset the master.info by executing "RESET SLAVE" after all tables arelocked, and before they are copied. Useful if you are recovering aserver in a mutual replication setup.=item --regexp patternCopy all databases with names matching the pattern=item --regexp /pattern1/./pattern2/Copy all tables with names matching pattern2 from all databases withnames matching pattern1. For example, to select all tables whichnames begin with 'bar' from all databases which names end with 'foo': mysqlhotcopy --indices --method=cp --regexp /foo$/./^bar/=item db_name./pattern/Copy only tables matching pattern. Shell metacharacters ( (, ), |, !,etc.) have to be escaped (e.g. \). For example, to select all tablesin database db1 whose names begin with 'foo' or 'bar': mysqlhotcopy --indices --method=cp db1./^\(foo\|bar\)/=item db_name./~pattern/Copy only tables not matching pattern. For example, to copy tablesthat do not begin with foo nor bar: mysqlhotcopy --indices --method=cp db1./~^\(foo\|bar\)/=item -?, --helpDisplay helpscreen and exit=item -u, --user=# user for database login if not current user=item -p, --password=# password to use when connecting to the server. Note that you are stronglyencouraged *not* to use this option as every user would be able to see thepassword in the process list. Instead use the '[mysqlhotcopy]' section inone of the config files, normally /etc/my.cnf or your personal ~/.my.cnf.(See the chapter 'my.cnf Option Files' in the manual)=item -h, -h, --host=#Hostname for local server when connecting over TCP/IP. By specifying thisdifferent from 'localhost' will trigger mysqlhotcopy to use TCP/IP connection.=item -P, --port=# port to use when connecting to MySQL server with TCP/IP. This is only usedwhen using the --host option.=item -S, --socket=# UNIX domain socket to use when connecting to local server=item --noindices Don\'t include index files in copy. Only up to the first 2048 bytesare copied; You can restore the indexes with isamchk -r or myisamchk -ron the backup.=item --method=# method for copy (only "cp" currently supported). Alpha support for"scp" was added in November 2000. Your experience with the scp methodwill vary with your ability to understand how scp works. 'man scp'and 'man ssh' are your friends.The destination directory _must exist_ on the target machine using thescp method. --keepold and --allowold are meaningless with scp.Liberal use of the --debug option will help you figure out what\'sreally going on when you do an scp.Note that using scp will lock your tables for a _long_ time unlessyour network connection is _fast_. If this is unacceptable to you,use the 'cp' method to copy the tables to some temporary area and thenscp or rsync the files at your leisure.=item -q, --quiet be silent except for errors=item --debugDebug messages are displayed =item -n, --dryrunDisplay commands without actually doing them=back=head1 WARRANTYThis software is free and comes without warranty of any kind. Youshould never trust backup software without studying the code yourself.Study the code inside this script and only rely on it if I<you> believethat it does the right thing for you.Patches adding bug fixes, documentation and new features are welcome.Please send these to internals@lists.mysql.com.=head1 TO DOExtend the individual table copy to allow multiple subsets of tablesto be specified on the command line: mysqlhotcopy db newdb t1 t2 /^foo_/ : t3 /^bar_/ : +where ":" delimits the subsets, the /^foo_/ indicates all tableswith names begining with "foo_" and the "+" indicates all tablesnot copied by the previous subsets.newdb is either another not existing database or a full path to a directorywhere we can create a directory 'db'Add option to lock each table in turn for people who don\'t needcross-table integrity.Add option to FLUSH STATUS just before UNLOCK TABLES.Add support for other copy methods (eg tar to single file?).Add support for forthcoming MySQL ``RAID'' table subdirectory layouts.=head1 AUTHORTim BunceMartin Waite - added checkpoint, flushlog, regexp and dryrun options Fixed cleanup of targets when hotcopy fails. Added --record_log_pos. RAID tables are now copied (don't know if this works over scp).Ralph Corderoy - added synonyms for commandsScott Wiersdorf - added table regex and scp supportMonty - working --noindex (copy only first 2048 bytes of index file) Fixes for --method=scpAsk Bjoern Hansen - Cleanup code to fix a few bugs and enable -w again.Emil S. Hansen - Added resetslave and resetmaster.Jeremy D. Zawodny - Removed depricated DBI calls. Fixed bug whichresulted in nothing being copied when a regexp was specified but nodatabase name(s).Martin Waite - Fix to handle database name that contains space.Paul DuBois - Remove end '/' from directory names
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -