【pgBackRest备份工具使用】

news2024/11/15 4:17:57

一、工具介绍

pgBackRest是PostgreSQL数据库的一个开源备份工具,支持全量、增量和差异备份。这一点优于PG原生的备份工具pg_basebackup,因为pg_basebackup不支持增量备份,并且支持备份完整性检查等等。pgBackRest v2.49是当前的稳定版本。

项目主页:https://pgbackrest.org/
Github链接:https://github.com/pgbackrest/pgbackrest

优点:

支持本地或远程操作, 但是远程操作需要配置SSH
支持全量、增量和差异备份
支持从standby实例备份,减小主库的IO压力
支持备份完整性检查(checksum 算法)
支持压缩:支持gzip, bzip, lz4, zstd等压缩算法,并提供不同压缩等级

二、部署pgBackRest备份工具

1.安装依赖

yum -y install libxml2 libxml2-devel libyaml libyaml-devel bzip2 bzip2-devel 

2.创建所需目录并赋予权限

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

3.编译安装pgbackrest备份工具

su - postgres
unzip pgbackrest-release-2.49.zip
cd pgbackrest-release-2.49/
ls
CODING.md  CONTRIBUTING.md  LICENSE  README.md  doc  meson.build  meson_options.txt  src  test

cd src/
./configure
make -j 24
make install -j 24

image.png

4.验证版本和命令

-bash-4.2$ pgbackrest version
pgBackRest 2.49
-bash-4.2$ pgbackrest help
pgBackRest 2.49 - 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.

三、使用方式

1.配置数据库的 archive_command 命令

开启归档,修改archive_command
stanza是指备份集的名称,可以在pgBackRest的配置文件中进行设置。

vi postgresql.conf 

archive_mode = on
archive_command = 'pgbackrest --stanza=pgxmaster archive-push %p'

需要修改archive_command并开启归档,否则可能有相关报错和提示

image.png

2.初始化实例(创建 Stanza)

pgbackrest --stanza=pgxmaster --log-level-console=info stanza-create

执行结果如下:

-bash-4.2$ pgbackrest --stanza=pgxmaster --log-level-console=info stanza-create
2023-12-29 20:53:25.054 P00   INFO: stanza-create command begin 2.49: --exec-id=59466-2c8ab1f2 --log-level-console=info --log-path=/var/log/pgbackrest --pg1-path=/data/pg16/data --pg1-port=5432 --pg1-socket-path=/tmp --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --stanza=pgxmaster
2023-12-29 20:53:25.681 P00   INFO: stanza-create for stanza 'pgxmaster' on repo1
2023-12-29 20:53:25.703 P00   INFO: stanza-create command end: completed successfully (652ms)

image.png

3.修改配置文件

vi /etc/pgbackrest/pgbackrest.conf

-bash-4.2$ cat /etc/pgbackrest/pgbackrest.conf
[pgxmaster]
pg01-path=/data/pg16/data
pg01-port=5432
pg01-socket-path=/tmp
#pg1-user=postgres
#pg1-host-config-path=/etc/pgbackrest 
#pg1-host-port=22
#pg1-host-user=postgres

[global]
#repo1-path指定 备份和归档仓库路径 
repo1-path=/var/lib/pgsql/pgbackreset_bakdir
backup-user=pgsql
retention-full=2
#repo1-cipher-pass=
#repo1-cipher-type=aes-256-cbc
start-fast=y
process-max=3
log-path=/var/log/pgbackrest
[global:archive-push]
#compress-level指定压缩级别
compress-level=3

如果需要进行远程备份,需要配置pg1-host、pg1-host-port、pg1-host-user等。

4.检查配置

pgbackrest --stanza=pgxmaster --log-level-console=info check

执行结果如下:

-bash-4.2$ pgbackrest --stanza=pgxmaster --log-level-console=info check
2023-12-29 20:54:27.959 P00   INFO: check command begin 2.49: --exec-id=59474-9f746218 --log-level-console=info --log-path=/var/log/pgbackrest --pg1-path=/data/pg16/data --pg1-port=5432 --pg1-socket-path=/tmp --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --stanza=pgxmaster
2023-12-29 20:54:28.575 P00   INFO: check repo1 configuration (primary)
2023-12-29 20:54:28.587 CST [59475] LOG:  restore point "pgBackRest Archive Check" created at 0/9000090
2023-12-29 20:54:28.587 CST [59475] STATEMENT:  select pg_catalog.pg_create_restore_point('pgBackRest Archive Check')::text
2023-12-29 20:54:29.293 P00   INFO: check repo1 archive for WAL (primary)
2023-12-29 20:54:29.396 P00   INFO: WAL segment 000000010000000000000009 successfully archived to '/var/lib/pgsql/pgbackreset_bakdir/archive/pgxmaster/16-1/0000000100000000/000000010000000000000009-1ff7c856c782639a8401763893d294e666946657.gz' on repo1
2023-12-29 20:54:29.397 P00   INFO: check command end: completed successfully (1439ms)

