Skip to content

Provide a guideline to prepare mysql logical backup and restore

License

Notifications You must be signed in to change notification settings

17media/mysql-restore

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

前言

請優先善用storage的snapshot功能作backup and restore。

當你需要用上logical restore時:

  • master和slave機器都趴掉
  • 沒法從storage snapshot恢復過來
  • 其中一個use-case:整個datacenter趴掉,需要在別的datacenter重建mysql

這篇沒使用csv格式,因為所在的datacenter沒法這麼做

mysql.cnf

  • mysqlmysqldump會從這檔案讀取所需的username和password
  • 位置: ~/.my.cnf
  • 內容:
[client]
host=abc.com
user=root
password=abc123456
port=3306

backup script

  • 把純資料backup,不包schema,並且進行gzip
mysqldump --max_allowed_packet=512M --single-transaction --extended-insert --compress \
--no-create-info --no-create-db --skip-triggers \
--databases test1 | gzip -c > dump.sql.gz
  • 把database schema作backup
mysqldump --max_allowed_packet=512M --single-transaction --extended-insert --compress \
--no-data \
--databases test1 > schema.sql
  • autoinc的數值存放於schema部份而不是在data部份,所以每次logical backup一定要2者都跑
  • 次序一定是先跑data然後跑schema,否則autoinc數值會出錯
  • backup過程中不能跑alter table

restore script

  • 先重建schema:mysql < schema.sql
  • import data部份,mysql是用single thread來跑,100GB的資料需要超過6小時的,這樣子太慢
gzip -cd dump.gz | mysql --max_allowed_packet=512M --compress 

parallel data import

  • 原理

    • 先把本來的dump.sql.gz切成table_XXX.sqltable_YYY.sql……
    • 當準備好一個table_XXX.sql後,立即呼叫mysql < table_XXX.sql來做data import,因為同一時間能import多個table,所以時間大幅縮短了
    • 部份可以事後再補回的table可以先不做import,再進一步加快速度
  • compile program: go build

  • 執行import:

mysql-restore \
--path <your dump.sql.gz, in absolute path> \
--concurrency <max concurrency table import, default=8>
--ignore-tables <the tables you want to skip, comma as delimitor>
  • 範例
./mysql-restore --path /backup/dump-20180429.sql.gz --ignore-tables "NotImportantTable,JustLogTable"

About

Provide a guideline to prepare mysql logical backup and restore

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages