偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

數(shù)據(jù)庫(kù)PostgreSQL備份與恢復(fù)工具使用詳解

原創(chuàng) 精選
數(shù)據(jù)庫(kù) PostgreSQL
PostgreSQL 提供了一系列強(qiáng)大的工具來(lái)備份和恢復(fù)數(shù)據(jù)庫(kù)。這些工具包括 pg_dump, pg_restore, pg_dumpall, 和 pg_basebackup。

目錄

概述

pg_dump

pg_dumpall

pg_basebackup

pg_restore

總結(jié)

概述

PostgreSQL 提供了一系列強(qiáng)大的工具來(lái)備份和恢復(fù)數(shù)據(jù)庫(kù)。這些工具包括 pg_dump, pg_restore, pg_dumpall, 和 pg_basebackup。下面是這些工具的簡(jiǎn)要概述和一些注意事項(xiàng):

1. pg_dump

pg_dump 是一個(gè)用于備份單個(gè) PostgreSQL 數(shù)據(jù)庫(kù)的工具。它可以生成 SQL 文件或自包含的歸檔文件,其中包含了創(chuàng)建和填充數(shù)據(jù)庫(kù)所需的所有 SQL 命令。

用途:備份單個(gè)數(shù)據(jù)庫(kù)。

2. pg_dumpall

pg_dumpall 用于備份整個(gè) PostgreSQL 集群,包括所有數(shù)據(jù)庫(kù)、角色、表空間等。

用途:備份整個(gè) PostgreSQL 集群。

3. pg_basebackup

pg_basebackup 用于創(chuàng)建整個(gè) PostgreSQL 數(shù)據(jù)目錄的物理備份,這對(duì)于災(zāi)難恢復(fù)尤為重要。

用途:備份整個(gè) PostgreSQL 數(shù)據(jù)目錄。

4. pg_restore

pg_restore 是一個(gè)用于從 pg_dump 創(chuàng)建的備份文件恢復(fù)數(shù)據(jù)的工具。

用途:恢復(fù)單個(gè)數(shù)據(jù)庫(kù)。

一、pg_dump

pg_dump參數(shù)詳解

# 通用選項(xiàng)
-f, --file=FILENAME # 輸出文件或目錄的名稱。
-F, --format=c|d|t|p # 輸出文件格式(自定義[c]、目錄[d]、tar[t]、純文本[p],默認(rèn)為純文本)。
-j, --jobs=NUM # 使用指定數(shù)量的并行任務(wù)來(lái)執(zhí)行數(shù)據(jù)庫(kù)轉(zhuǎn)儲(chǔ)。
-v, --verbose # 詳細(xì)模式。
-V, --version # 輸出版本信息,然后退出。
-Z, --compress=METHOD[:DETAIL] # 按指定的方式壓縮輸出。
--lock-wait-timeout=TIMEOUT # 在等待表鎖超時(shí)后失敗,超時(shí)時(shí)間為TIMEOUT。
--no-sync # 不等待更改安全地寫入磁盤。
-?, --help # 顯示幫助,然后退出。

