1、mydumper備份
一、開啓16個備份進程,備份出全部的庫以及二進制日誌文件,到指定目錄
/docker/mydumper/all_backup
下
mydumper -u root -p root123 --threads=16 -o /docker/mydumper/all_backup
這裏的--threads=16指定開啓16個備份線程數,這個能夠在MySQL裏面show processlist看出來。
備份完成後,每一個表都會有兩個文件:
一個是表結構文件database.table-schema.sql;
一個是表數據文件database.table.sql。
此外還有一次二進制日誌文件metadata。
$
cat metadata
Started dump at: 2017-06-15 10:57:24
SHOW MASTER STATUS:
Log: mysql-bin.000025
Pos: 681395159
GTID:6fbc72af-348e-11e7-865c-fa163e5807c3:1-22,
ad9b2529-348d-11e7-bef0-fa163e9e3c14:1-2663607
SHOW SLAVE STATUS:
Host: 10.157.24.87
Log: mysql-bin.000047
Pos: 144635857
GTID:6fbc72af-348e-11e7-865c-fa163e5807c3:1-22,
ad9b2529-348d-11e7-bef0-fa163e9e3c14:1-2663607
Finished dump at: 2017-06-15 10:58:50
注:若是沒有指定輸出的目錄:mydumper -u root -p root123
則默認自動生成export-20170615-105920目錄(export-年月日-時分秒)
二、備份出全部的庫到指定文件夾下並壓縮備份文件(這個備份壓縮過程會消耗過多的時間)
mydumper -u root -p root123 -c -o /docker/mydumper/all_backup_compress
會將表結構文件和表數據文件都壓縮,二進制日誌文件不變,以下,文件大小從17G壓縮到了7.7G。
root@dpsvstadbs05 11:20:02:mydumper$
du -sh *
17G all_backup
7.7G all_backup_compress
三、備份出sampson庫全部表的表結構,不備份數據,到指定輸出的目錄
/docker/mydumper/sampson-d
下
mydumper -u root -p root123 -B sampson -d -o /docker/mydumper/sampson-d
則/docker/mydumper/sampson-d目錄下只有metadata和sampson.table-schema.sql文件。
四、備份出sampson庫全部表的數據,不備份表結構,到指定輸出的目錄
/docker/mydumper/sampson-m
下
mydumper -u root -p root123 -B sampson -m -o /docker/mydumper/sampson-m
則/docker/mydumper/sampson-m目錄下只有metadata和sampson.table.sql文件。
五、備份出sampson庫全部dsns和t1表到指定輸出的目錄
/docker/mydumper/sampson-T
下
mydumper -u root -p root123 -B sampson -T dsns,t1 -o /docker/mydumper/sampson-T
則/docker/mydumper/sampson-T目錄下有metadata和sampson建庫文件以及dsns和t1表的備份文件。
注:若是備份須要後臺運行,能夠加--daemon實現後臺執行導出命令。
2、
myloader
恢復
一、從全備的目錄中恢復sampson庫:
myloader -u root -p root123 --threads=16 --database=sampson -d /docker/mydumper/all_backup
二、從只備份了表結構的目錄中恢復sampson庫表結構到
sampson-d庫中去
:
myloader -u root -p root123 --database=sampson-d -d /docker/mydumper/sampson-d
三、從全備的文件夾中恢復sampson庫
到samp庫中去:
myloader -u root -p root123 --database=samp --source-db=sampson -d /docker/mydumper/all_backup
注意:建議備份恢復的時候加參數-v 3,顯示詳細的日誌。
root@dpsvstadbs05 12:13:35:mydumper$ myloader -u root -p root123 -d /docker/mydumper/all_backup --database=huihui --source-db=lizhi -v 3
** Message: 4 threads created
** Message: Creating database `huihui`
** Message: Creating table `huihui`.`my1`
** Message: Creating table `huihui`.`my2`
** Message: Creating table `huihui`.`my3`
** Message: Creating table `huihui`.`my4`
** Message: Creating table `huihui`.`my5`
** Message: Creating table `huihui`.`my6`
** Message: Creating table `huihui`.`my7`
** Message: Creating table `huihui`.`t1`
** Message: Thread 1 restoring `lizhi`.`my1` part 0
** Message: Thread 3 restoring `lizhi`.`my2` part 0
** Message: Thread 2 restoring `lizhi`.`my3` part 0
** Message: Thread 4 restoring `lizhi`.`my4` part 0
** Message: Thread 3 restoring `lizhi`.`my5` part 0
** Message: Thread 1 restoring `lizhi`.`my6` part 0
** Message: Thread 4 restoring `lizhi`.`my7` part 0
** Message: Thread 2 restoring `lizhi`.`t1` part 0
** Message: Thread 2 shutting down
** Message: Thread 1 shutting down
** Message: Thread 4 shutting down
** Message: Thread 3 shutting down
附:
mydumper參數詳解
$ mydumper --help
Usage:
mydumper [OPTION...] multi-threaded MySQL dumping
Help Options:
-?, --help Show help options
Application Options:
-B, --database 要備份的數據庫,不指定則備份全部庫
-T, --tables-list 須要備份的表,名字用逗號隔開
-o, --outputdir 備份文件輸出的目錄
-s, --statement-size 生成的insert語句的字節數,默認1000000
-r, --rows Try to split tables into chunks of this many rows. This option turns off --chunk-filesize
-F, --chunk-filesize Split tables into chunks of this output file size. This value is in MB
-c, --compress Compress output files壓縮輸出文件
-e, --build-empty-files 若是表數據是空,仍是產生一個空文件(默認無數據則只有表結構文件)
-x, --regex Regular expression for 'db.table' matching 使用正則表達式匹配'db.table'
-i, --ignore-engines Comma delimited list of storage engines to ignore忽略的存儲引擎,用逗號分割
-m, --no-schemas Do not dump table schemas with the data不備份表結構,只備份數據
-d, --no-data Do not dump table data備份表結構,不備份數據
-G, --triggers Dump triggers備份觸發器
-E, --events Dump events
-R, --routines Dump stored procedures and functions備份存儲過程和函數
-k, --no-locks 不使用臨時共享只讀鎖,使用這個選項會形成數據不一致
--less-locking Minimize locking time on InnoDB tables.減小對InnoDB表的鎖施加時間
-l, --long-query-guard 設定阻塞備份的長查詢超時時間,單位是秒,默認是60秒(超時後默認mydumper將會退出)
-K, --kill-long-queries Kill long running queries (instead of aborting)殺掉長查詢 (不退出)
-D, --daemon Enable daemon mode啓用守護進程模式,守護進程模式以某個間隔不間斷對數據庫進行備
-I, --snapshot-interval dump快照間隔時間,默認60s,須要在daemon模式下
-L, --logfile 使用的日誌文件名(mydumper所產生的日誌), 默認使用標準輸出
--tz-utc SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones, defaults to on use --skip-tz-utc to disable.
--skip-tz-utc
--use-savepoints 使用savepoints來減小採集metadata所形成的鎖時間,須要 SUPER 權限
--success-on-1146 Not increment error count and Warning instead of Critical in case of table doesn't exist
--lock-all-tables Use LOCK TABLE for all, instead of FTWRL
-U, --updated-since Use Update_time to dump only tables updated in the last U days
--trx-consistency-only Transactional consistency only
-h, --host 鏈接的主機名
-u, --user 用來備份的用戶名
-p, --password 用戶密碼
-P, --port 鏈接端口
-S, --socket 使用socket通訊時的socket文件
-t, --threads 開啓的備份線程數,默認是4
-C, --compress-protocol 壓縮與mysql通訊的數據
-V, --version 顯示版本號
-v, --verbose 輸出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默認爲2
myloader參數詳解html
$ 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每次事物執行的查詢數量,默認是1000
-o, --overwrite-tables Drop tables if they already exist若是要恢復的表存在,則先drop掉該表,使用該參數,須要備份時候要備份表結構
-B, --database An alternative database to restore into還原到指定的數據庫
-s, --source-db Database to restore選擇被還原的數據庫,將這個數據庫數據還原到--database指定的數據庫裏
-e, --enable-binlog Enable binary logging of the restore data啓用還原數據的二進制日誌
-h, --host 鏈接的主機名
-u, --user 用來備份的用戶名
-p, --password 用戶密碼
-P, --port 鏈接端口
-S, --socket 使用socket通訊時的socket文件
-t, --threads 開啓的備份線程數,默認是4
-C, --compress-protocol 壓縮與mysql通訊的數據
-V, --version 顯示版本號
-v, --verbose 輸出信息模式, 0 = silent, 1 = errors, 2 = warnings, 3 = info, 默認爲2