PostgreSQL pgbackrest 怎麼設計帶遠端控制端的PG備份系統系統

資料庫工作筆記發表於2023-12-15

來源:AustinDatabases

前兩期介紹了關於pgbackrest 的基本搭建和一些設定以及日常的使用,在實際的工作中,pgbackrest本身是可以進行集中化管理的,透過集中化的管理方式來進行批次的大範圍的資料庫管理如何進行相關的操作和配置。實際上pgbackrest 在單機備份的簡便性還是不如pgrman的的便利性,但是在下圖的備份方案中,則pgbackrest 是好的備份的方案選擇。

同時這篇文章,和上兩篇之間有很大時間的差距的原因在,這篇在測試中失敗N次,各種問題,各種引數在文件中未說明等問題。

問題的難點,在於中文,英文,沒有一篇文件說明了其中還包含了操作的順序,最後找到其中的奧秘還是透過某個繁體字的網站的一點資訊發現其中一直沒有成功的原因。

PostgreSQL  pgbackrest 怎麼設計帶遠端控制端的PG備份系統系統


這裡需要將pgbackrest 單獨進行安裝到一臺伺服器中,並部署pgbackrest的主機,同時打通主機與各個postgresql資料庫的SSH 免密連線。

 下面是整個搭建的過程

1  搭建裝置 
pgbackrest 主機  IP 地址  192.168.198.150 

postgresql IP 地址  192.168.198.101

2  賬號 

pgbackrest  賬號為  pgbackrest

postgresql  賬號為   postgres

3  主機之間進行ssh 免密設定
ssh-keygen -t rsa
ssh-copy-id  postgres@192.168.198.101
ssh-copy-id  postgres@192.168.198.150

在不同的主機上進行ssh 的測試

pgbackrest@pgbackrest:~$ 
pgbackrest@pgbackrest:~$ ssh postgres@192.168.198.101
Last login: Thu Feb 16 20:54:36 2023
[postgres@postgre14 ~]$ 


[postgres@postgre14 ~]$ ssh pgbackrest@192.168.198.150
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-87-generic x86_64)

 * Documentation:  
 * Management:     
 * Support:        

  System information as of Wed Dec  6 06:18:53 AM UTC 2023


4  pgbackrest 的安裝
這裡有幾個注意事項
1 pgbackrest 的配置檔案的預設位置在 /etc/pgbackrest/pgbackrest.conf
這個位置最好不要進行變化,同時日誌目錄在 /var/log/pgbackrest/目錄中,這些目錄都需要給 pgbackrest postgres 賬號具有全部的目錄和檔案的寫入許可權

2  資料庫,控制端全部需要安裝pgbackrest

3  pgbackrest 的安裝目錄最好一致,並且可以在對應的賬號下直接引用命令。

4  針對多例項的備份,應建立不同的資料夾進行資料儲存,不能將各個例項的備份不進行目錄分割

5  基於備份資料透過網路進行備份和恢復,需要網路,網路的頻寬至少為千兆

——————————————————————————————
以下為資料庫服務端的配置檔案
[test]
pg1-path=/pgdata/data
pg1-socket-path=/tmp


[global]
repo1-host=192.168.198.150
log-level-file=detail
log-path=/var/log/pgbackrest

以下為pgbackrest 服務端的配置
[test]
pg1-path=/pgdata/data
pg1-port=5432
pg1-socket-path=/tmp
pg1-host=192.168.198.101
pg1-user=backup
pg1-pgpass=/home/postgres/.pgpass

[global]
repo1-path=/pgbackrest/backup
repo1-retention-full=2
log-level-console=info
log-level-file=debug
buffer-size=16MiB
compress-type=gz


[global:archive-push]
compress-level=3




配置錯誤,以及沒有按照操作順序,導致的備份初始化失敗
pgbackrest@pgbackrest:~$ pgbackrest --stanza=test stanza-create
WARN: configuration file contains invalid option 'pg1-pgpass'
2023-12-06 07:38:07.986 P00   INFO: stanza-create command begin 2.48: --buffer-size=16MiB --exec-id=45679-3c58b42b --log-level-console=info --log-level-file=debug --pg1-host=192.168.198.101 --pg1-host-config-path=/etc/pgbackrest --pg1-host-port=22 --pg1-host-user=postgres --pg1-path=/pgdata/data --pg1-port=5432 --pg1-user=postgres --repo1-path=/pgbackrest/backup --stanza=test
WARN: unable to check pg1: [DbConnectError] raised from remote-0 ssh protocol on '192.168.198.101': unable to connect to 'dbname='postgres' port=5432 user='postgres'': could not connect to server: No such file or directory
       Is the server running locally and accepting
       connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
