1. 程式人生 > 其它 >6.pgBackRest 介紹及示例

6.pgBackRest 介紹及示例

1.pgBackRest 介紹

pgBackRest是一款開源的備份還原工具,目標旨在為備份和還原提供可靠易用的備份。

pgBackRest旨在成為一個可靠、易於使用的備份和恢復解決方案,通過利用針對資料庫特定要求優化的演算法,可以無縫擴充套件到最大的資料庫和工作負載。
pgBackRest 放棄了其他傳統備份工具依賴 tar 和 rsync 的套路,它的備份功能都是從軟體內部實現的,並採用客戶端協議與遠端伺服器互動。**移除了對 tar 和 rsync 的依賴**,使它能夠更好的應對針對特定資料庫的備份挑戰。客戶端遠端協議更加靈活,協議可以按照要求限制連線型別以保證備份過程更安全。

  • pgBackRest  功能支援:
    ○  支援並行備份和恢復 ,解決了壓縮操作中的瓶頸       
    ○  支援本地或遠端操作, 需配置TLS/SSH 在本地或遠端備份、恢復和存檔 
    ○  支援完整、增量和差異備份,增量恢復
    ○  支援多個儲存庫
    ○  支援備份輪換和存檔過期, 備份完整性檢查,頁面校驗
    ○  支援斷點備份
    ○  支援並行、非同步 WAL 推送和獲取 
    ○  支援表空間和連結支援
    ○  支援加密        
    ○  **S3、Azure 和 GCS 相容的物件儲存支援**

  • pgBackRest 注意事項:
    ○  需要在資料庫伺服器上修改引數 如 archive_command = 'pgbackrest --stanza=demo archive-push %p'
    ○  資料庫伺服器 和 備份伺服器,都需要安裝,且要求相同版本  。 
    ○  資料庫的page 大小隻能是8k,但預設pg是16k ;  show block_size 檢視資料庫的page size 
         而block_size  只能在編譯的設定  --with-blocksize=8,16,32

2.安裝使用

2.1 安裝

# 安裝依賴包: 
yum -y install   libyaml-devel
yum -y install bzip2*

#下載:
https://github.com/pgbackrest/pgbackrest/tree/release/2.37

#  (資料庫伺服器 和 備份伺服器 都要安裝)
# 建立相應目錄指定為 /usr/bin/pgbackrest
mkdir  /usr/bin/pgbackrest
chown postgres.postgres /usr/bin/pgbackrest/
chmod 755 /usr/bin/pgbackrest/

mkdir -p /etc/pgbackrest/conf.d
touch /etc/pgbackrest/pgbackrest.conf
chmod 640 /etc/pgbackrest/pgbackrest.conf
chown postgres.postgres -R /etc/pgbackrest/
mkdir -p -m 770 /var/log/pgbackrest
chown postgres.postgres /var/log/pgbackrest/

#解壓安裝
postgres@s2ahumysqlpg01-> unzip pgbackrest-release-2.37
postgres@s2ahumysqlpg01-> cd  pgbackrest-release-2.37/src
postgres@s2ahumysqlpg01-> ./configure --prefix=/usr/bin/pgbackrest/
postgres@s2ahumysqlpg01-> make  -j24
postgres@s2ahumysqlpg01-> make install -j24
install -d /usr/bin/pgbackrest/bin
install -m 755 pgbackrest /usr/bin/pgbackrest/bin



# 注意: 預設安裝路徑為 /usr/bin/pgbackrest  ,如果不是的話,做一個軟連線 否會會找不到遠端的執行命令 
如: unexpectedly [127]: bash: pgbackrest: command not found
ln -s /home/postgres/pgbackrest/bin/pgbackrest   /usr/bin/pgbackrest

#  設定環境變更 
cd  \
 echo "export PATH=/usr/bin/pgbackrest/bin:\$PATH" >> .bashrc 
.  .bashrc 


# 檢視命令
cd  /usr/bin/pgbackrest/bin
postgres@s2ahumysqlpg01-> ./pgbackrest 
pgBackRest 2.37 - General help

Usage:
    pgbackrest [options] [command]

Commands:
    archive-get     Get a WAL segment from the archive.
    archive-push    Push a WAL segment to the archive.
    backup          Backup a database cluster.
    check           Check the configuration.
    expire          Expire backups that exceed retention.
    help            Get help.
    info            Retrieve information about backups.
    repo-get        Get a file from a repository.
    repo-ls         List files in a repository.
    restore         Restore a database cluster.
    server          pgBackRest server.
    server-ping     Ping pgBackRest server.
    stanza-create   Create the required stanza data.
    stanza-delete   Delete a stanza.
    stanza-upgrade  Upgrade a stanza.
    start           Allow pgBackRest processes to run.
    stop            Stop pgBackRest processes from running.
    version         Get version.

Use 'pgbackrest help [command]' for more information.

2.2 互信配置

#備份伺服器 為 s2ahumysqlpg01 ,  資料庫伺服器為s2ahumysqlpg02
# 備份伺服器
 ssh-keygen -t rsa  
 ssh-copy-id s2ahumysqlpg02

# 資料庫伺服器
 ssh-keygen -t rsa  
 ssh-copy-id s2ahumysqlpg01

3.配置pgbackrest

3.1 pgbackrest.conf

pgBackRest可以完全與命令列引數一起使用,但配置檔案對於複雜或設定很多選項的安裝更實用。配置檔案的預設位置是/etc/pgbackrest/pgbackrest.conf。如果該位置不存在檔案,則將檢查 /etc/pgbackrest.conf的舊預設值。

3.1.1 資料庫伺服器上的配置

vi /etc/pgbackrest/pgbackrest.conf
[global]
log-level-file=detail
repo1-host=s2ahumysqlpg01
log-path=/u01/postgresql/backup/log

