xtrabackup quickstart
使用xtrabackup在线备份数据库。
使用上面的备份文件增加一个Slave数据库。
现有环境:
现有一个数据库环境,信息如下:
Master:代号A
IP地址:192.168.115.149
端口:3340
Slave:代号B
IP地址:192.168.115.150
端口:3340
配置文件:/apps/mysql_3340/my.cnf
socket路径:/tmp/mysql_3340.sock
通过本地socket访问用户名:root
通过本地socket访问密码:无
新搭建数据库:代号C
以下实践都在B数据库所在的机器上操作。
xtrabackup相关程序下载地址:
http://www.percona.com/percona-builds/XtraBackup/XtraBackup-1.0/binary/xtrabackup-1.0.tar.gz
下载后解压,将三个可执行文件放到/usr/local/sbin/即可。
当前目录:/apps/backup/
使用innobackupex脚本对数据库进行备份,相关参数可以通过–help参数了解。
以下程序输出经过筛选,略去不必要信息:
[root@slave150 backup]# innobackupex-1.5.1 –defaults-file=/apps/mysql_3340/my.cnf –slave-info –socket=/tmp/mysql_3340.sock 3340
InnoDB Backup Utility v1.5.1-xtrabackup; Copyright 2003, 2009 Innobase Oy.
All Rights Reserved.
…版权信息…
innobackupex-1.5.1: Using mysql Ver 14.14 Distrib 5.1.43, for unknown-linux-gnu (x86_64) using readline 5.1
innobackupex-1.5.1: Using mysql server version Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
innobackupex-1.5.1: Created backup directory /apps/backup/3340/2010-03-19_16-51-33
100319 16:51:33 innobackupex-1.5.1: Starting mysql with options: –unbuffered –socket=/tmp/mysql_3340.sock –
100319 16:51:33 innobackupex-1.5.1: Connected to database with mysql child process (pid=25246)
100319 16:51:37 innobackupex-1.5.1: Connection to database server closed
100319 16:51:37 innobackupex-1.5.1: Starting ibbackup with command: xtrabackup –defaults-file=”/apps/mysql_3340/my.cnf” –backup –suspend-at-end –target-dir=/apps/backup/3340/2010-03-19_16-51-33
innobackupex-1.5.1: Waiting for ibbackup (pid=25252) to suspend
innobackupex-1.5.1: Suspend file ‘/apps/backup/3340/2010-03-19_16-51-33/xtrabackup_suspended’
xtrabackup Ver 1.0 Rev 113 for 5.0.84 unknown-linux-gnu (x86_64)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /apps/mysql_3340
xtrabackup: Target instance is assumed as followings.
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 5242880
xtrabackup: use O_DIRECT
>> log scanned up to (23 3600029795)
Copying ./ibdata1
to /apps/backup/3340/2010-03-19_16-51-33/ibdata1
>> log scanned up to (23 3600060713)
>> log scanned up to (23 3600087337)
>> log scanned up to (23 3600121153)
…done
Copying ./xjqy2/configs.ibd
to /apps/backup/3340/2010-03-19_16-51-33/xjqy2/configs.ibd
Copying ./game/answer_count.ibd
to /apps/backup/3340/2010-03-19_16-51-33/game/answer_count.ibd
…done
100319 16:56:03 innobackupex-1.5.1: Continuing after ibbackup has suspended
100319 16:56:03 innobackupex-1.5.1: Starting mysql with options: –unbuffered –socket=/tmp/mysql_3340.sock –
100319 16:56:03 innobackupex-1.5.1: Connected to database with mysql child process (pid=25269)
>> log scanned up to (23 3602333556)
100319 16:56:07 innobackupex-1.5.1: Starting to lock all tables…
>> log scanned up to (23 3602373264)
>> log scanned up to (23 3602458529)
>> log scanned up to (23 3602458529)
100319 16:56:19 innobackupex-1.5.1: All tables locked and flushed to disk
100319 16:56:19 innobackupex-1.5.1: Starting to backup .frm, .MRG, .MYD, .MYI,
innobackupex-1.5.1: .TRG, .TRN, .ARM, .ARZ and .opt files in
innobackupex-1.5.1: subdirectories of ‘/apps/mysql_3340′
innobackupex-1.5.1: Backing up files ‘/apps/mysql_3340/mysql/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,opt,par}’ (65 files)
innobackupex-1.5.1: Backing up files ‘/apps/mysql_3340/xjqy2/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,opt,par}’ (29 files)
innobackupex-1.5.1: Backing up files ‘/apps/mysql_3340/game/*.{frm,MYD,MYI,MRG,TRG,TRN,ARM,ARZ,opt,par}’ (23 files)
100319 16:56:19 innobackupex-1.5.1: Finished backing up .frm, .MRG, .MYD, .MYI, .TRG, .TRN, .ARM, .ARZ and .opt files
innobackupex-1.5.1: Resuming ibbackup
xtrabackup: The latest check point (for incremental): ’23:3602295070′
>> log scanned up to (23 3602458752)
xtrabackup: Stopping log copying thread.
xtrabackup: Transaction log of lsn (23 3599800027) to (23 3602458752) was copied.
100319 16:56:22 innobackupex-1.5.1: All tables unlocked
100319 16:56:22 innobackupex-1.5.1: Connection to database server closed
innobackupex-1.5.1: Backup created in directory ‘/apps/backup/3340/2010-03-19_16-51-33′
innobackupex-1.5.1: MySQL binlog position: filename ‘mysql-bin.000005′, position 989572152
innobackupex-1.5.1: MySQL slave binlog position: master host ’192.168.115.149′, filename ‘mysql-bin.000006′, position 690623257
100319 16:56:22 innobackupex-1.5.1: completed OK!
此时在备份的目录下会有一个xtrabackup_slave_info文件,是设置Slave的命令:
[root@slave150 2010-03-19_16-51-33]# cat xtrabackup_slave_info
CHANGE MASTER TO MASTER_LOG_FILE=’mysql-bin.000006′, MASTER_LOG_POS=690623257
恢复备份文件:
[root@slave150 2010-03-19_16-51-33]# innobackupex-1.5.1 –apply-log –defaults-file=/apps/backup/3340/2010-03-19_16-51-33/backup-my.cnf /apps/backup/3340/2010-03-19_16-51-33/
100319 16:57:48 innobackupex-1.5.1: Starting ibbackup with command: xtrabackup –defaults-file=”/apps/backup/3340/2010-03-19_16-51-33/backup-my.cnf” –prepare –target-dir=/apps/backup/3340/2010-03-19_16-51-33
xtrabackup Ver 1.0 Rev 113 for 5.0.84 unknown-linux-gnu (x86_64)
xtrabackup: cd to /apps/backup/3340/2010-03-19_16-51-33
xtrabackup: This target seems to be not prepared yet.
xtrabackup: xtrabackup_logfile detected: size=2998272, start_lsn=(23 3599800027)
xtrabackup: Temporary instance for recovery is set as followings.
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 1
xtrabackup: innodb_log_file_size = 2998272
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by –use-memory parameter)
InnoDB: Log scan progressed past the checkpoint lsn 23 3599800027
100319 16:57:48 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files…
InnoDB: Doing recovery: scanned up to log sequence number 23 3602458752 (99 %)
100319 16:57:49 InnoDB: Starting an apply batch of log records to the database…
InnoDB: Progress in percents: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
InnoDB: Apply batch completed
下面两行是说,B数据库作为Slave时,所同步的A库日志的位置。
如果新的C库作为A的Slave,那么应该配置这个日志位置。
这个位置也是和上面的xtrabackup_slave_info是一致的。
InnoDB: In a MySQL replication slave the last master binlog file
InnoDB: position 0 690623257, file name mysql-bin.000006
下面两行是说,B数据库自己的binlog位置。如果新的C库作为B的Slave,那么应该配置这个日志位置。
InnoDB: Last MySQL binlog file position 0 989572152, file name ./mysql-bin.000005
100319 16:57:52 InnoDB: Started; log sequence number 23 3602458752
[notice (again)]
If you use binary log and don’t use any hack of group commit,
the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 989572152, file name ./mysql-bin.000005
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
100319 16:57:52 InnoDB: Starting shutdown…
100319 16:57:54 InnoDB: Shutdown completed; log sequence number 23 3602458752
100319 16:57:54 innobackupex-1.5.1: Restarting xtrabackup with command: xtrabackup –defaults-file=”/apps/backup/3340/2010-03-19_16-51-33/backup-my.cnf” –prepare –target-dir=/apps/backup/3340/2010-03-19_16-51-33
for creating ib_logfile*
xtrabackup Ver 1.0 Rev 113 for 5.0.84 unknown-linux-gnu (x86_64)
xtrabackup: cd to /apps/backup/3340/2010-03-19_16-51-33
xtrabackup: This target seems to be already prepared.
xtrabackup: notice: xtrabackup_logfile was already used to ‘–prepare’.
xtrabackup: Temporary instance for recovery is set as followings.
xtrabackup: innodb_data_home_dir = ./
xtrabackup: innodb_data_file_path = ibdata1:10M:autoextend
xtrabackup: innodb_log_group_home_dir = ./
xtrabackup: innodb_log_files_in_group = 2
xtrabackup: innodb_log_file_size = 5242880
xtrabackup: Starting InnoDB instance for recovery.
xtrabackup: Using 104857600 bytes for buffer pool (set by –use-memory parameter)
100319 16:57:54 InnoDB: Log file ./ib_logfile0 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile0 size to 5 MB
InnoDB: Database physically writes the file full: wait…
100319 16:57:54 InnoDB: Log file ./ib_logfile1 did not exist: new to be created
InnoDB: Setting log file ./ib_logfile1 size to 5 MB
InnoDB: Database physically writes the file full: wait…
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
100319 16:57:54 InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files…
InnoDB: In a MySQL replication slave the last master binlog file
InnoDB: position 0 690623257, file name mysql-bin.000006
InnoDB: Last MySQL binlog file position 0 989572152, file name ./mysql-bin.000005
100319 16:57:54 InnoDB: Started; log sequence number 23 3602459148
[notice (again)]
If you use binary log and don’t use any hack of group commit,
the binary log position seems to be:
InnoDB: Last MySQL binlog file position 0 989572152, file name ./mysql-bin.000005
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
100319 16:57:54 InnoDB: Starting shutdown…
100319 16:57:55 InnoDB: Shutdown completed; log sequence number 23 3602459148
100319 16:57:55 innobackupex-1.5.1: completed OK!
至此,数据文件已经可用。
将准备好的数据移动到合适的位置,做好新的配置文件,即可启动数据库。
新的配置文件与原有Slave配置文件基本一致,需要注意修改几个参数:
server-id必须修改,否则会与原有主库产生冲突。
如果在同一台机器上进行测试,端口和socket文件必须修改。
启动后,通过mysql客户端连接新的数据库,执行xtrabackup_slave_info中的命令,执行时应该加上master_host等参数,然后执行start slave即可。