使用 barman的備份和歸檔PostgreSQL


 

1 前言

1.1 Barman簡介

barman(備份和恢復管理器)是用於PostgreSQL服務器進行災難恢復的開源管理工具,是以Python編寫的。它支持對多台服務器執行遠程備份,以降低風險並幫助DBA進行數據庫恢復。

1.2 Barman的備份方式

本文假定讀者熟悉理論上的災難恢復概念,並且具有在PostgreSQL物理備份和災難恢復方面的基礎知識。

我們知道 PostgreSQL 的連續備份包含一個或多個基礎備份和連續歸檔的WAL日志。Barman 支持兩種方法實現這樣的備份。我們討論的情況是數據庫服務和備份文件在不同服務器上的情況。

1.2.1 基於流協議的備份

基於流協議的備份方法是barman 提供的獨特的方法。它適用於PostgreSQL 9.4或更高版本。它使用pg_basebackup進行基礎備份,使用pg_receivewal ( PostgreSQL 10 以下是 pg_receivexlog)歸檔WAL。其結構如下圖所示:

在這種情況下,您將需要配置:

  1. 與PostgreSQL的標准連接,用於管理,協調和監視
  2. 供pg_basebackup(用於基本備份操作)和pg_receivewal(用於WAL流歸檔)使用的流復制連接

 

 

用Barman 的術語來說,此設置稱為 streaming-only設置,因為它不需要任何SSH連接即可進行備份和歸檔操作。Barman 也支持基於基於流協議備份與基於SSH 的WAL 歸檔結合,下圖描繪了這種實現:

 

這種方案要求:

  1. 額外的SSH連接,以允許用戶postgres 在PostgreSQL服務器以barman的用戶身份連接到Barman服務器上。
  2. 在PostgreSQL的配置文件postgresql.conf 中配置archive_command,內容是將WAL文件歸檔到Barman的歸檔目錄。具體格式可參考官方手冊

1.2.2 基於rsync/ SSH 的備份

基於rsync/ SSH 的備份是一種傳統的基於文件的備份方式。它一般適用於下面的情形。

  1. PostgreSQL服務器版本是8.3、8.4、9.0或9.1
  2. 使用表空間的PostgreSQL服務器版本是9.2或9.3
  3. 增量備份,並行備份和重復數據刪除
  4. 備份期間的網絡壓縮
  5. 更好地控制帶寬使用,包括在表空間的基礎上

它的體系結構如下圖所示:

 

 

在這種情況下,您將需要配置:

  1. 與PostgreSQL的標准連接,用於管理,協調和監視
  2. 用於基礎備份操作的SSH連接,rsync會使用它,以允許barman用戶在Barman服務器上以用戶postgres的身份連接到PostgreSQL服務器
  3. 用於WAL歸檔的SSH連接,archive_command會使用它,以允許用戶postgres 在PostgreSQL服務器以用戶barman的身份連接到Barman服務器。

從PostgreSQL 9.2開始,您可以添加用於WAL流式傳輸的流復制連接。下圖描繪了這種實現:

 

1.3 實驗環境

實驗需要兩台服務器,下面是它們的一些基本信息。

 

操作系統: CentOS 7.5

內存: 32G

CPU: 8個邏輯核

軟件:

PostgreSQL 11.4

yum

python 3.6

barman 2.11

需要的python 模塊:

argcomplete-1.12.0

argh-0.26.2

psycopg2-2.8.5

python-dateutil-2.8.1

setuptools-49.6.0

      

數據庫服務器的IP地址:10.40.239.228

備份服務器的IP地址:10.40.239.229

ssh 端口:22,默認值

postgresql 的運行端口:5432

postgresql 的bin目錄 /opt/postgresql/bin/

postgresql 的data 目錄 /opt/postgresql/data

    注意,barman 要求特定版本操作系統和所依賴的軟件。具體要求如附錄1所示。

2    Barman備份環境的搭建

2.1 安裝軟件

2.1.1 數據庫服務器上的操作

   1. 安裝 rsync

yum install rsync

2.1.2 備份服務器上的操作

下面的操作都使用用戶 root 完成。

1.    安裝 epel源  (Extra Packages for Enterprise Linux)
yum -y install epel-release 
 
  1. 安裝 Python 3.6

