关于postgresql增量热备份详细操作流程及策略

01. 概述

这里简略地介绍一下postgres单机是如何热备份的,演示整个过程,以及简要说明一下配置策略。
参照:
https://www.postgresql.org/docs/9.4/static/continuous-archiving.html
http://www.postgres.cn/docs/9.4/continuous-archiving.html

02. 部署postgres

这里使用postgres9.4版本,使用apt-get一键到位安装
$ sudo add-apt-repository "deb https://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main"
$ wget --quiet -O - https://postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - 
$ sudo apt-get update;sudo apt-get install postgresql-9.4 -y
$ sudo /etc/init.d/postgresql start; sudo netstat -tnlp |grep 5432

03. 修改配置

$ sudo vim /etc/postgresql/9.4/main/postgresql.conf    --修改下面的参数
wal_level = archive             # 归档级别为archive
archive_mode = on               # 开启归档模式
archive_command = 'test ! -f /data/back/pg_xlog2/%f && cp %p /data/back/pg_xlog2/%f'    # 将归档日志拷贝到自定义的备份目录下,这里是同一目录
archive_timeout = 60            # 每60秒归档切换一次,即是生成000000000*样子的文件
$ sudo mkdir -p /data/back/pg_xlog2; sudo chown postgres:postgres /data/back/pg_xlog2    --创建归档目录及授权
$ sudo /etc/init.d/postgresql restart; sudo netstat -tnlp|grep 5432      --可启动一下服务,并查看端口是否正常

04. 模拟生成数据过程

4.1 模拟创建数据
$ su - postgres     --切换postgres管理用户
$ createdb db1      --创建数据库,或者psql登录用语句创建库[# create database db2 encoding='UTF-8';]
$ psql db1          --登录数据库
db1=# create table t1(id int,primary key (id));         --创建一张测试表
db1=# create table t2(id int,primary key (id));         --再创建一张测试表
db1=# insert into t1(id) values(1);                     --t1插入N1条数据
db1=# insert into t1(id) values(2);                     --t1插入N2条数据
db1=# insert into t2(id) values(1);                     --t2插入N1条数据
db1=# insert into t2(id) values(2);                     --t2插入N2条数据
db1=# select * from t1;                                 --两条数据
 id 
----
  1
  2
(2 rows)

4.2 开启备份
db1=# select pg_start_backup('basedata');               --强制发生一次checkpoint点   
 pg_start_backup 
-----------------
 0/3000028
(1 row)
$ tar zcvf /data/back/pg_xlog2/basedata.20190927.tar.gz /var/lib/postgresql/9.4/main/     --基础数据备份
db1=# select pg_stop_backup();                         --备份完了,需要停止,不然恢复的时候会提示错误     
NOTICE:  pg_stop_backup complete, all required WAL segments have been archived
 pg_stop_backup 
----------------
 0/9000050
(1 row)
$ watch -n 1 'ls -lh /var/lib/postgresql/9.4/main/pg_xlog/'    --实时生成日志文件(最新部分),监控日志文件生成频率为60s,就是刚刚设置的archive_timeout
Every 1.0s: ls -lh /var/lib/postgresql/9.4/main/pg_xlog/             Thu Sep 27 23:18:27 2018
total 65M
-rw------- 1 postgres postgres  292 Sep 27 23:11 000000010000000000000003.00000028.backup
-rw------- 1 postgres postgres  16M Sep 27 23:10 000000010000000000000008
-rw------- 1 postgres postgres  16M Sep 27 23:11 000000010000000000000009
-rw------- 1 postgres postgres  16M Sep 27 23:16 00000001000000000000000A
-rw------- 1 postgres postgres  16M Sep 27 23:05 00000001000000000000000B
drwx------ 2 postgres postgres 4.0K Sep 27 23:16 archive_status
$ watch -n 1 'ls -lh /data/back/pg_xlog2/'                     --备份日志文件(全部文件)
Every 1.0s: ls -lh /data/back/pg_xlog2/                              Thu Sep 27 23:17:49 2018
total 165M
-rw------- 1 postgres postgres  16M Sep 27 22:47 000000010000000000000001
-rw------- 1 postgres postgres  16M Sep 27 22:47 000000010000000000000002
-rw------- 1 postgres postgres  16M Sep 27 22:47 000000010000000000000003
-rw------- 1 postgres postgres  292 Sep 27 23:11 000000010000000000000003.00000028.backup
-rw------- 1 postgres postgres  16M Sep 27 22:50 000000010000000000000004
-rw------- 1 postgres postgres  16M Sep 27 22:55 000000010000000000000005
-rw------- 1 postgres postgres  16M Sep 27 23:00 000000010000000000000006
-rw------- 1 postgres postgres  16M Sep 27 23:05 000000010000000000000007
-rw------- 1 postgres postgres  16M Sep 27 23:10 000000010000000000000008
-rw------- 1 postgres postgres  16M Sep 27 23:11 000000010000000000000009
-rw------- 1 postgres postgres  16M Sep 27 23:16 00000001000000000000000A
-rw-rw-r-- 1 postgres postgres 4.1M Sep 27 23:10 basedata.20190927.tar.gz
 
4.3 增量数据(基础备份后的数据)
db1=# insert into t1(id) values(3);       --插入数据,插多几次,数据也多条   
INSERT 0 1
db1=# select pg_switch_xlog();            --手动切换日志点,即是每执行一次每次会生成00000000*的日志文件
 pg_switch_xlog 
----------------
 0/C000000
(1 row)

05. 模拟恢复过程

5.1 恢复数据
$ sudo /etc/init.d/postgresql stop     --恢复需要停止数据库,不能再写入了,如果再写数据,则新写入的数据会丢失
$ mv /var/lib/postgresql/9.4/main/ /tmp/main   --直接将数据移走,或者直接rm -rf删除
$ tar zxvf /data/back/pg_xlog2/basedata.20190927.tar.gz   --将基本数据解压到本地
$ cd /var/lib/postgresql/9.4/main    --进入数据目录
$ rm -f pg_xlog/00000001000000000000000*                     --将原来归档的都删除,以防影响数据质量
$ rm -f pg_xlog/archive_status/00000001000000000000000*      --将原来归档的都删除,以防影响数据质量
$ vim recovery.conf      --创建恢复文件,从哪里去,到哪里来
restore_command='cp /data/back/pg_xlog2/%f "%p"'
$ sudo /etc/init.d/postgresql restart      --重新启动
$ tailf /var/log/postgresql/postgresql-9.4-main.log     --查看日志是否有错误
# select * from t1;          --表数据恢复
 id 
----
  1
  2
  3
  4
  5
(5 rows)
至此,增量热备份完成。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章