(詳細)Mysql使用XtraBackup備份數據庫

XtraBackup是比mysqldump好用很多的mysql備份方法,支持全量、增量備份,而且備份速度很快,對於生產的大量數據備份,那簡直是福音,下面介紹使用步驟。

目的

我的mysql配置:

centos7.2系統
--defaults-file=/apps/mysql/my.cnf
--port=7006
--user=root
--password=password 
--socket=/data/mysql7006/mysql.sock

計劃將mysql數據備份到:/data/backup/mysql7006-20181101

備份步驟

安裝XtraBackup

下載地址
https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.3/binary/tarball/percona-xtrabackup-2.4.3-Linux-x86_64.tar.gz
安裝到/apps/目錄,其實就是解壓和創建soft link,對系統無害

cd /tmp
# 下載
wget https://www.percona.com/downloads/XtraBackup/Percona-XtraBackup-2.4.3/binary/tarball/percona-xtrabackup-2.4.3-Linux-x86_64.tar.gz
# 解壓
tar zxvf percona-xtrabackup-2.4.3-Linux-x86_64.tar.gz
mv percona-xtrabackup-2.4.3-Linux-x86_64 xtrabackup
# mv到安裝目錄
mv xtrabackup /apps/
# 創建連接
ln -s /apps/xtrabackup/bin/* /usr/bin/
# 測試
innobackupex -v

備份mysql

首先停止mysql的寫功能:使用root登錄mysql,執行FLUSH TABLES WITH READ LOCK;對mysql進行全局鎖表,不允許寫。
然後執行備份命令:
innobackupex --defaults-file=/apps/mysql/my.cnf --no-timestamp --user=root --password=password --socket=/data/mysql7006/mysql.sock /data/backup/mysql7006-20181101
然後等着執行完畢即可,千萬級數據一般10分鐘內結束。
日誌如下:

181103 06:03:09 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!".

Can't locate Digest/MD5.pm in @INC (@INC contains: /usr/local/lib64/perl5 /usr/local/share/perl5 /usr/lib64/perl5/vendor_perl /usr/share/perl5/vendor_perl /usr/lib64/perl5 /usr/share/perl5 .) at - line 693.
BEGIN failed--compilation aborted at - line 693.
181103 06:03:09 Connecting to MySQL server host: localhost, user: root, password: set, port: 7006, socket: /data/mysql7006/mysql.sock
Using server version 5.6.41-log
innobackupex version 2.4.3 based on MySQL server 5.7.11 Linux (x86_64) (revision id: 6a46905)
xtrabackup: uses posix_fadvise().
xtrabackup: cd to /data/mysql7006/data
xtrabackup: open files limit requested 65535, set to 65535
xtrabackup: using the following InnoDB configuration:
xtrabackup:   innodb_data_home_dir = .
xtrabackup:   innodb_data_file_path = ibdata1:12M:autoextend
xtrabackup:   innodb_log_group_home_dir = .
xtrabackup:   innodb_log_files_in_group = 2
xtrabackup:   innodb_log_file_size = 4294967296
2018-11-03 06:03:09 0x7fc855260740 InnoDB: Using Linux native AIO
xtrabackup: using O_DIRECT
InnoDB: Number of pools: 1
181103 06:03:09 >> log scanned up to (1808750)
xtrabackup: Generating a list of tablespaces
InnoDB: Allocated tablespace ID 1 for mysql/innodb_table_stats, old maximum was 0
181103 06:03:09 [01] Copying ./ibdata1 to /data/backup/mysql7006-20181101/ibdata1
181103 06:03:09 [01]        ...done
181103 06:03:09 [01] Copying ./mysql/innodb_table_stats.ibd to /data/backup/mysql7006-20181101/mysql/innodb_table_stats.ibd
181103 06:03:09 [01]        ...done
。。。。。。
181103 06:03:10 Finished backing up non-InnoDB tables and files
181103 06:03:10 [00] Writing xtrabackup_binlog_info
181103 06:03:10 [00]        ...done
181103 06:03:10 Executing FLUSH NO_WRITE_TO_BINLOG ENGINE LOGS...
xtrabackup: The latest check point (for incremental): '1808750'
xtrabackup: Stopping log copying thread.
.181103 06:03:10 >> log scanned up to (1808750)

181103 06:03:11 Executing UNLOCK TABLES
181103 06:03:11 All tables unlocked
181103 06:03:11 Backup created in directory '/data/backup/mysql7006-20181101'
MySQL binlog position: filename 'bin-log.000025', position '120'
181103 06:03:11 [00] Writing backup-my.cnf
181103 06:03:11 [00]        ...done
181103 06:03:11 [00] Writing xtrabackup_info
181103 06:03:11 [00]        ...done
xtrabackup: Transaction log of lsn (1808750) to (1808750) was copied.
181103 06:03:11 completed OK!

當出現completed OK!就是成功備份成功了。
最後恢復寫功能:root登錄數據庫執行unlock tables;
說明:locktable是保證數據一致性的操作,其實xtrabackup可以實現不鎖表備份,稍微複雜些,見:https://segmentfault.com/a/1190000002575399

還原mysql

如果本庫還原以前的數據,只需要執行:
innobackupex --defaults-file=/apps/mysql/my.cnf --user=root --password=password --use-memory=2G --port=7006 --apply-log /data/backup/mysql7006-20181101

--use-memory=2G默認是100M,增大內存能加快速度。

如果用備份的數據製作從庫,需要執行:

# 停止從庫運行
mysql -u root --socket=*.sock --port=7006 -p shutdown
# 備份數據傳輸到從庫服務器
scp
# 用主庫數據恢復從庫
innobackupex --defaults-file=/apps/mysql/my.cnf --user=root --password=password --use-memory=2G --port=7006 --apply-log /data/backup/mysql7006-20181101
mv /path/to/old/data /path/to/old/data.bak
mv /data/backup/mysql7006-20181101 /path/to/old/data
# 從庫啓動
bin/mysqld_safe --defaults-file=/apps/mysql/my.cnf &

一些注意:

  • 如果數據庫被程序鎖表鎖庫,連select都不能操作,那麼xtrabackup會備份失敗,一直顯示log scanned up to (log id),但是log id不變。此時請先解決鎖庫鎖表問題。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章