邏輯備份在恢復(fù)時(shí),介于邏輯備份與故障時(shí)間點(diǎn)之間的數(shù)據(jù)難以恢復(fù),故一般不采取邏輯備份方式進(jìn)行數(shù)據(jù)庫(kù)備份,但邏輯適用于跨平臺(tái)跨版本的數(shù)據(jù)遷移;
邏輯備份恢復(fù)主要以下三種:
pg_dump
pg_dumpall
copy
本小節(jié)主要講解pg_dump
pg_dump備份
只能備份單個(gè)數(shù)據(jù)庫(kù),不會(huì)導(dǎo)出角色和表空間相關(guān)的信息
-F c 備份為二進(jìn)制格式,壓縮存儲(chǔ).并且可被pg_restore用于精細(xì)還原
-F p 備份為文本,大庫(kù)不推薦
pg_dump恢復(fù)
psql dbname -U username bakfile
或
pg_restore
– pg_restore [option] ... [filename]
– pg_restore -d dbname bakfile
二進(jìn)制格式的備份只能使用pg_restore來(lái)還原,可以指定還原的表,編輯TOC文件,定制還原的順序,表, 索引等。
文本格式的備份還原, 直接使用用戶連接到對(duì)應(yīng)的數(shù)據(jù)庫(kù)執(zhí)行備份文本即可,例如psql dbname -f bak.sql
pg_dump備份恢復(fù)示例
1)創(chuàng)建數(shù)據(jù)庫(kù)
createdb testdb
2)連入數(shù)據(jù)庫(kù)testdb
psql testdb
3)創(chuàng)建測(cè)試表,插入數(shù)據(jù)
testdb=# create table tt(a int) tablespace tbls_t;
testdb=# insert into tt(a) values(1);
testdb=# insert into tt(a) values(2);
4)查看數(shù)據(jù)
testdb=# select * from tt;
5)備份
pg_dump testdb>/dbbak/testdb.sql #簡(jiǎn)單語(yǔ)法,可結(jié)合選項(xiàng)靈活備份
6)刪除數(shù)據(jù)庫(kù)testdb
dropdb testdb
7)創(chuàng)建新數(shù)據(jù)庫(kù)(恢復(fù)之前需創(chuàng)建數(shù)據(jù)庫(kù))
createdb testdb
8)恢復(fù)數(shù)據(jù)
psql testdb /dbbak/testdb.sql
9)查看數(shù)據(jù)是否回復(fù)
psql testdb
testdb=# select * from tt;
至此,數(shù)據(jù)已成功恢復(fù)!
pg_restore -d postgres /dbbak/pgdumpbak/p.dmp
pg_dump備份恢復(fù)命令擴(kuò)展練習(xí)
pg_dump -F c -f /dbbak/pgdumpbak/c.dmp -C -E UTF8 -h 127.0.0.1 -U postgres testdb #二進(jìn)制格式備份文件
pg_dump -F p -f /dbbak/pgdumpbak/p.dmp -C -E UTF8 -h 127.0.0.1 -U postgres testdb #文本格式備份文件,”-C” 表示包含創(chuàng)建語(yǔ)句
pg_restore /dbbak/c.dmp|less 可以解析二進(jìn)制格式的備份文件
pg_restore -l /dbbak/c.dmp
pg_restore -d testdb /dbbak/pgdumpbak/c.dmp #需要先創(chuàng)建目標(biāo)庫(kù)
pg_restore -d postgres /dbbak/pgdumpbak/p.dmp #文件中包含創(chuàng)建數(shù)據(jù)庫(kù)的命令,不需要?jiǎng)?chuàng)建目標(biāo)庫(kù)
toc文件選擇性備份恢復(fù)
1)根據(jù)二進(jìn)制備份文件生成toc文件
pg_restore -l -f /dbbak/pgdumpbak/toc /dbbak/pgdumpbak/c.dmp
2)修改 toc文件,以首行加分號(hào)“;”的方式注釋掉不用還原的內(nèi)容
3)以toc文件列表做恢復(fù)
pg_restore -F c -L /dbbak/pgdumpbak/toc -d testdb /dbbak/pgdumpbak/c.dmp
補(bǔ)充:Postgresql備份與還原命令pg_dump
postgresql數(shù)據(jù)庫(kù)的備份和還原命令pg_dump
常用命令:
備份:
pg_dump -U postgres -d myDBname -f dump.sql
其中
postgres是用戶名
myDBname是數(shù)據(jù)庫(kù)名
dump.sql是文件名
還原:
createdb newDBname
psql -d newDBname -U postgres -f dump.sql
其中
postgres是用戶名
newDBname是數(shù)據(jù)庫(kù)名
dump.sql是文件名
參考:
pg_dump 把一個(gè)數(shù)據(jù)庫(kù)轉(zhuǎn)儲(chǔ)為純文本文件或者是其它格式.
用法:
pg_dump [選項(xiàng)]... [數(shù)據(jù)庫(kù)名字]
一般選項(xiàng):
-f, --file=FILENAME 輸出文件或目錄名
-F, --format=c|d|t|p 輸出文件格式 (定制, 目錄, tar)
明文 (默認(rèn)值))
-j, --jobs=NUM 執(zhí)行多個(gè)并行任務(wù)進(jìn)行備份轉(zhuǎn)儲(chǔ)工作
-v, --verbose 詳細(xì)模式
-V, --version 輸出版本信息,然后退出
-Z, --compress=0-9 被壓縮格式的壓縮級(jí)別
--lock-wait-timeout=TIMEOUT 在等待表鎖超時(shí)后操作失敗
-?, --help 顯示此幫助, 然后退出
控制輸出內(nèi)容選項(xiàng):
-a, --data-only 只轉(zhuǎn)儲(chǔ)數(shù)據(jù),不包括模式
-b, --blobs 在轉(zhuǎn)儲(chǔ)中包括大對(duì)象
-c, --clean 在重新創(chuàng)建之前,先清除(刪除)數(shù)據(jù)庫(kù)對(duì)象
-C, --create 在轉(zhuǎn)儲(chǔ)中包括命令,以便創(chuàng)建數(shù)據(jù)庫(kù)
-E, --encoding=ENCODING 轉(zhuǎn)儲(chǔ)以ENCODING形式編碼的數(shù)據(jù)
-n, --schema=SCHEMA 只轉(zhuǎn)儲(chǔ)指定名稱的模式
-N, --exclude-schema=SCHEMA 不轉(zhuǎn)儲(chǔ)已命名的模式
-o, --oids 在轉(zhuǎn)儲(chǔ)中包括 OID
-O, --no-owner 在明文格式中, 忽略恢復(fù)對(duì)象所屬者
-s, --schema-only 只轉(zhuǎn)儲(chǔ)模式, 不包括數(shù)據(jù)
-S, --superuser=NAME 在明文格式中使用指定的超級(jí)用戶名
-t, --table=TABLE 只轉(zhuǎn)儲(chǔ)指定名稱的表
-T, --exclude-table=TABLE 不轉(zhuǎn)儲(chǔ)指定名稱的表
-x, --no-privileges 不要轉(zhuǎn)儲(chǔ)權(quán)限 (grant/revoke)
--binary-upgrade 只能由升級(jí)工具使用
--column-inserts 以帶有列名的INSERT命令形式轉(zhuǎn)儲(chǔ)數(shù)據(jù)
--disable-dollar-quoting 取消美元 (符號(hào)) 引號(hào), 使用 SQL 標(biāo)準(zhǔn)引號(hào)
--disable-triggers 在只恢復(fù)數(shù)據(jù)的過(guò)程中禁用觸發(fā)器
--enable-row-security 啟用行安全性(只轉(zhuǎn)儲(chǔ)用戶能夠訪問(wèn)的內(nèi)容)
--exclude-table-data=TABLE 不轉(zhuǎn)儲(chǔ)指定名稱的表中的數(shù)據(jù)
--if-exists 當(dāng)刪除對(duì)象時(shí)使用IF EXISTS
--inserts 以INSERT命令,而不是COPY命令的形式轉(zhuǎn)儲(chǔ)數(shù)據(jù)
--no-security-labels 不轉(zhuǎn)儲(chǔ)安全標(biāo)簽的分配
--no-synchronized-snapshots 在并行工作集中不使用同步快照
--no-tablespaces 不轉(zhuǎn)儲(chǔ)表空間分配信息
--no-unlogged-table-data 不轉(zhuǎn)儲(chǔ)沒(méi)有日志的表數(shù)據(jù)
--quote-all-identifiers 所有標(biāo)識(shí)符加引號(hào),即使不是關(guān)鍵字
--section=SECTION 備份命名的節(jié) (數(shù)據(jù)前, 數(shù)據(jù), 及 數(shù)據(jù)后)
--serializable-deferrable 等到備份可以無(wú)異常運(yùn)行
--snapshot=SNAPSHOT 為轉(zhuǎn)儲(chǔ)使用給定的快照
--strict-names 要求每個(gè)表和/或schema包括模式以匹配至少一個(gè)實(shí)體
--use-set-session-authorization
使用 SESSION AUTHORIZATION 命令代替
ALTER OWNER 命令來(lái)設(shè)置所有權(quán)
聯(lián)接選項(xiàng):
-d, --dbname=DBNAME 對(duì)數(shù)據(jù)庫(kù) DBNAME備份
-h, --host=主機(jī)名 數(shù)據(jù)庫(kù)服務(wù)器的主機(jī)名或套接字目錄
-p, --port=端口號(hào) 數(shù)據(jù)庫(kù)服務(wù)器的端口號(hào)
-U, --username=名字 以指定的數(shù)據(jù)庫(kù)用戶聯(lián)接
-w, --no-password 永遠(yuǎn)不提示輸入口令
-W, --password 強(qiáng)制口令提示 (自動(dòng))
--role=ROLENAME 在轉(zhuǎn)儲(chǔ)前運(yùn)行SET ROLE
如果沒(méi)有提供數(shù)據(jù)庫(kù)名字, 那么使用 PGDATABASE 環(huán)境變量
的數(shù)值.
報(bào)告錯(cuò)誤至 pgsql-bugs@postgresql.org>.
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
您可能感興趣的文章:- postgresql 如何查看pg_wal目錄下xlog文件總大小
- postgresql之使用lsn 獲取 wal文件名的實(shí)例
- 修改postgresql存儲(chǔ)目錄的操作方式
- postgresql運(yùn)維之遠(yuǎn)程遷移操作
- postgreSQL自動(dòng)生成隨機(jī)數(shù)值的實(shí)例
- 使用pg_basebackup對(duì)Postgre進(jìn)行備份與恢復(fù)的實(shí)現(xiàn)