ERROR: [056]: unable to find primary cluster - cannot proceed
       HINT: are all available clusters in recovery?




[postgres@postgre14 pgbackrest]$ pgbackrest --stanza=test stanza-create
WARN: unable to check pg1: [DbConnectError] unable to connect to 'dbname='postgres' port=5432': could not connect to server: No such file or directory
       Is the server running locally and accepting
       connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
ERROR: [056]: unable to find primary cluster - cannot proceed


pgbackrest --stanza=test stanza-create
WARN: configuration file contains invalid option 'pg1-pgpass'
2023-12-06 07:52:26.993 P00   INFO: stanza-create command begin 2.48: --buffer-size=16MiB --exec-id=47078-8289a622 --log-level-console=info --log-level-file=debug --pg1-host=192.168.198.101 --pg1-path=/pgdata/data --pg1-port=5432 --pg1-socket-path=/tmp --pg1-user=backup --repo1-path=/pgbackrest/backup --stanza=test
2023-12-06 07:52:27.799 P00   INFO: stanza-create for stanza 'test' on repo1
ERROR: [055]: archive.info exists but backup.info is missing on repo1
       HINT: this may be a symptom of repository corruption!
2023-12-06 07:52:27.801 P00   INFO: stanza-create command end: aborted with exception [055]

[postgres@postgre14 pgbackrest]$ pgbackrest --stanza=test stanza-create
ERROR: [055]: archive.info exists but backup.info is missing on repo1
       HINT: this may be a symptom of repository corruption!



——————————————————————————————
pgbackrest 正確進行初始化的順序

1 需要在遠端控制端進行初始化,在遠端控制端完成初始化後,在再資料庫端進行初始化

pgbackrest --stanza=test stanza-create
WARN: configuration file contains invalid option 'pg1-pgpass'
2023-12-06 07:52:54.849 P00   INFO: stanza-create command begin 2.48: --buffer-size=16MiB --exec-id=47087-9c19c49c --log-level-console=info --log-level-file=debug --pg1-host=192.168.198.101 --pg1-path=/pgdata/data --pg1-port=5432 --pg1-socket-path=/tmp --pg1-user=backup --repo1-path=/pgbackrest/backup --stanza=test
2023-12-06 07:52:55.620 P00   INFO: stanza-create for stanza 'test' on repo1
2023-12-06 07:52:55.731 P00   INFO: stanza-create command end: completed successfully (885ms)



[postgres@postgre14 pgbackrest]$ pgbackrest --stanza=test stanza-create

