postgres的增量恢复
admin
2023-05-21 09:42:26
0

DBA童鞋对增量恢复的概念一定很熟悉,与mysql的增量恢复类似,使用“t1时刻的全备”+“t1至t2时刻的wal日志”,即可将postgres恢复至t2时刻。

前期准备:
配置postgres.conf:
wal_level=archive 或 hot_standby 或 更高级别
archive_mode = on
archive_command='DATE=date +%Y%m%d;DIR="/paic/pg6666/pg_archlog/$DATE";(test -d $DIR || mkdir -p $DIR) && cp %p $DIR/%f'

备份脚本backup.sh:

#!/bin/bash 

export LANG=en_US.utf8 
export PGHOME=/paic/postgres/base/9.4.0 
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:/usr/lib64:/usr/local/lib64:/lib:/usr/lib:/usr/local/lib:$LD_LIBRARY_PATH 
export DATE=`date +"%Y%m%d"` 
export PATH=$PGHOME/bin:$PATH:. 
export PGDATA=/paic/pg6666/data

BASEDIR="/paic/postgres/home/postgres/pg_bak" 

date +%F-%T 

if [ ! -d $BASEDIR/$DATE ]; then 
  mkdir -p $BASEDIR/$DATE 
  if [ $? -eq 0 ]; then 
    psql -h 127.0.0.1 -p 6666 -U postgres postgres -c "select pg_start_backup(now()::text)" 
    if [ $? -eq 0 ]; then 
      cp -r -L $PGDATA $BASEDIR/$DATE 
    else 
      echo -e "select pg_start_backup(now()::text) error" 
      exit 1 
    fi 
    psql -h 127.0.0.1 -p 6666 -U postgres postgres -c "select pg_stop_backup()" 
    date +%F-%T 
    echo -e "backup successed" 
    exit 0 
  else 
    echo -e "mkdir -p $BASEDIR/$DATE error" 
    exit 1 
  fi 
else 
  echo -e "$DATE backuped, don't backup repeated" 
  exit 1 
fi

恢复脚本recovery.sh:

#!/bin/bash 
export LANG=en_US.utf8 
export PGHOME=/paic/postgres/base/9.4.0 
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 PGDATA=/paic/pg6666/data
export DATE=`date +"%Y%m%d"`

if [ -z "$1" ]; then
  echo "1st argument is empty!"
