MysqlDump根據萬用字元批量匯出

壹頁書發表於2014-08-28
今天又遇到一個新的問題
原來有一個資料庫A,要將資料庫A以space開頭和tmp開頭的表移動到本例項的B資料庫。
A資料庫的表
  1. create table tmp_a (a int);
  2. create table tmp_b (a int);
  3. create table tmp_c (a int);
  4. create table tmp_d (a int);
  5. create table tmp_e (a int);
  6. create table tmp_f (a int);
  7. create table space_a (a int);
  8. create table space_b (a int);
  9. create table space_c (a int);
  10. create table space_d (a int);
  11. create table space_e (a int);
  12. create table space_f (a int);
  13. create table a (a int);
  14. create table b (a int);
  15. create table c (a int);
  16. create table d (a int);
  17. create table e (a int);


我想幹脆用mysqldump匯出,再匯入B資料庫。
但是mysqldump不支援萬用字元批量匯出。

檢視以tmp和space開頭的表

  1. mysql -uxx -pxx -e "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'";


只是查出來還不行,還要去除分割符等

  1. mysql -uxx -pxx -Da -Bse "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'";


將上面的結果作為一個Shell的變數傳給mysqldump進行批量匯出

  1. mysqldump -uxx -pxx a $(mysql -uxx -pxx -Da -Bse "show tables from a where tables_in_a like 'tmp_%' or tables_in_a like 'space_%'") > a.sql
最後將a.sql匯入B資料庫即可。

其中show tables 需要返回多個條件的記錄,需要使用以下的方式
  1. SHOW TABLES
  2. FROM ``
  3. WHERE
  4.     `Tables_in_` LIKE '%cms%'
  5.     OR `Tables_in_` LIKE '%role%';
參考:
http://stackoverflow.com/questions/5609620/show-tables-statement-with-multiple-like-values

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29254281/viewspace-1259796/,如需轉載,請註明出處,否則將追究法律責任。

相關文章