yum -y install python3-3.6.8-13.el7.x86_64

 

注意,如果你不想指定Python 3的版本,可以換成執行:

yum -y install python3

 

在Python 3安裝過程中,setuptools 和 pip 也會被安裝。

 

  1. 安裝rsync

yum -y install rsync

 

  1. 安裝 pgdg 源

rpm -ivh https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

 

  1. 安裝Barman

方法1:使用yum 安裝barman

yum –y install barman-2.11-1.rhel7.noarch

 

使用yum安裝的過程中,會自動安裝下面的模塊

python36-argcomplete.x86_64

python36-argh

python36-psycopg2

python36-dateutil

 

用 yum 安裝完barman后,程序會創建一個操作系統用戶 barman,並創建一個文件 /etc/barman.conf,內容是barman的一些全局配置項,一個目錄 /etc/barman.d,存放與數據庫服務器有關的配置信息。

 

 

方法2:使用源碼安裝。

  1. SourceForge 上下載barman源碼,這里我們下載 barman-2.11.tar.gz。
  2. 解壓文件后,進入解壓后的目錄,使用python3安裝它:

tar -zxvf barman-2.11.tar.gz

cd ./barman-2.11

python3 ./setup.py build

python3 ./setup.py install

 

  1. 使用pip安裝需要的python模塊。進入 python3 的安裝目錄,執行命令如下:

./pip install argcomplete -i https://pypi.tuna.tsinghua.edu.cn/simple/

./pip install argh -i https://pypi.tuna.tsinghua.edu.cn/simple/

./pip install psycopg2 -i https://pypi.tuna.tsinghua.edu.cn/simple/

./pip install python-dateutil -i https://pypi.tuna.tsinghua.edu.cn/simple/

   

  1. 在操作系統中創建用戶barman

useradd -m barman -d /var/lib/barman

 

  1. 將barman 安裝包解壓后的barman-2.11/barman, 將 文件barman.conf  和 目錄barman.d 拷貝到 /etc/ 中。

cd barman-2.11/barman

cp barman.conf  /etc/

cp -R barman.d  /etc/

2.2 配置

2.2.1  數據庫服務器上的操作

下面的操作都使用用戶 root 完成。

  1.  為用戶postgres創建ssh密鑰以及授權文件,如果它們不存在。

su postgres

cd ~

ssh-keygen -t rsa

echo “”>> ./ssh/authorized_key

 

隨后在barman用戶的主目錄 /var/lib/pgsql/ 下的目錄 .ssh中,會生成私鑰文件 id_rsa和 公鑰文件 id_rsa.pub。

 

  1. 把PostgreSQL 的 bin 目錄復制到備份服務器的目錄 /etc/barman.d/中。可以使用scp。例如,PostgreSQL的bin 的位置是 /opt/postgresql/bin,則可以執行如下命令:

 

scp -r /opt/postgresql/bin root@10.40.239.229:/etc/barman.d/

 

  1. 在數據庫中創建用戶 barman 和 streaming_barman,其中barman 要是superuser, streaming_barman 有復制權限。具體的sql如下。

 

CREATE ROLE barman with LOGIN PASSWORD 'barman123' SUPERUSER;

CREATE ROLE streaming_barman with LOGIN PASSWORD 'streaming_barman123' REPLICATION;

 

  1. 數據庫的配置文件 postgresql.conf 應該做如下配置:

listen_addresses = '*'

wal_level = replica

max_wal_senders = 10

archive_mode = off

 

小貼士:你可能注意到,這里的配置和開啟PostgreSQL原生的連續歸檔時的配置不同。他們的區別是,對於基於流協議的歸檔,我們需要保證 archive_mode = off,且不必配置archive_command;而對於PostgreSQL原生的連續歸檔,我們需要設置archive_mode = on,並配置archive_command。

 

  1. 在 pg_hba.conf 中,添加如下內容,允許用戶barman和streaming_barman訪問。

host      all           barman                   127.0.0.1/32            md5

host      all           barman                   10.40.239.228/32        md5

host      all           barman                   10.40.239.229/32        md5

host      replication    streaming_barman         127.0.0.1/32            md5

host      replication    streaming_barman         10.40.239.228/32        md5

host      replication    streaming_barman         10.40.239.229/32        md5

 

