暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

使用pgbackrest备份postgresql

2668

作者简介

Granthana Biswas cybertec公司工程师。

译者简介

王志斌,从事数据库产品相关工作,主要致力于postgresql数据库高可用解决方案及云端产品化工作。

校对者简介

李伟,任职于海能达通信股份有限公司,数据库开发高级工程师,致力于postgresql数据库在专网通信领域、公共安全领域的应用与推广。

pgBackRest是PostgreSQL的一个开源备份工具,它提供了简单的配置和可靠的备份。因此,如果您想保护您的数据库并轻松创建备份,pgBackRest是一个很好的解决方案。在本博客中,我们将介绍使用pgbackback,通过基本操作步骤,进行PostgreSQL的完全备份和差异备份。

 pgBackRest的一些关键特性:

1. 并行备份与还原

2. 本地和远端操作

3. 全量、增量、差异备份

4. 备份转换和归档过期

5. 备份完整性

6. 页面校验和

7. 恢复备份

8. 流式压缩和校验和

 想要阅读关于更多细节,请访问pgbackrest.org。(https://pgbackrest.org/)

现在我们将要从源码构建pgBackRest并安装到正在运行中的测试主机上。

 

Debian Ubuntu包中进行安装:

    sudo apt-get install pgbackrest

    对于手动安装,请在构建的主机上下载源代码。请避免在生产服务器上构建源代码,因为构建所需的工具不应安装在生产计算机上:

    下载pgBackRest(V2.14):

      sudo wget -q -O - \ https://github.com/pgbackrest/pgbackrest/archive/release/2.14.tar.gz | \ 
       sudo tar zx -C root

      安装和检查64系统下的依赖:

        sudo apt-get install build-essential libssl-dev \ 
             libxml2-dev libperl-dev zlib1g-dev
        perl -V | grep USE_64_BIT_INT

        构建pgbackrest包:

          (cd root/pgbackrest-release-2.14/src && ./configure) 
          make -s -C root/pgbackrest-release-2.14/src

          从构建主机上拷贝到数据库主机:

            sudo scp BUILD_HOST:/root/pgbackrest-release-2.14/src/pgbackrest  usr/bin/
            sudo chmod 755 usr/bin/pgbackrest

            安装Perl包:

              sudo apt-get install libdbd-pg-perl

              在测试数据库主机上初始化pgBackRest配置文件和路径:

                sudo mkdir -p -m 770 var/log/pgbackrest
                sudo chown 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


                创建pgBackRest仓库

                  sudo mkdir -p /var/lib/pgbackrest
                  sudo chmod 750 /var/lib/pgbackrest
                  sudo chown postgres:postgres /var/lib/pgbackrest


                  在pgbackrest 配置文件中,设置仓库路径和PostgreSQL数据目录:

                    cat /etc/pgbackrest/pgbackrest.conf
                    [demo]
                    pg1-path=/data/postgres/pgdata/data1
                    [global]
                    repo1-path=/var/lib/pgbackrest


                    配置PostgreSQL集群归档:

                    在postgresql.conf文件中修改如下参数:

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


                      使用postgres创建节:

                      必须在存储库所在的主机上运行 stanza-create 命令才能初始化节。建议在创建节之后运行check命令,以确保正确配置了归档和备份。

                        $ pgbackrest --stanza=demo --log-level-console=info stanza-create
                        2019-07-03 12:26:40.060 P00 INFO: stanza-create command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --stanza=demo
                        2019-07-03 12:26:40.494 P00 INFO: stanza-create command end: completed successfully (435ms)
                        $ pgbackrest --stanza=demo --log-level-console=info check
                        2019-07-03 12:27:11.996 P00 INFO: check command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --stanza=demo
                        2019-07-03 12:27:13.386 P00 INFO: WAL segment 000000010000000000000003 successfully stored in the archive at '/var/lib/pgbackrest/archive/demo/10-1/0000000100000000/000000010000000000000003-b346d07d4b31e54e31d9204204816cde3cfcca3a.gz'
                        2019-07-03 12:27:13.387 P00 INFO: check command end: completed successfully (1392ms)


                        使用info命令获取备份信息

                        由于我们尚未进行任何备份,因此将得到以下结果:

                          $ pgbackrest info
                          stanza: demo
                              status: error (no valid backups)
                              cipher: none
                              db (current)
                                  wal archive min/max (10-1): 000000010000000000000001/000000010000000000000003


                          备份:

                          让我们先创建第一个备份,默认情况下,即使我们将类型指定为差异,也将是full: 

                            $ pgbackrest --stanza=demo --log-level-console=info backup
                            2019-07-03 12:37:38.366 P00 INFO: backup command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo
                            WARN: no prior backup exists, incr backup has been changed to full
                            2019-07-03 12:37:39.200 P00 INFO: execute non-exclusive pg_start_backup() with label "pgBackRest backup started at 2019-07-03 12:37:38": backup begins after the next regular checkpoint completes
                            2019-07-03 12:37:39.500 P00 INFO: backup start archive = 000000010000000000000005, lsn = 0/5000028
                            2019-07-03 12:37:45.212 P00 INFO: full backup size = 22.5MB
                            2019-07-03 12:37:45.212 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
                            2019-07-03 12:37:45.313 P00 INFO: backup stop archive = 000000010000000000000005, lsn = 0/5000130
                            2019-07-03 12:37:45.558 P00 INFO: new backup label = 20190703-123738F
                            2019-07-03 12:37:45.586 P00 INFO: backup command end: completed successfully (7221ms)
                            2019-07-03 12:37:45.586 P00 INFO: expire command begin
                            2019-07-03 12:37:45.594 P00 INFO: full backup total < 2 - using oldest full backup for 10-1 archive retention
                            2019-07-03 12:37:45.596 P00 INFO: expire command end: completed successfully (10ms)

                            现在当再次运行info命令的时候:

                              $ pgbackrest info
                              stanza: demo
                                  status: ok
                                  cipher: none
                                  db (current)
                                      wal archive min/max (10-1): 000000010000000000000005/000000010000000000000005
                                      full backup: 20190703-123738F
                                          timestamp start/stop: 2019-07-03 12:37:38 / 2019-07-03 12:37:45
                                          wal start/stop: 000000010000000000000005 / 000000010000000000000005
                                          database size: 22.6MB, backup size: 22.6MB
                                          repository size: 2.7MB, repository backup size: 2.7MB


                              在集群上执行差异备份

                                $ pgbackrest --stanza=demo --log-level-console=info --type=diff backup
                                2019-07-03 12:40:05.749 P00 INFO: backup command begin 2.14: --log-level-console=info --pg1-path=/data/postgres/pgdata/data1 --repo1-path=/var/lib/pgbackrest --repo1-retention-full=2 --stanza=demo --type=diff
                                2019-07-03 12:40:05.951 P00 INFO: last backup label = 20190703-123738F, version = 2.14
                                2019-07-03 12:40:06.657 P00 INFO: execute non-exclusive pg_start_backup() with label "pgBackRest backup started at 2019-07-03 12:40:05": backup begins after the next regular checkpoint completes
                                2019-07-03 12:40:06.958 P00 INFO: backup start archive = 000000010000000000000007, lsn = 0/7000028
                                2019-07-03 12:40:08.414 P01 INFO: backup file /data/postgres/pgdata/data1/global/pg_control (8KB, 99%) checksum c8b3635ef4701b19bff56fcd5ca33d41eaf3ce5b
                                2019-07-03 12:40:08.421 P01 INFO: backup file /data/postgres/pgdata/data1/pg_logical/replorigin_checkpoint (8B, 100%) checksum 347fc8f2df71bd4436e38bd1516ccd7ea0d46532
                                2019-07-03 12:40:08.439 P00 INFO: diff backup size = 8KB
                                2019-07-03 12:40:08.439 P00 INFO: execute non-exclusive pg_stop_backup() and wait for all WAL segments to archive
                                2019-07-03 12:40:08.540 P00 INFO: backup stop archive = 000000010000000000000007, lsn = 0/70000F8
                                2019-07-03 12:40:08.843 P00 INFO: new backup label = 20190703-123738F_20190703-124005D
                                2019-07-03 12:40:08.938 P00 INFO: backup command end: completed successfully (3189ms)
                                2019-07-03 12:40:08.938 P00 INFO: expire command begin
                                2019-07-03 12:40:08.949 P00 INFO: full backup total < 2 - using oldest full backup for 10-1 archive retention
                                2019-07-03 12:40:08.951 P00 INFO: expire command end: completed successfully (13ms)


                                使用info命令,查看变化:

                                  $ pgbackrest info
                                  stanza: demo
                                      status: ok
                                      cipher: none
                                      db (current)
                                          wal archive min/max (10-1): 000000010000000000000005/000000010000000000000007
                                          full backup: 20190703-123738F
                                              timestamp start/stop: 2019-07-03 12:37:38 / 2019-07-03 12:37:45
                                              wal start/stop: 000000010000000000000005 / 000000010000000000000005 
                                              database size: 22.6MB, backup size: 22.6MB
                                              repository size: 2.7MB, repository backup size: 2.7MB
                                          diff backup: 20190703-123738F_20190703-124005D
                                              timestamp start/stop: 2019-07-03 12:40:05 / 2019-07-03 12:40:08
                                              wal start/stop: 000000010000000000000007 / 000000010000000000000007
                                              database size: 22.6MB, backup size: 8.2KB
                                              repository size: 2.7MB, repository backup size: 468B
                                              backup reference list: 20190703-123738F


                                  请点击文章底部“阅读原文”查看原文内容。






                                  PostgreSQL中文社区欢迎广大技术人员投稿
                                  投稿邮箱:press@postgres.cn


                                  文章转载自PostgreSQL中文社区,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                  评论