# 控制輸出內(nèi)容的選項(xiàng)
-a, --data-only # 只轉(zhuǎn)儲(chǔ)數(shù)據(jù),不轉(zhuǎn)儲(chǔ)模式。
-b, --large-objects # 在轉(zhuǎn)儲(chǔ)中包含大對(duì)象。
--blobs # (與--large-objects相同,已棄用)
-B, --no-large-objects # 在轉(zhuǎn)儲(chǔ)中排除大對(duì)象。
--no-blobs # (與--no-large-objects相同,已棄用)
-c, --clean # 清理(刪除)數(shù)據(jù)庫(kù)對(duì)象后再重新創(chuàng)建。
-C, --create # 在轉(zhuǎn)儲(chǔ)中包含創(chuàng)建數(shù)據(jù)庫(kù)的命令。
-e, --extension=PATTERN # 只轉(zhuǎn)儲(chǔ)指定的擴(kuò)展。
-E, --encoding=ENCODING # 以指定的編碼ENCODING轉(zhuǎn)儲(chǔ)數(shù)據(jù)。
-n, --schema=PATTERN # 只轉(zhuǎn)儲(chǔ)指定的模式。
-N, --exclude-schema=PATTERN # 不轉(zhuǎn)儲(chǔ)指定的模式。
-O, --no-owner # 在純文本格式中跳過(guò)對(duì)象所有權(quán)的恢復(fù)。
-s, --schema-only # 只轉(zhuǎn)儲(chǔ)模式,不轉(zhuǎn)儲(chǔ)數(shù)據(jù)。
-S, --superuser=NAME # 在純文本格式中使用的超級(jí)用戶名。
-t, --table=PATTERN # 只轉(zhuǎn)儲(chǔ)指定的表。
-T, --exclude-table=PATTERN # 不轉(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)引用,使用SQL標(biāo)準(zhǔn)引用。
--disable-triggers # 在僅數(shù)據(jù)恢復(fù)時(shí)禁用觸發(fā)器。
--enable-row-security # 啟用行級(jí)安全性(僅轉(zhuǎn)儲(chǔ)用戶有訪問權(quán)限的內(nèi)容)。
--exclude-table-and-children=PATTERN # 不轉(zhuǎn)儲(chǔ)指定的表及其子表和分區(qū)表。
--exclude-table-data=PATTERN # 不轉(zhuǎn)儲(chǔ)指定表的數(shù)據(jù)。
--exclude-table-data-and-children=PATTERN # 不轉(zhuǎn)儲(chǔ)指定表及其子表和分區(qū)表的數(shù)據(jù)。
--extra-float-digits=NUM # 覆蓋extra_float_digits的默認(rèn)設(shè)置。
--if-exists # 在刪除對(duì)象時(shí)使用IF EXISTS。
--include-foreign-data=PATTERN # 包含與指定模式匹配的外部服務(wù)器上的外部表數(shù)據(jù)。
--inserts # 作為INSERT命令而不是COPY命令轉(zhuǎn)儲(chǔ)數(shù)據(jù)。
--load-via-partition-root # 通過(guò)根表加載分區(qū)。
--no-comments # 不轉(zhuǎn)儲(chǔ)注釋。
--no-publications # 不轉(zhuǎn)儲(chǔ)發(fā)布。
--no-security-labels # 不轉(zhuǎn)儲(chǔ)安全標(biāo)簽分配。
--no-subscriptions # 不轉(zhuǎn)儲(chǔ)訂閱。
--no-table-access-method # 不轉(zhuǎn)儲(chǔ)表訪問方法。
--no-tablespaces # 不轉(zhuǎn)儲(chǔ)表空間分配。
--no-toast-compression # 不轉(zhuǎn)儲(chǔ)TOAST壓縮方法。
--no-unlogged-table-data # 不轉(zhuǎn)儲(chǔ)未登錄表的數(shù)據(jù)。
--on-conflict-do-nothing # 在INSERT命令中添加ON CONFLICT DO NOTHING。
--quote-all-identifiers # 引用所有標(biāo)識(shí)符,即使它們不是關(guān)鍵字。
--rows-per-insert=NROWS # 每個(gè)INSERT命令中的行數(shù);隱含--inserts。
--section=SECTION # 轉(zhuǎn)儲(chǔ)命名的段(預(yù)數(shù)據(jù)、數(shù)據(jù)或后數(shù)據(jù))。
--serializable-deferrable # 等待直到可以無(wú)異常地運(yùn)行轉(zhuǎn)儲(chǔ)。
--snapshot=SNAPSHOT # 使用給定的快照進(jìn)行轉(zhuǎn)儲(chǔ)。
--strict-names # 要求表和/或模式包含模式至少匹配一個(gè)實(shí)體。
--table-and-children=PATTERN # 只轉(zhuǎn)儲(chǔ)指定的表及其子表和分區(qū)表。
--use-set-session-authorization # 使用SET SESSION AUTHORIZATION命令代替ALTER OWNER命令來(lái)設(shè)置所有權(quán)。

# 連接選項(xiàng)
-d, --dbname=DBNAME # 要轉(zhuǎn)儲(chǔ)的數(shù)據(jù)庫(kù)名稱。
-h, --host=HOSTNAME # 數(shù)據(jù)庫(kù)服務(wù)器的主機(jī)名或套接字目錄。
-p, --port=PORT # 數(shù)據(jù)庫(kù)服務(wù)器的端口號(hào)。
-U, --username=NAME # 以指定的數(shù)據(jù)庫(kù)用戶連接。
-w, --no-password # 永遠(yuǎn)不要提示輸入密碼。
-W, --password # 強(qiáng)制提示輸入密碼(應(yīng)自動(dòng)發(fā)生)。
--role=ROLENAME # 在轉(zhuǎn)儲(chǔ)前執(zhí)行SET ROLE。

