设为首页 收藏本站
查看: 989|回复: 0

[经验分享] postgresql 备份恢复(二)

[复制链接]

尚未签到

发表于 2016-11-21 08:44:57 | 显示全部楼层 |阅读模式
  postgresql同样支持在线备份,该备份方式与oracle中用户热备的方式相同,手动拷贝数据文库文件与归档日志。可以根据测试过程将备份直接写成script,通过定制,完成数据库的自动备份。
postgresql的恢复支持基于时间戳与事务ID,可以通过时间戳或事务ID的方式,完成数据库的不完全恢复或者因错误操作的故障恢复。
该测试目的:postgresql的在线备份;通过在线备份完成恢复。

  1,开启归档



[postgre@daduxiong ~]$ more /usr/local/pgsql/data/postgresql.conf |grep archive_
archive_mode = on               # allows archiving to be done
archive_command = 'cp -i %p /archive/%f >/dev/null'
2,重新启动数据库



[iyunv@daduxiong ~]# service postgresql stop
Stopping PostgreSQL: server stopped
ok
[iyunv@daduxiong ~]# service postgresql start
Starting PostgreSQL: ok
3,启动备份
  



[postgre@daduxiong archive]$ psql postgres -c "select pg_start_backup('hot_backup');"
pg_start_backup
-----------------
0/7000020
(1 row)
4,使用tar命令备份数据库文件,不包含pg_xlog目录
  



[postgre@daduxiong archive]$ tar --exclude $PGDATA/pg_xlog -cvjpf /archive/pgbackup.tar.bz2 $PGDATA


  5,完成备份
  



[postgre@daduxiong archive]$ psql postgres -c "select pg_stop_backup();"
pg_stop_backup
----------------
0/70133A0
(1 row)


  6,在postgres数据库中创建表并插入记录,作为恢复时的判断。

DSC0000.gif DSC0001.gif 代码



[postgre@daduxiong archive]$ psql postgres
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
postgres=# create table abc(id integer);
CREATE TABLE
postgres=# insert into abc values(1);
INSERT 0 1
postgres=# \q


  7,此时假设数据库出现问题,停止数据库,拷贝日志



[iyunv@daduxiong pgsql]# service postgresql stop
Stopping PostgreSQL: server stopped
ok
[postgre@daduxiong archive]$ cp $PGDATA/pg_xlog/*00* /archive/


  8,删除"发生错误"的data目录



[iyunv@daduxiong pgsql]# rm -rf data


  9,解压之前的备份文件压缩包

代码



[postgre@daduxiong pgsql]$ tar -xvf /archive/pgbackup.tar.bz2
....省略
/usr/local/pgsql/data/global/2843
/usr/local/pgsql/data/postmaster.opts
/usr/local/pgsql/data/pg_twophase/
/usr/local/pgsql/data/postmaster.pid
/usr/local/pgsql/data/backup_label
/usr/local/pgsql/data/PG_VERSION


  10,恢复data目录,重新创建pg_xlog目录及其子目录archive_status

代码



[iyunv@daduxiong pgsql]# mv /archive/usr/local/pgsql/data /usr/local/pgsql
[iyunv@daduxiong data]# mkdir pg_xlog
[iyunv@daduxiong data]# chmod 0700 pg_xlog/
[iyunv@daduxiong data]# chown postgre:postgre pg_xlog/
[iyunv@daduxiong data]# cd pg_xlog/
[iyunv@daduxiong pg_xlog]# mkdir archive_status
[iyunv@daduxiong pg_xlog]# chmod 0700 archive_status/
[iyunv@daduxiong pg_xlog]# chown postgre:postgre archive_status/
[iyunv@daduxiong pg_xlog]# mv /archive/*00* /usr/local/pgsql/data/pg_xlog
[iyunv@daduxiong pg_xlog]# cd ..
[iyunv@daduxiong data]# ls
backup_label  pg_clog        pg_multixact  pg_twophase  postgresql.conf
base          pg_hba.conf    pg_subtrans   PG_VERSION   postmaster.opts
global        pg_ident.conf  pg_tblspc     pg_xlog      postmaster.pid


  11,配置恢复配置文件

代码



[iyunv@daduxiong data]# touch recovery.conf
[iyunv@daduxiong data]# echo "restore_command='cp -i /archive/%f %p'" >>recovery.conf
[iyunv@daduxiong data]# chown postgre:postgre recovery.conf
[iyunv@daduxiong data]# chmod 0750 recovery.conf


  12,启动数据库,观察数据库启动的日志

代码



[iyunv@daduxiong data]# service postgresql start
Starting PostgreSQL: ok
---省略日志部分内容
LOG:  selected new timeline ID: 3
LOG:  restored log file "00000002.history" from archive
LOG:  archive recovery complete
LOG:  autovacuum launcher started
LOG:  database system is ready to accept connections
cp: overwrite `/archive/000000010000000000000008'? cp: overwrite `/archive/000000010000000000000009'? cp: overwrite `/archive/000000020000000000000009'? cp: overwrite `/archive/00000002000000000000000A'?


  13,验证恢复结果。检查之前创建的表与记录。
  

代码



[postgre@daduxiong archive]$ psql postgres
Welcome to psql 8.3.10, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms
       \h for help with SQL commands
       \? for help with psql commands
       \g or terminate with semicolon to execute query
       \q to quit
postgres=# select * from abc;
id
----
  1
(1 row)
postgres=# \q

[iyunv@daduxiong data]# ls -l
total 80
-rw------- 1 postgre postgre   147 Aug 31 10:26 backup_label.old
drwx------ 6 postgre postgre  4096 Aug 27 11:33 base
drwx------ 2 postgre postgre  4096 Aug 31 10:41 global
drwx------ 2 postgre postgre  4096 Aug 10 11:06 pg_clog
-rwx------ 1 postgre postgre  3429 Aug 10 11:10 pg_hba.conf
-rwx------ 1 postgre postgre  1460 Aug 10 11:06 pg_ident.conf
drwx------ 4 postgre postgre  4096 Aug 10 11:06 pg_multixact
drwx------ 2 postgre postgre  4096 Aug 10 11:06 pg_subtrans
drwx------ 2 postgre postgre  4096 Aug 10 11:06 pg_tblspc
drwx------ 2 postgre postgre  4096 Aug 10 11:06 pg_twophase
-rwx------ 1 postgre postgre     4 Aug 10 11:06 PG_VERSION
drwx------ 3 postgre postgre  4096 Aug 31 10:35 pg_xlog
-rwx------ 1 postgre postgre 16727 Aug 31 09:42 postgresql.conf
-rwx------ 1 postgre postgre    59 Aug 31 10:35 postmaster.opts
-rw------- 1 postgre postgre    47 Aug 31 10:35 postmaster.pid
-rwxr-x--- 1 postgre postgre    39 Aug 31 10:34 recovery.done


  说明:
该测试中采用的是系统安装默认的数据,如果是生产库需要注意备份所有的表空间,不能仅仅备份软连接.
数据库完成备份后,recovery.conf文件变成recovery.done,backup_label文件变成了backup_lable.old.
  oracle的备份通常采用RMAN工具备份,该工具功能强大,使用方便,得到大家的认可。
  开源项目组也为postgresql开发了一款类似的工具pg-rman,其功能也很强大,使用方式和特点与ORACLE的RMAN非常类似。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-303192-1-1.html 上篇帖子: 【Postgresql】字符串操作函数 下篇帖子: Postgresql数据库的一些字符串操作函数
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表