image.png

5.全量备份

使用 pgbackrest help backup命令可以查看备份可以带的一些命令参数

pgbackrest help backup

全量备份命令如下

pgbackrest --stanza=pgxmaster --log-level-console=info backup --type=full

执行结果如下:

-bash-4.2$ pgbackrest --stanza=pgxmaster --log-level-console=info backup --type=full
2023-12-29 21:02:03.548 P00   INFO: backup command begin 2.49: --exec-id=59518-ff63d8ae --log-level-console=info --log-path=/var/log/pgbackrest --pg1-path=/data/pg16/data --pg1-port=5432 --pg1-socket-path=/tmp --process-max=3 --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --repo1-retention-full=2 --stanza=pgxmaster --start-fast --type=full
2023-12-29 21:02:04.283 P00   INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2023-12-29 21:02:04.705 CST [59454] LOG:  checkpoint starting: immediate force wait
2023-12-29 21:02:04.730 CST [59454] LOG:  checkpoint complete: wrote 19 buffers (0.1%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.010 s, sync=0.008 s, total=0.026 s; sync files=16, longest=0.006 s, average=0.001 s; distance=16384 kB, estimate=29633 kB; lsn=0/B000098, redo lsn=0/B000060
2023-12-29 21:02:04.991 P00   INFO: backup start archive = 00000001000000000000000B, lsn = 0/B000060
2023-12-29 21:02:04.992 P00   INFO: check archive for prior segment 00000001000000000000000A
2023-12-29 21:02:07.833 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2023-12-29 21:02:08.237 P00   INFO: backup stop archive = 00000001000000000000000B, lsn = 0/B000138
2023-12-29 21:02:08.244 P00   INFO: check archive for segment(s) 00000001000000000000000B:00000001000000000000000B
2023-12-29 21:02:08.279 P00   INFO: new backup label = 20231229-210204F
2023-12-29 21:02:08.380 P00   INFO: full backup size = 23MB, file total = 974
2023-12-29 21:02:08.380 P00   INFO: backup command end: completed successfully (4835ms)
2023-12-29 21:02:08.380 P00   INFO: expire command begin 2.49: --exec-id=59518-ff63d8ae --log-level-console=info --log-path=/var/log/pgbackrest --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --repo1-retention-full=2 --stanza=pgxmaster
2023-12-29 21:02:08.390 P00   INFO: expire command end: completed successfully (10ms)

image.png

查看备份

-bash-4.2$ date
Fri Dec 29 21:03:05 CST 2023
-bash-4.2$ pgbackrest info
stanza: pgxmaster
    status: ok
    cipher: none

    db (current)
        wal archive min/max (16): 000000010000000000000008/00000001000000000000000B

        full backup: 20231229-210204F
            timestamp start/stop: 2023-12-29 21:02:04+08 / 2023-12-29 21:02:08+08
            wal start/stop: 00000001000000000000000B / 00000001000000000000000B
            database size: 23MB, database backup size: 23MB
            repo1: backup set size: 3MB, backup size: 3MB

image.png

备份路径分成两个,archive和backup两个路径

具体结构如下

-bash-4.2$ pwd
/var/lib/pgsql/pgbackreset_bakdir
-bash-4.2$ tree -d
.
├── archive
│   └── pgxmaster
│       └── 16-1
│           └── 0000000100000000
└── backup
    └── pgxmaster
        ├── 20231229-210204F
        │   └── pg_data
        │       ├── base
        │       │   ├── 1
        │       │   ├── 14484
        │       │   └── 4
        │       ├── global
        │       ├── pg_commit_ts
        │       ├── pg_dynshmem
        │       ├── pg_logical
        │       │   ├── mappings
        │       │   └── snapshots
        │       ├── pg_multixact
        │       │   ├── members
        │       │   └── offsets
        │       ├── pg_notify
        │       ├── pg_replslot
        │       ├── pg_serial
        │       ├── pg_snapshots
        │       ├── pg_stat
        │       ├── pg_stat_tmp
        │       ├── pg_subtrans
        │       ├── pg_tblspc
        │       ├── pg_twophase
        │       ├── pg_upgrade_output.d
        │       │   ├── 20231229T185426.620
        │       │   │   ├── dump
        │       │   │   └── log
        │       │   └── 20231229T185705.267
        │       │       ├── dump
        │       │       └── log
        │       ├── pg_wal
        │       │   └── archive_status
        │       └── pg_xact
        ├── backup.history
        │   └── 2023
        └── latest -> 20231229-210204F

43 directories

---------------------------------------------------------------------------------------------------------------

//backup下每个备份实例对应一个目录,下边带有版本号的目录里存储wal归档相关备份,archive.info里存放归档备份相关信息。

-bash-4.2$ pwd
/var/lib/pgsql/pgbackreset_bakdir/backup/pgxmaster
-bash-4.2$ ls
20231229-210204F  backup.history  backup.info  backup.info.copy  latest
-bash-4.2$ cat backup.info
[backrest]
backrest-format=5
backrest-version="2.49"

[backup:current]
20231229-210204F={"backrest-format":5,"backrest-version":"2.49","backup-archive-start":"00000001000000000000000B","backup-archive-stop":"00000001000000000000000B","backup-error":false,"backup-info-repo-size":3225873,"backup-info-repo-size-delta":3225873,"backup-info-size":24131917,"backup-info-size-delta":24131917,"backup-lsn-start":"0/B000060","backup-lsn-stop":"0/B000138","backup-timestamp-start":1703854924,"backup-timestamp-stop":1703854928,"backup-type":"full","db-id":1,"option-archive-check":true,"option-archive-copy":false,"option-backup-standby":false,"option-checksum-page":false,"option-compress":true,"option-hardlink":false,"option-online":true}

[db]
db-catalog-version=202307071
db-control-version=1300
db-id=1
db-system-id=7317967753805234269
db-version="16"

[db:history]
1={"db-catalog-version":202307071,"db-control-version":1300,"db-system-id":7317967753805234269,"db-version":"16"}

[backrest]
backrest-checksum="1bc5921e775f76660ff4ab7e54f04125f2b93ced"

-------------------------------------------------

-bash-4.2$ pwd
/var/lib/pgsql/pgbackreset_bakdir/backup/pgxmaster
-bash-4.2$ ls
20231229-210204F  backup.history  backup.info  backup.info.copy  latest
-bash-4.2$ cd 20231229-210204F/
-bash-4.2$ ls
backup.manifest  backup.manifest.copy  pg_data
-bash-4.2$ cd pg_data/
-bash-4.2$ ls
backup_label.gz  pg_dynshmem       pg_multixact  pg_snapshots  pg_tblspc            pg_wal
base             pg_hba.conf.gz    pg_notify     pg_stat       pg_twophase          pg_xact
global           pg_ident.conf.gz  pg_replslot   pg_stat_tmp   pg_upgrade_output.d  postgresql.auto.conf.gz
pg_commit_ts     pg_logical        pg_serial     pg_subtrans   PG_VERSION.gz        postgresql.conf.gz

---------------------------------------------------------------------------------------------------------------

//archive下每个备份实例对应一个目录,下边带有版本号的目录里存储wal归档相关备份,backup.info里存放物理备份相关信息。

/var/lib/pgsql/pgbackreset_bakdir/archive/pgxmaster
-bash-4.2$ ls
16-1  archive.info  archive.info.copy

-------------------------------------------------

-bash-4.2$ cat archive.info
[backrest]
backrest-format=5
backrest-version="2.49"

[db]
db-id=1
db-system-id=7317967753805234269
db-version="16"

[db:history]
1={"db-id":7317967753805234269,"db-version":"16"}

[backrest]
backrest-checksum="9125f9a1f113ce1ccb7b5d9e13fb5bfacfcfdce4"
-------------------------------------------------
-bash-4.2$ pwd
/var/lib/pgsql/pgbackreset_bakdir/archive/pgxmaster/16-1/0000000100000000

-bash-4.2$ cat 00000001000000000000000B.00000060.backup
START WAL LOCATION: 0/B000060 (file 00000001000000000000000B)
STOP WAL LOCATION: 0/B000138 (file 00000001000000000000000B)
CHECKPOINT LOCATION: 0/B000098
BACKUP METHOD: streamed
BACKUP FROM: primary
START TIME: 2023-12-29 21:02:04 CST
LABEL: pgBackRest backup started at 2023-12-29 21:02:04.486765+08
START TIMELINE: 1
STOP TIME: 2023-12-29 21:02:08 CST
STOP TIMELINE: 1

归档备份如下所示

image.png

物理备份如下所示

image.png

6.增量备份

pgbackrest --stanza=pgxmaster --log-level-console=info backup --type=incr

执行结果如下所示

-bash-4.2$ pgbackrest --stanza=pgxmaster --log-level-console=info backup --type=incr
2023-12-29 21:15:19.913 P00   INFO: backup command begin 2.49: --exec-id=59681-61772c9f --log-level-console=info --log-path=/var/log/pgbackrest --pg1-path=/data/pg16/data --pg1-port=5432 --pg1-socket-path=/tmp --process-max=3 --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --repo1-retention-full=2 --stanza=pgxmaster --start-fast --type=incr
2023-12-29 21:15:20.721 P00   INFO: last backup label = 20231229-210204F, version = 2.49
2023-12-29 21:15:20.721 P00   INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2023-12-29 21:15:21.532 CST [59454] LOG:  checkpoint starting: immediate force wait
2023-12-29 21:15:21.534 CST [59454] LOG:  checkpoint complete: wrote 0 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.001 s, sync=0.001 s, total=0.003 s; sync files=0, longest=0.000 s, average=0.000 s; distance=16383 kB, estimate=27115 kB; lsn=0/D000060, redo lsn=0/D000028
2023-12-29 21:15:21.807 P00   INFO: backup start archive = 00000001000000000000000D, lsn = 0/D000028
2023-12-29 21:15:21.807 P00   INFO: check archive for prior segment 00000001000000000000000C
2023-12-29 21:15:23.793 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2023-12-29 21:15:24.196 P00   INFO: backup stop archive = 00000001000000000000000D, lsn = 0/D000100
2023-12-29 21:15:24.200 P00   INFO: check archive for segment(s) 00000001000000000000000D:00000001000000000000000D
2023-12-29 21:15:24.233 P00   INFO: new backup label = 20231229-210204F_20231229-211520I
2023-12-29 21:15:24.324 P00   INFO: incr backup size = 8.3KB, file total = 974
2023-12-29 21:15:24.324 P00   INFO: backup command end: completed successfully (4414ms)
2023-12-29 21:15:24.324 P00   INFO: expire command begin 2.49: --exec-id=59681-61772c9f --log-level-console=info --log-path=/var/log/pgbackrest --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --repo1-retention-full=2 --stanza=pgxmaster
2023-12-29 21:15:24.331 P00   INFO: expire command end: completed successfully (7ms)

image.png

查看备份如下所示

-bash-4.2$ pgbackrest info
stanza: pgxmaster
    status: ok
    cipher: none

    db (current)
        wal archive min/max (16): 000000010000000000000008/00000001000000000000000D

        full backup: 20231229-210204F
            timestamp start/stop: 2023-12-29 21:02:04+08 / 2023-12-29 21:02:08+08
            wal start/stop: 00000001000000000000000B / 00000001000000000000000B
            database size: 23MB, database backup size: 23MB
            repo1: backup set size: 3MB, backup size: 3MB

        incr backup: 20231229-210204F_20231229-211520I
            timestamp start/stop: 2023-12-29 21:15:20+08 / 2023-12-29 21:15:24+08
            wal start/stop: 00000001000000000000000D / 00000001000000000000000D
            database size: 23MB, database backup size: 8.3KB
            repo1: backup set size: 3MB, backup size: 438B
            backup reference list: 20231229-210204F
-bash-4.2$
-bash-4.2$ date
Fri Dec 29 21:17:52 CST 2023

image.png

备份路径下backup数据备份路径下,latest是一个指向最新备份目录的软链。

-bash-4.2$ pwd
/var/lib/pgsql/pgbackreset_bakdir/backup/pgxmaster
-bash-4.2$ ll
total 8
drwxr-x--- 3 postgres postgres   72 Dec 29 21:02 20231229-210204F
drwxr-x--- 3 postgres postgres   72 Dec 29 21:15 20231229-210204F_20231229-211520I
drwxr-x--- 3 postgres postgres   18 Dec 29 21:02 backup.history
-rw-r----- 1 postgres postgres 1797 Dec 29 21:15 backup.info
-rw-r----- 1 postgres postgres 1797 Dec 29 21:15 backup.info.copy
lrwxrwxrwx 1 postgres postgres   33 Dec 29 21:15 latest -> 20231229-210204F_20231229-211520I

backup.info下包含两次备份的信息。

image.png

增量备份的backup路径下包含控制文件相关,具体的增量数据,主要还是以wal日志的备份为主。

image.png

备份路径下归档备份路径

image.png

7.差异备份

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

先做个数据更改。

-bash-4.2$ psql
psql (16.1)
Type "help" for help.

postgres=# \d tab_ysl
              Table "public.tab_ysl"
 Column |  Type   | Collation | Nullable | Default
--------+---------+-----------+----------+---------
 id     | integer |           |          |

postgres=# insert into tab_ysl values(6);
INSERT 0 1

执行差异备份

-bash-4.2$ pgbackrest --stanza=pgxmaster --log-level-console=info backup --type=diff
2023-12-29 21:46:54.601 P00   INFO: backup command begin 2.49: --exec-id=59867-16372c14 --log-level-console=info --log-path=/var/log/pgbackrest --pg1-path=/data/pg16/data --pg1-port=5432 --pg1-socket-path=/tmp --process-max=3 --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --repo1-retention-full=2 --stanza=pgxmaster --start-fast --type=diff
2023-12-29 21:46:55.382 P00   INFO: last backup label = 20231229-210204F, version = 2.49
2023-12-29 21:46:55.382 P00   INFO: execute non-exclusive backup start: backup begins after the requested immediate checkpoint completes
2023-12-29 21:46:55.830 CST [59454] LOG:  checkpoint starting: immediate force wait
2023-12-29 21:46:55.840 CST [59454] LOG:  checkpoint complete: wrote 2 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.003 s, sync=0.002 s, total=0.010 s; sync files=2, longest=0.001 s, average=0.001 s; distance=16383 kB, estimate=25076 kB; lsn=0/F000060, redo lsn=0/F000028
2023-12-29 21:46:56.096 P00   INFO: backup start archive = 00000001000000000000000F, lsn = 0/F000028
2023-12-29 21:46:56.096 P00   INFO: check archive for prior segment 00000001000000000000000E
2023-12-29 21:46:57.771 P00   INFO: execute non-exclusive backup stop and wait for all WAL segments to archive
2023-12-29 21:46:58.176 P00   INFO: backup stop archive = 00000001000000000000000F, lsn = 0/F000100
2023-12-29 21:46:58.178 P00   INFO: check archive for segment(s) 00000001000000000000000F:00000001000000000000000F
2023-12-29 21:46:58.308 P00   INFO: new backup label = 20231229-210204F_20231229-214655D
2023-12-29 21:46:58.371 P00   INFO: diff backup size = 24.3KB, file total = 974
2023-12-29 21:46:58.371 P00   INFO: backup command end: completed successfully (3773ms)
2023-12-29 21:46:58.372 P00   INFO: expire command begin 2.49: --exec-id=59867-16372c14 --log-level-console=info --log-path=/var/log/pgbackrest --repo1-path=/var/lib/pgsql/pgbackreset_bakdir --repo1-retention-full=2 --stanza=pgxmaster
2023-12-29 21:46:58.382 P00   INFO: expire command end: completed successfully (11ms)

image.png

查看备份

image.png

差异备份方式在backup目录下主要包含发生变化的对象的数据文件。

-bash-4.2$ pwd
/var/lib/pgsql/pgbackreset_bakdir/backup/pgxmaster/20231229-210204F_20231229-214655D
-bash-4.2$ cd pg_data/
-bash-4.2$ ls
backup_label.gz  base  global  pg_logical  pg_xact
-bash-4.2$ tree
.
├── backup_label.gz
├── base
│   └── 14484
│       └── 24594.gz
├── global
│   └── pg_control.gz
├── pg_logical
│   └── replorigin_checkpoint.gz
└── pg_xact
    └── 0000.gz

5 directories, 5 files

postgres=# select pg_relation_filepath('tab_ysl');
 pg_relation_filepath
----------------------
 base/14484/24594
(1 row)

image.png

archive备份 目录下

image.png

8.查看备份情况

pgbackrest info --repo-path=path 

如果写了配置文件,可以省略–repo-path=path ,走配置文件。

-bash-4.2$ pgbackrest info
stanza: pgxmaster
    status: ok
    cipher: none

    db (current)
        wal archive min/max (16): 000000010000000000000008/00000001000000000000000D

        full backup: 20231229-210204F
            timestamp start/stop: 2023-12-29 21:02:04+08 / 2023-12-29 21:02:08+08
            wal start/stop: 00000001000000000000000B / 00000001000000000000000B
            database size: 23MB, database backup size: 23MB
            repo1: backup set size: 3MB, backup size: 3MB

        incr backup: 20231229-210204F_20231229-211520I
            timestamp start/stop: 2023-12-29 21:15:20+08 / 2023-12-29 21:15:24+08
            wal start/stop: 00000001000000000000000D / 00000001000000000000000D
            database size: 23MB, database backup size: 8.3KB
            repo1: backup set size: 3MB, backup size: 438B
            backup reference list: 20231229-210204F

9.监控备份信息

doc/example下有两个SQL文件,使用这两个文件可以帮助我们监控上一次成功备份的状态,

image.png

image.png

psql -f pgsql-pgbackrest-info.sql
psql -f pgsql-pgbackrest-query.sql

执行结果如下

-bash-4.2$ psql -f pgsql-pgbackrest-info.sql
CREATE SCHEMA
CREATE FUNCTION
-bash-4.2$ psql -f pgsql-pgbackrest-query.sql
    name     | last_successful_backup |    last_archived_wal
-------------+------------------------+--------------------------
 "pgxmaster" | 2023-12-29 21:46:58+08 | 000000010000000000000011
(1 row)

同样的,pgbackrest info可以查看更详细的备份信息

10.恢复数据

常用的一些例子如下:

–delta参数可以进行增量恢复

pgbackrest --stanza=pgxmaster --delta=2024-01-01 restore

-基于时间点恢复

pgbackrest --stanza=pgxmaster --delta --log-level-console=detail --type=time "--target=2023-12-29 21:46:58+08" restore

-设置恢复目录

pgbackrest --stanza=pgxmaster --set=restore-command="cp %f /var/lib/pgsql/16/data/pg_restore/%p" restore

也可以使用 pgbackrest help restore查看可以添加的一些参数选项。如下是一个实际恢复数据的案例

创建恢复路径

-bash-4.2$ mkdir /data/pg16/data2

查看可用备份

-bash-4.2$ pgbackrest info
stanza: pgxmaster
    status: ok
    cipher: none

    db (current)
        wal archive min/max (16): 000000010000000000000008/000000010000000000000012

        full backup: 20231229-210204F
            timestamp start/stop: 2023-12-29 21:02:04+08 / 2023-12-29 21:02:08+08
            wal start/stop: 00000001000000000000000B / 00000001000000000000000B
            database size: 23MB, database backup size: 23MB
            repo1: backup set size: 3MB, backup size: 3MB

        incr backup: 20231229-210204F_20231229-211520I
            timestamp start/stop: 2023-12-29 21:15:20+08 / 2023-12-29 21:15:24+08
            wal start/stop: 00000001000000000000000D / 00000001000000000000000D
            database size: 23MB, database backup size: 8.3KB
            repo1: backup set size: 3MB, backup size: 438B
            backup reference list: 20231229-210204F

        diff backup: 20231229-210204F_20231229-214655D
            timestamp start/stop: 2023-12-29 21:46:55+08 / 2023-12-29 21:46:58+08
            wal start/stop: 00000001000000000000000F / 00000001000000000000000F
            database size: 23MB, database backup size: 24.3KB
            repo1: backup set size: 3MB, backup size: 590B
            backup reference list: 20231229-210204F

恢复命令

-bash-4.2$ pgbackrest --stanza=pgxmaster --delta --log-level-console=detail --type=time "--target=2023-12-29 21:15:24+08" restore --repo-path=/var/lib/pgsql/pgbackreset_bakdir --pg1-path=/data/pg16/data2

image.png

image.png


 

image.png


 

image.png

image.png

可以配置recovery相关参数,然后拉起数据库后,再使用pg_wal_replay_resume()函数提升为主节点。
 

image.png

image.png

11.远程备份

数据库端配置文件里的repo1-host写远程备份节点的主机名或IP,远程备份节点的配置文件里的pg1-host写数据库锁在节点的主机名或者IP,并且需要开通两个节点的SSH。设置免密。

11.1数据库端配置

pgbackrest.conf配置

vi /etc/pgbackrest/pgbackrest.conf

[global]

log-level-file=detail
repo1-host=node_back
log-path=/var/log/pgbackrest

[pgxmaster]
pg1-path=/data/pg16/data

设置数据库参数

vi postgresql.auto.conf 

archive_command = 'pgbackrest --stanza=pgxmaster archive-push %p'
archive_mode = on
listen_addresses = '*'
log_line_prefix = ''
max_wal_senders = 3
wal_level = replica

重启数据库

pg_ctl restart

11.2 备份端配置

vi   /etc/pgbackrest/pgbackrest.conf
[global]
repo1-path=/data/pg16/data
repo1-retention-full=2
log-path=/var/log/pgbackrest
#compress-type=gz

[global:archive_push]
compress-level=3

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

备注:
repo1-path 指定 备份和归档仓库路径
compress-level 指定压缩级别 bz2 - 9 ; gz - 6 ; lz4 - 1 ; zst - 3

11.3 创建存储空间

需要在远程控制端进行初始化,在远程控制端完成初始化后,再在数据库端进行初始化
两个节点都需要执行如下命令。

pgbackrest --stanza=pgxmaster --log-level-console=info stanza-create

11.4 备份测试

远程节点执行备份相关命令
可以参照前文部分,此处略。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1391280.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

新手怎么投稿各大网络媒体网站

在当今信息爆炸的时代,网络媒体已成为人们获取新闻信息的主要渠道之一。无论是企业宣传、个人推广,还是新闻报道,都离不开网络媒体的力量。而要将自己的信息传达到更广大的受众群体,投稿各大网络媒体网站就成为了一个重要的环节。…

ARM day2、day3 汇编

一、汇编学习:可以向上理解软件、向下感知硬件 二、符号(注释) 注释#注释(放在行首表示注释一行)/* */注释#数字立即数:一种标号(比如main: loop:).text .end换行…

[足式机器人]Part2 Dr. CAN学习笔记-Advanced控制理论 Ch04-9 可观测性与分离原理

本文仅供学习使用 本文参考: B站:DR_CAN Dr. CAN学习笔记-Advanced控制理论 Ch04-9 可观测性与分离原理

【Java JVM】栈帧

执行引擎是 Java 虚拟机核心的组成部分之一。 在《Java虚拟机规范》中制定了 Java 虚拟机字节码执行引擎的概念模型, 这个概念模型成为各大发行商的 Java 虚拟机执行引擎的统一外观 (Facade)。 不同的虚拟机的实现中, 通常会有 解释执行 (通过解释器执行)编译执行 (通过即时编…

Servlet项目教学(附实例代码)

【员工信息管理】 1.员工信息管理 1.1 介绍 用户进行登录后,可以对员工信息进行管理(增删查改),等操作.如果用户没有登录,不能访问员工操作页面.并且员工操作页面显示当前登录用户信息. 1.2 技术点 使用VueElementUI充当前端界面,使用ServletJDBCMysql提供数据管理控制.后端统…

嵌入式学习-网络编程-Day3

思维导图 多进程并发服务器通信模型如下 void handler(int signo) {while(waitpid()>0); //以非阻塞配合信号完成僵尸进程的回收 } int main() {//将信号与信号处理函数绑定signal(SIGCHLD, handler);socket(); //创建用于连接的套接字bind(); //绑定IP…

Python和Java代码实现:切线法求解一维最优化问题

Python和Java代码实现:切线法求解一维最优化问题 代码实现Python代码Java代码 求解实例 根据概念查询,切线法定义如下: 切线法(Tangent Method)是一种用于求解非线性方程的数值方法。它也被称为牛顿法(Newt…

C++ 程序文档生成器(doxygen)使用说明

程序文档,是每个程序员必看文档,在日常业务开发中,难免会封装一些组件。没有很好的组件文档,再好的组件都是废物,。因此大型业务中,文档和思维导图,两个都是必备! 一、注释风格 …

IPv6隧道--GRE隧道

GRE隧道 通用路由封装协议GRE(Generic Routing Encapsulation)可以对某些网络层协议(如IPX、ATM、IPv6、AppleTalk等)的数据报文进行封装,使这些被封装的数据报文能够在另一个网络层协议(如IPv4)中传输。 GRE提供了将一种协议的报文封装在另一种协议报文中的机制,是一…

各种设备上恢复已删除的文件和文件夹的数据恢复软件清单

最好的数据恢复软件可以简单轻松地恢复计算机、移动设备或存储介质上已删除的文件和文件夹。 询问任何经历过数据丢失的人这是否是一种有趣的经历,他们会告诉您数据丢失,无论是由于硬件或软件故障、意外删除还是网络犯罪,都会带来极大的压力…

美国 SEC 批准比特币现货 ETF 上市,SEC 告诉我们的风险包含哪些?

撰文:Will 阿望 查看原文:美国 SEC 批准比特币现货 ETF 上市,SEC 告诉我们的风险包含哪些? 历经十年的 BTC ETF 艰辛审批之路终于迎来了胜利的曙光,2024 年 1 月 11 日凌晨 4 时,美国证监会(S…

AIGC实战——像素卷积神经网络(PixelCNN)

AIGC实战——像素卷积神经网络 0. 前言1. PixelCNN 工作原理1.1 掩码卷积层 1.2 残差块2. 训练 PixelCNN3. PixelCNN 分析4. 使用混合分布改进 PixelCNN小结系列链接 0. 前言 像素卷积神经网络 (Pixel Convolutional Neural Network, PixelCNN) 是于 2016 年提出的一种图像生成…

礼贺新春,徐坊大曲新品【中国红】

梁山徐坊大曲新推出中国风礼盒,以中国红为主题,为即将到来的新春佳节增添了浓厚的节日气氛。为您呈现一场视觉与味觉的盛宴。从礼盒的颜色到图案设计,无不体现出中国红的热情与活力,象征着吉祥、喜庆与团圆。梁山徐坊大曲&#xf…

ubuntu qt 运行命令行

文章目录 1.C实现2.python实现 1.C实现 下面是封装好的C头文件&#xff0c;直接调用run_cmd_fun()即可。 #ifndef GET_CMD_H #define GET_CMD_H#endif // GET_CMD_H #include <iostream> #include<QString> using namespace std;//system("gnome-terminal -…

USB8814动态信号采集卡——声音振动类信号处理的理想之选!

背景介绍&#xff1a; 科技的发展在一定程度上依赖于对信号的处理&#xff0c;信号处理技术的先进性在很大程度上决定了科技发展的速度和方向。数字信号处理技术的崛起&#xff0c;彻底改变了传统的信息与信号处理方式&#xff0c;使得数据采集这一前期工作在数字系统中发挥着…

FTP文件传输协议 、多种方式安装yum仓库

一、网络文件共享服务 1.存储类型分三种&#xff1a; 直连式存储&#xff1a;Direct-Attached Storage&#xff0c;简称DAS 存储区域网络&#xff1a;Storage Area Network&#xff0c;简称SAN&#xff08;可以使用空间&#xff0c;管理也是你来管理&#xff09; 网络附加存储…

ImageNet Classification with Deep Convolutional 论文笔记

✅作者简介&#xff1a;人工智能专业本科在读&#xff0c;喜欢计算机与编程&#xff0c;写博客记录自己的学习历程。 &#x1f34e;个人主页&#xff1a;小嗷犬的个人主页 &#x1f34a;个人网站&#xff1a;小嗷犬的技术小站 &#x1f96d;个人信条&#xff1a;为天地立心&…

Leetcode23-数组能形成多少数对(2341)

1、题目 给你一个下标从 0 开始的整数数组 nums 。在一步操作中&#xff0c;你可以执行以下步骤&#xff1a; 从 nums 选出 两个 相等的 整数 从 nums 中移除这两个整数&#xff0c;形成一个 数对 请你在 nums 上多次执行此操作直到无法继续执行。 返回一个下标从 0 开始、长…

SpringMVC参数接收见解4

# 4.参数接收Springmvc中&#xff0c;接收页面提交的数据是通过方法形参来接收&#xff1a; 处理器适配器调用springmvc使用反射将前端提交的参数传递给controller方法的形参 springmvc接收的参数都是String类型&#xff0c;所以spirngmvc提供了很多converter&#xff08;转换…

第二证券:大盘探底回升走出底部还看成交量配合

持续震动数日后&#xff0c;大盘再现探底上升走势。 上证指数周二小幅低开后窄幅震动&#xff0c;午后快速回落改写本轮回调新低后&#xff0c;有资金开始出手介入&#xff0c;尾盘指数翻红。深证成指同样是在午后呈现探底上升走势&#xff0c;最终重回5日均线上方。截至收盘&…