pg_rman进行备份恢复

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)
上一篇:How To Trigger A Power Automate Flow From PowerApps


下一篇:pysimpleGui FilesBrowse函数原始说明