码迷,mamicode.com
首页 > 其他好文 > 详细

xtrabackup的使用

时间:2019-12-13 12:12:06      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:usr   size   end   thread   因此   down   mysq   use   SQL_error   

Percona-xtrabackup是 Percona公司开发的一个用于MySQL数据库物理热备的备份工具。

一、安装xtrabackup

采用rpm包的方式进行安装

[root@server-1 ~]# cd /usr/local/src/
[root@server-1 src]# wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.9/binary/redhat/7/x86_64/percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm
[root@server-1 src]# yum install -y percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm

 二、xtrabackup工具介绍

xtrabackup主要包含两个工具:

xtrabackup:用于热备innodb,xtraxdb表中数据的工具,不能备份其他类型的表,也不能备份数据表结构。

innobackupex:是将xtrabackup进行封装的perl脚本,提供了备份myisam表的能力。

常用选项:

--host:指定主机

--user:指定用户名

--password:指定密码

--port:指定端口

--databases:指定数据库

--incremental:创建增量备份

--incremental-basedir:指定包含完全备份的目录

--incremental-dir:指定包含增量备份的目录

--apply-log:对备份进行预处理操作

一般情况下,在备份完成后,数据尚且不能用于恢复操作,因为备份的数据中可能会包含尚未提交的事务或已经提交但尚未同步至数据文件中的事务。因此,此时数据文件仍处理不一致状态。“准备”的主要作用正是通过回滚未提交的事务及同步已经提交的事务至数据文件也使得数据文件处于一致性状态。

--redo-only: 不回滚未提交事务

--copy-back:恢复备份目录

使用innobackupex备份时,其会调用xtrabackup备份所有的InnoDB表,复制所有关于表结构定义的相关文件(.frm)、以及MyISAM、MERGE、CSV和ARCHIVE表的相关文件,同时还会备份触发器和数据库配置信息相关的文件,这些文件会被保存到一个以时间命名的目录当中。在备份的同时,innobackupex还会在备份目录中创建如下文件:

xtrabackup_checkpoints -- 备份类型(如完全或增量)、备份状态(如是否已经为prepared状态)和LSN(日志序列号)范围信息:每个InnoDB页(通常为16k大小)都会包含一个日志序列号,即LSN,LSN是整个数据库系统的系统版本号,每个页面相关的LSN能够表明此页面最近是如何发生改变的。

xtrabackup_binlog_info -- mysql服务器当前正在使用的二进制日志文件及备份这一刻位置二进制日志时间的位置。xtrabackup_binlog_pos_innodb -- 二进制日志文件及用于InnoDB或XtraDB表的二进制日志文件的当前position。

xtrabackup_binary -- 备份中用到的xtrabackup的可执行文件;

backup-my.cnf -- 备份命令用到的配置选项信息:在使用innobackupex进行备份时,还可以使用--no-timestamp选项来阻止命令自动创建一个以时间命名的目录:如此一来,innobackupex命令将会创建一个BACKUP-DIR目录来存储备份数据。

三、主库全库备份

在主库上执行如下命令

[root@server-1 bak]# innobackupex --defaults-file=/etc/mysql/my.cnf --user=root --password=xxxxxx /home/mysql-5.7.26/bak
191213 10:52:21 innobackupex: Starting the backup operation

IMPORTANT: Please check that the backup run completes successfully.
           At the end of a successful backup run innobackupex
           prints "completed OK!".

191213 10:52:21  version_check Connecting to MySQL server with DSN dbi:mysql:;mysql_read_default_group=xtrabackup as root  (using password: YES).
Failed to connect to MySQL server: DBI connect(;mysql_read_default_group=xtrabackup,root,...) failed: Cant connect to local MySQL server through socket /var/lib/mysql/mysql.sock (2) at - line 1314.
191213 10:52:21 Connecting to MySQL server host: localhost, user: root, password: set, port: not set, socket: not set
Failed to connect to MySQL server: Cant connect to local MySQL server through socket /var/lib/mysql/mysql.sock (2).
[root@server-1 bak]# 

报错,sock文件找不到,因为my.cnf里配置了另外一个路径的socket,所以找不到默认路径的文件,我们指定Socket连接方式来连接mysql

innobackupex --user=root --password=xxxxxxx --socket=/home/mysql-5.7.26/mysql.sock /home/mysql-5.7.26/bak/

