pg_rman进行备份恢复

环境参数:

linux版本:CentOS 7.6

PG版本:13.2

docker版本: 18.06.3

1、在容器内源码安装PostgreSQL13.2

要先根据搭建CentOS镜像搭建好相应的镜像,然后再开始。

[root@wcbpg ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
wcbcentos76         1.0                 6877e73a2018        2 weeks ago         1.9GB
wcbpg               1.0                 988fc4d35414        2 weeks ago         1.9GB
centos              7.6.1810            f1cb7c7d58b7        2 years ago         202MB
#创建CentOS容器
[root@wcbpg ~]#docker run -d --name wcbpg -h wcbpg \-p 15432-15439:5432-5439 \-v /sys/fs/cgroup:/sys/fs/cgroup \--privileged=true wcbcentos76:1.0 \/usr/sbin/init
6c1a765e97e33bf69c1892c7403c0d39498dabff44903e7186b0c9307756b206
[root@wcbpg ~]#  docker exec -it wcbpg bash
#在容器内安装依赖
[root@wcbpg /]# yum install -y cmake make gcc zlib gcc-c++ perl readline readline-devel zlib zlib-devel perl python36 tcl openssl ncurses-devel openldap pam
#删除已存在的PG,如果没有安装则不用删除
[root@wcbpg /]# yum remove -y postgresql* && rm -rf  /var/lib/pgsql && rm -rf  /usr/pgsql* && userdel -r postgres && groupdel postgres
[root@wcbpg /]# yum install -y sysbench
#创建用户
[root@wcbpg /]# groupadd -g 60000 pgsql
[root@wcbpg /]# useradd -u 60000 -g pgsql pgsql
[root@wcbpg /]# echo "wcb" | passwd --stdin pgsql
Changing password for user pgsql.
passwd: all authentication tokens updated successfully.
# 创建目录
[root@wcbpg /]# mkdir -p /postgresql/{pgdata,archive,scripts,backup,pg13,soft}
[root@wcbpg /]# chown -R pgsql:pgsql /postgresql
[root@wcbpg /]# chmod -R 775 /postgresql
#切换用户
[root@wcbpg /]# su - pgsql
[pgsql@wcbpg ~]$ cd /postgresql/soft
#用docker cp  postgresql-13.2.tar.gz wcbpg:/ 把源码从Linux复制到docker内
[pgsql@wcbpg soft]$ cp /postgresql-13.2.tar.gz ./
[pgsql@wcbpg soft]$ ls -lth
total 27M
-rw-r--r-- 1 pgsql pgsql 27M Dec 16 23:07 postgresql-13.2.tar.gz
[pgsql@wcbpg soft]$ tar zxvf postgresql-13.2.tar.gz
[pgsql@wcbpg soft]$ cd postgresql-13.2
#检查
[pgsql@wcbpg postgresql-13.2]$ ./configure --prefix=/postgresql/pg13 --without-readline
#安装PG
[pgsql@wcbpg postgresql-13.2]$ make -j 8 && make install
#配置环境变量
[pgsql@wcbpg postgresql-13.2]$cat >>  ~/.bash_profile <<"EOF"
export LANG=en_US.UTF-8
export PS1="[\u@\h \W]\$ "
export PGPORT=5433
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg13
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH:.
export DATE=`date +"%Y%m%d%H%M"`
export MANPATH=$PGHOME/share/man:$MANPATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgresalias psql='rlwrap psql'
EOF#初始化
[pgsql@wcbpg postgresql-13.2]$ /postgresql/pg13/bin/initdb -D /postgresql/pgdata -E UTF8 --locale=en_US.utf8 -U postgres#修改postgresql.conf文件
[pgsql@wcbpg postgresql-13.2]cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
listen_addresses = '*'
port=5433
unix_socket_directories='/postgresql/pgdata'
logging_collector = on
log_directory = 'pg_log'
log_filename = 'postgresql-%a.log'
log_truncate_on_rotation = on
EOF
#修改pg_hba.conf文件
[pgsql@wcbpg postgresql-13.2]cat   > /postgresql/pgdata/pg_hba.conf << EOF
# TYPE  DATABASE    USER    ADDRESS       METHOD
local     all       all                    trust
host      all       all    ::1/128         trust
host      all       all   127.0.0.1/32     trust
host      all       all    0.0.0.0/0        md5
host   replication  all    0.0.0.0/0        md5
EOF
[pgsql@wcbpg postgresql-13.2]$ pg_ctl start
waiting for server to start....2021-12-16 23:18:21.382 CST [9238] LOG:  redirecting log output to logging collector process
2021-12-16 23:18:21.382 CST [9238] HINT:  Future log output will appear in directory "pg_log".done
server started
[root@wcbpg /]# su - pgsql
Last login: Thu Dec 16 23:19:06 CST 2021 on pts/0
[pgsql@wcbpg ~]$ psql
psql (13.2)
Type "help" for help.postgres=#

2、安装pg_rman

[root@wcbpg /]# su - pgsql
Last login: Thu Dec 16 23:19:06 CST 2021 on pts/0
[pgsql@wcbpg ~]$ wget https://github.com/ossc-db/pg_rman/releases/download/V1.3.11/pg_rman-1.3.11-pg13.tar.gz
[pgsql@wcbpg ~]$ tar -zxvf pg_rman-1.3.11-pg13.tar.gz
[pgsql@wcbpg ~]$ cd pg_rman-1.3.11-pg13/
[pgsql@wcbpg pg_rman-1.3.11-pg13]$ make && make install
[pgsql@wcbpg pg_rman-1.3.11-pg13]$ which pg_rman
/postgresql/pg13/bin/pg_rman

3、使用pg_rman的前提

#创建归档目录
[pgsql@wcbpg pg_rman-1.3.11-pg13]$mkdir -p /postgresql/archive
#开启归档
[pgsql@wcbpg pg_rman-1.3.11-pg13]$cat >> /postgresql/pgdata/postgresql.conf <<"EOF"
wal_level='replica'
archive_mode='on'
archive_command='test ! -f /postgresql/archive/%f && cp %p /postgresql/archive/%f'
restore_command='cp /postgresql/archive/%f %p'
EOF#查询是否开启归档
[pgsql@wcbpg pg_rman-1.3.11-pg13]$ psql
psql (13.2)
Type "help" for help.postgres=# select * from pg_settings where name in ('wal_level','archive_mode','archive_command');

4、全量备份、增量备份

[root@wcbpg /]# mkdir /bk
[root@wcbpg /]# chown -R pgsql:pgsql  /bk
[root@wcbpg /]# su - pgsql
Last login: Thu Dec 16 23:20:17 CST 2021 on pts/0
[pgsql@wcbpg ~]$ pg_rman init -B /bk
INFO: ARCLOG_PATH is set to '/postgresql/archive'
INFO: SRVLOG_PATH is set to '/postgresql/pgdata/pg_log'
[pgsql@wcbpg ~]$ cd /bk/
[pgsql@wcbpg bk]$ lls
total 8
drwx------ 4 pgsql pgsql 34 Dec 16 23:27 backup
-rw-rw-r-- 1 pgsql pgsql 75 Dec 16 23:27 pg_rman.ini
-rw-rw-r-- 1 pgsql pgsql 40 Dec 16 23:27 system_identifier
drwx------ 2 pgsql pgsql  6 Dec 16 23:27 timeline_history
[pgsql@wcbpg bk]$ more pg_rman.ini
ARCLOG_PATH='/postgresql/archive'
SRVLOG_PATH='/postgresql/pgdata/pg_log'[pgsql@wcbpg bk]$
[pgsql@wcbpg bk]$ more system_identifier
SYSTEM_IDENTIFIER='7042319584642446350'[pgsql@wcbpg bk]$ psql
psql (13.2)
Type "help" for help.postgres=# create table test1(id int,char varchar);
CREATE TABLE
postgres=# insert into test1 values(1,'1');
INSERT 0 1
postgres=# select * from test1;id | char
----+------1 | 1
(1 row)
postgres=# \q
#进行全备
[pgsql@wcbpg bk]$ pg_rman backup --backup-mode=full -B /bk
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
[pgsql@wcbpg bk]$ pg_rman show -B /bk
=====================================================================StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2021-12-16 23:31:15  2021-12-16 23:31:17  FULL    49MB     1  DONE
[pgsql@wcbpg bk]$ pg_rman validate -B /bk
INFO: validate: "2021-12-16 23:31:15" backup and archive log files by CRC
INFO: backup "2021-12-16 23:31:15" is valid
[pgsql@wcbpg bk]$ pg_rman show -B /bk
=====================================================================StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2021-12-16 23:31:15  2021-12-16 23:31:17  FULL    49MB     1  OK
[pgsql@wcbpg bk]$ psql
psql (13.2)
Type "help" for help.postgres=# insert into test1 values(2,'2');
INSERT 0 1
postgres=# \q#进行增量备份
[pgsql@wcbpg bk]$ pg_rman backup --backup-mode=incremental -B /bk
INFO: copying database files
INFO: copying archived WAL files
INFO: backup complete
INFO: Please execute 'pg_rman validate' to verify the files are correctly copied.
[pgsql@wcbpg bk]$ pg_rman show -B /bk
=====================================================================StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2021-12-16 23:33:49  2021-12-16 23:33:51  INCR    33MB     1  DONE
2021-12-16 23:31:15  2021-12-16 23:31:17  FULL    49MB     1  OK
[pgsql@wcbpg bk]$ pg_rman validate -B /bk
INFO: validate: "2021-12-16 23:33:49" backup and archive log files by CRC
INFO: backup "2021-12-16 23:33:49" is valid
[pgsql@wcbpg bk]$ pg_rman show -B /bk
=====================================================================StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2021-12-16 23:33:49  2021-12-16 23:33:51  INCR    33MB     1  OK
2021-12-16 23:31:15  2021-12-16 23:31:17  FULL    49MB     1  OK

5、恢复到最新时间

#先停止pg数据库
[pgsql@wcbpg bk]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
#删除之前的数据库数据
[pgsql@wcbpg bk]$ rm -rf /postgresql/pgdata/
#进行恢复
[pgsql@wcbpg bk]$ pg_rman restore -B /bk
WARNING: pg_controldata file "/postgresql/pgdata/global/pg_control" does not exist
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of latest full backup as recovery target: 1
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: "2021-12-16 23:31:15"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2021-12-16 23:31:15" backup and archive log files by SIZE
INFO: backup "2021-12-16 23:31:15" is valid
INFO: restoring database files from the full mode backup "2021-12-16 23:31:15"
INFO: searching incremental backup to be restored
INFO: validate: "2021-12-16 23:33:49" backup and archive log files by SIZE
INFO: backup "2021-12-16 23:33:49" is valid
INFO: restoring database files from the incremental mode backup "2021-12-16 23:33:49"
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2021-12-16 23:33:49" is valid
INFO: restoring WAL files from backup "2021-12-16 23:33:49"
INFO: restoring online WAL files and server log files
INFO: add recovery related options to postgresql.conf
INFO: generating recovery.signal
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.[pgsql@wcbpg bk]$ pg_ctl start
waiting for server to start....2021-12-16 23:37:00.222 CST [9554] LOG:  redirecting log output to logging collector process
2021-12-16 23:37:00.222 CST [9554] HINT:  Future log output will appear in directory "pg_log".done
server started
[pgsql@wcbpg bk]$ psql
psql (13.2)
Type "help" for help.postgres=# select * from test1;id | char
----+------1 | 12 | 22 | 2
(3 rows)

6、恢复到指定的时间点

[pgsql@wcbpg bk]$ pg_ctl stop
waiting for server to shut down.... done
server stopped
[pgsql@wcbpg bk]$ rm -rf /postgresql/pgdata/
[pgsql@wcbpg bk]$ pg_rman show -B /bk
=====================================================================StartTime           EndTime              Mode    Size   TLI  Status
=====================================================================
2021-12-16 23:33:49  2021-12-16 23:33:51  INCR    33MB     1  OK
2021-12-16 23:31:15  2021-12-16 23:31:17  FULL    49MB     1  OK
[pgsql@wcbpg bk]$ ^C
[pgsql@wcbpg bk]$  pg_rman restore -B /bk --recovery-target-time="2021-12-16 23:31:17"
WARNING: pg_controldata file "/postgresql/pgdata/global/pg_control" does not exist
INFO: the recovery target timeline ID is not given
INFO: use timeline ID of latest full backup as recovery target: 1
INFO: calculating timeline branches to be used to recovery target point
INFO: searching latest full backup which can be used as restore start point
INFO: found the full backup can be used as base in recovery: "2021-12-16 23:31:15"
INFO: copying online WAL files and server log files
INFO: clearing restore destination
INFO: validate: "2021-12-16 23:31:15" backup and archive log files by SIZE
INFO: backup "2021-12-16 23:31:15" is valid
INFO: restoring database files from the full mode backup "2021-12-16 23:31:15"
INFO: searching incremental backup to be restored
INFO: searching backup which contained archived WAL files to be restored
INFO: backup "2021-12-16 23:31:15" is valid
INFO: restoring WAL files from backup "2021-12-16 23:31:15"
INFO: backup "2021-12-16 23:33:49" is valid
INFO: restoring WAL files from backup "2021-12-16 23:33:49"
INFO: restoring online WAL files and server log files
INFO: add recovery related options to postgresql.conf
INFO: generating recovery.signal
INFO: restore complete
HINT: Recovery will start automatically when the PostgreSQL server is started.
[pgsql@wcbpg bk]$ pg_ctl start
waiting for server to start....2021-12-16 23:38:14.350 CST [9609] LOG:  redirecting log output to logging collector process
2021-12-16 23:38:14.350 CST [9609] HINT:  Future log output will appear in directory "pg_log".done
server started
[pgsql@wcbpg bk]$ psql
psql (13.2)
Type "help" for help.postgres=# select * from test1;id | char
----+------1 | 1
(1 row)

PG13用pg_rman进行备份恢复相关推荐

  1. 备份集过期时间_TiDB备份恢复方式你知多少?

    背景 学习一款数据库,要学会备份和恢复.备份是一个严谨的工作,作为一个dba,掌握数据库备份.恢复的各种手段. 下面让我们一起来看看TiDB的备份恢复有那些手段吧. 基于MVCC的恢复方式 相关原理已 ...

  2. 虚拟桌面的备份恢复最佳实践 第一部分

    摘要 VMware View 是目前虚拟桌面市场的旗舰产品.借助它,企业可以将办公系统.应用和基础架构以高性能.高度可扩展的集中式托管服务形式交付给用户.View 还可提供保护它所支持的系统和应用以及 ...

  3. mysql备份数据库语句6_13.4 MySQL用户管理;13.5 常用sql语句;13.6 MySQL数据库备份恢复...

    扩展 : SQL语句教程 什么是事务?事务的特性有哪些? 根据binlog恢复指定时间段的数据 mysql字符集调整 使用xtrabackup备份innodb引擎的数据库  innobackupex  ...

  4. mysql用户管理,常用sql语句,mysql数据库备份恢复

    2019独角兽企业重金招聘Python工程师标准>>> mysql用户管理 grant all on . to 'user1' identified by 'passwd'; gra ...

  5. 什么鬼!基于备份恢复的实例数据还能变多?

    此文已由作者温正湖授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 对数据库进行数据备份无非两种方式,一种是逻辑备份,也就是直接连上数据库导出所有的数据,对于MySQL,就是通 ...

  6. mysql 数据库 xtrabackup (完全备份恢复,恢复后重启失败总结)

    一. 完全备份恢复说明 xtrabackup二进制文件有一个xtrabackup --copy-back选项,它将备份复制到服务器的datadir目录下.下面是通过 --target-dir 指定完全 ...

  7. RDS for MySQL 5.7 备份恢复为本地实例

    RDS for MySQL 5.7 备份恢复为本地实例 1. 本地环境 2. 恢复步骤 3. 后记 近期同学咨询反馈 RDS for MySQL 5.7 备份恢复本地实例的问题比较多,提供一个恢复样例 ...

  8. 完整恢复模式下的备份/恢复

    SQL SERVER 2005 支持三种方式的备份/恢复 1.简单恢复模式下的备份/恢复模 2.完整恢复模式下的备份/恢复 3.大容量日志恢复模式下的备份/恢复   [注]Microsoft关于备份/ ...

  9. mysql备份恢复实验

    内容大纲: 一 mysqldump备份 二 sql语句备份 三 mysqlbinlog备份恢复 一 mysqldump数据文件备份恢复 mysql> use cw_d; Database cha ...

最新文章

  1. linux nfs如何配置,Linux上的NFS如何配置?
  2. 在Windows server 中搭建sftp服务器(快速)
  3. python精通-干货|Python学习必须精通的几个模块
  4. mvn test BUILD FAILURE
  5. Python正则表达式之额外补充(7)
  6. 4.聚合aggregate
  7. 数值分析-泰勒公式及泰勒级数
  8. pandas 学习 —— pivot table
  9. 编程趣谈_k++_++k_k一行顺序输出,结果会有差异吗?
  10. VC6中编译器/zm问题
  11. IDEA配置LeetCode刷题环境
  12. python获取管理员权限
  13. 图片压缩工具ImageOptim
  14. ​从ASML年报看半导体产业的未来
  15. Android开源框架:Retrofit
  16. GitHub创建仓库
  17. [No000026]365种创业、办公、和生活成长的精华资源
  18. 机器学习基石(林轩田)第一章 笔记与感悟总结
  19. java/php/net/python驾校学员管理系统设计
  20. Anroid 逆向工具

热门文章

  1. arm-linux-gcc /lib/libc.so.6,链接器 – 找不到/lib/libc.so.6
  2. 装饰器python的通俗理解_2道极好的Python算法题 | 带你透彻理解装饰器的妙用
  3. 2017.10.9 放棋子 思考记录
  4. 2017.7.19 Race 思考记录
  5. 【英语学习】【Level 07】U02 Live Work L5 This is where we work
  6. mysql查询时间出来数字_mysql查询时间出来数字的解决方法
  7. iphone降级 无需电脑_App 降级无需电脑,手机直接搞
  8. LVS 工作模式以及工作原理
  9. 网站备案负责人_三分钟教你如何做好网站备案
  10. python post 远程主机强迫关闭了一个现有的连接_Python最强动态可视化库!