else
  if [ -z "$2" ]; then
    echo "2nd argument is empty!"
  else
    if [ -f $PGDATA/postmaster.pid ]; then
      echo "shutdown database first!"
    else 
      cd $PGDATA
      rm -rf *
      cp -r /paic/postgres/home/postgres/pg_bak/$DATE/data/* $PGDATA/
      cd $PGDATA/pg_xlog
      rm -rf *
      cd $PGDATA
      cp $PGHOME/share/recovery.conf.sample ./recovery.conf
      echo restore_command = \'cp /paic/pg6666/pg_archlog/$DATE/%f %p\' >> ./recovery.conf
      echo recovery_target_time = \'$1 $2\' >> ./recovery.conf
      pg_ctl start
    fi
  fi
fi

backup.sh和recovery.sh中的目录请自行修改。

模拟故障恢复:
1.准备阶段
-bash-4.1$ psql
psql (9.4.0)
Type "help" for help.
postgres=# \c mydb alex
You are now connected to database "mydb" as user "alex".
确认数据库初始状态
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | test | table | alex
(1 row)
此时mydb数据库只有test表

创建aaa
mydb=# create table aaa(id int);
CREATE TABLE
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | aaa | table | alex
public | test | table | alex
(2 rows)
mydb=# checkpoint; --确保修改写入文件(非必须)
CHECKPOINT
mydb=# select pg_switch_xlog(); --确保修改写入归档(非必须)
pg_switch_xlog
·····················
0/E6000120
(1 row)
上述操作于2017-12-22 13:52:00前完成

接着在2017-12-22 13:53:00时,创建表bbb
mydb=# create table bbb(id int);
CREATE TABLE
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | aaa | table | alex
public | bbb | table | alex
public | test | table | alex
(3 rows)
mydb=# checkpoint;
CHECKPOINT
mydb=# select pg_switch_xlog();
pg_switch_xlog
·····················
0/E7013FC0
(1 row)
mydb=# \q
上述操作于2017-12-22 13:54:00前完成

2.现在尝试回滚数据库至指定时间点
-bash-4.1$ pg_ctl stop -m fast
waiting for server to shut down..... done
server stopped
-bash-4.1$ cd --我的recovery.sh 文件放在home目录,所以需切换目录
-bash-4.1$ . recovery.sh 2017-12-22 13:52:00 --传入时间参数$1:2017-12-22, $2:13:52:00 并执行脚本
pg_ctl: another server might be running; trying to start server anyway
server starting
-bash-4.1$ 2017-12-22 13:54:26 HKT:undefined:[13323]: LOG: redirecting log output to logging collector process
2017-12-22 13:54:26 HKT:undefined:[13323]: HINT: Future log output will appear in directory "/paic/pg6666/data/pg_log".
-bash-4.1$
-bash-4.1$
-bash-4.1$ psql
psql (9.4.0)
Type "help" for help.
postgres=# \c mydb alex
You are now connected to database "mydb" as user "alex".
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | aaa | table | alex
public | test | table | alex
(2 rows)
mydb=# \q
可知目前数据库已回滚至2017-12-22 13:52:00时,刚创建完表aaa状态

继续测试
-bash-4.1$ pg_ctl stop
waiting for server to shut down.... done
server stopped
-bash-4.1$ cd
-bash-4.1$ . recovery.sh 2017-12-22 13:53:10
pg_ctl: another server might be running; trying to start server anyway
server starting
-bash-4.1$ 2017-12-22 13:56:39 HKT:undefined:[13390]: LOG: redirecting log output to logging collector process
2017-12-22 13:56:39 HKT:undefined:[13390]: HINT: Future log output will appear in directory "/paic/pg6666/data/pg_log".
-bash-4.1$
-bash-4.1$ psql
psql (9.4.0)
Type "help" for help.
postgres=# \c mydb alex
You are now connected to database "mydb" as user "alex".
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | aaa | table | alex
public | bbb | table | alex
public | test | table | alex
(3 rows)
可知目前数据库已回滚至2017-12-22 13:53:10时,刚创建完表bbb的状态

3.现在模拟删表误操作的回滚
2017-12-22 14:01:00 删除表test
mydb=# drop table test;
DROP TABLE
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | aaa | table | alex
public | bbb | table | alex
mydb=# checkpoint;
CHECKPOINT
mydb=# select pg_switch_xlog();
pg_switch_xlog
·····················
0/E9005140
(1 row)
mydb=# \q

执行recovery.sh回滚数据库
-bash-4.1$ pg_ctl stop
waiting for server to shut down.... done
server stopped
-bash-4.1$ cd
-bash-4.1$ . recovery.sh 2017-12-22 14:00:00
pg_ctl: another server might be running; trying to start server anyway
server starting
-bash-4.1$ 2017-12-22 14:02:09 HKT:undefined:[13583]: LOG: redirecting log output to logging collector process
2017-12-22 14:02:09 HKT:undefined:[13583]: HINT: Future log output will appear in directory "/paic/pg6666/data/pg_log".
-bash-4.1$
-bash-4.1$ psql
psql (9.4.0)
Type "help" for help.
postgres=# \c mydb alex
You are now connected to database "mydb" as user "alex".
mydb=# \d
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------
public | aaa | table | alex
public | bbb | table | alex
public | test | table | alex
(3 rows)
可知目前数据库已回滚至2017-12-22 13:14:00时,表test未被删除的状态

本实验仅限在测试环境模拟,有助于理解postgres的备份恢复机制,禁止用于生产!

相关内容

热门资讯

缅甸提出打击电诈新法草案,最高... 据凤凰卫视报道,缅甸政府近日公布打击跨境网络诈骗的新法律草案,对经营诈骗园区与相关犯罪行为祭出重刑,...
红夹克、机舱合影,李显龙晒访华... 5月18日,新加坡国务资政李显龙在社交平台发文,他已抵达广西南宁,开启本周访华行程,并晒出机舱照、与...
“赌王”女儿何超蕸,真正死因公... 5月17日,已故“赌王”何鸿燊女儿何超蕸的追思会,在香港举行。此次追思会由何超琼亲自操办,一早她便到...
宇通4款高端车型亮相米兰交通运... 近日,2026米兰交通运输展览会(NME)在意大利米兰国际展览中心拉开帷幕。在这场汇聚全球目光的行业...
伊朗已逮捕6500多名间谍及通... 据伊朗迈赫尔通讯社18日报道,伊朗治安部队司令艾哈迈德-礼萨·拉丹说,自2月底美国和以色列对伊朗发动...
男子与13岁智力缺陷少女发生性... 澎湃新闻记者 刘璐男子与13岁智力缺陷少女发生性关系,犯强奸罪被判刑。5月18日,最高检联合中国残联...
屋里墙面潮湿起皮脱落怎么处理 1、倘若轻微掉皮先把掉皮位置铲除干净,其次把墙面清理干净,并且用砂纸把墙面打磨平整,然后涂刷一遍防水...
墙面乳胶漆出现起皮和脱落现象怎... 1、如果墙面属于大面积的起皮脱落,就应当将开裂的基层裂缝凿成一个平整的斜面并进行打磨,同时还要对其进...
油漆起皮脱落怎么处理 1、对于起皮脱落的漆面,先把脱落部分用油灰刀刮掉,再使用纤维素腻子把不平和坑凹之处补平,干燥之后用砂...
原乳胶漆墙面可以直接批腻子刷漆... 原乳胶漆墙面可以直接批腻子刷漆吗 ?1、不可以直接在乳胶漆上批刮腻子,因为乳胶漆的表面十分光滑,会...