說明
xtrabackup --percona
特點(diǎn):
地址:https://www./downloads/XtraBackup
xtrabackup備份原理
實(shí)用腳本innobackupex
安裝問題
問題一:innobackupex發(fā)現(xiàn)mysql模塊沒有安裝
140312 13:30:40 innobackupex: Connecting to MySQL server with DSN 'dbi:mysql:;mysql_read_default_file=/etc/my.cnf;mysql_read_default_group=xtrabackup' as 'root' (using password: YES).
innobackupex: Error: Failed to connect to MySQL server as DBD::mysql module is not installed at /usr/local/mysql/bin/innobackupex line 2956.
因?yàn)榄h(huán)境是使用二進(jìn)制文件安裝的mysql,在Ubuntu下沒有安裝mysql-server;
解決:安裝mysql-server
aiapple@ubuntu:~$ sudo apt-get install mysql-server
innobackupex使用
1)全量備份:
aiapple@ubuntu:~$ innobackupex-1.5.1 --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf dbbackup/ innobackupex-1.5.1: Backup created in directory '/home/aiapple/dbbackup/2016-08-20_06-16-26' innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1643 160820 06:16:30 innobackupex-1.5.1: Connection to database server closed 160820 06:16:30 innobackupex-1.5.1: completed OK! #備份成功;
aiapple@ubuntu:~$ ls dbbackup/
2016-08-20_06-16-26
2)增量備份,需指定上一次備份目錄
注意:innobackupex 增量備份僅針對(duì)InnoDB這類支持事務(wù)的引擎,對(duì)于MyISAM等引擎,則仍然是全備。
增量備份:
aiapple@ubuntu:~$ innobackupex-1.5.1 --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf --incremental --incremental-dir dbbackup/2016-08-20_06-16-26/ dbbackup/ innobackupex-1.5.1: Backup created in directory '/home/aiapple/dbbackup/2016-08-20_06-24-08' innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1749 160820 06:24:13 innobackupex-1.5.1: Connection to database server closed 160820 06:24:13 innobackupex-1.5.1: completed OK! #備份成功 aiapple@ubuntu:~$ ls dbbackup/ 2016-08-20_06-16-26 2016-08-20_06-24-08
3)流式備份()
aiapple@ubuntu:~$ innobackupex-1.5.1 --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf --stream=xbstream dbbackup/ > dbbackup/stream.bak innobackupex-1.5.1: Backup created in directory '/home/aiapple/dbbackup' innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1749 160820 06:28:06 innobackupex-1.5.1: Connection to database server closed 160820 06:28:06 innobackupex-1.5.1: completed OK! #備份成功 aiapple@ubuntu:~$ ls dbbackup/ 2016-08-20_06-16-26 2016-08-20_06-24-08 stream.bak
4)并行備份--使用4個(gè)線程
aiapple@ubuntu:~$ innobackupex-1.5.1 --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf --parallel=4 dbbackup/ innobackupex-1.5.1: Backup created in directory '/home/aiapple/dbbackup/2016-08-20_06-36-34' innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1749 160820 06:36:38 innobackupex-1.5.1: Connection to database server closed 160820 06:36:38 innobackupex-1.5.1: completed OK! #備份成功 aiapple@ubuntu:~$ ls dbbackup/ 2016-08-20_06-16-26 2016-08-20_06-24-08 2016-08-20_06-36-34 stream.bak
5)限流備份--讀寫速度限制10M
aiapple@ubuntu:~$ innobackupex-1.5.1 --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf --throttle=10 dbbackup/ innobackupex-1.5.1: Backup created in directory '/home/aiapple/dbbackup/2016-08-20_06-38-26' innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1749 160820 06:38:31 innobackupex-1.5.1: Connection to database server closed 160820 06:38:31 innobackupex-1.5.1: completed OK! #備份成功 aiapple@ubuntu:~$ ls dbbackup/ 2016-08-20_06-16-26 2016-08-20_06-36-34 stream.bak 2016-08-20_06-24-08 2016-08-20_06-38-26
6)壓縮備份--可以指定壓縮備份線程
aiapple@ubuntu:~$ innobackupex-1.5.1 --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf --compress --compress-thread 4 dbbackup/ innobackupex-1.5.1: Backup created in directory '/home/aiapple/dbbackup/2016-08-20_06-40-26' innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1749 160820 06:40:30 innobackupex-1.5.1: Connection to database server closed 160820 06:40:30 innobackupex-1.5.1: completed OK! #備份成功 aiapple@ubuntu:~$ ls dbbackup/ 2016-08-20_06-16-26 2016-08-20_06-36-34 2016-08-20_06-40-26 2016-08-20_06-24-08 2016-08-20_06-38-26 stream.bak
7)常用參數(shù)
innobackupex --help | less
恢復(fù)
1.全量備份與恢復(fù):
1)全量備份
2)刪除表
3)通過全量備份恢復(fù)
#只需通過apply-log指定備份文件
aiapple@ubuntu:~$ innobackupex --apply-log dbbackup/2016-08-20_06-16-26 InnoDB: Starting shutdown... InnoDB: Shutdown completed; log sequence number 1638934 160820 06:46:20 innobackupex: completed OK! #回滾redo log文件;
aiapple@ubuntu:~/dbbackup/2016-08-20_06-16-26$ ls backup-my.cnf mysql xtrabackup_binlog_info ibdata1 performance_schema xtrabackup_checkpoints ib_logfile0 tt xtrabackup_logfile ib_logfile1 xtrabackup_binary
4)復(fù)制恢復(fù)文件到數(shù)據(jù)目錄
#注意:數(shù)據(jù)目錄要求是空,最好先備份,再清空;
aiapple@ubuntu:~/dbbackup$ innobackupex --defaults-file=/tmp/mysqldata/my.cnf --user=root --password=000000 --copy-back 2016-08-20_06-16-26/ innobackupex-1.5.1: Copying '/home/aiapple/dbbackup/2016-08-20_06-16-26/ib_logfile1' to '/tmp/mysqldata/node1/ib_logfile1' innobackupex-1.5.1: Finished copying back files. 160820 07:11:09 innobackupex-1.5.1: completed OK! 5)查看
2.增量備份與恢復(fù)
#需指定上次備份目錄
1)增加表zengliang;
mysql> show tables; +--------------+ | Tables_in_tt | +--------------+ | course | | t1 | +--------------+ 2 rows in set (0.00 sec) mysql> create table zengliang(a int ,b int); Query OK, 0 rows affected (0.06 sec)
2)增量備份
#注意--incremental-dir后面跟基礎(chǔ)備份目錄,之后再跟增量備份目錄,
#與應(yīng)用增量備份日志相反
aiapple@ubuntu:~$ innobackupex --user=root --password=000000 --defaults-file=/tmp/mysqldata/my.cnf --incremental --incremental-dir dbbackup/2016-08-20_06-16-26/ dbbackup/ innobackupex-1.5.1: MySQL binlog position: filename 'mysql-bin.000001', position 1749 160820 06:24:13 innobackupex-1.5.1: Connection to database server closed 160820 06:24:13 innobackupex-1.5.1: completed OK! #備份成功
aiapple@ubuntu:~$ ls dbbackup/ 2016-08-20_06-16-26 2016-08-20_06-24-08
3)應(yīng)用日志恢復(fù)數(shù)據(jù)
--apply-log 回滾日志
--redo-only 回滾合并(多個(gè)增量的時(shí)候,增量也需要用到,直到最后一個(gè)增量不用)
a)恢復(fù)完全備份數(shù)據(jù)
aiapple@ubuntu:~/dbbackup$ innobackupex --apply-log --redo-only 2016-08-20_06-16-26 b)應(yīng)用增量備份日志
#注意此時(shí)--incremental-dir后面跟的是增量備份的目錄,之后再跟基礎(chǔ)備份的目錄;
#與增量備份相反
#注意:由于權(quán)限問題,使用innobackupex,應(yīng)該使用root賬戶,不然這條不會(huì)通過;
aiapple@ubuntu:~/dbbackup$ innobackupex --apply-log --incremental-dir=(增量備份目錄) (基礎(chǔ)備份目錄)
#不知何原因總是不太容易成功;
4)復(fù)制恢復(fù)文件到數(shù)據(jù)目錄
#注意:數(shù)據(jù)目錄要求是空,最好先備份,再清空;
aiapple@ubuntu:~/dbbackup$ innobackupex --defaults-file=/tmp/mysqldata/my.cnf --user=root --password=000000 --copy-back 2016-08-20_06-16-26/ innobackupex-1.5.1: Copying '/home/aiapple/dbbackup/2016-08-20_06-16-26/ib_logfile1' to '/tmp/mysqldata/node1/ib_logfile1' innobackupex-1.5.1: Finished copying back files. 160820 07:11:09 innobackupex-1.5.1: completed OK!
3.還原壓縮
解壓--innobackupex --decompress /dbbackup/(備份目錄)
應(yīng)用日志--innobackupex --apply-log /dbbackup/(壓縮后目錄)
4)還原流備
mkdir stream
xbstream -C stream -x < stream.bak
并行恢復(fù)--innobackupex --parallel=4 --apply-log --use-memory=200MB /dbbackup/stream
--use-memory:加快恢復(fù)速度;
binlog恢復(fù)
在備份恢復(fù)之后,使用binlog恢復(fù)沒有備份的數(shù)據(jù);
1)查看備份時(shí)binlog點(diǎn);
aiapple@ubuntu:~/dbbackup/mysql3309$ cat xtrabackup_binlog_info mysql-bin.000002 1467 2)使用mysqlbinlog分析二進(jìn)制日志
aiapple@ubuntu:/tmp/mysqldata/node1$ mysqlbinlog -vv mysql-bin.000002 | less # at 1958 #160820 9:10:29 server id 1 end_log_pos 2078 CRC32 0x683bcdc6 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1471698629/*!*/; insert into t6 values(111,222),(333,444),(555,666) /*!*/; # at 2078 #160820 9:10:29 server id 1 end_log_pos 2109 CRC32 0xf264071a Xid = 104 COMMIT/*!*/; # at 2109 #160820 9:11:51 server id 1 end_log_pos 2184 CRC32 0x3f63ae99 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1471698711/*!*/; BEGIN /*!*/; # at 2184 #160820 9:11:51 server id 1 end_log_pos 2298 CRC32 0x5c3b7667 Query thread_id=2 exec_time=0 error_code=0 SET TIMESTAMP=1471698711/*!*/; insert into t6 values(11,22),(33,44),(55,66) /*!*/; # at 2298 #160820 9:11:51 server id 1 end_log_pos 2329 CRC32 0x4ea5c481 Xid = 114 COMMIT/*!*/; 可以看到結(jié)束節(jié)點(diǎn)在2298
3)使用mysqlbinlog+管道+mysql sock登陸
aiapple@ubuntu:/tmp/mysqldata/node1$ mysqlbinlog -vv --start-position=1467 --stop-position=2298 mysql-bin.000002 | mysql -uroot -p --socket=/home/aiapple/dbbackup/mysql3309/mysql.sock
也可以按照時(shí)間恢復(fù):
aiapple@ubuntu:/tmp/mysqldata/node1$ mysqlbinlog -vv --start-datetime='2016-08-20 9:03:58' --stop-datetime='2016-08-20 9:11:51' mysql-bin.000002 | cat
|
|