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

pg_rman物理备份和还原

运维开发圈 2018-10-21
896

pg_rman工具是实现PostgreSQL数据库在线备份,物理备份和恢复的备份工具。可以实现全量备份,增量备份。

pg_rman 是一款优秀的postgresql 在线备份和恢复的工具.

因为数据库版本是:postgresql-10.5,所以需要下载相对应的pg_rman REL_10_STABLE: branch for postgresql-10.5

安装pg_rman

$ git  clone  https://github.com/ossc-db/pg_rman.git
$ cd pg_rman/
$ git branch -a      查看所有版本的分支
$ git checkout REL_10_STABLE   指定postgresql版本对应的分支
$ git status         查看状态
$ make install     开始安装pg_rman
pg_rman备份工具已经安装完成。

全量: full
增量: incremental
归档: archive

Usage:
  pg_rman OPTION init
  pg_rman OPTION backup
  pg_rman OPTION restore
  pg_rman OPTION show [DATE]
  pg_rman OPTION show detail [DATE]
  pg_rman OPTION validate [DATE]
  pg_rman OPTION delete DATE
  pg_rman OPTION purge

命令选项:
  -D, --pgdata=PATH        # 数据存储目录的路径
  -A, --arclog-path=PATH   # 归档wal (预写)日志的路径
  -S, --srvlog-path=PATH   # 数据库服务器存储日志的路径。
  -B, --backup-path=PATH   # 备份数据存储的路径。
  -c, --check              # 检查
    -v, --verbose          # 显示详细消息        
    -P, --progress         # 显示已处理文件的进度

备份选项:
  -b, --backup-mode=MODE   # 全备,增备,归档
  -s, --with-serverlog     # 备份服务器日志文件
  -Z, --compress-data      # 使用zlib压缩数据备份
  -C, --smooth-checkpoint  # 在备份前做平滑检查点
  -F, --full-backup-on-error    # 切换到完全备份模式,注意:此选项仅用于--backup-mode = incremental或archive

  --keep-data-generations=NUM # 保留NUM代完整数据备份
  --keep-data-days=NUM        # 保持足够的数据备份以恢复到N天前
  --keep-arclog-files=NUM     # 保留NUM个归档的WAL日志
  --keep-arclog-days=DAY      # 保持存档的WAL在DAY天内修改
  --keep-srvlog-files=NUM     # 保留NUM个服务器日志
  --keep-srvlog-days=DAY      # 在DAY天内修改服务器日志
  --standby-host=HOSTNAME     # 从待机状态进行备份时备用主机
  --standby-port=PORT         # 从待机状态进行备份时的备用端口


还原选项:
--recovery-target-time      # 恢复将继续进行的时间戳
--recovery-target-xid       # 事务ID,恢复将继续进行
--recovery-target-inclusive # 我们是否在恢复目标之后停止
--recovery-target-timeline  # 恢复到特定的时间
--hard-copy                 # 复制archivelog而不是符号链接

目录选项:
  -a    # 显示已删除的备份

删除选项:
 -f     # 强制删除比规定日期更旧的备份

连接选项: 
  -d, --dbname=DBNAME   # 连接指定库
  -h, --host=HOSTNAME   # 数据库主机名/IP地址
  -p, --port=PORT       # 数据库端口
  -U, --username=USERNAME  # 数据库用户名
  -w, --no-password        # 从不提示密码
  -W, --password           # 强制密码提示

通用选项:
-q, --quiet # 不显示任何INFO或者DEBUG信息。
--debug     # 查看debug信息。     