下面是,遠端啟動,check, 全備,差異備份
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test check
WARN: configuration file contains invalid option 'pg1-pgpass'
2023-12-06 07:53:19.995 P00   INFO: check command begin 2.48: --buffer-size=16MiB --exec-id=47148-5c776784 --log-level-console=info --log-level-file=debug --pg1-host=192.168.198.101 --pg1-path=/pgdata/data --pg1-port=5432 --pg1-socket-path=/tmp --pg1-user=backup --repo1-path=/pgbackrest/backup --stanza=test
2023-12-06 07:53:20.836 P00   INFO: check repo1 configuration (primary)
2023-12-06 07:53:21.500 P00   INFO: check repo1 archive for WAL (primary)
2023-12-06 07:53:22.106 P00   INFO: WAL segment 00000001000000070000000D successfully archived to '/pgbackrest/backup/archive/test/14-1/0000000100000007/00000001000000070000000D-46144492923763da2d73d3b681abb903320a3f24.gz' on repo1
2023-12-06 07:53:22.210 P00   INFO: check command end: completed successfully (2218ms)
pgbackrest@pgbackrest:/pgbackrest$ 
pgbackrest@pgbackrest:/pgbackrest$ 
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test --log-level-console=info backup
WARN: configuration file contains invalid option 'pg1-pgpass'
2023-12-06 08:10:38.578 P00   INFO: backup command begin 2.48: --buffer-size=16MiB --compress-type=gz --exec-id=47204-2ecf5294 --log-level-console=info --log-level-file=debug --pg1-host=192.168.198.101 --pg1-path=/pgdata/data --pg1-port=5432 --pg1-socket-path=/tmp --pg1-user=backup --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
WARN: no prior backup exists, incr backup has been changed to full
2023-12-06 08:10:39.684 P00   INFO: execute non-exclusive backup start: backup begins after the next regular checkpoint completes
2023-12-06 08:10:41.103 P00   INFO: backup start archive = 00000001000000070000000F, lsn = 7/3C000060
2023-12-06 08:10:41.103 P00   INFO: check archive for prior segment 00000001000000070000000E
2023-12-06 08:14:40.153 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2023-12-06 08:14:40.811 P00   INFO: backup stop archive = 00000001000000070000000F, lsn = 7/3C000170
2023-12-06 08:14:40.815 P00   INFO: check archive for segment(s) 00000001000000070000000F:00000001000000070000000F
2023-12-06 08:14:42.051 P00   INFO: new backup label = 20230217-035638F
2023-12-06 08:14:42.108 P00   INFO: full backup size = 12.7GB, file total = 2860
2023-12-06 08:14:42.109 P00   INFO: backup command end: completed successfully (243533ms)
2023-12-06 08:14:42.109 P00   INFO: expire command begin 2.48: --buffer-size=16MiB --exec-id=47204-2ecf5294 --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2023-12-06 08:14:42.112 P00   INFO: expire command end: completed successfully (3ms)
pgbackrest@pgbackrest:/pgbackrest$ 
pgbackrest@pgbackrest:/pgbackrest$ 
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test --log-level-console=info --type=diff
ERROR: [030]: no command found
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test --log-level-console=info --type-diff
ERROR: [031]: invalid option '--type-diff'
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test --log-level-console=info --type-diff  backup
ERROR: [031]: invalid option '--type-diff'
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test  --type-diff  backup
ERROR: [031]: invalid option '--type-diff'
pgbackrest@pgbackrest:/pgbackrest$ pgbackrest --stanza=test  --type=diff backup
WARN: configuration file contains invalid option 'pg1-pgpass'
2023-12-06 08:27:07.388 P00   INFO: backup command begin 2.48: --buffer-size=16MiB --compress-type=gz --exec-id=47390-89b37813 --log-level-console=info --log-level-file=debug --pg1-host=192.168.198.101 --pg1-path=/pgdata/data --pg1-port=5432 --pg1-socket-path=/tmp --pg1-user=backup --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test --type=diff
2023-12-06 08:27:08.322 P00   INFO: last backup label = 20230217-035638F, version = 2.48
2023-12-06 08:27:08.322 P00   INFO: execute non-exclusive backup start: backup begins after the next regular checkpoint completes
2023-12-06 08:27:09.279 P00   INFO: backup start archive = 000000010000000700000011, lsn = 7/44000028
2023-12-06 08:27:09.280 P00   INFO: check archive for prior segment 000000010000000700000010
2023-12-06 08:27:11.620 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2023-12-06 08:27:12.279 P00   INFO: backup stop archive = 000000010000000700000011, lsn = 7/44000100
2023-12-06 08:27:12.284 P00   INFO: check archive for segment(s) 000000010000000700000011:000000010000000700000011
2023-12-06 08:27:13.010 P00   INFO: new backup label = 20230217-035638F_20230217-041429D
2023-12-06 08:27:13.070 P00   INFO: diff backup size = 8.3KB, file total = 2860
2023-12-06 08:27:13.071 P00   INFO: backup command end: completed successfully (5686ms)
2023-12-06 08:27:13.071 P00   INFO: expire command begin 2.48: --buffer-size=16MiB --exec-id=47390-89b37813 --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2023-12-06 08:27:13.074 P00   INFO: expire command end: completed successfully (3ms)



整體的備份都很順利,並無問題。

總結:之前的失敗的主要因素
1 對於配置檔案的引數理解不深刻
2 沒有獲知初始化的順序,這是導致遠端備份失敗的最大的原因

後續會對以上的引數,以及更多的一些設定方式以及資料恢復等進行測試和文字的撰寫。

來自 “ ITPUB部落格 ” ,連結:https://blog.itpub.net/70027826/viewspace-3000235/,如需轉載,請註明出處,否則將追究法律責任。

相關文章