mysql小白系列_06 備份與恢複 - mydumper與myloader

來源:互聯網
上載者:User

標籤:hub   compress   resource   second   zone   nsa   local   lock   prot   

http://blog.csdn.net/leshami/article/details/46815553
https://www.cnblogs.com/zhoujinyi/p/3423641.html

編譯安裝mydumper

https://launchpad.net/mydumper
https://github.com/maxbube/mydumper

wget https://launchpad.net/mydumper/0.9/0.9.1/+download/mydumper-0.9.1.tar.gz  cmake .makemake install

在/usr/local/bin下面產生2個檔案:mydumper和myloader

備份結果集
  • 所有備份在一個目錄,不指定目錄則以日期事件命名:export-20180223-102900
  • 目錄中有metadata檔案,記錄備份時的主庫binlog檔案和位置
[[email protected] mydumper]# cat metadataStarted dump at: 2018-02-23 11:56:52SHOW MASTER STATUS:        Log: binlog.000009        Pos: 403849416        GTID:Finished dump at: 2018-02-23 11:56:52
  • 備份包含建庫sql,建表sql,表內容sql
mydumper工作過程
  1. 串連目標資料庫
  2. 通過show processlist判斷是否有長查詢,根據long-query-guard和kill-long-queries來決定是否退出或者殺掉長查詢
  3. 鎖定myisam表,FTWRL
  4. 針對innodb引擎表開啟事務,start transaction
  5. 建立worker子線程
  6. 確定候選表,根據類別分別插入innodb_table,non_innodb_table,table_schemas鏈表(表結構)
  7. 將候選表通過g_async_queue_push加入任務隊列,有worker子線程從隊列中讀取表資訊並執行資料匯出
  8. 執行unlock tables,處理完myisam表後立即解鎖,以減少鎖定時間
  9. 等待worker退出
mydumper命令選項

http://blog.itpub.net/22664653/viewspace-2132759/

[[email protected] mydumper-0.9.1]# mydumper --helpUsage:  mydumper [OPTION...] multi-threaded MySQL dumpingHelp Options:  -?, --help                  Show help optionsApplication Options:  -B, --database              Database to dump  指定備份的資料庫,不指定則備份所有資料庫,包含mysql,但是不含performance_schema  -T, --tables-list           Comma delimited table list to dump (does not exclude regex option)  指定備份的表,多個表以逗號隔開  -o, --outputdir             Directory to output files to  指定目錄輸出為止,不指定則在當前路徑組建目錄:export-20180223-102900  -s, --statement-size        Attempted size of INSERT statement in bytes, default 1000000  指定產生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     Build dump files even if no data available from table  備份空表的結構  -x, --regex                 Regular expression for ‘db.table‘ matching  -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              Do not execute the temporary shared read lock.  WARNING: This will cause inconsistent backups  不加共用鎖定,會引起資料不一致  --less-locking              Minimize locking time on InnoDB tables.  -l, --long-query-guard      Set long query timer in seconds, default 60  設定最長查詢時間,預設60s,防止鎖  -K, --kill-long-queries     Kill long running queries (instead of aborting)  kill掉長查詢  -D, --daemon                Enable daemon mode  使用守護進程模式  -I, --snapshot-interval     Interval between each dump snapshot (in minutes), requires --daemon, default 60  -L, --logfile               Log file name to use, by default stdout is used  --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            Use savepoints to reduce metadata locking issues, needs SUPER privilege  --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                  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  預設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
常用mydumper備份命令備份所有資料庫

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --outputdir=/backup/mydumper --compress --verbose=3 --logfile=/backup/mydumper/mydumper.log

備份指定庫指定表 -b

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql))‘ -B db1 -T t1,t2 --outputdir=/backup/mydumper --compress --verbose=3 --logfile=/backup/mydumper/mydumper.log

不帶表結構備份表-只有資料,只有dbname.tabname.sql -m

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql))‘ -B db1 -m --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log

不帶表資料備份表-只有表結構,只有dbname-schema-create.sql和dbname.tabname-schema.sql -d

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql))‘ -B db1 -d --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log

遠程壓縮及使用壓縮協議 -c

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql))‘ -B db1 -d --outputdir=/backup/mydumper -c -C tar --verbose=3 --logfile=/backup/mydumper/mydumper.log

匹配多張表備份

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql))‘ -B db1 --regex=db* --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log

過濾多個庫

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql|db2))‘ --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log

即使是空表,也備份結構 -e

mydumper --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --regex ‘^(?!(mysql))‘ -B db1 -e --outputdir=/backup/mydumper --verbose=3 --logfile=/backup/mydumper/mydumper.log

-rw-r--r--. 1 root root 0 Feb 23 11:48 db1.t3.sql

myloader恢複myloader命令參數
[[email protected] mydumper]# myloader --helpUsage:  myloader [OPTION...] multi-threaded MySQL loaderHelp Options:  -?, --help                        Show help optionsApplication Options:  -d, --directory                   Directory of the dump to import  -q, --queries-per-transaction     Number of queries per transaction, default 1000  -o, --overwrite-tables            Drop tables if they already exist  -B, --database                    An alternative database to restore into  -s, --source-db                   Database to restore  -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
單庫備份與恢複
  • 備份一個庫
    mydumper --user=root --password=‘123456‘ --host=172.16.2.154 --socket=/data/my3306/run/mysql.sock -B db1 --outputdir=/backup/mydumper --compress -C --verbose=3 --logfile=/backup/mydumper/mydumper.log
  • 恢複一個庫到原來的執行個體
    myloader --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -s db1 -B db11
[[email protected] mydumper]# myloader --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper  --verbose=3 -B db1 -o** Message: 4 threads created** Message: Dropping table or view (if exists) `db1`.`t1`** Message: Creating table `db1`.`t1`** Message: Dropping table or view (if exists) `db1`.`t2`** Message: Creating table `db1`.`t2`** Message: Dropping table or view (if exists) `db1`.`t3`** Message: Creating table `db1`.`t3`** Message: Thread 2 restoring `db1`.`t2` part 0** Message: Thread 1 restoring `db1`.`t1` part 0** Message: Thread 4 shutting down** Message: Thread 3 shutting down** Message: Thread 2 shutting down** Message: Thread 1 shutting down
  • 恢複一個庫到其他執行個體
    myloader --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -B db11
從全備恢複一個庫
  • 備份全庫
    mydumper --user=root --password=‘123456‘ --host=172.16.2.154 --socket=/data/my3306/run/mysql.sock --outputdir=/backup/mydumper --compress -C --verbose=3 --logfile=/backup/mydumper/mydumper.log
  • 恢複指定庫
    myloader --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -s db3
  • 恢複指定庫並改名
    myloader --user=root --password=‘123456‘ --host=127.0.0.1 --socket=/data/my3306/run/mysql.sock --directory=/backup/mydumper --verbose=3 -s db3 -B db33

    以上兩條如果同時匯入同一個執行個體,後匯入的庫會報錯

    mysql> select * from t1;ERROR 1412 (HY000): Table definition has changed, please retry transaction
從全備恢複一張表
mysql> drop table t1;Query OK, 0 rows affected (0.02 sec)mysql> source /backup/mydumper/db3.t1-schema.sqlQuery OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.05 sec)mysql> source /backup/mydumper/db3.t1.sqlQuery OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 0 rows affected (0.00 sec)Query OK, 4 rows affected (0.00 sec)Records: 4  Duplicates: 0  Warnings: 0mysql> select * from t1;+------+-------+-------+| id   | name1 | name2 |+------+-------+-------+|    1 | yzw1  | yzw11 ||    2 | yzw2  | yzw22 ||    3 | yzw3  | yzw33 ||    4 | yzw4  | yzw44 |+------+-------+-------+4 rows in set (0.00 sec)

mysql小白系列_06 備份與恢複 - mydumper與myloader

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.