最後更新日期:2019 年 08 月 19 日

狀況描述

我有一個資料庫的匯出檔 (db_dump.sql),非常的大,大約有 4.6 GB,存放於遠端的 VPS 主機上( 2 CPU, 4GB Ram),想要取出其中一個資料表 ( organizations )的資料(11萬筆),用於回復受到使用者誤刪的資料。

使用 vi 之類的文書處理器不太現實,因為檔案實在太大。

解決方法

使用 grep

# 注意!organization 前後的 ` 非常重要,如果不加,會取到一些意料之外的資料列
grep '`organizations`' db_dump.sql > organizations.sql

此時 organizations.sql 只剩下 62 MB,就可以用 vi 打開。

這種方式無法取得資料表的結構,所以要 vi 把原來的 DROP TABLE 那行改為 TRUNCATE TABLE,還有其他一些無用的指令、註解刪除,就可以匯入資料庫了。

TRUNCATE TABLE `organizations`;
INSERT INTO `organizations` VALUES(......); 

注意!如果有設定 key 的敘述,記得要加回來!
例如:

alter table organizations change id id int not null auto_increment primary key;

參考資料

  1. 正規表示法與文件格式化處理 -- 鳥哥的 Linux 私房菜
Last modified: 2019-08-19

Author

Comments

Write a Reply or Comment

Your email address will not be published.