2.2.2  備份服務器上的操作

下面的操作都使用用戶 barman完成。

  1. 為用戶barman創建ssh密鑰。

ssh-keygen -t rsa

 

隨后在barman用戶的主目錄 /var/lib/pgsql/ 下的目錄 .ssh中,會生成私鑰文件 id_rsa和 公鑰文件 id_rsa.pub。

 

  1. 復制用戶barman 的公鑰文件 id_rsa.pub 中的內容,並將它追加到數據庫服務器上

的用戶 postgres 的主目錄中的文件 .ssh/authorized_keys 中。這樣做的目的是允許barman以用戶postgres的身份免密訪問數據庫服務器

 

你可以手動復制此內容,並將它粘貼到數據庫服務器上的用戶 postgres 的主目錄中的文件 .ssh/authorized_keys 末尾。

你也可以通過下面的命令完成復制。如果使用下面的命令,你需要保證數據庫服務器上 postgres用戶已經設置了密碼。

ssh-copy-id postgres@10.40.239.228

  

  1. 創建 barman 的日志目錄 /var/log/barman

mkdir /var/log/barman

 

  1. 編輯  /etc/barman.conf,在 “[barman]” 之下修改這些配置項,以設置全局的備份參數:

 

; System user

barman_user = barman

 

; Directory of configuration files. Place your sections in separate files with .conf extension

; For example place the 'main' server section in /etc/barman.d/main.conf

configuration_files_directory = /etc/barman.d

 

; Main directory

barman_home = /var/lib/barman

 

; Log location

log_file = /var/log/barman/barman.log

 

