使用xtrabackup搭建复制环境

使用xtrabackup在slave服务器进行数据备份时,有用的两个选项:
--slave-info:
当备份复制环境slave服务器的数据时非常有用的选项,打印master服务器的二进制日志坐标和文件名,以CHANGE MASTER语句记录到xtrabackup_slave_info文件。
能够基于该备份开启master的新slave服务器。

--safe-slave-backup:使用该选项需要SUPER权限(grant SUPER on *.* to backup@'localhost';)
为了保证一致性复制状态,该选项停止slave SQL线程,等待开启备份直到Slave_open_temp_tables的状态变量(show status)为0,如果超过--safe-slave-backup-timeout时间后,默认300s,Slave_open_temp_tables的值仍然没有变为0备份将错误,当备份完成后,SQL线程将重新启动。当从slave服务器进行备份,推荐总是使用该选项。

在使用slave进行备份之前,确保slave是跟master数据一致的,可以使用pt-table-checksum工具对slave数据进行验证。

一、在master备份数据添加第一个slave
1、在master上进行备份
    
    
    
    
  1. innobackupex --defaults-file=/opt/mysql-5.5.38-linux2.6-x86_64/my.cnf --user=backup --password=backup@123 --host=localhost --port=3307 --parallel=4 --stream=tar ./ | ssh root@10.0.37.123 "cat - |bzip2 > /data/backup/physical/full/$(date +%Y%m%d-%H%M%S).tar.bz2"

2、在slave进行备份数据准备
    
    
    
    
  1. mkdir 20140809-211156
  2. tar xvif 20140809-211156.tar.bz2 -C 20140809-211156
  3. innobackupex --apply-log 20140809-211156

3、进行数据还原
    
    
    
    
  1. cd /data/mariadb10/
  2. rm -rf *  #生产环境建议先对原数据进行备份
  3. innobackupex --defaults-file=/opt/mariadb-10.0.12-linux-x86_64/my.cnf --copy-back /data/backup/physical/full/20140809-211156/

或直接拷贝
    
    
    
    
  1. cp -a /data/backup/physical/full/20140809-211156/* .

4、修改权限:启动mysqld服务
    
    
    
    
  1. chown mysql.mysql -R *

5、查看创建备份时,master的当前二进制日志文件和位置:
    
    
    
    
  1. # cat xtrabackup_binlog_pos_innodb 
  2. /log/mysql55/mysql-bin.000010   54604082

6、执行change master to语句:
    
    
    
    
  1. root@[(none)] 13:30:01>CHANGE MASTER TO
  2.     -> MASTER_HOST='10.0.37.122',
  3.     -> MASTER_USER='repl',
  4.     -> MASTER_PASSWORD='repl',
  5.     -> MASTER_PORT=3307,
  6.     -> MASTER_LOG_FILE='mysql-bin.000010',
  7.     -> MASTER_LOG_POS=54604082;
  8. Query OK, 0 rows affected (0.00 sec)
  9. root@[(none)] 13:31:11>start slave;
  10. Query OK, 0 rows affected (0.00 sec)

二、在现有slave备份数据添加更多的slave
1、在slave服务器上进行备份
使用bzip2进行压缩,然后直接传到新slave服务器的数据目录解压:
    
    
    
    
  1. time innobackupex --defaults-file=/opt/mariadb-10.0.12-linux-x86_64/my.cnf --user=backup --password=backup@123 --host=localhost --port=3307 --parallel=4 --slave-info --safe-slave-backup --stream=xbstream ./ |bzip2 -|ssh root@10.0.37.122 "bunzip2 -|xbstream -x -C /data/mysql56"

xtrabackup: Creating suspend file '/tmp/xtrabackup_log_copied' with pid '26394'
xtrabackup: Transaction log of lsn (2395524781) to (2395524781) was copied.
140809 14:42:53  innobackupex: All tables unlocked
innobackupex:: Starting slave SQL thread

innobackupex: Backup created in directory '/opt/mariadb-10.0.12-linux-x86_64'
innobackupex: MySQL slave binlog position: master host '10.0.37.122', filename 'mysql-bin.000010', position 54604158
140809 14:42:53  innobackupex: Connection to database server closed
140809 14:42:53  innobackupex: completed OK!

real    3m12.189s
user    3m4.627s
sys     0m2.494s

第二种方法是用lz4进行压缩,并传到新slave服务器的数据目录解压:
    
    
    
    
  1. time innobackupex --defaults-file=/opt/mariadb-10.0.12-linux-x86_64/my.cnf --user=backup --password=backup@123 --host=localhost --port=3307 --parallel=4 --slave-info --safe-slave-backup --stream=xbstream ./ |lz4 -|ssh root@10.0.37.122 "lz4 -d -|xbstream -x -C /data/mysql56"

xtrabackup: Creating suspend file '/tmp/xtrabackup_log_copied' with pid '28832'
xtrabackup: Transaction log of lsn (2395524781) to (2395524781) was copied.
140809 14:45:28  innobackupex: All tables unlocked
innobackupex:: Starting slave SQL thread

innobackupex: Backup created in directory '/opt/mariadb-10.0.12-linux-x86_64'
innobackupex: MySQL slave binlog position: master host '10.0.37.122', filename 'mysql-bin.000010', position 54604158
140809 14:45:28  innobackupex: Connection to database server closed
140809 14:45:28  innobackupex: completed OK!

real    0m19.805s
user    0m15.554s
sys     0m2.514s

这两种方法,主要是压缩方式不同,使用lz4压缩和解压效率更高,整个备份过程时间更短。新slave服务器的数据目录在备份之前保证为空。

2、在新slave上准备备份和修改权限
    
    
    
    
  1. innobackupex --apply-log /data/mysql56/
  2. cd /data/mysql56
  3. chown mysql.mysql -R *
启动mysqld服务.

3、查看备份时原slave服务器对应的master二进制日志文件和位置
    
    
    
    
  1. # cat /data/mysql56/xtrabackup_slave_info 
  2. CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=54604158

4、执行change master to语句
    
    
    
    
  1. root@[(none)] 14:50:07>CHANGE MASTER TO MASTER_LOG_FILE='mysql-bin.000010', MASTER_LOG_POS=54604158,
  2.     -> MASTER_HOST='10.0.37.122',
  3.     -> MASTER_PORT=3307,
  4.     -> MASTER_USER='repl',
  5.     -> MASTER_PASSWORD='repl';
  6. Query OK, 0 rows affected, 2 warnings (0.01 sec)

启动slave后,查看slave的io和sql线程是否正常
root@[(none)] 14:51:21>show slave status\G;
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

在搭建slave时,需要保证server_id不会与现有主从环境的mysqld实例server-id重复,同时在slave的配置文件(my.cnf)加上skip-slave-start,以免启动mysqld服务时,自动开启slave。

参考:
1、"Taking Backups in Replication Environments": http://www.percona.com/doc/percona-xtrabackup/2.2/innobackupex/replication_ibk.html
2、"How to setup a slave for replication in 6 simple steps with Percona XtraBackup": http://www.percona.com/doc/percona-xtrabackup/2.2/howtos/setting_up_replication.html#replication-howto



来自为知笔记(Wiz)


你可能感兴趣的:(使用xtrabackup搭建复制环境)