pgBackRest使用指南
pgBackRest的简单使用
1. 安装
表1 软硬件要求
软件 | 版本 |
---|---|
pgbackrest | 2.52 |
操作系统 | 处理器架构 |
CentOS | x86_64 |
从源代码构建时,最好使用构建主机,而不是在生产环境中构建。生成所需的许多工具通常不应安装在生产环境中。pgBackRest 由一个可执行文件组成,因此一旦构建就很容易复制到新主机。
build ⇒ 将pgBackRest 的 2.52 版本下载到 /build 路径
[root@localhost]# mkdir -p /build
[root@localhost]# cd /build
[root@localhost build]# wget https://github.com/pgbackrest/pgbackrest/archive/refs/tags/release/2.52.tar.gz
[root@localhost build]#tar -zxvf 2.52.tar.gz -C /build
build ⇒ 安装 build 依赖项
sudo yum install make gcc openssl-devel libxml2-devel lz4-devel libzstd-devel bzip2-devel libyaml-devel libssh2-devel postgresql-devel
build⇒配置和编译 pgBackRest
[root@localhost build]#cd /build/pgbackrest-release-2.52/src && ./configure && make -j8
[root@localhost src]# make install -j8
安装完成之后检查。
[fbase@localhost backup]$ pgbackrest
pgBackRest 2.52 - General help
Usage:
pgbackrest [options] [command]
Commands:
annotate Add or modify backup annotation.
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.
verify Verify contents of the repository.
version Get version.
Use 'pgbackrest help [command]' for more information.
2. 配置
在安装完毕后,后面就是配置相关的pgbackrest需要的配置信息,这里需要进行设置一个供pgbackrest放置配置文件和日志的目录,这里在磁盘目录上建立一个目录 pgbackrest 并且需要postgres 账号在此目录有绝对权限。
这里需要注意,严格按照配置文档中的信息填写,基于编译后,默认文件夹和配置文件读取的位置暂时不能变化。
# 创建日志文件夹
sudo mkdir -p -m 770 /var/log/pgbackrest
# 给fbase用户权限
sudo chown fbase:fbase /var/log/pgbackrest
# 创建
sudo mkdir -p /etc/pgbackrest
# 创建
sudo mkdir -p /etc/pgbackrest/conf.d
# 创建配置文件
sudo touch /etc/pgbackrest/pgbackrest.conf
# 修改配置文件权限
sudo chmod 640 /etc/pgbackrest/pgbackrest.conf
# 给fbase用户配置文件权限
sudo chown fbase:fbase /etc/pgbackrest/pgbackrest.conf
然后针对pgbackrest 的配置文件进行简单的配置
[test]
pg1-path=/data/fbase/fbdata
pg1-port=8432
pg1-socket-path=/home/fbase
[global]
repo1-path=/pgbackrest/backup
repo1-retention-full=2
log-level-console=info
log-level-file=debug
[global:archive-push]
compress-level=3
修改PostgreSQL数据库的postgresql.conf
文件。
archive_command = 'pgbackrest --stanza=test archive-push %p'
3. 检查
使用pgbackrest --stanza=test check
进行检查。
注意:
-
使用
fbase
用户而不是root
用户执行该命令。 -
得手动创建repo1-path的路径并且将权限赋予给
fbase
用户。sudo mkdir -p /pgbackrest/backup sudo chown fbase:fbase /pgbackrest/backup
-
创建完目录之后,如果出现如下报错信息,需要进行pgbackrest的初始化操作。
[fbase@localhost backup]$ pgbackrest --stanza=test stanza-create 2024-07-31 17:43:23.544 P00 INFO: stanza-create command begin 2.52: --exec-id=25893-ff3b6559 --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --stanza=test 2024-07-31 17:43:24.211 P00 INFO: stanza-create for stanza 'test' on repo1 2024-07-31 17:43:24.233 P00 INFO: stanza-create command end: completed successfully (691ms) [fbase@localhost backup]$ ll total 0 drwxr-x---. 3 fbase fbase 17 Jul 31 17:43 archive drwxr-x---. 3 fbase fbase 17 Jul 31 17:43 backup
完成这些操作之后,就可以再次执行pgbackrest --stanza=test check
来查看是否可以使用。
[fbase@localhost backup]$ pgbackrest --stanza=test check
2024-07-31 17:43:31.516 P00 INFO: check command begin 2.52: --exec-id=25902-afd5299b --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --stanza=test
2024-07-31 17:43:32.135 P00 INFO: check repo1 configuration (primary)
2024-07-31 17:43:32.352 P00 INFO: check repo1 archive for WAL (primary)
2024-07-31 17:43:32.659 P00 INFO: WAL segment 000000010000000000000005 successfully archived to '/pgbackrest/backup/archive/test/16-1/0000000100000000/000000010000000000000005-bb69fe3aa1ef407b0682b987a1670edec6c4c310.gz' on repo1
2024-07-31 17:43:32.660 P00 INFO: check command end: completed successfully (1147ms)
4. 本地服务器备份操作
先针对数据库进行全备,然后在进行增量备份等。
[fbase@localhost backup]$ pgbackrest --stanza=test --type=full backup
2024-07-31 18:16:06.187 P00 INFO: backup command begin 2.52: --exec-id=27616-913efd45 --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test --type=full
2024-07-31 18:16:07.144 P00 INFO: execute non-exclusive backup start: backup begins after the next regular checkpoint completes
WARN: start-fast is disabled and db-timeout (1800s) is smaller than the PostgreSQL checkpoint_timeout (1800s) - timeout may occur before the backup starts
2024-07-31 18:16:07.868 P00 INFO: backup start archive = 000000010000000000000007, lsn = 0/7000060
2024-07-31 18:16:07.868 P00 INFO: check archive for prior segment 000000010000000000000006
2024-07-31 18:16:17.739 P00 INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2024-07-31 18:16:17.944 P00 INFO: backup stop archive = 000000010000000000000007, lsn = 0/7000138
2024-07-31 18:16:17.949 P00 INFO: check archive for segment(s) 000000010000000000000007:000000010000000000000007
2024-07-31 18:16:17.977 P00 INFO: new backup label = 20240731-181606F
2024-07-31 18:16:18.029 P00 INFO: full backup size = 22.7MB, file total = 976
2024-07-31 18:16:18.029 P00 INFO: backup command end: completed successfully (11845ms)
2024-07-31 18:16:18.030 P00 INFO: expire command begin 2.52: --exec-id=27616-913efd45 --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2024-07-31 18:16:18.037 P00 INFO: expire command end: completed successfully (8ms)
可以进行差异备份。
[fbase@localhost backup]$ pgbackrest --stanza=test --type=diff --log-level-console=info backup
2024-07-31 18:25:47.044 P00 INFO: backup command begin 2.52: --exec-id=28116-2901a500 --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test --type=diff
2024-07-31 18:25:47.834 P00 INFO: last backup label = 20240731-181606F, version = 2.52
2024-07-31 18:25:47.834 P00 INFO: execute non-exclusive backup start: backup begins after the next regular checkpoint completes
WARN: start-fast is disabled and db-timeout (1800s) is smaller than the PostgreSQL checkpoint_timeout (1800s) - timeout may occur before the backup starts
2024-07-31 18:25:48.553 P00 INFO: backup start archive = 00000001000000000000000A, lsn = 0/A000028
2024-07-31 18:25:48.553 P00 INFO: check archive for prior segment 000000010000000000000009
2024-07-31 18:25:49.838 P00 INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2024-07-31 18:25:50.046 P00 INFO: backup stop archive = 00000001000000000000000A, lsn = 0/A000138
2024-07-31 18:25:50.052 P00 INFO: check archive for segment(s) 00000001000000000000000A:00000001000000000000000A
2024-07-31 18:25:50.073 P00 INFO: new backup label = 20240731-181606F_20240731-182547D
2024-07-31 18:25:50.118 P00 INFO: diff backup size = 1.6MB, file total = 981
2024-07-31 18:25:50.118 P00 INFO: backup command end: completed successfully (3077ms)
2024-07-31 18:25:50.118 P00 INFO: expire command begin 2.52: --exec-id=28116-2901a500 --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2024-07-31 18:25:50.124 P00 INFO: expire command end: completed successfully (6ms)
在备份后对数据库进行恢复。
# 停止数据库
pg_ctl -D /data/fbase/fbdata stop
# 对原来的数据库文件进行备份
mkdir -p /tmp/data/fbdata
cd /data/fbase/fbdata
tar czvf /tmp/data/fbdata/fbdata.tar.gz *
# 使用pgbackrest命令
pgbackrest --stanza=test --log-level-console=info restore
结果显示。
[fbase@localhost backup]$ pgbackrest --stanza=test --log-level-console=info restore
2024-07-31 18:39:13.554 P00 INFO: restore command begin 2.52: --exec-id=28853-0e5ddf8c --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --repo1-path=/pgbackrest/backup --stanza=test
2024-07-31 18:39:13.645 P00 INFO: repo1: restore backup set 20240731-181606F_20240731-182547D, recovery will start at 2024-07-31 18:25:47
ERROR: [040]: unable to restore to path '/data/fbase/fbdata' because it contains files
HINT: try using --delta if this is what you intended.
2024-07-31 18:39:13.647 P00 INFO: restore command end: aborted with exception [040]
删除原先数据库的数据文件,再使用pgbackup工具恢复。
cd /data/fbase/fbdata/
rm -rf *
pgbackrest --stanza=test --log-level-console=info restore
[fbase@localhost root]$ cd /data/fbase/fbdata/
[fbase@localhost fbdata]$ rm -rf *
[fbase@localhost fbdata]$ ll
total 0
[fbase@localhost fbdata]$ pgbackrest --stanza=test --log-level-console=info restore
2024-07-31 18:41:31.978 P00 INFO: restore command begin 2.52: --exec-id=29018-c21e87e0 --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --repo1-path=/pgbackrest/backup --stanza=test
2024-07-31 18:41:32.005 P00 INFO: repo1: restore backup set 20240731-181606F_20240731-182547D, recovery will start at 2024-07-31 18:25:47
2024-07-31 18:41:36.537 P00 INFO: write updated /data/fbase/fbdata/postgresql.auto.conf
2024-07-31 18:41:36.553 P00 INFO: restore global/pg_control (performed last to ensure aborted restores cannot be started)
2024-07-31 18:41:36.555 P00 INFO: restore size = 22.7MB, file total = 981
2024-07-31 18:41:36.556 P00 INFO: restore command end: completed successfully (4583ms)
[fbase@localhost fbdata]$ ll
total 100
-rw-------. 1 fbase fbase 257 Jul 31 18:25 backup_label
drwx------. 5 fbase fbase 30 Jul 31 18:41 base
-rw-------. 1 fbase fbase 44 Jul 31 17:02 current_logfiles
drwx------. 2 fbase fbase 4096 Jul 31 18:41 global
drwx------. 2 fbase fbase 4096 Jul 31 18:41 log
drwx------. 2 fbase fbase 17 Jul 31 18:41 pg_commit_ts
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_dynshmem
-rw-------. 1 fbase fbase 5847 Jul 31 16:24 pg_hba.conf
-rw-------. 1 fbase fbase 2640 Jul 31 16:24 pg_ident.conf
drwx------. 4 fbase fbase 65 Jul 31 18:41 pg_logical
drwx------. 4 fbase fbase 34 Jul 31 18:41 pg_multixact
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_notify
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_replslot
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_serial
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_snapshots
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_stat
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_stat_tmp
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_subtrans
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_tblspc
drwx------. 2 fbase fbase 6 Jul 31 18:41 pg_twophase
-rw-------. 1 fbase fbase 3 Jul 31 16:24 PG_VERSION
drwx------. 3 fbase fbase 27 Jul 31 18:41 pg_wal
drwx------. 2 fbase fbase 17 Jul 31 18:41 pg_xact
-rw-------. 1 fbase fbase 229 Jul 31 18:41 postgresql.auto.conf
-rw-------. 1 fbase fbase 30600 Jul 31 17:02 postgresql.conf
-rw-------. 1 fbase fbase 29635 Jul 31 16:24 postgresql.conf.backup
-rw-------. 1 fbase fbase 0 Jul 31 18:41 recovery.signal
尝试启动数据库。
pg_ctl -D /data/fbase/fbdata start
[fbase@localhost fbdata]$ pg_ctl -D /data/fbase/fbdata start
waiting for server to start....2024-07-31 18:42:47 CST [29083]: [1-1] user=,db= LOG: 00000: redirecting log output to logging collector process
2024-07-31 18:42:47 CST [29083]: [2-1] user=,db= HINT: Future log output will appear in directory "log".
2024-07-31 18:42:47 CST [29083]: [3-1] user=,db= LOCATION: SysLogger_Start, syslogger.c:715
done
server started
[fbase@localhost fbdata]$ psql
psql (16.3)
Type "help" for help.
postgres=#
查看表结构和内容。
postgres=# \l
List of databases
Name | Owner | Encoding | Locale Provider | Collate | Ctype | ICU Locale | ICU Rules | Access privileges
-----------+-------+----------+-----------------+---------+-------+------------+-----------+-------------------
postgres | fbase | UTF8 | libc | C | C | | |
template0 | fbase | UTF8 | libc | C | C | | | =c/fbase +
| | | | | | | | fbase=CTc/fbase
template1 | fbase | UTF8 | libc | C | C | | | =c/fbase +
| | | | | | | | fbase=CTc/fbase
(3 rows)
postgres=# \d
List of relations
Schema | Name | Type | Owner
--------+------------------------+----------+-------
public | cities | table | fbase
public | cities_id_seq | sequence | fbase
public | computer_config | table | fbase
public | computer_config_id_seq | sequence | fbase
(4 rows)
postgres=# select * from computer_config;
id | cpu | gpu | power_supply | case_type | ram | storage | motherboard | price
----+----------------------+-------------------------+---------------+------------+-----------+--------------+-----------------------+---------
1 | Intel Core i9-13900K | NVIDIA GeForce RTX 4090 | 750W Gold | Mid Tower | 32GB DDR5 | 1TB NVMe SSD | ASUS ROG Maximus Z790 | 3500.00
2 | AMD Ryzen 7 7800X | AMD Radeon RX 7900 XT | 650W Platinum | ATX | 16GB DDR4 | 500GB SSD | MSI MAG B550 | 2200.00
3 | Intel Core i7-13700K | NVIDIA GeForce RTX 4080 | 850W Gold | Full Tower | 32GB DDR4 | 2TB HDD | Gigabyte Z690 AORUS | 2900.00
4 | AMD Ryzen 5 7600X | NVIDIA GeForce RTX 4070 | 600W Gold | Micro ATX | 16GB DDR5 | 1TB SSD | ASRock B650 | 1800.00
5 | Intel Core i5-13600K | Intel Arc A770 | 700W Bronze | Mini ITX | 32GB DDR4 | 1TB SSD | ASUS ROG Strix Z690 | 2100.00
(5 rows)
查看pgbackup的日志文件。
[root@localhost test]# cd /var/log/pgbackrest/
[root@localhost pgbackrest]# ll
总用量 2600
-rw-r-----. 1 fbase fbase 1486139 7月 31 18:25 test-backup.log
-rw-r-----. 1 fbase fbase 21873 7月 31 18:25 test-expire.log
-rw-r-----. 1 fbase fbase 624595 7月 31 18:41 test-restore.log
-rw-r-----. 1 fbase fbase 95947 7月 31 17:43 test-stanza-create.log
4.1backup 创建 Stanza
su - fbase
pgbackrest --stanza=test --log-level-console=info stanza-create
4.2 backup 检查配置文件
pgbackrest --stanza=test --log-level-console=info check
4.3 backup full
pgbackrest --stanza=test --log-level-console=info --start-fast --type=full backup
[fbase@pg1 backup]$ pgbackrest --stanza=test --log-level-console=info --start-fast --type=full backup
2024-08-01 01:29:33.510 P00 INFO: backup command begin 2.52: --exec-id=50115-16e389e8 --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test --start-fast --type=full
2024-08-01 01:29:34.535 P00 INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2024-08-01 01:29:35.255 P00 INFO: backup start archive = 00000002000000000000000E, lsn = 0/E000060
2024-08-01 01:29:35.255 P00 INFO: check archive for prior segment 00000002000000000000000D
2024-08-01 01:29:42.323 P00 INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2024-08-01 01:29:42.526 P00 INFO: backup stop archive = 00000002000000000000000E, lsn = 0/E000138
2024-08-01 01:29:42.531 P00 INFO: check archive for segment(s) 00000002000000000000000E:00000002000000000000000E
2024-08-01 01:29:42.555 P00 INFO: new backup label = 20240801-012934F
2024-08-01 01:29:42.596 P00 INFO: full backup size = 22.8MB, file total = 985
2024-08-01 01:29:42.596 P00 INFO: backup command end: completed successfully (9089ms)
2024-08-01 01:29:42.596 P00 INFO: expire command begin 2.52: --exec-id=50115-16e389e8 --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2024-08-01 01:29:42.622 P00 INFO: repo1: 16-1 remove archive, start = 000000010000000000000005, stop = 000000010000000000000006
2024-08-01 01:29:42.622 P00 INFO: expire command end: completed successfully (26ms)
4.4 backup diff
pgbackrest --stanza=test --log-level-console=info --start-fast --type=diff backup
[fbase@pg1 backup]$ pgbackrest --stanza=test --log-level-console=info --start-fast --type=diff backup
2024-08-01 01:30:38.060 P00 INFO: backup command begin 2.52: --exec-id=50173-84a77e2e --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test --start-fast --type=diff
2024-08-01 01:30:38.855 P00 INFO: last backup label = 20240801-012934F, version = 2.52
2024-08-01 01:30:38.855 P00 INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2024-08-01 01:30:39.594 P00 INFO: backup start archive = 000000020000000000000010, lsn = 0/10000028
2024-08-01 01:30:39.594 P00 INFO: check archive for prior segment 00000002000000000000000F
2024-08-01 01:30:40.583 P00 INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2024-08-01 01:30:40.793 P00 INFO: backup stop archive = 000000020000000000000010, lsn = 0/10000138
2024-08-01 01:30:40.799 P00 INFO: check archive for segment(s) 000000020000000000000010:000000020000000000000010
2024-08-01 01:30:40.823 P00 INFO: new backup label = 20240801-012934F_20240801-013038D
2024-08-01 01:30:40.867 P00 INFO: diff backup size = 12KB, file total = 985
2024-08-01 01:30:40.867 P00 INFO: backup command end: completed successfully (2810ms)
2024-08-01 01:30:40.867 P00 INFO: expire command begin 2.52: --exec-id=50173-84a77e2e --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2024-08-01 01:30:40.877 P00 INFO: repo1: 16-1 no archive to remove
2024-08-01 01:30:40.877 P00 INFO: expire command end: completed successfully (10ms)
4.5 backup incr
pgbackrest --stanza=test --log-level-console=info --start-fast --type=incr backup
[fbase@pg1 backup]$ pgbackrest --stanza=test --log-level-console=info --start-fast --type=incr backup
2024-08-01 01:32:00.307 P00 INFO: backup command begin 2.52: --exec-id=50264-16df3efa --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --pg1-port=8432 --pg1-socket-path=/home/fbase --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test --start-fast --type=incr
2024-08-01 01:32:01.121 P00 INFO: last backup label = 20240801-012934F_20240801-013038D, version = 2.52
2024-08-01 01:32:01.121 P00 INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2024-08-01 01:32:01.856 P00 INFO: backup start archive = 000000020000000000000012, lsn = 0/12000028
2024-08-01 01:32:01.856 P00 INFO: check archive for prior segment 000000020000000000000011
2024-08-01 01:32:02.557 P00 INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2024-08-01 01:32:02.764 P00 INFO: backup stop archive = 000000020000000000000012, lsn = 0/12000138
2024-08-01 01:32:02.772 P00 INFO: check archive for segment(s) 000000020000000000000012:000000020000000000000012
2024-08-01 01:32:02.799 P00 INFO: new backup label = 20240801-012934F_20240801-013200I
2024-08-01 01:32:02.848 P00 INFO: incr backup size = 14KB, file total = 985
2024-08-01 01:32:02.848 P00 INFO: backup command end: completed successfully (2544ms)
2024-08-01 01:32:02.848 P00 INFO: expire command begin 2.52: --exec-id=50264-16df3efa --log-level-console=info --log-level-file=debug --repo1-path=/pgbackrest/backup --repo1-retention-full=2 --stanza=test
2024-08-01 01:32:02.855 P00 INFO: repo1: 16-1 no archive to remove
2024-08-01 01:32:02.855 P00 INFO: expire command end: completed successfully (7ms)
4.6 backup restore
pgbackrest --stanza=test --log-level-console=info restore
[fbase@localhost backup]$ pgbackrest --stanza=test --log-level-console=info restore
2024-07-31 18:39:13.554 P00 INFO: restore command begin 2.52: --exec-id=28853-0e5ddf8c --log-level-console=info --log-level-file=debug --pg1-path=/data/fbase/fbdata --repo1-path=/pgbackrest/backup --stanza=test
2024-07-31 18:39:13.645 P00 INFO: repo1: restore backup set 20240731-181606F_20240731-182547D, recovery will start at 2024-07-31 18:25:47
ERROR: [040]: unable to restore to path '/data/fbase/fbdata' because it contains files
HINT: try using --delta if this is what you intended.
2024-07-31 18:39:13.647 P00 INFO: restore command end: aborted with exception [040]
4.7 backup info
pgbackrest info
[fbase@pg1 backup]$ pgbackrest info
stanza: test
status: ok
cipher: none
db (current)
wal archive min/max (16): 000000010000000000000007/000000020000000000000012
full backup: 20240731-181606F
timestamp start/stop: 2024-07-31 18:16:06+08 / 2024-07-31 18:16:17+08
wal start/stop: 000000010000000000000007 / 000000010000000000000007
database size: 22.7MB, database backup size: 22.7MB
repo1: backup set size: 3MB, backup size: 3MB
diff backup: 20240731-181606F_20240731-182547D
timestamp start/stop: 2024-07-31 18:25:47+08 / 2024-07-31 18:25:49+08
wal start/stop: 00000001000000000000000A / 00000001000000000000000A
database size: 22.7MB, database backup size: 1.6MB
repo1: backup set size: 3MB, backup size: 181.7KB
backup reference list: 20240731-181606F
full backup: 20240801-012934F
timestamp start/stop: 2024-08-01 01:29:34+08 / 2024-08-01 01:29:42+08
wal start/stop: 00000002000000000000000E / 00000002000000000000000E
database size: 22.8MB, database backup size: 22.8MB
repo1: backup set size: 3MB, backup size: 3MB
diff backup: 20240801-012934F_20240801-013038D
timestamp start/stop: 2024-08-01 01:30:38+08 / 2024-08-01 01:30:40+08
wal start/stop: 000000020000000000000010 / 000000020000000000000010
database size: 22.8MB, database backup size: 12KB
repo1: backup set size: 3MB, backup size: 1.4KB
backup reference list: 20240801-012934F
incr backup: 20240801-012934F_20240801-013200I
timestamp start/stop: 2024-08-01 01:32:00+08 / 2024-08-01 01:32:02+08
wal start/stop: 000000020000000000000012 / 000000020000000000000012
database size: 22.8MB, database backup size: 14KB
repo1: backup set size: 3MB, backup size: 1.6KB
backup reference list: 20240801-012934F, 20240801-012934F_20240801-013038D
5. 远程服务器备份操作
5.1 环境准备
环境准备 | 主机地址 | 主机名 | 版本 | 作用 |
---|---|---|---|---|
CentOS-7 | 192.168.198.153 | standby | pg15.7 | 数据库备服务器 |
CentOS-7 | 192.168.198.152 | primary | pg15.7 | 数据库主服务器 |
CentOS-7 | 192.168.198.158 | build | fbase16.3 | pgbackrest复制服务器 |
CentOS-7 | 192.168.198.160 | repository | 无 | pgbackrest备份服务器 |
5.2 在备份服务器上安装pgbackrest
创建 pgbackrest 用户是为了拥有 pgBackRest 存储库。任何用户都可以拥有存储库,但最好不要使用 postgres(如果存在)以避免混淆。
repository ⇒ 创建 pgbackrest 用户
[root@repository ~]#useradd pgbackrest
repository ⇒ 安装依赖项
[root@repository ~]#yum install -y libzstd postgresql-libs libssh2
repository ⇒ 从构建主机复制 pgBackRest 二进制文件
[root@repository ~]#scp build:/build/pgbackrest-release-2.52/src/pgbackrest /usr/bin
[root@repository ~]#chmod 755 /usr/bin/pgbackrest
[root@repository pgbackrest]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.198.158 build
192.168.198.152 primary
192.168.198.153 standby
192.168.198.160 repository
pgBackRest 需要日志和配置目录以及配置文件。
repository ⇒ 创建 pgBackRest 配置文件和目录
sudo mkdir -p -m 770 /var/log/pgbackrest
sudo chown -Rf pgbackrest:pgbackrest /var/log/pgbackrest
sudo mkdir -p /etc/pgbackrest
sudo mkdir -p /etc/pgbackrest/conf.d
sudo touch /etc/pgbackrest/pgbackrest.conf
sudo chmod 640 /etc/pgbackrest/pgbackrest.conf
sudo chown pgbackrest:pgbackrest /etc/pgbackrest/pgbackrest.conf
repository ⇒ 创建 pgBackRest 存储库
sudo mkdir -p /var/lib/pgbackrest
sudo chmod 750 /var/lib/pgbackrest
sudo chown -Rf pgbackrest:pgbackrest /var/lib/pgbackrest
5.3 在数据库主服务器上安装pgbackrest
根据单机安装文档操作。
安装步骤与pgBackRest在备份服务器上的安装方式没有区别。唯一的区别是在这种情况下日志目录将由 postgres 用户拥有。
[root@primary ~]#su - postgres
[postgres@primary ~]#yum install -y libzstd postgresql-libs libssh2
[postgres@primary ~]#scp build:/build/pgbackrest-release-2.52/src/pgbackrest /usr/bin
[postgres@primary ~]#chmod 755 /usr/bin/pgbackrest
pgBackRest 需要日志和配置目录以及配置文件。
sudo mkdir -p -m 770 /var/log/pgbackrest
sudo chown -Rf postgres:postgres /var/log/pgbackrest
sudo mkdir -p /etc/pgbackrest
sudo mkdir -p /etc/pgbackrest/conf.d
sudo touch /etc/pgbackrest/pgbackrest.conf
sudo chmod 640 /etc/pgbackrest/pgbackrest.conf
sudo chown postgres:postgres /etc/pgbackrest/pgbackrest.conf
primary⇒ 创建 pgBackRest 存储库。(可以不配置)
sudo mkdir -p /var/lib/pgbackrest
sudo chmod 750 /var/lib/pgbackrest
sudo chown -Rf postgres:postgres /var/lib/pgbackrest
5.4 数据库配置配置
在安装好postgresql数据库的服务器中配置。
pg_hba.conf配置:
su postgres
cd /home/postgres/data
vi pg_hba.conf
# 加入下面这行
host all all 0.0.0.0/0 trust
postgresql.conf配置:
vi postgresql.conf
# 修改下面的配置
archive_command = 'pgbackrest --stanza=demo archive-push %p'
archive_mode = on
listen_addresses = '*'
log_line_prefix = '%m [%p]'
max_wal_senders = 10
wal_level = replica
5.5 pgbackrest配置
Postgres 集群应该知道它可以将档案流式传输到的 pgBackRest 存储库。相同的存储库信息还可用于恢复备份或执行时间点恢复。为此,我们需要创建一个 pgBackRest 配置文件,并将一些细节添加到 postgres 服务器上的这个文件中。
primary ⇒ 配置pgBackRest /etc/pgbackrest/pgbackrest.conf
。
[demo]
pg1-path=/home/postgres/data
pg1-socket-path=/tmp
[global]
log-level-file=detail
repo1-host=repository
repo1-retention-full=7
repository ⇒ 配置pgBackRest /etc/pgbackrest/pgbackrest.conf
[demo]
pg1-host=primary
pg1-path=/home/postgres/data
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=7
start-fast=y
5.6 配置SSH
pgBackRest 可以将 TLS 与客户端证书结合使用,以启用主机之间的通信。也可以使用 SSH
如下是使用ssh通信的设置步骤:
primary ⇒ 生成SSH密钥对
su - postgres
[postgres@primary ~]$ ssh-keygen -t rsa -b 4096 -C "pgbackrest"
生成的密钥默认存储在~/.ssh/id_rsa
和~/.ssh/id_rsa.pub
。
primary ⇒ 将公钥复制到备份仓库主机
[postgres@primary ~]$ ssh-copy-id pgbackrest@repository
如果默认路径不是~/.ssh/id_rsa.pub
,请指定公钥路径。
repository ⇒ 生成SSH密钥对
su - pgbackrest
[pgbackrest@repository ~]$ ssh-keygen -t rsa -b 4096 -C "pgbackrest"
[pgbackrest@repository ~]$ ssh-copy-id postgres@primary
5.7 在 PostgreSQL 服务器上配置必填参数
以下是要在 postgres 服务器上配置的强制参数,以确保 pgBackRest 成功运行。
archive_mode :此参数必须设置为“ON”以确保启用存档。如果未启用归档,pgBackRest 将无法工作。
$ psql -c "ALTER SYSTEM SET archive_mode TO 'ON'"
archive_command : archive_command 必须设置为以下命令。这将由 pgBackRest 处理。
$ psql -c "ALTER SYSTEM SET archive_command TO 'pgbackrest --stanza=pg1-server archive-push %p'"
如果必须修改*archive_mode,*则可能需要重新启动 PostgreSQL 服务器。否则,重新加载就足够了。
以下是执行重新加载或 SIGHUP 的命令。
$ pg_ctl -D /data/fbase/fbdata reload
如果需要,以下是执行重新启动的命令。
$ pg_ctl -D /data/fbase/fbdata restart -mf
5.8 验证
在新存储库中创建节。需要在远程控制端进行初始化,在远程控制端完成初始化后,在再数据库端进行初始化。
repository ⇒ 创建节
[root@repository ~]# sudo -u pgbackrest pgbackrest --stanza=demo stanza-create
如果出现以下错误,说明pgbackrest的权限不够
ERROR: [050]: unable to acquire lock on file '/tmp/pgbackrest/demo-archive.lock': Permission denied
HINT: does 'pgbackrest:pgbackrest' running pgBackRest have permissions on the '/tmp/pgbackrest/demo-archive.lock' file?
执行一下命令可解决
[root@repository pgbackrest]# chown -Rf pgbackrest:pgbackrest /tmp/pgbackrest/
检查数据库和存储库主机上的配置是否正确。
primary ⇒ 检查配置
sudo -u postgres
pgbackrest --stanza=demo check
[postgres@primary pgbackrest]$ pgbackrest --stanza=demo check
2024-08-02 14:40:00.010 CST [17226] LOG: restore point "pgBackRest Archive Check" created at 0/7000228
2024-08-02 14:40:00.010 CST [17226] STATEMENT: select pg_catalog.pg_create_restore_point('pgBackRest Archive Check')::text
repository⇒ 检查配置
sudo -u pgbackrest
pgbackrest --stanza=demo check
5.9 执行备份
要执行 PostgreSQL 集群的备份,请在存储库主机上使用 backup 命令运行 pgBackRest。
repository⇒备份演示集群
sudo -u pgbackrest
pgbackrest --stanza=demo backup
P00 WARN: no prior backup exists, incr backup has been changed to full
由于在存储库主机上创建了新的存储库,因此发出了有关增量备份更改为完整备份的警告。
5.10 还原备份
要执行 PostgreSQL 集群的还原,请在数据库主机上使用 restore 命令运行 pgBackRest。
primary ⇒ 停止演示集群,恢复并重启 PostgreSQL
pg_ctl stop
sudo -u postgres
pgbackrest --stanza=demo --delta restore
pg_ctl start
5.11 问题总结
问题一 SSH怎么无需密码登录
在使用命令生成密钥的时候,输入密码时不要输入密码,直接按两下空格即可。
ssh-keygen -t rsa -b 4096 -C "pgbackrest"
并且有时候登录需要你输入postgres或者pgbackrest用户的密码。进行下面的步骤。
# 查看用户是否需要密码,如果是LK的话最好是设置一下密码解锁
sudo passwd -S postgres
[root@primary ~]# sudo passwd -S postgres
postgres PS 2024-08-02 0 99999 7 -1 (密码已设置,使用 SHA512 算法。)
# 设置密码
sudo passwd postgres
# 解锁
sudo passwd -u postgres
还可以检查一下自己的SSH配置文件。
确保 /etc/ssh/sshd_config
文件中允许公钥认证并禁用密码认证:
vi /etc/ssh/sshd_config
# 修改配置
PubkeyAuthentication yes
PasswordAuthentication no
# 重新启动 SSH 服务
sudo systemctl restart sshd
问题二 如果使用了python环境,可能导致lib库有依赖问题,进行创建SSH密钥的时候会报错
可以在环境变量的LD_LIBRARY_PATH
路径。
su - fbase
vi ~/.bashrc
export LD_LIBRARY_PATH=/usr/local/fbase/16.3/lib:$LD_LIBRARY_PATH
# 修改为
export LD_LIBRARY_PATH=/usr/lib64:/usr/local/fbase/16.3/lib:$LD_LIBRARY_PATH
source ~/.bashrc
必须将/usr/lib64
放在第一个不然还是会报依赖错误。但是这样也会导致psql命令无法使用,但是数据库还是正常运行。
**问题三 ** 配置存储库主机时报错
[pgbackrest@repository .ssh]$ pgbackrest --stanza=demo stanza-create
WARN: unable to check pg1: [FileMissingError] raised from remote-0 ssh protocol on 'primary': unable to open missing file '/home/postgres/pgsql/data/global/pg_control' for read
ERROR: [056]: unable to find primary cluster - cannot proceed
HINT: are all available clusters in recovery?
这个问题是因为SSH的免密通道没有配置好,可以结合配置SSH和问题一的流程再次配置SSH密钥,可以将原来的密钥进行删除。
su - postgres
rm -rf /home/postgres/.ssh
su - pgbackrest
rm -rf /home/pgbackrest/.ssh
问题四 repo1-retention-ful
未设置
[pgbackrest@repository .ssh]$ pgbackrest --stanza=demo backup
WARN: option 'repo1-retention-full' is not set for 'repo1-retention-full-type=count', the repository may run out of space
HINT: to retain full backups indefinitely (without warning), set option 'repo1-retention-full' to the maximum.
WARN: no prior backup exists, incr backup has been changed to full
问题五 服务器处理libpq问题
# 数据库服务器
ln -s /usr/local/fbase/16.3/lib/libpq.so.5 /usr/lib64/libpq.so.5
# 备份服务器
ln -s /usr/local/fbase/16.3/lib/libpq.so.5 /usr/lib64/libpq.so.5
6. 总结
6.1 pgbackRest特征
-
并行备份和还原
自定义协议允许 pgBackRest 在本地或通过 TLS/SSH 远程,进行备份、恢复和归档,只需很少的配置。还通过协议层提供了查询 PostgreSQL 的接口,因而不需要远程访问 PostgreSQL,从而增强了安全性。
-
本地或远程操作
自定义协议允许 pgBackRest 在本地或通过 TLS/SSH 远程,进行备份、恢复和归档,只需很少的配置。还通过协议层提供了查询 PostgreSQL 的接口,因而不需要远程访问 PostgreSQL,从而增强了安全性。
-
多个存储库
支持多个存储库。例如,采用具有最少保留期的本地存储库进行快速恢复,以及采用具有较长保留期的远程存储库,以实现冗余和企业内的集中备份。
-
完整,增量和差异备份
支持全量备份、差异备份和增量备份。pgBackRest 不受 rsync 时间解析问题的影响,确保了差异备份和增量备份的安全,而无需对每个文件进行校验和。块级备份仅复制已更改的文件部分,以节省备份空间。
-
备份轮换和存档到期
可以为全量备份和差异备份设置保留策略,以创建覆盖任何时间范围的保留期。可以针对所有备份维护 WAL 归档文件,也可以严格针对最新备份进行维护。在后一种情况下,使旧备份保持一致所需的 WAL 文件会保留在归档中。
-
备份完整性
为备份中的每个文件计算校验和,并在还原或验证期间重新检查。在一次备份完成文件复制后,会一直等到使备份保持一致所需的每个 WAL 段到达存储库。
存储库中的备份,可以采用与 PostgreSQL 标准集群相同的格式(包括表空间)进行存储。如果禁用了压缩并启用了硬链接,则可以在存储库中创建备份快照,并直接在快照上启动 PostgreSQL 集群。这对于以传统方式恢复非常耗时的 TB 级数据库非常有利。
所有操作都会使用文件和目录级别的 fsync 来确保持久性。
-
页面校验和
如果启用了页面校验和,pgBackRest 会验证备份期间复制的每个文件的校验和。在全量备份期间会验证所有页面校验和,在差异备份和增量备份期间会验证已更改文件中的校验和。
验证失败不会停止备份过程,但会输出警告到控制台和日志文件,其中包含了哪些页面未通过验证的详细信息。
此功能允许,在包含有效数据副本的备份过期之前,及早检测到页面级损坏。
-
备份重启
中断的备份可以从停止的位置恢复。已经复制过的文件,会与清单中的校验和进行比较,以确保完整性。由于此操作可以完全在存储库主机上进行,因此它减少了 PostgreSQL 主机上的负载并节省了时间,因为校验和计算比压缩和重新传输数据更快。
-
流压缩和校验和
压缩跟校验和计算,是在将文件复制到存储库的过程中执行的,无论存储库位于本地还是远程。
如果存储库位于一个存储库主机上,则在 PostgreSQL 主机上执行压缩,以压缩格式传输文件,并简单地存储在存储库主机上。禁用压缩时,会采用一种较低级别的压缩来有效利用可用带宽,同时将 CPU 成本降至最低。
-
增量还原
清单包含了备份中每个文件的校验和,因此在还原期间可以使用这些校验和,来大大加快处理速度。在增量还原中,首先会删除备份中不存在的任何文件,然后为其余文件生成校验和。与备份匹配的文件会保留在原位,其余文件将照常还原。并行处理可以大大缩短还原时间。
-
并行,异步WAL Push&Get
提供了专用的命令,用于将 WAL 文件推送到归档,和从归档获取 WAL 文件。这两个命令都支持并行,以加速处理和异步运行,以提供对 PostgreSQL 的最快响应时间。
WAL 推送会自动检测多次推送的 WAL 段文件,并在段文件相同时进行重复数据删除,不然会引发错误。异步 WAL 推送允许将传输交接给另一个进程,该进程并行压缩 WAL 段文件以获得最大吞吐量。对于写入量极高的数据库来说,这可能是一个关键功能。
异步 WAL 获取维护了一个 WAL 段文件的本地队列,这些段文件已经解压缩并准备好重放。这减少了向 PostgreSQL 提供 WAL 所需的时间,从而最大限度地提高了重放速度。高延迟的连接和存储(如 S3)会受益最大。
推送和获取命令,会比较 PostgreSQL 版本和系统标识符,来确保数据库和存储库匹配。这实际上消除了错误配置 WAL 归档位置的可能性。
-
表空间和链接支持
表空间是完全受支持的,并且在恢复时,表空间可以重新映射到任何位置。还可以使用单个命令,将所有表空间重新映射到一个位置,这对于开发环境的还原很有用。
PostgreSQL 集群中的任何文件或目录,都支持文件和目录链接。还原时,可以将所有链接还原到其原始位置,重新映射部分或全部链接,或者将部分或全部链接还原为集群目录中的普通文件或目录。
-
加密
pgBackRest 可以加密存储库以保护备份,无论它们存储在何处。
-
与PostgreSQL > = 8.3的兼容性
pgBackRest 支持 PostgreSQL 最近的 10 个版本,包括 5 个当前支持版本和最后 5 个 EOL 版本。这样就有充足的时间升级到一个支持的版本。
-
支持 S3、Azure 和 GCS 兼容性对象存储
pgBackRest 存储库可以位于 S3、Azure 和 GCS 兼容的对象存储中,以实现几乎无限的容量和保留。
6.2 备份方式
-
全量备份:
将数据库集群的全部内容复制到备份中。数据库集群的第一个备份始终是全量备份。始终能够直接还原全量备份。全量备份不依赖于完整备份之外的任何文件来保持一致性。
-
增量备份:
仅复制自上次全量备份以来已更改的那些数据库集群文件。通过复制所选差异备份中的所有文件以及先前全量备份中的相应未更改文件来还原差异备份。差异备份的优点在于,与全量备份相比,它需要的磁盘空间更少,但是,差异备份和全量备份都必须有效才能还原差异备份。
-
差异备份:
仅复制自上次备份(可以是另一个增量备份,差异备份或全量备份)以来发生更改的那些数据库集群文件。由于增量备份仅包括自上次备份以来已更改的文件,因此它们通常比全量备份或差异备份小得多。与差异备份一样,增量备份依赖于其他备份才能有效还原增量备份。由于增量备份仅包括自上次备份以来的那些文件,因此,所有先前的增量备份(回到先前的差异备份),先前的差异备份和先前的全量备份都必须有效,才能执行增量备份的还原。如果不存在差异备份,则所有先前的所有增量备份都将还原为必须存在的先前的完整备份,并且完整备份本身必须有效才能还原增量备份。
附录
附录一 pgbackrest 常用命令
表3 pgbackrest 常用命令
命令 | 解释 | 示例 |
---|---|---|
backup | 创建数据库的备份 常用选项: `--type=full |
diff |
restore | 从备份恢复数据库--delta :执行增量恢复,只恢复不同的文件。--target-xid=XID :恢复到指定事务 ID。 --target-time='YYYY-MM-DD HH:MM:SS' :恢复到指定时间点。--target-name=restore-point-name :恢复到指定恢复点。 |
pgbackrest --stanza=your-stanza-name restore |
archive-push | 将 WAL 日志归档到备份存储 | pgbackrest --stanza=your-stanza-name archive-push /path/to/wal |
archive-get | 从备份存储中检索 WAL 日志。 | pgbackrest --stanza=your-stanza-name archive-get |
check | 检查配置和环境是否正确。 | pgbackrest --stanza=your-stanza-name check |
info | 显示存储库中的备份信息。 | pgbackrest --stanza=your-stanza-name info |
expire | 删除过期的备份和存档文件。 | pgbackrest --stanza=your-stanza-name expire |
stanza-create | 创建一个新的存储仓(stanza)。--log-level-console=level :设置控制台日志级别(如 info , error , warn )。--repo-path=/path/to/repo :指定备份存储库路径。--pg1-path=/path/to/pgdata :指定 PostgreSQL 数据目录路径。--process-max=N :设置最大并行进程数 |
pgbackrest --stanza=your-stanza-name stanza-create |
stanza-delete | 删除节,需要先停止数据库,指定repo | pgbackrest --stanza=demo --repo=1 --log-level-console=info stanza-delete |
附录二 stanza可设置参数详解
表4 stanza可设置参数详解
参数 | 解释 | 示例 |
---|---|---|
pg-path | PostgreSQL 数据目录 | example: pg1-path=/data/db |
pg-port | PostgreSQL 端口 | default: 5432 allowed: 0-65535 example: pg1-port=6543 |
pg-socket-path | 启动 PostgreSQL 时指定的 unix 套接字目录。 | example: pg1-socket-path=/var/run/postgresql |
pg1-user | 连接到 PostgreSQL 时使用的数据库用户名。如果未指定,pgBackRest 将与本地操作系统用户或 PGUSER 连接 | example: pg1-user=backupuser |
pg1-database | 连接到 PostgreSQL 时使用的数据库名称。默认值通常是最好的 | default: postgres example: pg1-database=backupdb |
pg1-host | 用于远程操作的 PostgreSQL 主机。 | example: pg1-host=db.domain.com |
pg-host-ca-file | 使用系统默认值以外的 CA 文件连接到 PostgreSQL 主机 | example: pg1-host-ca-file=/etc/pki/tls/certs/ca-bundle.crt |
pg-host-ca-path | 使用系统默认路径以外的 CA 路径连接到 PostgreSQL 主机 | example: pg1-host-ca-path=/etc/pki/tls/certs |
pg-host-cert-file | 发送到 PostgreSQL 主机以证明客户端身份 | example: pg1-host-cert-file=/path/to/client.crt |
pg-host-cmd | 仅当本地主机和 PostgreSQL 主机上 pgBackRest 命令的路径不同时,才需要。 | example: pg1-host-cmd=/usr/lib/backrest/bin/pgbackrest |
pg-host-config | 设置配置文件在 PostgreSQL 主机上的位置。 | default: CFGOPTDEF_CONFIG_PATH "/" PROJECT_CONFIG_FILE example: pg1-host-config=/conf/pgbackrest/pgbackrest.conf |
pg-host-config-include-path | 设置 PostgreSQL 主机上配置包含路径的位置。 | default: CFGOPTDEF_CONFIG_PATH "/" PROJECT_CONFIG_INCLUDE_PATH example: pg1-host-config-include-path=/conf/pgbackrest/conf.d |
pg-host-config-path | 设置配置路径在 PostgreSQL 主机上的位置 | default: CFGOPTDEF_CONFIG_PATH example: pg1-host-config-path=/conf/pgbackrest |
pg-host-key-file | PostgreSQL 主机密钥文件。证明客户端证书是由所有者发送的。 | example: pg1-host-key-file=/path/to/client.key |
pg-host-port | 设置 pg-host 时的 PostgreSQL 主机端口。 | allowed: 0-65535 example: pg1-host-port=25 |
pg-host-type | PostgreSQL 主机协议类型 | default: ssh example: pg1-host-type=tls |
pg-host-user | 设置 pg-host 时的 PostgreSQL 主机登录用户。 | default: postgres example: pg1-host-user=db_owner |
pgBackRest 配置文件遵循 Windows INI 约定。各部分用括号中的文本表示,每个部分都包含键/值对。以 # 开头的行将被忽略,并可用作注释。
可以通过多种方式加载 pgBackRest 配置文件:
- config 和 config-include-path 是默认的:将加载默认配置文件(如果存在),并且将附加默认配置包含路径中的 *.conf 文件(如果存在)。
- 指定 config 选项:仅加载指定的配置文件,并且预期存在。
- config-include-path 指定:将加载 config include 路径中的 *.conf 文件,并且该路径必须存在。如果存在默认配置文件,则将加载该文件。如果希望仅加载指定配置包含路径中的文件,则也可以传递 --no-config 选项。
- 指定 config 和 config-include-path:使用用户指定的值,将加载配置文件,并附加 config include 路径中的 *.conf 文件。这些文件应存在。
- 指定 config-path:除非显式设置了 config 和/或 config-include-path 选项,否则此设置将覆盖配置文件默认位置的基本路径和/或默认 config-include-path 设置的基本路径。
pgBackRest 也可以使用环境变量进行配置,如下.
pg-primary ⇒ 使用环境配置日志路径
sudo -u postgres bash -c ' \
export PGBACKREST_LOG_PATH=/var/log/pgbackrest && \
pgbackrest --log-level-console=error help backup log-path'
help backup log-path 显示log-path的指导文档
附录三 存储库的常用参数
表5 有关存储库的常用参数
参数 | 解释 | 示例 |
---|---|---|
repo | 设置要操作的命令的存储库。此选项可用于从特定存储库执行还原,而不是让 pgBackRest 选择 | allowed: 1-256 example: repo=1 |
repo-bundle | 将文件捆绑在存储库中。捆绑(合并)较小的文件以减少写入存储库的文件总数。写入更少的文件通常效率更高 | default: n example: repo1-bundle=y |
repo-bundle-limit | 将包含在捆绑包中的文件的大小限制。大于此大小的文件将单独存储。 | default: 2MiB allowed: 8KiB-1PiB example: repo1-bundle-limit=10MiB |
repo-bundle-size | 定义将添加到单个捆绑包的文件的总大小。大多数捆绑包将小于此大小,但有些捆绑包可能会稍大一些,因此请勿将此选项设置为文件系统允许的最大大小 | default: 20MiB allowed: 1MiB-1PiB example: repo1-bundle-size=10MiB |
repo-block | 启用块增量备份。块增量通过将文件拆分为可以独立备份的块,允许更精细的备份。这样可以节省存储库中的空间,并且可以提高增量还原性能,因为可以在不从存储库读取整个文件的情况下获取单个块。 | default: n example: repo1-block=y |
repo-cipher-pass | 用于加密/解密存储库文件的密码。 | 可以用openssl rand -base64 48获取随机加密字符串 |
repo-cipher-type | 指定加密类型 | default: none example: repo1-cipher-type=aes-256-cbc |
repo-host | 远程操作时的存储库主机。 | example: repo1-host=repo1.domain.com |
repo-host-cmd | 存储库主机 pgBackRest 命令。 | example: repo1-host-cmd=/usr/lib/backrest/bin/pgbackrest |
repo-host-config | pgBackRest 存储库主机配置文件 | default: CFGOPTDEF_CONFIG_PATH "/" PROJECT_CONFIG_FILE example: repo1-host-config=/conf/pgbackrest/pgbackrest.conf |
repo-host-config-path | pgBackRest 存储库主机配置路径。 | default: CFGOPTDEF_CONFIG_PATH example: repo1-host-config-path=/conf/pgbackrest |
repo-host-user | 存储库主机用户(如果设置了 repo-host) | default: pgbackrest example: repo1-host-user=repo-user |
repo-path | 存储备份和存档的路径。 | default: /var/lib/pgbackrest example: repo1-path=/backup/db/backrest |
repo-retention-archive | 要保留的连续 WAL 的备份数。如果 repo-retention-archive-type 设置为 incr,则必须设置此选项。如果磁盘空间非常宝贵,则此设置与 repo-retention-archive-type 结合使用,可用于主动使 WAL 段过期。但是,这样做会否定从具有过期 WAL 的备份执行 PITR 的功能,因此不建议这样做。 | allowed: 1-9999999 example: repo1-retention-archive=2 |
repo-retention-archive-type | WAL 保留的备份类型 | default: full example: repo1-retention-archive-type=diff |
repo1-retention-full-type | 完整备份的保留类型。如果设置为 time,则将从存储库中删除早于 repo-retention-full 的完整备份。如果设置为count,超过count份时则保留后面count份删除最早的一份 | default: count example: repo1-retention-full-type=time |
repo-retention-diff | 要保留的差异备份数。 | allowed: 1-9999999 example: repo1-retention-diff=3 |
repo-retention-full | 完整备份保留计数/时间。 | allowed: 1-9999999 example: repo1-retention-full=2 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?