mysqlhotcopy は MySQL サーバを停止せずにデータベースをホット・バックアップするツール。
mysqlhotcopy は、LOCK TABLES、FLUSH TABLES、および cp(または scp)を使用して、すばやくデータベースのバックアップを行う Perl スクリプトです。これは、データベースや単一のテーブルのバックアップを行う最速の方法ですが、データベースディレクトリのある同一マシンだけでしか実行できません。 mysqlhotcopy は、Unix のみ、および MyISAM テーブルと ISAM テーブルでのみ使用できます。
MySQL AB :: MySQL 4.1 リファレンスマニュアル :: 4.9.8 mysqlhotcopy(MySQL のデータベースとテーブルのコピー)
テスト環境
OS は Debian GNU/Linux Sarge.
MySQL 4.1 を Debian パッケージでインストール済み。
# COLUMNS=110 dpkg -l | grep mysql
ii libdbd-mysql-perl 2.9006-1 A Perl5 database interface to the MySQL database
ii libmysqlclient12 4.0.24-10sarge2 mysql database client library
ii libmysqlclient14 4.1.11a-4sarge7 mysql database client library
ii libmysqlclient14-dev 4.1.11a-4sarge7 mysql database development files
ii mysql-client-4.1 4.1.11a-4sarge7 mysql database client binaries
ii mysql-common-4.1 4.1.11a-4sarge7 mysql database common files (e.g. /etc/mysql/my.cnf)
ii mysql-server-4.1 4.1.11a-4sarge7 mysql database server binaries
実際にバックアップしてみる
バックアップ先のディレクトリを作成。
# mkdir ./dbbackup
データベース(hogedb)をまとめてバックアップ。
# mysqlhotcopy -u root -p hogepassword hogedb ./dbbackup
Locked 4 tables in 0 seconds.
Flushed tables (`hogedb`.`hogetable1`, `hogedb`.`hogetable2`, `hogedb`.`hogetable3`, `hogedb`.`hogetable4`) in 0 seconds.
Copying 13 files...
Copying indices for 0 files...
Unlocked tables.
mysqlhotcopy copied 4 tables (13 files) in 1 second (1 seconds overall).
テーブル(hogedbデータベースのhogetable1テーブル)をバックアップ。
# mysqlhotcopy -u root -p hogepassword hogedb./hogetable1/ ./dbbackup
Locked 1 tables in 0 seconds.
Flushed tables (`hogedb`.`hogetable1`) in 0 seconds.
Copying 3 files...
Copying indices for 0 files...
Unlocked tables.
mysqlhotcopy copied 1 tables (3 files) in 0 seconds (0 seconds overall).
テーブル(hogedbデータベースのhogetable1テーブルとhogetable2テーブル)をバックアップ。--addtodest オプションで同じディレクトリにデータがあったら上書き。
# mysqlhotcopy -u root -p hogepassword --addtodest hogedb./hogetable1/ hogedb./hogetable2/ ./dbbackup
Locked 2 tables in 0 seconds.
Flushed tables (`hogedb`.`hogetable1`, `hogedb`.`hogetable2`) in 0 seconds.
Copying 3 files...
Copying indices for 0 files...
Copying 3 files...
Copying indices for 0 files...
Unlocked tables.
mysqlhotcopy copied 2 tables (6 files) in 0 seconds (0 seconds overall).
バックアップデータのファイル(MYD,MYI,frm)。
# ls -lR ./dbbackup/
./dbbackup/:
total 4
drwxr-x--- 2 mysql mysql 4096 Jul 2 15:45 hogedb
./dbbackup/hogedb:
total 34340
-rw-rw---- 1 mysql mysql 34570016 Jan 11 18:22 hogetable1.MYD
-rw-rw---- 1 mysql mysql 534528 Jan 11 18:22 hogetable1.MYI
-rw-rw---- 1 mysql mysql 8852 Jan 11 18:22 hogetable1.frm
失敗例
OS の root で実行しないと /var/lib/mysql にあるデータベースファイルの読み取り権限の問題でエラーが発生する(たぶん)。
$ mysqlhotcopy -u root -p hogepassword hogedb ./dbbackup
(中略)
Cannot open dir '/var/lib/mysql/hogedb': 許可がありません at /usr/bin/mysqlhotcopy line 293.
(中略)
cp: `/var/lib/mysql/mysql/columns_priv.MYD' を 読み込み用でオープンできません: 許可がありません
バックアップ先のディレクトリがないと失敗する。
# mysqlhotcopy -u root -p hogepassword hogedb ./dbbackup
Last argument (./dbbackup) is not a directory
MySQL の root ユーザでないと失敗する? MySQL ユーザの権限の問題かも。
# mysqlhotcopy -u hogeuser -p hogepassword hogedb ./dbbackup
Locked 4 tables in 0 seconds.
DBD::mysql::db do failed: Access denied; you need the RELOAD privilege for this operation at /usr/bin/mysqlhotcopy line 475.
mysqlhotcopy のマニュアル(usage)
# mysqlhotcopy
Database name to hotcopy not specified
/usr/bin/mysqlhotcopy Ver 1.22
Usage: /usr/bin/mysqlhotcopy db_name[./table_regex/] [new_db_name | directory]
-?, --help display this helpscreen and exit
-u, --user=# user for database login if not current user
-p, --password=# password to use when connecting to server (if not set
in my.cnf, which is recommended)
WARNING: Providing a password on command line is
insecure as it is visible through /proc to anyone
for a short time.
-h, --host=# Hostname for local server when connecting over TCP/IP
-P, --port=# port to use when connecting to local server with TCP/IP
-S, --socket=# socket to use when connecting to local server
--allowold don't abort if target dir already exists (rename it _old)
--addtodest don't rename target dir if it exists, just add files to it
--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)
-q, --quiet be silent except for errors
--debug enable debug
-n, --dryrun report actions without doing them
--regexp=# copy all databases with names matching regexp
--suffix=# suffix for names of copied databases
--checkpoint=# insert checkpoint entry into specified db.table
--flushlog flush logs once all tables are locked
--resetmaster reset the binlog once all tables are locked
--resetslave reset the master.info once all tables are locked
--tmpdir=# temporary directory (instead of /tmp)
--record_log_pos=# record slave and master status in specified db.table
--chroot=# base directory of chroot jail in which mysqld operates
Try 'perldoc /usr/bin/mysqlhotcopy' for more complete documentation
mysqlhotcopy のマニュアル(Perl Documentation)
perldoc mysqlhotcopy | col -bfx > doc.txt
という感じで、mysqlhotcopy のドキュメントを出力してみた($ man mysqlhotcopy としてもドキュメントが見れた。perldocを入れるとmanでも見れるようになるのかな?)ので、ここに載せておく。
MYSQLHOTCOPY(1) User Contributed Perl Documentation MYSQLHOTCOPY(1)
NAME
mysqlhotcopy - fast on-line hot-backup utility for local MySQL
databases and tables
SYNOPSIS
mysqlhotcopy db_name
mysqlhotcopy --suffix=_copy db_name_1 ... db_name_n
mysqlhotcopy db_name_1 ... db_name_n /path/to/new_directory
mysqlhotcopy db_name./regex/
mysqlhotcopy db_name./^\(foo\|bar\)/
mysqlhotcopy db_name./~regex/
mysqlhotcopy db_name_1./regex_1/ db_name_1./regex_2/ ... db_name_n./regex_n/ /path/to/new_directory
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
(INSECURE)
WARNING: THIS PROGRAM IS STILL IN BETA. Comments/patches welcome.
DESCRIPTION
mysqlhotcopy is designed to make stable copies of live MySQL databases.
Here "live" means that the database server is running and the database
may be in active use. And "stable" means that the copy will not have
any corruptions that could occur if the table files were simply copied
without first being locked and flushed from within the server.
OPTIONS
--checkpoint checkpoint-table
As each database is copied, an entry is written to the specified
checkpoint-table. This has the happy side-effect of updating the
MySQL update-log (if it is switched on) giving a good indication of
where roll-forward should begin for backup+rollforward schemes.
The name of the checkpoint table should be supplied in database.ta-
ble format. The checkpoint-table must contain at least the follow-
ing fields:
time_stamp timestamp not null
src varchar(32)
dest varchar(60)
msg varchar(255)
--record_log_pos log-pos-table
Just before the database files are copied, update the record in the
log-pos-table from the values returned from "show master status"
and "show slave status". The master status values are stored in the
log_file and log_pos columns, and establish the position in the
binary logs that any slaves of this host should adopt if ini-
tialised from this dump. The slave status values are stored in
master_host, master_log_file, and master_log_pos, and these are
useful if the host performing the dump is a slave and other sibling
slaves are to be initialised from this dump.
The name of the log-pos table should be supplied in database.table
format. A sample log-pos table definition:
CREATE 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)
);
--suffix suffix
Each database is copied back into the originating datadir under a
new name. The new name is the original name with the suffix
appended.
If only a single db_name is supplied and the --suffix flag is not
supplied, then "--suffix=_copy" is assumed.
--allowold
Move any existing version of the destination to a backup directory
for the 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.
--keepold
Behaves as for the --allowold, with the additional feature of keep-
ing the backup directory after the copy successfully completes.
--addtodest
Don't rename target directory if it already exists, just add the
copied files into it.
This is most useful when backing up a database with many large
tables and you don't want to have all the tables locked for the
whole duration.
In this situation, if you are happy for groups of tables to be
backed up separately (and thus possibly not be logically consistant
with one another) then you can run mysqlhotcopy several times on
the same database each with different db_name./table_regex/. All
but the first should use the --addtodest option so the tables all
end up in the same directory.
--flushlog
Rotate the log files by executing "FLUSH LOGS" after all tables are
locked, and before they are copied.
--resetmaster
Reset the bin-log by executing "RESET MASTER" after all tables are
locked, and before they are copied. Useful if you are recovering a
slave in a replication setup.
--resetslave
Reset the master.info by executing "RESET SLAVE" after all tables
are locked, and before they are copied. Useful if you are recover-
ing a server in a mutual replication setup.
--regexp pattern
Copy all databases with names matching the pattern
--regexp /pattern1/./pattern2/
Copy all tables with names matching pattern2 from all databases
with names matching pattern1. For example, to select all tables
which names begin with 'bar' from all databases which names end
with 'foo':
mysqlhotcopy --indices --method=cp --regexp /foo$/./^bar/
db_name./pattern/
Copy only tables matching pattern. Shell metacharacters ( (, ), |,
!, etc.) have to be escaped (e.g. \). For example, to select all
tables in database db1 whose names begin with 'foo' or 'bar':
mysqlhotcopy --indices --method=cp db1./^\(foo\|bar\)/
db_name./~pattern/
Copy only tables not matching pattern. For example, to copy tables
that do not begin with foo nor bar:
mysqlhotcopy --indices --method=cp db1./~^\(foo\|bar\)/
-?, --help
Display helpscreen and exit
-u, --user=#
user for database login if not current user
-p, --password=#
password to use when connecting to the server. Note that you are
strongly encouraged *not* to use this option as every user would be
able to see the password in the process list. Instead use the
'[mysqlhotcopy]' section in one of the config files, normally
/etc/my.cnf or your personal ~/.my.cnf. (See the chapter 'my.cnf
Option Files' in the manual)
WARNING: Providing a password on command line is insecure as it is
visible through /proc to anyone for a short time.
-h, -h, --host=#
Hostname for local server when connecting over TCP/IP. By specify-
ing this different from 'localhost' will trigger mysqlhotcopy to
use TCP/IP connection.
-P, --port=#
port to use when connecting to MySQL server with TCP/IP. This is
only used when using the --host option.
-S, --socket=#
UNIX domain socket to use when connecting to local server
--noindices
Don\'t include index files in copy. Only up to the first 2048 bytes
are copied; You can restore the indexes with isamchk -r or myisam-
chk -r on the backup.
--method=#
method for copy (only "cp" currently supported). Alpha support for
"scp" was added in November 2000. Your experience with the scp
method will 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
the scp method. --keepold and --allowold are meaningless with scp.
Liberal use of the --debug option will help you figure out what\'s
really going on when you do an scp.
Note that using scp will lock your tables for a _long_ time unless
your network connection is _fast_. If this is unacceptable to you,
use the 'cp' method to copy the tables to some temporary area and
then scp or rsync the files at your leisure.
-q, --quiet
be silent except for errors
--debug
Debug messages are displayed
-n, --dryrun
Display commands without actually doing them
WARRANTY
This software is free and comes without warranty of any kind. You
should never trust backup software without studying the code yourself.
Study the code inside this script and only rely on it if you believe
that 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.
TO DO
Extend the individual table copy to allow multiple subsets of tables to
be specified on the command line:
mysqlhotcopy db newdb t1 t2 /^foo_/ : t3 /^bar_/ : +
where ":" delimits the subsets, the /^foo_/ indicates all tables with
names begining with "foo_" and the "+" indicates all tables not copied
by the previous subsets.
newdb is either another not existing database or a full path to a
directory where we can create a directory 'db'
Add option to lock each table in turn for people who don\'t need cross-
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.
AUTHOR
Tim Bunce
Martin 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 commands
Scott Wiersdorf - added table regex and scp support
Monty - working --noindex (copy only first 2048 bytes of index file)
Fixes for --method=scp
Ask 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 which
resulted in nothing being copied when a regexp was specified but no
database name(s).
Martin Waite - Fix to handle database name that contains space.
Paul DuBois - Remove end '/' from directory names
perl v5.8.4 2006-08-29 MYSQLHOTCOPY(1)
その他のバックアップ方法
Open Tech Press | MySQLデータのバックアップ方法 で以下のようなバックアップ方法が紹介されている。
-MySQLサーバを停止してMySQLのデータファイルおよびディレクトリ(/var/lib/mysql)をコピー
-mysqlhotcopy
-mysqlsnapshot
-レプリケーション(replication)
-mysqldump
-Zmanda Recovery Manager for MySQL
tags: zlashdot Database Debian MySQL
Posted by NI-Lab. (@nilab)