ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pg_rman进行备份恢复

2021-12-17 22:02:37  阅读:213  来源: 互联网

标签:INFO wcbpg 23 备份 16 pgsql pg rman 2021


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=postgres

alias 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 | 1
  2 | 2
  2 | 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)

标签:INFO,wcbpg,23,备份,16,pgsql,pg,rman,2021
来源: https://blog.csdn.net/weixin_49756466/article/details/121986575

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有