聯繫:手機/微信(+86 17813235971) QQ(107644445)
標題:PostgreSQL恢復系列:pg_filedump批量處理
作者:惜分飛©版權所有[未經本人同意,不得以任何形式轉載,否則有進一步追究法律責任的權利.]
pg_filedump工具使用起來比較麻煩,主要存在問題:
1. 需要人工一個個枚舉各個列類型無法實現批量恢復,參考以前寫的PostgreSQL恢復系列:pg_filedump基本使用
2. 特別是在pg庫無法正常運行的情況下,如果沒有業務提供表創建語句,恢復基本上無法正常進行.
基於這兩個問題,在以前的文章中寫過PostgreSQL恢復系列:pg_filedump恢復字典構造,爲了解決上述的兩個,弄了一個pg_filedump_batch腳本實現批量恢復需求
在測試的pg庫中創建了一些測試表,並查看部分表數據,便於對比後續恢復效果
postgres=# \d List of relations Schema | Name | Type | Owner --------+----------------+-------+---------- public | t_tbs | table | postgres public | t_xff | table | postgres public | t_xff2 | table | postgres public | t_xff3 | table | postgres public | t_xff4 | table | postgres public | t_xifenfei | table | postgres public | tab_attribute | table | postgres public | tab_class | table | postgres public | tab_database | table | postgres public | tab_namespace | table | postgres public | tab_tablespace | table | postgres public | tab_type | table | postgres (12 rows ) postgres=# select * from tab_database; oid | datname | datdba | encoding | datcollate | datctype | datistemplate | datallowconn | datconnlimit | datlastsysoi d | datfrozenxid | datminmxid | dattablespace -------+-------------+--------+----------+-------------+-------------+---------------+--------------+--------------+------------- --+--------------+------------+--------------- 14187 | postgres | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | f | t | -1 | 1418 6 | 479 | 1 | 1663 16403 | db_xff | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | f | t | -1 | 1418 6 | 479 | 1 | 1663 1 | template1 | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | t | t | -1 | 1418 6 | 479 | 1 | 1663 14186 | template0 | 10 | 6 | en_US.UTF-8 | en_US.UTF-8 | t | f | -1 | 1418 6 | 479 | 1 | 1663 16407 | db_xifenfei | 16405 | 6 | en_US.UTF-8 | en_US.UTF-8 | f | t | -1 | 1418 6 | 479 | 1 | 16406 (5 rows ) postgres=# select count (1) from tab_class; count ------- 407 (1 row) postgres=# select * from pg_tablespace; oid | spcname | spcowner | spcacl | spcoptions -------+--------------+----------+--------+------------ 1663 | pg_default | 10 | | 1664 | pg_global | 10 | | 16406 | tbs_xifenfei | 16405 | | (3 rows ) |
使用pg_filedump_bath腳本來實現批量恢復
[root@xifenfei tmp] # ./pg_filedump_batch recover --database-oid=14187 \ --output-directory= /data/recovery --pgdata= /var/lib/pgsql/12/data Recover tables in database with oid: 14187 LOG: starting to process table tab_attribute LOG: starting to process table tab_class LOG: starting to process table tab_database LOG: starting to process table tab_namespace LOG: starting to process table tab_tablespace LOG: starting to process table tab_type LOG: starting to process table t_tbs LOG: starting to process table t_xff LOG: starting to process table t_xff2 LOG: starting to process table t_xff3 LOG: starting to process table t_xff4 LOG: starting to process table t_xifenfei Check dumps in /data/recovery |
參考數據恢復
[root@xifenfei tmp] # cd /data/recovery/ [root@xifenfei recovery] # ls -ltr total 156 -rw-r--r-- 1 root root 82797 Apr 18 20:35 recovered-14187-tab_attribute.csv -rw-r--r-- 1 root root 31129 Apr 18 20:35 recovered-14187-tab_class.csv -rw-r--r-- 1 root root 343 Apr 18 20:35 recovered-14187-tab_database.csv -rw-r--r-- 1 root root 118 Apr 18 20:35 recovered-14187-tab_namespace.csv -rw-r--r-- 1 root root 50 Apr 18 20:35 recovered-14187-tab_tablespace.csv -rw-r--r-- 1 root root 7907 Apr 18 20:35 recovered-14187-tab_type.csv -rw-r--r-- 1 root root 0 Apr 18 20:35 recovered-14187-t_tbs.csv -rw-r--r-- 1 root root 38 Apr 18 20:35 recovered-14187-t_xff.csv -rw-r--r-- 1 root root 38 Apr 18 20:35 recovered-14187-t_xff2.csv -rw-r--r-- 1 root root 38 Apr 18 20:35 recovered-14187-t_xff3.csv -rw-r--r-- 1 root root 38 Apr 18 20:35 recovered-14187-t_xff4.csv -rw-r--r-- 1 root root 38 Apr 18 20:35 recovered-14187-t_xifenfei.csv [root@xifenfei recovery] # cat recovered-14187-tab_database.csv 14187 postgres 10 6 en_US.UTF-8 en_US.UTF-8 f t -1 14186 479 1 1663 16403 db_xff 10 6 en_US.UTF-8 en_US.UTF-8 f t -1 14186 479 1 1663 1 template1 10 6 en_US.UTF-8 en_US.UTF-8 t t -1 14186 479 1 1663 14186 template0 10 6 en_US.UTF-8 en_US.UTF-8 t f -1 14186 479 1 1663 16407 db_xifenfei 16405 6 en_US.UTF-8 en_US.UTF-8 f t -1 14186 479 1 16406 [root@xifenfei recovery] # cat recovered-14187-tab_class.csv|wc -l 407 [root@xifenfei recovery] # cat recovered-14187-tab_tablespace.csv 1663 pg_default 1664 pg_global 16406 tbs_xifenfei |
把pg_class恢復數據導入庫中進行對比,證明恢復的數據完全正確
postgres=# COPY tab_class_new FROM '/data/recovery/recovered-14187-tab_class.csv' ; COPY 407 postgres=# select count (1) from tab_class; count ------- 407 (1 row) count ------- 407 (1 row) postgres=# select count (1) from tab_class_new; count ------- 407 (1 row) postgres=# select * from tab_class_new postgres-# EXCEPT postgres-# select * from tab_class; oid | relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | rel allvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind -----+---------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---- -----------+---------------+-------------+-------------+----------------+--------- (0 rows ) postgres=# select * from tab_class postgres-# EXCEPT postgres-# select * from tab_class_new; oid | relname | relnamespace | reltype | reloftype | relowner | relam | relfilenode | reltablespace | relpages | reltuples | rel allvisible | reltoastrelid | relhasindex | relisshared | relpersistence | relkind -----+---------+--------------+---------+-----------+----------+-------+-------------+---------------+----------+-----------+---- -----------+---------------+-------------+-------------+----------------+--------- (0 rows ) |
通過上述操作證明:
1. 在沒有人工列出列類型的情況下實現批量pg_filedump恢復功能
2. 在pg庫沒有啓動的情況下直接解析字典實現恢復功能
3. 實現pg數據庫的批量恢復
如果有PostgreSQL的數據庫故障,自行無法解決,請聯繫我們提供專業數據庫恢復技術支持:
電話/微信:17813235971 Q Q:107644445 E-Mail:[email protected]