mydumper是針對mysql數據庫備份的一個輕量級第三方的開源工具,備份方式為邏輯備份。它支持多線程,備份速度遠高於原生態的mysqldump以及眾多優異特性。與其相配套的恢復工具則是myloader,主要用於將dump出來的sql以並行的方式進行恢復。本文主要描述myloader的使用方法並給出示例。
有關mydumper的相關參考
mydumper備份mysql數據庫示例
mydumper安裝及安裝故障匯總
1、單庫的備份與恢復
[root@app ~]# mydumper -u leshami -p xxx -B sakila -o /tmp/bak
[root@app ~]# mysql -urobin -pxxx -e "show databases"|grep restoredb
[root@app ~]# mysql -urobin -pxxx \
> -e "create table sakila.tb like sakila.actor; ###創建測試表
> insert into sakila.tb select * from sakila.actor"
###將備份庫恢復到一個新數據庫,如restoredb
[root@app ~]# myloader -u leshami -p xxx -B restoredb -d /tmp/bak
[root@app ~]# mysql -urobin -pxxx -e "show databases"|grep restoredb
restoredb
###恢復到原庫
[root@app ~]# myloader -u leshami -p xxx -B sakila -d /tmp/bak
** (myloader:3642): CRITICAL **: Error restoring sakila.category from file sakila.category-schema.sql: Table 'category' already exists
---增加-o參數進行覆蓋恢復
[root@app ~]# myloader -u leshami -p xxx -o -B sakila -d /tmp/bak
2、單表恢復
[root@app ~]# mysql -urobin -pxxx -e "drop table sakila.tb"
[root@app ~]# mysql -urobin -pxxx -e "select count(*) from sakila.tb"
Warning: Using a password on the command line interface can be insecure.
ERROR 1146 (42S02) at line 1: Table 'sakila.tb' doesn't exist
###直接調用備份的schema以及數據文件執行即可
[root@app ~]# mysql -urobin -pxxx \
> -e "use sakila;
> source /tmp/bak/sakila.tb-schema.sql
> source /tmp/bak/sakila.tb.sql"
###驗證結果
[root@app ~]# mysql -urobin -pxxx -e "select count(*) from sakila.tb"
Warning: Using a password on the command line interface can be insecure.
+----------+
| count(*) |
+----------+
| 200 |
+----------+
3、實例級別的備份與恢復
[root@app ~]# rm -rf /tmp/bak/*
[root@app ~]# mydumper -u leshami -p xxx --regex '^(?!(mysql|test))' -o /tmp/bak
###嘗試刪除部分數據庫
[root@app ~]# mysql -urobin -pxxx \
> -e "drop database tempdb;drop database sakila"
###基於全部備份文件進行恢復
[root@app ~]# myloader -u leshami -p xxx -o -d /tmp/bak
4、獲取幫助
[root@app ~]# myloader --help
Usage:
myloader [OPTION...] multi-threaded MySQL loader
Help Options:
-?, --help Show help options
Application Options:
-d, --directory Directory of the dump to import
-q, --queries-per-transaction Number of queries per transaction, default 1000
還原期間每個事務insert的數目,缺省是1k
-o, --overwrite-tables Drop tables if they already exist(表存在即覆蓋)
-B, --database An alternative database to restore into
-e, --enable-binlog Enable binary logging of the restore data
-h, --host The host to connect to
-u, --user Username with privileges to run the dump
-p, --password User password
-P, --port TCP/IP port to connect to
-S, --socket UNIX domain socket file to use for connection
-t, --threads Number of threads to use, default 4
-C, --compress-protocol Use compression on the MySQL connection
-V, --version Show the program version and exit
-v, --verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2