天天看點

使用mysql備份工具innobackupex将本地資料直接備份到遠端伺服器、備份、恢複操作執行個體...

innobackupex 支援所有mysql引擎資料備份恢複安裝配置方法及原理介紹,通路下面連結

​​http://michaelkang.blog.51cto.com/1553154/1216826​​

測試環境介紹:

#mysql資料目錄

/data/3306/data

#備份檔案存放目錄

/data/3306/backup

#mysql配置檔案位置

/data/3306/my.cnf

#備份前首先所需要建立一個備份所需的目錄

mkdir -p /data/3306/backup

mysql 版本 :

[root@slave2 3306]# mysql -V

mysql  Ver 14.14 Distrib 5.5.25, for Linux (x86_64) using  EditLine wrapper

系統版本:

more /etc/redhat-release

CentOS release 6.2 (Final)

核心版本:

[root@slave2 3306]# uname -a

Linux slave2 2.6.32-220.el6.x86_64 #1 SMP Tue Dec 6 19:48:22 GMT 2011 x86_64 x86_64 x86_64 GNU/Linux

本地IP:92.168.1.199

遠端伺服器IP:192.168.1.200

系統環境同“4.1測試環境介紹”

目的:備份目前機器下的資料到192.168.1.200的/data/3306/backup/目錄下,進行資料恢複啟動。(建立主從同步時使用)

step:1
檢視本地資料庫
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| ptmind             |
| ptmind_common      |
+--------------------+
5 rows in set (0.29 sec)

mysql> use ptmind_common;
Database changed
mysql> show tables;
+------------------------------------+
| Tables_in_ptmind_common            |
+------------------------------------+
| dtl_full_pv_detail                 |
中間省略|
| tst_pn_detail                      |
| tst_pv_detail                      |
+------------------------------------+
60 rows in set (0.00 sec)      

step:2

我為了友善做了證書,導入到了對端伺服器,另外發現如果兩台伺服器不做無密碼認證,從本地往遠端伺服器無法備份。

證書生成及導入到對端伺服器操作如下:

ssh-keygen

ssh-copy-id [email protected]

step:3

備份目前機器下的所有資料到192.168.1.200的/data/3306/backup/目錄下

備份指令1

innobackupex --user=root --defaults-file=/data/3306/my.cnf --stream=tar /data/3306/data | ssh [email protected] cat ">" /data/3306/backup/test.tar

備份指令2

innobackupex --user=root --defaults-file=/data/3306/my.cnf --stream=tar /data/3306/data|ssh [email protected] cat ">"/data/3306/backup/mysqlbackup$(date +%Y-%m-%d_%H_%M_%S).tar

參數解釋:

–stream=tar : 告訴xtrabackup,備份需要以tar的檔案流方式輸出。

--no-lock的選項說明:

       Use this option to disable table lock with "FLUSH TABLES WITH READ

       LOCK". Use it only if ALL your tables are InnoDB and you DO NOT CARE

       about the binary log position of the backup.

no-lock 參數用于指定備份時不鎖定表。

--database:可以指定要備份的庫   --database="ptmind_common kk"

建議使用2組參數,在遠端機器生成的檔案名按日期命名,mysqlbackup2013-06-04_15_30_50.tar

step:4

核實備份檔案:

遠端伺服器生成備份檔案;

[root@slave2 backup]# du -sh *

1.5G    mysqlbackup2013-06-04_16_23_23.tar

在本地生成一個全備檔案,對比兩邊生成全備檔案大小是否一至。

innobackupex --defaults-file=/data/3306/my.cnf  --user=root /data/3306/backup/

[root@slave1 backup]# du -sh *

1.5G    2013-06-04_16-36-03

經對比發現本地和遠端生成備份檔案大小一緻,

本地備份檔案内容如下:

[root@slave1 backup]# ll 2013-06-04_16-36-03/
total 1534520
-rw-r--r-- 1 root root        262 Jun  4 16:36 backup-my.cnf
-rw-r----- 1 root root 1570766848 Jun  4 16:36 ibdata1
drwxr-xr-x 2 root root       4096 Jun  4 16:38 mysql
drwxr-xr-x 2 root root       4096 Jun  4 16:37 performance_schema
drwxr-xr-x 2 root root       4096 Jun  4 16:37 ptmind
drwxr-xr-x 2 root root     540672 Jun  4 16:37 ptmind_common
-rw-r--r-- 1 root root         13 Jun  4 16:38 xtrabackup_binary
-rw-r--r-- 1 root root         23 Jun  4 16:36 xtrabackup_binlog_info
-rw-r----- 1 root root         95 Jun  4 16:38 xtrabackup_checkpoints
-rw-r----- 1 root root       2560 Jun  4 16:38 xtrabackup_logfile      

解壓遠端伺服器備份檔案,檢視内容如下:

[root@slave2 backup]# tar -ixvf mysqlbackup2013-06-04_16_23_23.tar   
backup-my.cnf
ibdata1
xtrabackup_logfile
xtrabackup_checkpoints
xtrabackup_binary      

檢視遠端解壓完成的資料,進行對比和本地備份的檔案一緻

[root@slave2 backup]# ll
total 3121060
-rw-r--r-- 1 root root        262 Jun  4 16:54 backup-my.cnf
-rw-rw---- 1 root root 1570766848 Jun  4 01:35 ibdata1
drwxr-xr-x 2 root root       4096 Jun  4 17:01 mysql
drwxr-xr-x 2 root root       4096 Jun  4 17:01 performance_schema
drwxr-xr-x 2 root root       4096 Jun  4 17:01 ptmind
drwxr-xr-x 2 root root     393216 Jun  4 17:01 ptmind_common
-rw-r--r-- 1 root root         13 Jun  4 16:57 xtrabackup_binary
-rw-r--r-- 1 root root         23 Jun  4 16:56 xtrabackup_binlog_info
-rw-rw---- 1 root root         95 Jun  4 16:57 xtrabackup_checkpoints
-rw-rw---- 1 root root       2560 Jun  4 16:57 xtrabackup_logfile      

step:7

删除遠端/data/下檔案夾,模拟資料丢失;

rm -rf /data/3306/data/*

step:8

首先全備恢複

innobackupex --defaults-file=/data/3306/my.cnf --user=root --apply-log --redo-only /data/3306/backup/

step: 9

copy備份資料檔案到原來資料位置

innobackupex-1.5.1 --defaults-file=/data/3306/my.cnf --user=root --copy-back /data/3306/backup/

step:10

修改資料檔案權限為mysql

chown mysql.mysql -R data

[root@slave2 3306]# /etc/init.d/mysqld start  

Starting MySQL................. SUCCESS!

step:11

檢視驗證本地資料庫

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| ptmind             |
| ptmind_common      |
+--------------------+
5 rows in set (0.29 sec)

mysql> use ptmind_common;
Database changed
mysql> show tables;
+------------------------------------+
| Tables_in_ptmind_common            |
+------------------------------------+
| dtl_full_pv_detail                 |
中間省略|
| tst_pn_detail                      |
| tst_pv_detail                      |
+------------------------------------+
60 rows in set (0.00 sec)