开始使用pg_rman工具:

  • 创建备份目录:

  # mkdir /database-backup/postgresql-backup/{fullbackup,walbackup,pg_srvlog} -p
  # chown -R postgres.postgres  /database-backup/postgresql-backup/

  • 设置环境变量:

  $ vim .profile                
    export BACKUP_PATH=/database-backup/postgresql-backup/fullbackup
    export ARCLOG_PATH=/database-backup/postgresql-backup/walbackup
    export SRVLOG_PATH=/database-backup/postgresql-backup/pg_srvlog
  $ source .profile     #立即生效

  • 修改postgresql.conf配置文件:

 $ vim data/postgresql.conf
    archive_mode = on
    archive_command = 'DATE=`date +%Y%m%d`; DIR="/database-backup/postgresql-backup/walbackup/"; (test -d $DIR ||  mkdir -p $DIR)  && cp %p $DIR/%f'

  保存并退出。

  • pg_rman init 初始化

  $ pg_rman init  #输出的信息:
    INFO: ARCLOG_PATH is set to '/database-backup/postgresql-backup/walbackup'
    INFO: SRVLOG_PATH is set to '/database-backup/postgresql-backup/pg_srvlog'

  • pg_rman全量备份

  $ pg_rman backup --backup-mode=full --with-serverlog --progress

   INFO: copying database files Processed 1298 of 1298 files, skipped 0
   INFO: copying archived WAL files Processed 3 of 3 files, skipped 0
   INFO: copying server log files
   INFO: backup complete
   INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.

  • pg_rman 校验备份集
    pg_rman 的备份必须都是经过验证过的,否则不能进行恢复和增量备份。

  $ pg_rman validate
   INFO: validate: "2018-10-14 14:55:34" backuparchive log files and server log files by CRC
   INFO: backup "2018-10-14 14:55:34" is valid

  • pg_rman 列出备份集

  $ pg_rman show
  $ ls  /database-backup/postgresql-backup/fullbackup/
   20181014
   backup
   pg_rman.ini
   system_identifier
   timeline_history



  • pg_rman增量备份:

  增量备份是基于文件系统的update time时间线.
  增量备份必须有个对应的全库备份.

  $ pg_rman backup --backup-mode incremental --progress --compress-data

  $ pg_rman validate  列出备份
    INFO: validate"2018-10-14 15:59:57" backup and archive log files by CRC
    INFO: backup "2018-10-14 15:59:57" is valid

  $ pg_rman show        

  • pg_rman删除备份集

  $ pg_rman delete   -f   '2018-10-14 15:59:57'
    INFO: delete the backup with start time"2018-10-14 15:59:57"
    INFO: delete the backup with start time"2018-10-14 14:55:34"

   注意: 删除增量备份的同时,也已经把全量备份给删除了。

  $ pg_rman show
    =====================================================================
    StartTime           EndTime              Mode    Size   TLI  Status 
    =====================================================================

  注意: 创建增量备份之前必须先创建一个全量备份。

  • pg_rman restore  还原

  模拟数据库误删除:
    $ /etc/init.d/postgresql  stop
    $ ps  -ef  | grep  -i  postmaster | grep -v grep
    $cd  data

    $ ls   -l
    total 116
    drwx------ 6 postgres postgres  4096 Oct  2 19:17 base
    drwx------ 2 postgres postgres  4096 Oct 14 14:50 global
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_commit_ts
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_dynshmem
    -rw------- 1 postgres postgres  4622 Sep 30 11:58 pg_hba.conf
    -rw------- 1 postgres postgres  1636 Sep 30 10:49 pg_ident.conf
    drwx------ 4 postgres postgres  4096 Oct 14 16:25 pg_logical
    drwx------ 4 postgres postgres  4096 Sep 30 10:49 pg_multixact
    drwx------ 2 postgres postgres  4096 Oct 14 14:49 pg_notify
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_replslot
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_serial
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_snapshots
    drwx------ 2 postgres postgres  4096 Oct 14 16:25 pg_stat
    drwx------ 2 postgres postgres  4096 Sep 30 11:04 pg_stat_tmp
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_subtrans
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_tblspc
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_twophase
    -rw------- 1 postgres postgres     3 Sep 30 10:49 PG_VERSION
    drwx------ 3 postgres postgres  4096 Oct 14 16:25 pg_wal
    drwx------ 2 postgres postgres  4096 Sep 30 10:49 pg_xact
    -rw------- 1 postgres postgres    88 Sep 30 10:49 postgresql.auto.conf
    -rw------- 1 postgres postgres 23264 Oct 14 14:49 postgresql.conf
    -rw------- 1 postgres postgres    87 Oct 14 14:49 postmaster.opts


    $ mv data   data-bak  # 将之间的目录作为备份。
    $ mkdir   data        # 创建数据目录。
    $ chmod 700 ./data

  • 使用pg_rman  restore还原:

    $ pg_rman restore
    $ cd $PGDATA

    $ cat recovery.conf
      restore_command = 'cp /database- 
      backup/postgresql-backup/walbackup/%f %p'

      recovery_target_timeline = '1'     

   启动PostgreSQL数据库服务:
    $ /etc/init.d/postgresql start
    $ ps -ef |grep -i postgres 

  • 还原时,注意事项:

  restore后启动会遇到下列错误:
    invalid primary checkpoint record
    invalid secondary checkpoint record
    could not locate a valid checkpoint record

    此时只能重置wal,并取消恢复模式
    $ pg_resetwal  --f  $PGDATA
    $ mv $PGDATA/recovery.conf $PGDATA/recovery.done

   使用pg_rman备份时对wal的归档是通过软链接来实现。建议添加 --hard-copy。

增量备份和全量备份, 备份还原已经完成。 需要注意的是:    物理备份时的全量和增量备份时可以在线备份(不停机备份).    但是还原时,需要停机进行还原。

  • postgres用户的环境变量配置文件内容

$cat .profile    
  PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin:/services/install/postgresql10/bin/
  export  LD_LIBRARY_PATH=/services/install/postgresql10/lib:$LD_LIBRARY_PATH
  export LD_LIBRARY_PATH=/services/install/python3.6/lib:$LD_LIBRARY_PATH

  export PGDATA PATH
  export BACKUP_PATH=/database-backup/postgresql-backup/fullbackup
  export ARCLOG_PATH=/database-backup/postgresql-backup/walbackup
  export SRVLOG_PATH=/database-backup/postgresql-backup/pg_srvlog

下载参考
pg_rman文档参考

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

评论