; Log level (see https://docs.python.org/3/library/logging.html#levels)

log_level = INFO

 

 

; Global retention policy (REDUNDANCY or RECOVERY WINDOW) - default empty

retention_policy = RECOVERY WINDOW OF 4 WEEKS

 

; Number of parallel jobs for backup and recovery via rsync (default 1)

parallel_jobs = 3

 

; Immediate checkpoint for backup command - default false

immediate_checkpoint = true

 

; Enable network compression for data transfers - default false

network_compression = false

 

; Number of retries of data copy during base backup after an error - default 0

basebackup_retry_times = 3

 

; Number of seconds of wait after a failed copy, before retrying - default 30

basebackup_retry_sleep = 30

 

 

; Minimum number of required backups (redundancy)

minimum_redundancy = 2

 

 

這里我們用中文解釋一下這些參數的含義:

barman_user 運行barman 的用戶

configuration_files_directory 配置文件所在目錄。 將您的備份放在擴展名為.conf的單獨文件中

barman_home barmn的主目錄

log_file barman 日志文件的位置

log_level 日志級別

retention_policy 備份的保留策略。空表示禁用;REDUNDANCY 2 表示保留兩份基礎備份;RECOVERY WINDOW OF 4 WEEKS 表示保留4星期之內的備份。

parallel_jobs 通過rsync備份和恢復的並行作業數

immediate_checkpoint 備份命令是否執行立即檢查點

network_compression 啟用網絡壓縮以進行數據傳輸。對於流備份,這個參數設置為false。

basebackup_retry_times 在基礎備份期間發生錯誤后重新嘗試的次數

basebackup_retry_sleep 復制失敗后,重試之前等待的秒數

minimum_redundancy 所需的最小備份數量

 

  1. 配置要備份的數據庫的信息。

5.1  進入 /etc/barman.d, 將 streaming-server.conf-template 復制為 pg.conf,文件名中的“pg”也是此備份任務的名稱。

cd /etc/barman.d

cp streaming-server.conf-template pg.conf

 

5.2 編輯 pg.conf,將 [streaming] 修改為 [pg],這是備份任務的名稱,並配置如下參數:

conninfo =  host=10.40.239.228 port=5432 user=barman dbname=postgres password=barman123

 

streaming_conninfo = host=10.40.239.228 port=5432 dbname=postgres user=streaming_barman password=streaming_barman123

backup_method = postgres

streaming_archiver = on

slot_name = barman

path_prefix = "/etc/barman.d/bin"

   

下面是這些參數的含義:

conninfo 基礎備份的連接信息。

streaming_conninfo 流歸檔的連接信息。

backup_method 基礎備份的方式。“postgres”表示使用 pg_basebackup 進行備份;rsync 表示使用rsync備份。

streaming_archiver 是否啟用流歸檔。on 表示是。

slot_name 復制槽的名稱

path_prefix 客戶端的postgresql 的bin 的路徑。

 

3    使用barman備份和恢復

3.1 備份

下面的操作都在備份服務器上進行。

1.執行barman的命令,創建名為 pg的復制槽

barman receive-wal --create-slot pg

 

2. 在后台不間斷地從數據庫服務端接收wal日志:

barman receive-wal pg &

    注意,& 表示在后台執行前面的命令。

 

3.檢查備份任務pg 的運行狀態

barman check pg

 

 

如果各項結果均為 OK,則表示狀態正常。

 

4. 做一次基礎備份

        barman backup pg

基礎備份文件位於 /var/lib/barman/pg/base 中。

 

5. 設置常規的定時備份方案

 

設置每5分鍾檢查一次barman 服務的狀態,進行一次維護操作:

echo "*/5 * * * * barman barman cron" >> /etc/crontab

 

其中,barman cron這條命令還可以維護barman后台的“穩態”。例如,它可以檢查備份文件是否缺失,清理過期的備份文件。

 

設置 7天做一次基礎備份:

echo "* * */7 * * barman barman backup pg" /etc/crontab

 

3.2 恢復

  1. 在數據庫服務器上停止數據庫。

 

  1. 在數據庫服務器,將數據庫的data目錄的屬主修改為barman

chown -R barman.barman ./data

 

  1. 在barman 服務器上,執行命令恢復數據庫。

3.1 首先查看有哪些基礎備份:

    barman list-backup pg

結果示例如下:

pg 20200817T120000 - Mon Aug 17 12:00:00 2020 - Size: 515.6 MiB - WAL Size: 64.0 MiB - WAITING_FOR_WALS

pg 20200810T120000 - Mon Aug 10 12:00:00 2020 - Size: 412.5 MiB - WAL Size: 128.0 MiB

 

可以看到,pg有2個備份,分別是20200824T120000和20200817T120000。

 

3.2 恢復數據庫,我們選擇用20200810T120000恢復,恢復到 2020年8月23日 12時。

barman recover --target-time '2020-08-23 12:00:00' pg 20200810T120000 /opt/postgresql/data/

 

可以使用四個互斥選項之一指定恢復目標:

--target-time TARGET_TIME:指定時間戳

--target-xid TARGET_XID:指定交易ID

--target-name TARGET_NAME:指定先前使用pg_create_restore_point(name)函數創建的命名還原點

--target-immediate:達到一致狀態(即基本備份過程的結束)時,恢復將結束

恢復成功后,會收到提示:

Your PostgreSQL server has been successfully prepared for recovery!

 

  1. 在PostgreSQL服務器上,將data的屬主改回原來的用戶。如果以前的用戶是 postgres,那么命令就是:

chown -R postgres.postgres ./data

 

  1. 重新啟動 PostgreSQL,檢查服務是否正常。

 

  1. 數據庫服務器上的postgresql正常后,在備份服務器上

barman receive-wal --create-slot pg

 

  1. 在備份服務器上,在從數據庫服務端接收wal日志

barman receive-wal pg &

 

 

附錄

1 不同版本barman 對系統和軟件的要求

 

Barman 2.4 ~ 2.7

  • Linux/Unix
  • Python 2.6 or 2.7
  • Python modules:
    • argcomplete
    • argh >= 0.21.2 <= 0.26.2
    • argparse (Python 2.6 only)
    • psycopg2 >= 2.4.2
    • python-dateutil <> 2.0
    • setuptools
    • PostgreSQL >= 8.3
    • rsync >= 3.0.4 (對 PostgreSQL >= 9.2 是可選的)

 

Barman 2.8 ~ 2.11

  • Linux/Unix
  • Python >= 3.4
  • Python modules:
    • argcomplete
    • argh >= 0.21.2
    • psycopg2 >= 2.4.2
    • python-dateutil
    • setuptools
    • PostgreSQL >= 8.3
    • rsync >= 3.0.4 (對於 PostgreSQL >= 9.2 是可選的)

2 barman 用戶手冊

http://docs.pgbarman.org/release/2.11/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM