1. 程式人生 > >Greeplum 系列(六) 備份與恢復

Greeplum 系列(六) 備份與恢復

移除 cat eat 適合 主機 pdb SQ 簡單 mail

Greeplum 系列(六) 備份與恢復

一、備份

1.1 並行備份(gp_dump)

GP 同時備份 Master 和所有活動的 Segment 實例,備份消耗的時間與系統中實例的數量沒有關系。在 Master 主機上備份所有 DDL 文件和 GP 相關的數據字典表,每個 Segment 備份各自的數據。所有備份文件組成一個完整的備份集合,通過唯一 14 位數字的時間戳來識別。

gp_dump dbname;

gp_dump 命令將在數據目錄生成如下的備份文件:

-- 在 Master 主機上
gp_catalog_1_<dbid>_<timestamp>     -- 數據字典表
gp_cdatabase_1_<dbid>_<timestamp>   -- 創建數據庫 SQL 語句
gp_dump_1_<dbid>_<timestamp>        -- 創建 schema SQL 語句
gp_dump_1_<dbid>_<timestamp>_post_data  -- 創建 Table SQL 語句

-- 在 Segment 主機上
gp_dump_0_ <dbid>_<timestamp>           -- 用戶數據文件
gp_dump_status_0_ <dbid>_<timestamp>    -- 日誌文件

1.2 並行備份(gpcrondump)

在 Master 和 Segment 的數據目錄創建備份文件:

-- Segment 數據的備份使用 gzip 壓縮格式
<data_directory>/db_dumps/YYYYMMDD

使用 CRON 調度備份操作,定義一個調用 gpcrondump 的 crontab 條目。

-- 例如,在午夜1點備份 testdw 數據庫
0 1 0 * * * gpadmin source $GPHOME/greenplum_path.sh; 
gpcrondump –x testdw –c –g –G –a –q >> gp_testdwdump.log;

-- 創建一個名為 mail_contacts 的文件放置在 GP SUPERUSER 的根目錄。
vi /home/gpadmin/mail_contacts -- mail_contacts 放入郵件地址 [email protected]

1.3 非並行備份(pg_dump)

GP 依然支持常規的 PostgreSQL 備份命令 pg_dump 和 pg_dumpall,備份將在 Master 主機上創建一個包含所有 Segment 數據的大的備份文件。因此,不適合於全部數據備份,適用於小部分數據的遷移或備份。

pg_dump testdw > testdw.sql;        -- 導出 SQL 腳本文件
pg_dump –Ft –gp-syntax testdw > testdw.tar;         -- 導出包含分布鍵信息的 tar 文件
pg_dump –Fc testdw > testdw.dump; -- 導出到定制格式的歸檔文件 pg_dump –t tb_cp_02 testdw > tb_cp_02_testdw.sql; -- 導出單個表 pg_dump –t ‘"MixedTableName"‘ testdw > tab_testdw.sql; -- 導出混合大小寫名稱的表 pg_dumpall > all.dump; -- 集群備份

二、恢復

2.1 並行恢復(gp_restore)

通過 gp_dump 產生的時間戳來辨識備份集合,恢復數據庫對象和數據到分布式數據庫中,每個 Segment 並行恢復各自的數據。被恢復的 GP 系統必須與備份的系統同構,否則只能使用串行恢復。

如果在備份時使用了參數:-s(僅模式),-a(僅數據),--gp-c(壓縮),--gp-d(修改備份文件目錄),那麽在恢復時也要指定這些參數。

gp_restore 命令將執行如下操作

(1) 在 Master 主機上

  1. 運行由 gp_dump 生成的 gp_dump_1_
  2. 在 Master 數據目錄生成日誌文件,日誌文件的名稱為:gp_restore_status_1_
  3. gp_restore 在每個需要恢復的 Instance 上啟動一個名為 gp_restore_agent 的程序,gp_restore_agent 進程在 Segment 主機上運行並向 Master 主機上的 gp_restore 進程報告狀態

(2) 在 Segment 主機上

  1. 每個 Instance 使用 gp_dump 生成的 gp_dump_1
  2. 每個 Instance 生成一個日誌文件,名字為:gp_restore_status_1_

恢復數據庫

createdb testdw;
-- 在Master主機,運行gp_restore命令(--gp-k 指定備份操作時間戳標識符,-d 指定恢復的數據庫)
gp_restore –gp-k=20131231001327 –d testdw;

2.2 並行恢復(gpdbrestore)

gpdbrestore 命令是對 gp_restore 命令的包裝,提供更靈活的選項,使用 gpcrondump 備份生成的備份文件來進行恢復。

createdb testdw;        -- 創建需要被恢復的數據庫
gpdbrestore –b 20131231;
-- 在 Master 主機上執行 gpdbrestore 命令(-R 指定備份文件所在的主機名和路徑)
gpdbrestore –R archive_host:/gpdb/backups/archive/20131231;

2.3 非並行恢復(pg_restore)

使用由 pg_dump 或 pg_dumpall 創建的備份文件來恢復,使用非並行恢復可以實現異構系統恢復。

-- 使用 pg_restore 或 psql 進行恢復
pg_restore –d testdw testdw.dump;
psql -d testdw –f tb_cp_02_testdw.sql;

在決定使用恢復程序時,需確定以下幾個問題:

  1. 備份文件在哪裏?

    如果備份文件位於 gp_dump 生成的原始位置,可以簡單的通過 gp_restore 命令恢復;如果備份文件已經移除 GP 集群,使用 gpdbrestore 來恢復

  2. 是否需要恢復整個系統,還是只恢復數據?

    如果 GP 仍在運行並僅需要恢復數據,使用 gp_restore 或 gpdbrestore 命令來恢復;如果丟失了整個集群或者需要從備份來重建整個集群,使用 gpinitsystem 命令

  3. 是否恢復的系統與備份時的系統具有相同數量的 Instance?

    如果相同,使用 gp_restore 或 gpdbrestore 命令來恢復; 如果是在不同集群間遷移,必須使用非並行恢復。

2.4 非並行恢復異構系統

確保具備了全部的備份文件,包括 Master 和每一個 Segment 的文件,所有的文件具有相同的時間戳標識符

-- 1. 創建需要恢復的數據庫
createdb testdw;
-- 2. 裝載 Master 備份文件以恢復數據庫對象
psql -d testdw -f /data/backups/gp_dump_1_1_20131231001327;
-- 3. 裝載每個 Segment 的備份文件以恢復數據
psql –d testdw -f /data/backups/gp_dump_0_2_20131231001327;
psql –d testdw -f /data/backups/gp_dump_0_3_20131231001327;
-- 4. 恢復 Table 相關的對象,比如索引、觸發器、主鍵約束等
psql –d testdw -f /data/backups/ gp_dump_1_1_20131231001327_post_data;

每天用心記錄一點點。內容也許不重要,但習慣很重要!

Greeplum 系列(六) 備份與恢復