执行到最后,显示如下信息

191213 11:00:55 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): 2591312
xtrabackup: Stopping log copying thread.
.191213 11:00:55 >> log scanned up to (2591321)

191213 11:00:56 Executing UNLOCK TABLES
191213 11:00:56 All tables unlocked
191213 11:00:56 [00] Copying ib_buffer_pool to /home/mysql-5.7.26/bak/2019-12-13_11-00-50/ib_buffer_pool
191213 11:00:56 [00]        ...done
191213 11:00:56 Backup created in directory /home/mysql-5.7.26/bak/2019-12-13_11-00-50/
MySQL binlog position: filename master-18-69.000001, position 1857
191213 11:00:56 [00] Writing /home/mysql-5.7.26/bak/2019-12-13_11-00-50/backup-my.cnf
191213 11:00:56 [00]        ...done
191213 11:00:56 [00] Writing /home/mysql-5.7.26/bak/2019-12-13_11-00-50/xtrabackup_info
191213 11:00:56 [00]        ...done
xtrabackup: Transaction log of lsn (2591312) to (2591321) was copied.
191213 11:00:56 completed OK!
completed OK!,表示备份成功
查看备份目录显示如下文件:
[root@server-1 2019-12-13_11-00-50]# ll
总用量 12340
-rw-r----- 1 root root      429 12月 13 11:00 backup-my.cnf
-rw-r----- 1 root root      301 12月 13 11:00 ib_buffer_pool
-rw-r----- 1 root root 12582912 12月 13 11:00 ibdata1
drwxr-x--- 2 root root     4096 12月 13 11:00 mysql
drwxr-x--- 2 root root     8192 12月 13 11:00 performance_schema
drwxr-x--- 2 root root     8192 12月 13 11:00 sys
drwxr-x--- 2 root root      202 12月 13 11:00 test
-rw-r----- 1 root root       25 12月 13 11:00 xtrabackup_binlog_info
-rw-r----- 1 root root      113 12月 13 11:00 xtrabackup_checkpoints
-rw-r----- 1 root root      537 12月 13 11:00 xtrabackup_info
-rw-r----- 1 root root     2560 12月 13 11:00 xtrabackup_logfile

backup-my.cnf:备份用到的配置文件
ibdata1:数据文件
xtrabackup_binlog_info:
mysql服务器当前正在使用的二进制日志文件及备份这一刻位置二进制日志时间的位置
xtrabackup_checkpoints:备份的类型、状态和LSN状态信息文件
xtrabackup_logfile:备份的日志文件

四、从库恢复备份

1、将从库的msyql停掉

[root@localhost data]# mysqladmin -uroot -pZaq1xsw@ -S /home/mysql-5.7.26/mysql.sock shutdown

2、在主库上复制备份目录到从库一个指定目录上

[root@server-1 bak]# scp -r 2019-12-13_11-00-50/ root@172.28.18.71:/home/mysql-5.7.26

3、从库上删除已有的data目录

root@localhost mysql-5.7.26]# ll
总用量 4
drwxr-x--- 6 root  root   235 12月 13 11:22 2019-12-13_11-00-50
drwxr-xr-x 6 mysql mysql 4096 12月 13 11:17 data
drwxr-xr-x 2 mysql mysql   24 6月   6 2019 log

查看已经有了刚才复制的主库备份目录,删除从库的data目录

[root@localhost mysql-5.7.26]# rm -rf data/

4、从库上也安装好percona-xtrabackup工具

执行--apply-log ,将数据合并,使数据文件处于一致性状态

[root@localhost mysql-5.7.26]# innobackupex --apply-log /home/mysql-5.7.26/2019-12-13_11-33-29/
InnoDB: PUNCH HOLE support available
InnoDB: Mutexes and rw_locks use GCC atomic builtins
InnoDB: Uses event mutexes
InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
InnoDB: Compressed tables use zlib 1.2.7
InnoDB: Number of pools: 1
InnoDB: Using CPU crc32 instructions
InnoDB: Initializing buffer pool, total size = 100M, instances = 1, chunk size = 100M
InnoDB: Completed initialization of buffer pool
InnoDB: page_cleaner coordinator priority: -20
InnoDB: Highest supported file format is Barracuda.
InnoDB: Removed temporary tablespace data file: "ibtmp1"
InnoDB: Creating shared tablespace for temporary tables
InnoDB: Setting file ./ibtmp1 size to 12 MB. Physically writing the file full; Please wait ...
InnoDB: File ./ibtmp1 size is now 12 MB.
InnoDB: 96 redo rollback segment(s) found. 1 redo rollback segment(s) are active.
InnoDB: 32 non-redo rollback segment(s) are active.
InnoDB: 5.7.13 started; log sequence number 2591803
xtrabackup: starting shutdown with innodb_fast_shutdown = 1
InnoDB: FTS optimize thread exiting.
InnoDB: Starting shutdown...
InnoDB: Shutdown completed; log sequence number 2591822
191213 11:43:57 completed OK!

最后显示191213 11:43:57 completed OK!,表示数据文件一致性准备成功

 5、将主库备份目录更名为从库的data目录

[root@localhost mysql-5.7.26]# mv 2019-12-13_11-00-50/ data
[root@localhost mysql-5.7.26]# ll
总用量 0
drwxr-x--- 6 root  root  235 12月 13 11:22 data
drwxr-xr-x 2 mysql mysql  24 6月   6 2019 log
[root@localhost mysql-5.7.26]# 

此时看到data目录的所有者是root,我们将mysql授权给data

[root@localhost mysql-5.7.26]# chown -R mysql:mysql data/
[root@localhost mysql-5.7.26]# ll
总用量 0
drwxr-x--- 6 mysql mysql 235 12月 13 11:22 data
drwxr-xr-x 2 mysql mysql  24 6月   6 2019 log

启动msyql

[root@localhost mysql-5.7.26]# systemctl start mysqld

进入mysql

[root@localhost mysql-5.7.26]# mysql -uroot -pxxxx -S /home/mysql-5.7.26/mysql.sock

查看数据库

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.01 sec)

跟主库的数据一样了

五、设置从库同步

1、查看复制过来的data目录下的xtrabackup_info文件获取到binlog和pos位置

uuid = 57dcc063-1d59-11ea-a759-14feb5dc2c77
name = 
tool_name = innobackupex
tool_command = --user=root --password=... --socket=/home/mysql-5.7.26/mysql.sock /home/mysql-5.7.26/bak/
tool_version = 2.4.9
ibbackup_version = 2.4.9
server_version = 5.7.26-log
start_time = 2019-12-13 11:33:29
end_time = 2019-12-13 11:33:35
lock_time = 0 
binlog_pos = filename master-18-69.000001, position 2121
innodb_from_lsn = 0 
innodb_to_lsn = 2591653
partial = N 
incremental = N 
format = file
compact = N 
compressed = N 
encrypted = N 
binlog_pos = filename ‘master-18-69.000001‘, position ‘2121‘

2、连接mysql,执行从库命令
mysql> change master to
    -> master_host=172.28.18.69,
    -> master_port=3306,
    -> master_user=repl,
    -> master_password=Xxxxxxxx,
    -> master_log_file=master-18-69.000001,
    -> master_log_pos=2121;
Query OK, 0 rows affected, 2 warnings (0.10 sec)

3、启动从库同步

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

4、显示从库状态

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.28.18.69
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-18-69.000001
          Read_Master_Log_Pos: 2121
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 323
        Relay_Master_Log_File: master-18-69.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           Replicate_Do_Table: 
       Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                   Last_Errno: 0
                   Last_Error: 
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 2121
              Relay_Log_Space: 534
              Until_Condition: None
               Until_Log_File: 
                Until_Log_Pos: 0
           Master_SSL_Allowed: No
           Master_SSL_CA_File: 
           Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
               Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
                Last_IO_Errno: 0
                Last_IO_Error: 
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 693306
                  Master_UUID: ee3e292b-866b-11e9-9df8-14feb5dc2c77
             Master_Info_File: /home/mysql-5.7.26/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           Master_Retry_Count: 86400
                  Master_Bind: 
      Last_IO_Error_Timestamp: 
     Last_SQL_Error_Timestamp: 
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: d24d8a53-880d-11e9-b1f3-842b2b5cdc15:1-7,
ee3e292b-866b-11e9-9df8-14feb5dc2c77:1-23
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 
 Slave_IO_Running: Yes
 Slave_SQL_Running: Yes
从库同步成功,


xtrabackup的使用

标签:usr   size   end   thread   因此   down   mysq   use   SQL_error   

原文地址:https://www.cnblogs.com/sky-cheng/p/12034369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!