# 如果沒有提供數(shù)據(jù)庫(kù)名稱,則使用PGDATABASE環(huán)境變量的值。

pg_dump使用示例

要將數(shù)據(jù)庫(kù)轉(zhuǎn)儲(chǔ)到自定義格式的存檔文件中,請(qǐng)執(zhí)行以下操作:

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -Fc -d mydb > db.dump

要將數(shù)據(jù)庫(kù)轉(zhuǎn)儲(chǔ)到目錄格式存檔中,請(qǐng)執(zhí)行以下操作:

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -Fd -d mydb -f dumpdir

要將數(shù)據(jù)庫(kù)轉(zhuǎn)儲(chǔ)到目錄格式存檔中,同時(shí)執(zhí)行 5 個(gè)工作線程作業(yè):

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -Fd -d mydb -j 5 -f dumpdir

轉(zhuǎn)儲(chǔ)名為:mytab

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -d mydb > mytab.sql

轉(zhuǎn)儲(chǔ)以emp開頭的所有表,排除名為 :empdetroitemployee_log集合

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -t 'detroit.emp*' -T detroit.employee_log mydb > db.sql

轉(zhuǎn)儲(chǔ)名稱以 east 開頭且以 gsm結(jié)尾的所有集合,排除名稱包含單詞test : 的集合

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -n 'east*gsm' -N '*test*' mydb > db.sql

同樣,使用正則表達(dá)式表示法來(lái)合并開關(guān):

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -n '(east|west)*gsm' -N '*test*' mydb > db.sql

轉(zhuǎn)儲(chǔ)除名稱以ts_*開頭的集合之外的所有集合

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -T 'ts_*' mydb > db.sql

轉(zhuǎn)儲(chǔ)具有混合大小寫名稱的單個(gè)表,您需要類似-t

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -t "\"MixedCaseName\"" mydb > mytab.sql

備份數(shù)據(jù)庫(kù)結(jié)構(gòu)(不包含數(shù)據(jù))

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W --schema-only mydatabase > mydatabase_schema_only.sql

備份數(shù)據(jù)庫(kù)數(shù)據(jù)(不包含結(jié)構(gòu))

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W --data-only mydatabase > mydatabase_schema_only.sql

備份多個(gè)特定表

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -t table1 -t table2 mydatabase > tables_backup.sql

備份數(shù)據(jù)庫(kù)并壓縮

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W -d mydb | gzip > mydatabase_backup.sql.gz

備份并導(dǎo)出為 INSERT 語(yǔ)句

pg_dump -U postgres -h 127.0.0.1 -p 5432 -W --format=c --large-objects --inserts mydatabase > insert_statements.sql
pg_dump -U postgres -h 127.0.0.1 -p 5432 -W --schema-only mydatabase > all_schemas_structure.sql

二、pg_dumpall

pg_dumpall參數(shù)詳解

# 使用語(yǔ)法
pg_dumpall [OPTION]...

# 通用選項(xiàng)
-f, --file=FILENAME # 輸出文件名
-v, --verbose # 詳細(xì)模式
-V, --version # 輸出版本信息,然后退出
--lock-wait-timeout=TIMEOUT # 鎖等待超時(shí)后失敗,單位為毫秒
-?, --help # 顯示此幫助,然后退出

