1. 程式人生 > >基於xtrabackup實現數據備份還原——高效的數據備份還原工具

基於xtrabackup實現數據備份還原——高效的數據備份還原工具

xtrabackup 數據備份 數據還原

準備一個幹凈的備份目錄;

[root@mysql ~]$ll /backups/
total 0

MariaDB [(none)]> show binary logs;
+------------------+-----------+
| Log_name         | File_size |
+------------------+-----------+
| mysql-bin.000001 |     30331 |
| mysql-bin.000002 |   1038814 |
| mysql-bin.000003 |      7698 |
| mysql-bin.000004 |       442 |
| mysql-bin.000005 |       423 |
+------------------+-----------+
5 rows in set (0.00 sec)


數據備份:

默認就是以root用戶的身份進行的備份;
[root@mysql ~]$innobackupex --user=root /backups/

[root@mysql ~]$ll /backups/
total 0
drwxr-x--- 6 root root 217 Feb 25 14:14 2018-02-25_14-14-07
[root@mysql ~]$ll /backups/2018-02-25_14-14-07/
total 18460
-rw-r----- 1 root root      417 Feb 25 14:14 backup-my.cnf
drwxr-x--- 2 root root      272 Feb 25 14:14 hellodb
-rw-r----- 1 root root 18874368 Feb 25 14:14 ibdata1
drwxr-x--- 2 root root     4096 Feb 25 14:14 mysql
drwxr-x--- 2 root root     4096 Feb 25 14:14 performance_schema
drwxr-x--- 2 root root       20 Feb 25 14:14 test
-rw-r----- 1 root root       21 Feb 25 14:14 xtrabackup_binlog_info
-rw-r----- 1 root root      113 Feb 25 14:14 xtrabackup_checkpoints
-rw-r----- 1 root root      454 Feb 25 14:14 xtrabackup_info
-rw-r----- 1 root root     2560 Feb 25 14:14 xtrabackup_logfile


[root@mysql ~]$cd /backups/2018-02-25_14-14-07/
[root@mysql 2018-02-25_14-14-07]$ls
backup-my.cnf  ibdata1  performance_schema  xtrabackup_binlog_info  xtrabackup_info
hellodb        mysql    test                xtrabackup_checkpoints  xtrabackup_logfile
[root@mysql 2018-02-25_14-14-07]$


# lsn是日誌序列號,在磁盤上保存了數據庫的所有數據;文件很大,分成很多的小塊存儲在了磁盤上;
每個數據塊的小塊都有所謂的lsn號;如從100-200,如果將100這個數據塊中的數據做了修改,那麽他的LSN會加1,即
變為了201;所以根據LSN的大小,可以判斷數據塊中的數據是否備份過;

這裏是全備份,所以就是將所有的LSN對應的數據塊的數據都進行了備份。
    [root@mysql 2018-02-25_14-14-07]$less xtrabackup_checkpoints 
    backup_type = full-backuped
    from_lsn = 0
    to_lsn = 1640915
    last_lsn = 1640915
    compact = 0
    recover_binlog_info = 0


盡管表面來看是拷貝的文件,但是底層實際拷貝的是數據塊,所有效率很高。

這個文件中顯示的是全備份備份到二進制文件的哪個位置;
[root@mysql 2018-02-25_14-14-07]$cat xtrabackup_binlog_info 
mysql-bin.000005    423


[root@mysql 2018-02-25_14-14-07]$file xtrabackup_logfile 
xtrabackup_logfile: data




還原數據到一個新的MySQL主機上

找一個幹凈的主機,將在27.7主機上備份的數據在27.17上實現還原;
[root@mysql17 ~]$vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
log_bin
innodb_file_per_table


[root@mysql17 ~]$yum install percona-xtrabackup-24-2.4.9-1.el7.x86_64.rpm 
[root@mysql backups]$scp -rp /backups/2018-02-25_14-14-07/ 192.168.27.17:/app/

[root@mysql17 ~]$ls /app/
2018-02-25_14-14-07




數據庫的整理操作:
包括將不完整的事務進行回滾;因為備份的時間點極有可能被一個事務橫跨;

[root@mysql17 ~]$innobackupex --apply-log /app/2018-02-25_14-14-07/
180225 01:53:05 completed OK!
確保要恢復的數據庫的數據目錄是空的;
[root@mysql17 ~]$ll /var/lib/mysql/
total 0
將整理過數據復制到數據庫的數據目錄;
這個過程就是復制數據,對於innodb引擎,他是基於塊的方式實現的;對於myISAM引擎,
那麽就是單個文件的復制;
[root@mysql17 ~]$innobackupex --copy-back /app/2018-02-25_14-14-07/
...
180225 01:56:10 completed OK!
[root@mysql17 ~]$ll /var/lib/mysql/
total 40976
drwxr-x--- 2 root root      272 Feb 25 01:56 hellodb
-rw-r----- 1 root root 18874368 Feb 25 01:56 ibdata1
-rw-r----- 1 root root  5242880 Feb 25 01:56 ib_logfile0
-rw-r----- 1 root root  5242880 Feb 25 01:56 ib_logfile1
-rw-r----- 1 root root 12582912 Feb 25 01:56 ibtmp1
drwxr-x--- 2 root root     4096 Feb 25 01:56 mysql
drwxr-x--- 2 root root     4096 Feb 25 01:56 performance_schema
drwxr-x--- 2 root root       20 Feb 25 01:56 test
-rw-r----- 1 root root       35 Feb 25 01:56 xtrabackup_binlog_pos_innodb
-rw-r----- 1 root root      454 Feb 25 01:56 xtrabackup_info
[root@mysql17 ~]$chown -R mysql.mysql /var/lib/mysql/
[root@mysql17 ~]$ll /var/lib/mysql/
total 40976
drwxr-x--- 2 mysql mysql      272 Feb 25 01:56 hellodb
-rw-r----- 1 mysql mysql 18874368 Feb 25 01:56 ibdata1
-rw-r----- 1 mysql mysql  5242880 Feb 25 01:56 ib_logfile0
-rw-r----- 1 mysql mysql  5242880 Feb 25 01:56 ib_logfile1
-rw-r----- 1 mysql mysql 12582912 Feb 25 01:56 ibtmp1
drwxr-x--- 2 mysql mysql     4096 Feb 25 01:56 mysql
drwxr-x--- 2 mysql mysql     4096 Feb 25 01:56 performance_schema
drwxr-x--- 2 mysql mysql       20 Feb 25 01:56 test
-rw-r----- 1 mysql mysql       35 Feb 25 01:56 xtrabackup_binlog_pos_innodb
-rw-r----- 1 mysql mysql      454 Feb 25 01:56 xtrabackup_info
[root@mysql17 ~]$ll /var/lib/mysql/ -d
drwxr-xr-x 6 mysql mysql 198 Feb 25 01:56 /var/lib/mysql/
[root@mysql17 ~]$systemctl start mariadb
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb            |
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)
MariaDB [(none)]> 
MariaDB [(none)]> show master logs;
+--------------------+-----------+
| Log_name           | File_size |
+--------------------+-----------+
| mariadb-bin.000001 |       245 |
+--------------------+-----------+
1 row in set (0.00 sec)
MariaDB [(none)]> select * from hellodb.students;
Empty set (0.00 sec)

以上就是使用xtrabackup實現將一個主機的數據全備份後還原到一個遠程的新的主機的過程。


基於xtrabackup實現數據備份還原——高效的數據備份還原工具