jsp高校運動會成績管理系統
使用 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。其結構如下圖所示:
在這種情況下,您將需要配置:
- 與PostgreSQL的標準連線,用於管理,協調和監視
- 供pg_basebackup(用於基本備份操作)和pg_receivewal(用於WAL流歸檔)使用的流複製連線
用Barman 的術語來說,此設定稱為streaming-only設定,因為它不需要任何
這種方案要求:
- 額外的SSH連線,以允許使用者postgres 在PostgreSQL伺服器以barman的使用者身份連線到Barman伺服器上。
- 在PostgreSQL的配置檔案postgresql.conf 中配置archive_command,內容是將WAL檔案歸檔到Barman的歸檔目錄。具體格式可參考官方手冊。
1.2.2 基於rsync/ SSH的備份
基於rsync/ SSH的備份是一種傳統的基於檔案的備份方式。它一般適用於下面的情形。
- PostgreSQL
- 使用表空間的PostgreSQL伺服器版本是9.2或9.3
- 增量備份,並行備份和重複資料刪除
- 備份期間的網路壓縮
- 更好地控制頻寬使用,包括在表空間的基礎上
它的體系結構如下圖所示:
在這種情況下,您將需要配置:
- 與PostgreSQL的標準連線,用於管理,協調和監視
- 用於基礎備份操作的SSH連線,rsync會使用它,以允許barman使用者在Barman伺服器上以使用者postgres的身份連線到PostgreSQL伺服器
- 用於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
2. 安裝 Python 3.6
yum -y install python3-3.6.8-13.el7.x86_64
注意,如果你不想指定Python 3的版本,可以換成執行:
yum -y install python3
在Python 3安裝過程中,setuptools 和 pip 也會被安裝。
3. 安裝rsync
yum -y install rsync
4. 安裝 pgdg 源
rpm -ivh https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
5. 安裝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:使用原始碼安裝。
- 在SourceForge 上下載barman原始碼,這裡我們下載 barman-2.11.tar.gz。
- 解壓檔案後,進入解壓後的目錄,使用python3安裝它:
tar -zxvf barman-2.11.tar.gz
cd ./barman-2.11
python3 ./setup.py build
python3 ./setup.py install
3. 使用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/
4. 在作業系統中建立使用者barman
useradd -m barman -d /var/lib/barman
5. 將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。
2. 把PostgreSQL 的 bin 目錄複製到備份伺服器的目錄 /etc/barman.d/中。可以使用scp。例如,PostgreSQL的bin 的位置是 /opt/postgresql/bin,則可以執行如下命令:
scp -r /opt/postgresql/bin [email protected]:/etc/barman.d/
3. 在資料庫中建立使用者 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;
4. 資料庫的配置檔案 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。
5. 在 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。
2. 複製使用者barman 的公鑰檔案 id_rsa.pub 中的內容,並將它追加到資料庫伺服器上
的使用者 postgres 的主目錄中的檔案 .ssh/authorized_keys 中。這樣做的目的是允許barman以使用者postgres的身份免密訪問資料庫伺服器
你可以手動複製此內容,並將它貼上到資料庫伺服器上的使用者 postgres 的主目錄中的檔案 .ssh/authorized_keys 末尾。
你也可以通過下面的命令完成複製。如果使用下面的命令,你需要保證資料庫伺服器上 postgres使用者已經設定了密碼。
ssh-copy-id [email protected]
3.建立 barman 的日誌目錄 /var/log/barman
mkdir /var/log/barman
4. 編輯 /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 所需的最小備份數量
5. 配置要備份的資料庫的資訊。
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. 在資料庫伺服器上停止資料庫。
2. 在資料庫伺服器,將資料庫的data目錄的屬主修改為barman
chown -R barman.barman ./data
3. 在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!
4. 在PostgreSQL伺服器上,將data的屬主改回原來的使用者。如果以前的使用者是 postgres,那麼命令就是:
chown -R postgres.postgres ./data
5. 重新啟動 PostgreSQL,檢查服務是否正常。
6. 資料庫伺服器上的postgresql正常後,在備份伺服器上
barman receive-wal --create-slot pg
7. 在備份伺服器上,在從資料庫服務端接收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 是可選的)