# 控制輸出內(nèi)容的選項(xiàng)
-a, --data-only # 僅轉(zhuǎn)儲(chǔ)數(shù)據(jù),不轉(zhuǎn)儲(chǔ)模式
-c, --clean # 清除(刪除)數(shù)據(jù)庫(kù)后再重新創(chuàng)建
-E, --encoding=ENCODING # 以編碼ENCODING轉(zhuǎn)儲(chǔ)數(shù)據(jù)
-g, --globals-only # 僅轉(zhuǎn)儲(chǔ)全局對(duì)象,不包括數(shù)據(jù)庫(kù)
-O, --no-owner # 跳過(guò)對(duì)象所有權(quán)的恢復(fù)
-r, --roles-only # 僅轉(zhuǎn)儲(chǔ)角色,不包括數(shù)據(jù)庫(kù)或表空間
-s, --schema-only # 僅轉(zhuǎn)儲(chǔ)模式,不包括數(shù)據(jù)
-S, --superuser=NAME # 超級(jí)用戶用戶名,用于轉(zhuǎn)儲(chǔ)中
-t, --tablespaces-only # 僅轉(zhuǎn)儲(chǔ)表空間,不包括數(shù)據(jù)庫(kù)或角色
-x, --no-privileges # 不轉(zhuǎn)儲(chǔ)權(quán)限(授予/撤銷)
--binary-upgrade # 僅供升級(jí)工具使用
--column-inserts # 將數(shù)據(jù)轉(zhuǎn)儲(chǔ)為帶有列名的INSERT命令
--disable-dollar-quoting # 禁用美元符引用,使用SQL標(biāo)準(zhǔn)引用
--disable-triggers # 在僅數(shù)據(jù)恢復(fù)時(shí)禁用觸發(fā)器
--exclude-database=PATTERN # 排除名稱匹配PATTERN的數(shù)據(jù)庫(kù)
--extra-float-digits=NUM # 覆蓋默認(rèn)的extra_float_digits設(shè)置
--if-exists # 刪除對(duì)象時(shí)使用IF EXISTS
--inserts # 將數(shù)據(jù)轉(zhuǎn)儲(chǔ)為INSERT命令,而非COPY命令
--load-via-partition-root # 通過(guò)根表加載分區(qū)
--no-comments # 不轉(zhuǎn)儲(chǔ)注釋
--no-publications # 不轉(zhuǎn)儲(chǔ)發(fā)布
--no-role-passwords # 不轉(zhuǎn)儲(chǔ)角色密碼
--no-security-labels # 不轉(zhuǎn)儲(chǔ)安全標(biāo)簽分配
--no-subscriptions # 不轉(zhuǎn)儲(chǔ)訂閱
--no-sync # 不等待變更被安全地寫入磁盤
--no-table-access-method # 不轉(zhuǎn)儲(chǔ)表訪問方法
--no-tablespaces # 不轉(zhuǎn)儲(chǔ)表空間分配
--no-toast-compression # 不轉(zhuǎn)儲(chǔ)TOAST壓縮方法
--no-unlogged-table-data # 不轉(zhuǎn)儲(chǔ)未登錄表的數(shù)據(jù)
--on-conflict-do-nothing # 在INSERT命令中添加ON CONFLICT DO NOTHING
--quote-all-identifiers # 引用所有標(biāo)識(shí)符,即使它們不是關(guān)鍵字
--rows-per-insert=NROWS # 每個(gè)INSERT語(yǔ)句中的行數(shù);隱含--inserts
--use-set-session-authorization

pg_dumpall使用示例

#備份整個(gè)數(shù)據(jù)庫(kù)以及角色
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W -f alldb_backup.sql
#備份所有數(shù)據(jù)庫(kù)角色和全局對(duì)象
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W --globals-only -f globals_backup.sql
#僅備份角色
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W --roles-only -f alldb_backup.sql
#僅備份數(shù)據(jù)
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W --data-only -f alldb_backup.sql
#忽略某個(gè)庫(kù)
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W -f alldb_backup.sql --exclude-database=mydb > alldb_backup.sql
#將數(shù)據(jù)轉(zhuǎn)儲(chǔ)為帶有列名的INSERT命令
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W --column-inserts -f alldb_backup.sql
#將數(shù)據(jù)轉(zhuǎn)儲(chǔ)為INSERT命令
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -W --inserts -f alldb_backup.sql
#備份壓縮
pg_dumpall -U postgres -h 127.0.0.1 -p 5432 -w --inserts | gzip > alldb_backup.sql.gz

三、pg_basebackup

pg_basebackup參數(shù)詳解

# 使用語(yǔ)法
pg_basebackup [OPTION]...

