优势
1、支持对象存储
2、支持从standby实例备份,可以减小主库的IO压力
使用
pgBackRest是一个可靠的备份和恢复工具,可以为PostgreSQL数据库提供快速和可靠的备份和还原。下面是使用pgBackRest进行备份和还原的基本步骤:
1、备份数据库
使用pgBackRest备份PostgreSQL数据库的命令如下:
pgbackrest --stanza=STANZA backup
其中,STANZA是指备份集的名称,可以在pgBackRest的配置文件中进行设置。
2、恢复数据库
在进行恢复之前,需要将备份文件拷贝到目标服务器上,并解压缩。恢复PostgreSQL数据库的命令如下:
pgbackrest --stanza=STANZA restore
此命令将恢复最新的备份。如果需要恢复早期的备份,则需要使用–set=restore-path=PATH参数指定备份文件的路径。
3、设置恢复参数
在进行恢复之前,还需要设置一些恢复参数,例如恢复目录、监听地址和端口等。可以在pgBackRest的配置文件中进行设置,或者在命令行中使用–set参数进行设置。
例如,设置恢复目录:
pgbackrest --stanza=STANZA --set=restore-command="cp %f /var/lib/pgsql/10/data/pg_restore/%p" restore
此命令将备份文件复制到指定的恢复目录中。
4、恢复数据库
设置好恢复参数后,使用–delta参数可以进行增量恢复,即只恢复指定时间点之后的更改。例如,恢复到2022年1月1日的命令如下:
pgbackrest --stanza=STANZA --delta=2022-01-01 restore
恢复过程可能需要一段时间,具体时间取决于数据库的大小和恢复参数的设置。恢复完成后,可以使用pgAdmin等工具连接数据库进行验证。
5、查看备份情况
pgbackrest info --repo-path=path where backups and archive are stored
6、归档
pgbackrest --archive-async --exec-id=29557-207a7762 --log-level-console=off --log-level-stderr=off --process-max=2 archive-push:async /pgdata/data/postgres-1c5ee2a6/pg_wal
pgbackrest --archive-async --exec-id=29557-207a7762 --log-level-console=off --log-level-file=off --log-level-stderr=error --process=1 --remote-type=repo archive-push:local
pgbackrest --archive-async --exec-id=29557-207a7762 --log-level-console=off --log-level-file=off --log-level-stderr=error --process=2 --remote-type=repo archive-push:local
这些进程在后台异步运行,以执行 /pgdata/data/postgres-1c5ee2a6/pg_wal 目录的存档推送。它们的目的是管理 PostgreSQL 预写日志 (WAL) 文件的归档。
–archive-async 选项表示归档是异步的,允许 pgbackrest 实用程序继续处理而无需等待归档操作完成。
每个进程都有不同的进程号(–process=1 和 --process=2)来分别标识它们。 --remote-type=repo 选项指定在远程存储库上执行归档。
pgbackrest
pgbackrest
pgBackRest 2.43 - 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.
pgbackrest help info
增量 vs 差异备份
Pgbackrest是一个强大的PostgreSQL备份和恢复工具,它使用增量备份策略。
增量备份是一种备份方法,只备份自上次完整备份之后更改的数据。这意味着每次备份只会备份新增或修改过的数据块,从而减少了备份所需的时间和存储空间。在Pgbackrest中,每个增量备份都建立在上一个完整备份或增量备份的基础上。
与增量备份相对的是差异备份。差异备份是备份自上次完整备份之后的所有更改,而不仅仅是自上次备份以来的更改。因此,差异备份需要更多的时间和存储空间,但在恢复时可能比增量备份更快一些。
总结起来,Pgbackrest使用增量备份策略来有效地备份和恢复PostgreSQL数据库。
创建全量备份。
pgbackrest --stanza=postgres --log-level-console=info backup
执行增量备份。
pgbackrest --stanza=postgres --log-level-console=info --type=incr backup
执行差异备份。
pgbackrest --stanza=postgres --log-level-console=info --type=diff backup