[pg12]
pg1-path=/u01/postgresql/data

# 設定資料庫引數 
vi postgresql.auto.conf 
archive_command = 'pgbackrest --stanza=pg02 archive-push %p'
archive_mode = on
listen_addresses = '*'
log_line_prefix = ''
max_wal_senders = 3
wal_level = replica

# 重啟資料庫
pg_ctl restart

3.1.2備份伺服器上的配置

# 示例
vi   /etc/pgbackrest/pgbackrest.conf
[global]
repo1-path=/u01/postgresql/backup/repos
repo1-retention-full=2
log-path=/home/postgres/pgbackrest/log

[global:archive_push]
compress-level=3

[pg01]
pg1-path=/u01/postgresql/data_bak/

[pg02]
pg1-path=/u01/postgresql/data/
pg1-host-config-path=/etc/pgbackrest
pg1-host-port=22
pg1-host-user=postgres
pg1-host=s2ahumysqlpg02
pg1-port=5432
pg1-user=postgres

#備註:
repo1-path  指定 備份和歸檔倉庫路徑 
compress-level 指定壓縮級別 bz2 - 9 ; gz - 6 ; lz4 - 1  ;  zst - 3

3.2 建立儲存空間

#本地節點
pgbackrest --stanza=pg01 --log-level-console=info stanza-create

#遠端節點
pgbackrest --stanza=pg02 --log-level-console=info stanza-create


#檢查 
postgres pgbackrest --stanza=pg01 --log-level-console=info check
postgres pgbackrest --stanza=pg01 --log-level-console=info check

4.備份

4.1全量備份

# 全量備份
pgbackrest --stanza=pg02  --log-level-console=info backup

#輸出類似以下資訊:#
P00   INFO: backup command begin 2.37: --exec-id=1041-336b8131 --log-level-console=info --log-level-stderr=off --no-log-timestamp --pg1-path=/var/lib/pgsql/10/data --repo1-cipher-pass= --repo1-cipher-type=aes-256-cbc --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo --start-fast
P00   WARN: no prior backup exists, incr backup has been changed to full
P00   INFO: execute non-exclusive pg_start_backup(): backup begins after the requested immediate checkpoint completes
P00   INFO: backup start archive = 000000010000000000000002, lsn = 0/2000028
       [filtered 3 lines of output]
P00   INFO: check archive for segment(s) 000000010000000000000002:000000010000000000000003
P00   INFO: new backup label = 20211231-194304F
P00   INFO: full backup size = 22.5MB, file total = 949
P00   INFO: backup command end: completed successfully
P00   INFO: expire command begin 2.37: --exec-id=1041-336b8131 --log-level-console=info --log-level-stderr=off --no-log-timestamp --repo1-cipher-pass= --repo1-cipher-type=aes-256-cbc --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo

4.2差異備份

pgbackrest --stanza=pg02  --type=diff   --log-level-console=info backup

#輸出類似以下資訊:
       [filtered 7 lines of output]
P00   INFO: check archive for segment(s) 000000010000000000000004:000000010000000000000005
P00   INFO: new backup label = 20211231-194304F_20211231-194310D
P00   INFO: diff backup size = 8.8KB, file total = 949
P00   INFO: backup command end: completed successfully
P00   INFO: expire command begin 2.37: --exec-id=1096-e5efad67 --log-level-console=info --log-level-stderr=off --no-log-timestamp --repo1-cipher-pass= --repo1-cipher-type=aes-256-cbc --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo

4.3定時備份

# 設定備份任務
crontab -e 
30 06  *   *   0     pgbackrest --type=full --stanza=demo backup
30 06  *   *   1-6   pgbackrest --type=diff --stanza=demo backup

4.4 檢視備份資訊

# 檢視備份資訊
 pgbackrest info

#輸出類似以下資訊:
stanza: demo
    status: ok
    cipher: aes-256-cbc

    db (current)
        wal archive min/max (10): 000000010000000000000001/000000010000000000000005
        full backup: 20211231-194304F
            timestamp start/stop: 2021-12-31 19:43:04 / 2021-12-31 19:43:08
            wal start/stop: 000000010000000000000002 / 000000010000000000000003
            database size: 22.5MB, database backup size: 22.5MB
            repo1: backup set size: 2.7MB, backup size: 2.7MB
        diff backup: 20211231-194304F_20211231-194310D
            timestamp start/stop: 2021-12-31 19:43:10 / 2021-12-31 19:43:12
            wal start/stop: 000000010000000000000004 / 000000010000000000000005
            database size: 22.5MB, database backup size: 8.8KB
            repo1: backup set size: 2.7MB, backup size: 752B
            backup reference list: 20211231-194304F

5.恢復

 pgbackrest --stanza=pg02  restore

6.備份監控

PostgreSQL 將通過COPY命令允許 pgBackRest資訊載入到表中。以下示例將該邏輯包裝在可用於執行實時查詢的函式中。
# 1.建立函式
 psql -f   /home/postgres/pgbackrest-release-2.37/doc/example/pgsql-pgbackrest-info.sql
# 2. 執行查詢 
  psql -f   /home/postgres/pgbackrest-release-2.37/doc/example/pgsql-pgbackrest-query.sql
 name  | last_successful_backup |    last_archived_wal     
--------+------------------------+--------------------------
 "demo" | 2021-12-31 19:43:12+00 | 000000010000000000000005

參考

    https://github.com/pgbackrest/pgbackrest 
    https://pgbackrest.org/
    https://pgbackrest.org/user-guide-index.html
    https://gitee.com/mirrors/PgBackRest
    https://mp.weixin.qq.com/s/CQAKe_BA6_3P1utoXghVPw 


來自為知筆記(Wiz)