# 控制輸出的選項(xiàng)
-D, --pgdata=DIRECTORY # 將基線備份接收至指定目錄
-F, --format=p|t # 輸出格式(plain(默認(rèn)),tar)
-r, --max-rate=RATE # 設(shè)置數(shù)據(jù)目錄傳輸?shù)淖畲笏俾剩▎挝粸閗B/s,或使用"k"或"M"后綴)
-R, --write-recovery-conf # 寫入用于復(fù)制的配置文件
-t, --target=TARGET[:DETAIL]
 # 備份目標(biāo)(如果不同于客戶端)
-T, --tablespace-mapping=OLDDIR=NEWDIR
 # 將位于OLDDIR的表空間重定位至NEWDIR
--waldir=WALDIR # 寫前日志(WAL)目錄的位置
-X, --wal-method=none|fetch|stream
 # 使用指定的方法包含所需的WAL文件
-z, --gzip # 壓縮tar輸出
-Z, --compress=[{client|server}-]METHOD[:DETAIL]
 # 按指定方式在客戶端或服務(wù)器上進(jìn)行壓縮
-Z, --compress=none # 不壓縮tar輸出

# 通用選項(xiàng)
-c, --checkpoint=fast|spread # 設(shè)置快速或分散的檢查點(diǎn)
-C, --create-slot # 創(chuàng)建復(fù)制槽
-l, --label=LABEL # 設(shè)置備份標(biāo)簽
-n, --no-clean # 出現(xiàn)錯(cuò)誤后不清理
-N, --no-sync # 不等待更改被安全地寫入磁盤
-P, --progress # 顯示進(jìn)度信息
-S, --slot=SLOTNAME # 使用的復(fù)制槽
-v, --verbose # 輸出詳細(xì)信息
-V, --version # 輸出版本信息,然后退出
--manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE
 # 使用算法進(jìn)行清單校驗(yàn)和
--manifest-force-encode # 對(duì)清單中的所有文件名進(jìn)行十六進(jìn)制編碼
--no-estimate-size # 不在服務(wù)器端估計(jì)備份大小
--no-manifest # 抑制生成備份清單
--no-slot # 阻止創(chuàng)建臨時(shí)復(fù)制槽
--no-verify-checksums # 不驗(yàn)證校驗(yàn)和
-?, --help # 顯示此幫助,然后退出

# 連接選項(xiàng)
-d, --dbname=CONNSTR # 連接字符串
-h, --host=HOSTNAME # 數(shù)據(jù)庫(kù)服務(wù)器主機(jī)名或套接字目錄
-p, --port=PORT # 數(shù)據(jù)庫(kù)服務(wù)器端口號(hào)
-s, --status-interval=INTERVAL
 # 發(fā)送至服務(wù)器的狀態(tài)包的時(shí)間間隔(單位為秒)
-U, --username=NAME # 作為指定的數(shù)據(jù)庫(kù)用戶連接
-w, --no-password # 從不提示輸入密碼
-W, --password # 強(qiáng)制密碼提示(應(yīng)自動(dòng)發(fā)生)

pg_basebackup使用示例

普通文件:

pg_basebackup -h 10.10.9.7 -U replication_user -p 5432 -Xs -v -Fp -P -r 100M -R -D /root/zz/

壓縮:

pg_basebackup -h 10.10.9.7 -U replication_user -p 5432 -Xs -v -Ft -P -r 100M -R -D /root/zz/

四、pg_restore

pg_restore參數(shù)詳解

# 使用語(yǔ)法
pg_restore [OPTION]... [FILE]

# 通用選項(xiàng)
-d, --dbname=NAME # 連接到指定的數(shù)據(jù)庫(kù)名
-f, --file=FILENAME # 輸出文件名 (- 表示標(biāo)準(zhǔn)輸出)
-F, --format=c|d|t # 備份文件格式(通常應(yīng)自動(dòng)識(shí)別)
-l, --list # 打印歸檔的TOC(目錄)摘要
-v, --verbose # 詳細(xì)模式
-V, --version # 輸出版本信息,然后退出
-?, --help # 顯示幫助信息,然后退出

