代码之家  ›  专栏  ›  技术社区  ›  muclux

从pg_basebackup还原

  •  2
  • muclux  · 技术社区  · 6 年前

    我使用命令每天备份一个PostgreSQL数据库

    /usr/bin/pg_basebackup -D $outdir -Ft -x -z -w -R -v
    

    现在我想在另一台服务器上恢复这个数据库。我用描述 https://www.postgresql.org/docs/9.5/static/continuous-archiving.html#BACKUP-PITR-RECOVERY .

    这个 recovery.conf 备份中包含的文件包含以下内容:

    standby_mode = 'on'
    primary_conninfo = 'user=postgres port=5432 sslmode=prefer sslcompression=1 krbsrvname=postgres'
    

    文档中的下一步(8)是启动PostgreSQL。这会导致由于超时而失败:

    3783 postgres: startup process   waiting for 0000000100000024000000B 
    

    在原始服务器上,我没有此文件。是否可以在不使用任何wal文件的情况下只恢复pg_basebackup的状态?那么recovery.conf文件中应该包含什么?

    按照@josmac的建议,我移动了recovery.conf,结果如下:

    shaun2:/var/lib/pgsql/data # service postgresql start
    Job for postgresql.service failed because the control process exited with error code. See "systemctl status postgresql.service" and "journalctl -xe" for details.
    shaun2:/var/lib/pgsql/data # service postgresql status
    â postgresql.service - PostgreSQL database server
       Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Mon 2018-06-18 12:02:53 CEST; 12s ago
      Process: 1340 ExecStop=/usr/lib/postgresql-init stop (code=exited, status=0/SUCCESS)
      Process: 9355 ExecStart=/usr/lib/postgresql-init start (code=exited, status=1/FAILURE)
     Main PID: 1060 (code=exited, status=0/SUCCESS)
    
    Jun 18 12:02:52 shaun2 postgres[9369]: [3-1] 2018-06-18 12:02:52 CEST   LOG:  invalid checkpoint record
    Jun 18 12:02:52 shaun2 postgres[9369]: [4-1] 2018-06-18 12:02:52 CEST   FATAL:  could not locate required checkpoint record
    Jun 18 12:02:52 shaun2 postgres[9369]: [4-2] 2018-06-18 12:02:52 CEST   HINT:  If you are not restoring from a backup, try removing the file "/var/lib/pgsql/data/backup_label".
    Jun 18 12:02:52 shaun2 postgres[9367]: [2-1] 2018-06-18 12:02:52 CEST   LOG:  startup process (PID 9369) exited with exit code 1
    Jun 18 12:02:52 shaun2 postgres[9367]: [3-1] 2018-06-18 12:02:52 CEST   LOG:  aborting startup due to startup process failure
    Jun 18 12:02:53 shaun2 postgresql-init[9355]: pg_ctl: could not start server
    Jun 18 12:02:53 shaun2 systemd[1]: postgresql.service: Control process exited, code=exited status=1
    Jun 18 12:02:53 shaun2 systemd[1]: Failed to start PostgreSQL database server.
    Jun 18 12:02:53 shaun2 systemd[1]: postgresql.service: Unit entered failed state.
    Jun 18 12:02:53 shaun2 systemd[1]: postgresql.service: Failed with result 'exit-code'.
    

    我想PostgreSQL仍然在寻找丢失的wal文件,因为 backup_label 以下内容:

    shaun2:/var/lib/pgsql/data # cat backup_label
    START WAL LOCATION: 24/B0000028 (file 0000000100000024000000B0)
    CHECKPOINT LOCATION: 24/B0000028
    BACKUP METHOD: streamed
    BACKUP FROM: master
    START TIME: 2018-06-14 02:55:08 CEST
    LABEL: pg_basebackup base backup
    

    移开备份标签后的结果:

    shaun2:/var/lib/pgsql/data # service postgresql status
    â postgresql.service - PostgreSQL database server
       Loaded: loaded (/usr/lib/systemd/system/postgresql.service; enabled; vendor preset: disabled)
       Active: failed (Result: exit-code) since Mon 2018-06-18 12:17:54 CEST; 4s ago
      Process: 1340 ExecStop=/usr/lib/postgresql-init stop (code=exited, status=0/SUCCESS)
      Process: 10401 ExecStart=/usr/lib/postgresql-init start (code=exited, status=1/FAILURE)
     Main PID: 1060 (code=exited, status=0/SUCCESS)
    
    Jun 18 12:17:53 shaun2 postgres[10414]: [4-1] 2018-06-18 12:17:53 CEST   LOG:  invalid secondary checkpoint record
    Jun 18 12:17:53 shaun2 postgres[10414]: [5-1] 2018-06-18 12:17:53 CEST   PANIC:  could not locate a valid checkpoint record
    Jun 18 12:17:54 shaun2 postgres[10412]: [2-1] 2018-06-18 12:17:54 CEST   LOG:  startup process (PID 10414) was terminated by signal 6: Aborted
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   JosMac    6 年前

    我们使用pg_-basebackup进行备份,还进行了几次恢复,因此一般来说,它工作得很好,没有问题。

    但我建议您使用参数 -X stream 而不是 -x (意思是“-x fetch”)。使用此参数pg_basebackup将捕获和存储备份期间创建的wal日志段以及数据文件。这些wal日志将单独存储 pg_xlog.tar pg_wal.tar 文件(取决于PG版本)。

    完整的恢复描述可以在这里找到- pg_basebackup / pg-barman – restore tar backup