pg_probackup包含新建表空間的備份及恢復(fù)
pg_probackup 包含新建表空間的備份及恢復(fù)
本文出處:https://www.modb.pro/db/404169
pg_probackup 是一個(gè)比較方便的備份管理工具,當(dāng)前 2.5.5 版本支持 PostgreSQL 9.6, 10, 11, 12, 13, 14,pg_probackup 基本配置操作可以查看這篇文章:?PostgreSQL 備份恢復(fù)管理器 pg_probackup
PostgreSQL 里大部分場(chǎng)景下并不需要新建表空間,當(dāng)用戶新建表空間之后,備份恢復(fù)需要做一些額外的設(shè)置。
一、pg_basebackup 與新建表空間
首先是 pg_basebackup 備份時(shí)需要做表空間映射,否則會(huì)出現(xiàn)如下錯(cuò)誤
[postgres@pg ~]$ pg_basebackup --pgdata=datarecpg_basebackup: error: directory "/home/postgres/test_spc1" exists but is not emptypg_basebackup: removing data directory "datarec"
提示/home/postgres/test_spc1 路徑非空,不能進(jìn)行備份,其實(shí)是數(shù)據(jù)庫(kù)有新建表空間,數(shù)據(jù)存放在/home/postgres/test_spc1 下。
postgres=# \db ? ? ? ? ? ? ? List of tablespaces ? ?Name ? ?| ?Owner ? | ? ? ? ? Location------------+----------+-------------------------- myspc1 ? ? | postgres | /home/postgres/test_spc1 pg_default | postgres | pg_global ?| postgres |(3 rows)
pg_basebackup 做備份,拷貝 PGDATA 目錄時(shí)也會(huì)把下面用戶新建表空間這個(gè)符號(hào)鏈接拷貝到 datarec 目錄,這在同一臺(tái)機(jī)器上會(huì)有沖突。
$ ll pg_tblspc/172428lrwxrwxrwx 1 postgres dba 24 May 17 15:19 pg_tblspc/172428 -> /home/postgres/test_spc1
所以需要使用–tablespace-mapping 來(lái)進(jìn)行表空間映射:
$ pg_basebackup --pgdata=datarec \--tablespace-mapping=/home/postgres/test_spc1=/home/postgres/test_spc2
備份完之后可以查看新的 PGDATA 目錄表空間符號(hào)鏈接指向了新的目錄:
$ ll datarec/pg_tblspc/172428lrwxrwxrwx 1 postgres dba 24 May 17 17:59 datarec/pg_tblspc/172428 -> /home/postgres/test_spc2
如此就可以在本機(jī)進(jìn)行恢復(fù)測(cè)試。
二、pg_probackup 備份新建表空間
不過(guò)當(dāng)我們使用 pg_probackup 進(jìn)行備份時(shí),pg_probackup 會(huì)自動(dòng)識(shí)別符號(hào)鏈接進(jìn)行實(shí)際數(shù)據(jù)拷貝,并不需要像 pg_basebackup 那樣進(jìn)行映射。如下所示:pg_probackup 會(huì)正常備份
$ pg_probackup backup \--backup-path=/home/postgres/pgdata_probackup \--instance local_1402 \--backup-mode=full \--stream --temp-slot
三、pg_probackup 恢復(fù)新建表空間
但是在本機(jī)進(jìn)行恢復(fù)時(shí)需要進(jìn)行映射,pg_probackup 恢復(fù)不指定表空間映射會(huì)提示下面的錯(cuò)誤:
$ pg_probackup restore ?\--backup-path=/home/postgres/pgdata_probackup \--pgdata=/home/postgres/data1402_rec \--instance local_1402INFO: Tablespace 172428 will be restored using old path "/home/postgres/test_spc1"ERROR: Restore tablespace destination is not empty: "/home/postgres/test_spc1"
下面則可以正?;謴?fù)
$ pg_probackup restore ?\--backup-path=/home/postgres/pgdata_probackup \--tablespace-mapping='/home/postgres/test_spc1=/home/postgres/test_spc1_bak' \--pgdata=/home/postgres/data1402_rec \--instance local_1402INFO: Tablespace 172428 will be remapped from "/home/postgres/test_spc1" to "/home/postgres/test_spc1_bak"INFO: Validating backup RC0N61INFO: Backup RC0N61 data files are validINFO: Backup RC0N61 WAL segments are validINFO: Backup RC0N61 is valid.INFO: Restoring the database from backup at 2022-05-17 15:22:49+08INFO: Start restoring backup files. PGDATA size: 117MBINFO: Backup files are restored. Transfered bytes: 117MB, time elapsed: 4sINFO: Restore incremental ratio (less is better): 100% (117MB/117MB)INFO: Syncing restored files to diskINFO: Restored backup files are synced, time elapsed: 10sINFO: Restore of backup RC0N61 completed.
恢復(fù)之后進(jìn)入數(shù)據(jù)庫(kù)查看表空間路徑已經(jīng)修改為上面映射的新目錄路徑了。
postgres=# \db ? ? ? ? ? ? ? ? List of tablespaces ? ?Name ? ?| ?Owner ? | ? ? ? ? ? Location------------+----------+------------------------------ myspc1 ? ? | postgres | /home/postgres/test_spc1_bak pg_default | postgres | pg_global ?| postgres |(3 rows)
用戶新建表空間下的數(shù)據(jù)也可正常使用。
四、新建表空間與 PGDATA 路徑的關(guān)系
PostgreSQL 里新建表空間可以放在 PGDATA 的上層,也可以放在 PGDATA 里面(包括 PGDATA 目錄),其實(shí)沒(méi)有限制。
openGauss 里新建表空間可以放在 PGDATA 的上層,不能放在 PGDATA 里面(包括 PGDATA 目錄)。
下面是在 openGauss 數(shù)據(jù)庫(kù)里使用 PGDATA 目錄或者 PGDATA 子目錄來(lái)新建表空間都會(huì)提示報(bào)錯(cuò)(PostgreSQL 可以成功創(chuàng)建,會(huì)有警告提示):
postgres=# create tablespace my_tblspace2 location '/opt/data3000/';ERROR: ?tablespace cannot be created under data directorypostgres=#postgres=# create tablespace my_tblspace3 location '/opt/data3000/test';ERROR: ?tablespace cannot be created under data directory
建議:新建表空間既不要放在 PGDATA 目錄的上層或下層,盡量獨(dú)立,否則一些備份工具可能不支持。
五、pg_probackup 與 gs_probackup 恢復(fù)對(duì)比
gs_probackup 全備與前面介紹 pg_probackup 備份一致,都不需要進(jìn)行表空間映射。
gs_probackup 本機(jī)恢復(fù)需要先刪除原 PGDATA 數(shù)據(jù)及新建表空間目錄數(shù)據(jù),pg_probackup 相對(duì)而言更靈活,可以指定新的 PGDATA 并對(duì)新建表空間進(jìn)行路徑映射(參考上面的第三節(jié))。
下面是使用 gs_probackup 嘗試恢復(fù)錯(cuò)誤提示:
$ gs_probackup restore ?\--backup-path=/home/omm/pgdata_probackup \--pgdata=/home/omm/datarec \--instance local_3000LOG: Restore begin.LOG: check tablespace directories of backup RC0SW8ERROR: restore tablespace destination is not empty: "/opt"
指定–tablespace-mapping
$ gs_probackup restore ?\--backup-path=/home/omm/pgdata_probackup \--tablespace-mapping='/opt=/home/omm/mytblspac' \--pgdata=/home/omm/datarec \--instance local_3000ERROR: If specify --tablespace-mapping option, you must specify --external-mapping option together
同時(shí)指定–tablespace-mapping 和–external-mapping
gs_probackup restore ?\--backup-path=/home/omm/pgdata_probackup \--tablespace-mapping='/opt=/home/omm/mytblspac' \--external-mapping='/opt=/home/omm/mytblspac' \--pgdata=/home/omm/datarec \--instance local_3000ERROR: --external-mapping option's old directory doesn't have an entry in list of external directories of current backup: "/opt"
六、結(jié)論
用戶新建表空間 pg_probackup 可以自動(dòng)處理,無(wú)需考慮 baseback 表空間映射問(wèn)題。
pg_probackup 可以同時(shí)進(jìn)行本機(jī)恢復(fù)測(cè)試,不會(huì)影響本地實(shí)例的正常運(yùn)行。