# 控制恢復(fù)的選項(xiàng)
-a, --data-only # 只恢復(fù)數(shù)據(jù),不恢復(fù)模式(schema)
-c, --clean # 清理(刪除)數(shù)據(jù)庫(kù)對(duì)象之前重新創(chuàng)建
-C, --create # 創(chuàng)建目標(biāo)數(shù)據(jù)庫(kù)
-e, --exit-on-error # 出錯(cuò)時(shí)退出,默認(rèn)是繼續(xù)執(zhí)行
-I, --index=NAME # 恢復(fù)指定名稱的索引
-j, --jobs=NUM # 使用多個(gè)并行任務(wù)來(lái)恢復(fù)
-L, --use-list=FILENAME # 使用此文件中的TOC選擇/排序輸出
-n, --schema=NAME # 只恢復(fù)此模式(schema)中的對(duì)象
-N, --exclude-schema=NAME # 不恢復(fù)此模式(schema)中的對(duì)象
-O, --no-owner # 跳過(guò)對(duì)象所有權(quán)的恢復(fù)
-P, --function=NAME(args) # 恢復(fù)指定名稱的函數(shù)
-s, --schema-only # 只恢復(fù)模式(schema),不恢復(fù)數(shù)據(jù)
-S, --superuser=NAME # 用于禁用觸發(fā)器的超級(jí)用戶名
-t, --table=NAME # 恢復(fù)指定名稱的關(guān)系(表,視圖等)
-T, --trigger=NAME # 恢復(fù)指定名稱的觸發(fā)器
-x, --no-privileges # 跳過(guò)訪問權(quán)限的恢復(fù)(grant/revoke)
-1, --single-transaction # 作為一個(gè)單一的事務(wù)恢復(fù)
--disable-triggers # 在僅數(shù)據(jù)恢復(fù)期間禁用觸發(fā)器
--enable-row-security # 啟用行級(jí)安全性
--if-exists # 在刪除對(duì)象時(shí)使用IF EXISTS
--no-comments # 不恢復(fù)注釋
--no-data-for-failed-tables # 不恢復(fù)未能創(chuàng)建的表的數(shù)據(jù)
--no-publications # 不恢復(fù)發(fā)布(publications)
--no-security-labels # 不恢復(fù)安全標(biāo)簽
--no-subscriptions # 不恢復(fù)訂閱
--no-table-access-method # 不恢復(fù)表訪問方法
--no-tablespaces # 不恢復(fù)表空間分配
--section=SECTION # 恢復(fù)指定部分(pre-data, data, 或 post-data)
--strict-names # 要求表和/或模式包含模式至少匹配每個(gè)實(shí)體
--use-set-session-authorization
 # 使用SET SESSION AUTHORIZATION命令而不是ALTER OWNER命令來(lái)設(shè)置所有權(quán)

# 連接選項(xiàng)
-h, --host=HOSTNAME # 數(shù)據(jù)庫(kù)服務(wù)器主機(jī)名或套接字目錄
-p, --port=PORT # 數(shù)據(jù)庫(kù)服務(wù)器端口號(hào)
-U, --username=NAME # 以指定的數(shù)據(jù)庫(kù)用戶身份連接
-w, --no-password # 從不提示輸入密碼
-W, --password # 強(qiáng)制密碼提示(應(yīng)自動(dòng)發(fā)生)
--role=ROLENAME # 在恢復(fù)前執(zhí)行SET ROLE

# 注意事項(xiàng)
# -I, -n, -N, -P, -t, -T 和 --section 選項(xiàng)可以組合并多次指定以選擇多個(gè)對(duì)象。

# 如果沒有提供輸入文件名,則使用標(biāo)準(zhǔn)輸入。

pg_restore使用示例

1.恢復(fù)整個(gè)數(shù)據(jù)庫(kù)

pg_restore -U postgres -h 127.0.0.1 -p 5432 -W -d mydb db.dump

2.恢復(fù)到指定的數(shù)據(jù)庫(kù)并使用不同的用戶名

pg_restore -U username -d new_database db.dump

3.恢復(fù)部分?jǐn)?shù)據(jù)庫(kù)對(duì)象

恢復(fù)指定表

pg_restore -d new_database -t table_name db.dump

恢復(fù)的模式名

pg_restore -d new_database -n schema_name db.dump

4.恢復(fù)時(shí)使用 --create 選項(xiàng)創(chuàng)建數(shù)據(jù)庫(kù)

pg_restore --create -d postgres db.dump

5.恢復(fù)到現(xiàn)有數(shù)據(jù)庫(kù),并使用并行恢復(fù)

pg_restore -d new_database -j 4 db.dump

6.將恢復(fù)輸出重定向到文件

pg_restore -U postgres -h 127.0.0.1 -p 5432 -W --file=output.sql db.dump

7.只恢復(fù)數(shù)據(jù),不恢復(fù)表結(jié)構(gòu)

pg_restore -U postgres -h 127.0.0.1 -p 5432 -W -d mydb --data-only -j 4 db.dump

8.報(bào)錯(cuò)時(shí)退出,默認(rèn)繼續(xù)執(zhí)行

pg_restore -d new_database -j 4 --exit-on-error db.dump 

9.不恢復(fù)此模式(schema)中的表

pg_restore -d new_database -j 4 --exit-on-error --exclude-schema=NAME db.dump 

10.跳過(guò)權(quán)限的恢復(fù)

pg_restore -d new_database -j 4 --exit-on-error --no-owner --no-privileges db.dump 

11.以事務(wù)方式導(dǎo)入

pg_restore -U postgres -h 127.0.0.1 -p 5432 -W -d mydb --single-transaction db.dump

總結(jié)

根據(jù)備份的需求選擇合適的工具。pg_dump 用于單個(gè)數(shù)據(jù)庫(kù)的備份和恢復(fù),pg_dumpall 用于整個(gè)集群的備份,pg_basebackup 用于物理備份。

  • 備份策略:制定定期備份計(jì)劃,并測(cè)試恢復(fù)過(guò)程,確保備份文件可用。
  • 安全:備份文件可能包含敏感數(shù)據(jù),應(yīng)妥善保管備份文件,并考慮使用加密。
  • 測(cè)試:定期測(cè)試備份文件的恢復(fù),以確保在需要時(shí)能夠正確恢復(fù)數(shù)據(jù)。
  • 性能:對(duì)于大型數(shù)據(jù)庫(kù),考慮使用流式備份或并行備份來(lái)提高備份和恢復(fù)的速度。
  • 權(quán)限:確保執(zhí)行備份和恢復(fù)的用戶具有適當(dāng)?shù)臋?quán)限。

作者介紹

吳守陽(yáng),51CTO社區(qū)編輯,擁有8年DBA工作經(jīng)驗(yàn),熟練管理MySQL、Redis、MongoDB等開源數(shù)據(jù)庫(kù)。精通性能優(yōu)化、備份恢復(fù)和高可用性架構(gòu)設(shè)計(jì)。善于故障排除和自動(dòng)化運(yùn)維,保障系統(tǒng)穩(wěn)定可靠。具備良好的團(tuán)隊(duì)合作和溝通能力,致力于為企業(yè)提供高效可靠的數(shù)據(jù)庫(kù)解決方案。

責(zé)任編輯:華軒 來(lái)源: 51CTO
相關(guān)推薦

2011-07-26 13:55:01

MongoDB備份與恢復(fù)

2009-04-03 10:54:49

Oracle備份恢復(fù)

2014-01-03 17:43:18

Linux文件恢復(fù)文件恢復(fù)工具

2023-05-31 08:54:14

MySQL邏輯備份

2011-04-11 13:46:17

Oracle數(shù)據(jù)庫(kù)備份

2009-10-13 09:43:43

Oracle數(shù)據(jù)庫(kù)備份

2010-03-31 10:39:40

RMANOracle

2009-03-17 16:00:47

Oracle數(shù)據(jù)庫(kù)備份

2010-04-13 11:09:21

Oracle數(shù)據(jù)庫(kù)

2011-05-20 09:35:24

Oracle數(shù)據(jù)庫(kù)恢復(fù)備份

2019-02-28 21:20:50

MySQL備份與恢復(fù)數(shù)據(jù)庫(kù)

2017-04-24 14:29:42

存儲(chǔ)備份寶災(zāi)備

2011-08-01 12:44:25

Oracle基于用戶管理備份與恢復(fù)

2023-09-12 09:45:54

Java數(shù)據(jù)庫(kù)

2023-12-27 22:08:39

vivo數(shù)據(jù)庫(kù)

2011-08-24 15:36:30

2011-05-17 15:02:15

ORACLE數(shù)據(jù)庫(kù)備份

2023-11-15 09:38:49

Oracle數(shù)據(jù)庫(kù)

2015-08-25 09:23:00

2009-03-25 17:43:09

備份DB2IBM
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)