Postgresql联机热备

Postgresql联机热备,第1张

概述核心提示:Postgresql联机热备Postgresql同样支持在线备份,该备份方式与oracle中用户热备的方式相同,手动拷贝数据文库文件与归档日志。可以根据测试过程将备份直接写成script,通过定制,完成数据库的自动备份。  postgres Postgresql联机热备 Postgresql同样支持在线备份,该备份方式与oracle中用户热备的方式相同,手动拷贝数据文库文件与归档日志。可 核心提示:Postgresql联机热备Postgresql同样支持在线备份,该备份方式与oracle中用户热备的方式相同,手动拷贝数据文库文件与归档日志。可以根据测试过程将备份直接写成script,通过定制,完成数据库的自动备份。  postgres Postgresql联机热备 Postgresql同样支持在线备份,完成数据库的自动备份。

  postgresql的恢复支持基于时间戳与事务ID,可以通过时间戳或事务ID的方式,完成数据库的不完全恢复或者因错误 *** 作的故障恢复。

  该测试目的:postgresql的在线备份;通过在线备份完成恢复。

  1,开启归档

  [postgre@daduxiong~]$more/usr/local/pgsql/data/postgresql.conf|greparchive_
archive_mode=on#allowsarchivingtobedone
archive_command='cp-i%p/archive/%f>/dev/null'

  2,重新启动数据库

[root@daduxiong~]#servicepostgresqlstop
StopPingPostgresql:serverstopped
ok
[root@daduxiong~]#servicepostgresqlstart
StartingPostgresql:ok

  3,启动备份

[postgre@daduxiongarchive]$psqlpostgres-c"selectpg_start_backup('hot_backup');"
pg_start_backup
-----------------
0/7000020
(1row)

  4,使用tar命令备份数据库文件,不包含pg_xlog目录

  

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

  5,完成备份

[postgre@daduxiongarchive]$psqlpostgres-c"selectpg_stop_backup();"pg_stop_backup----------------0/70133A0(1row)

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

[postgre@daduxiongarchive]$psqlpostgres
Welcometopsql8.3.10,thePostgresqlinteractiveterminal.

Type:\copyrightfordistributionterms
\hforhelpwithsqlcommands
\?forhelpwithpsqlcommands
\gorterminatewithsemicolontoexecutequery
\qtoquit

postgres=#createtableabc(IDinteger);
CREATEtable
postgres=#insertintoabcvalues(1);
INSERT01
postgres=#\q

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

[root@daduxiongpgsql]#servicepostgresqlstop
StopPingPostgresql:serverstopped
ok
[postgre@daduxiongarchive]$cp$PGDATA/pg_xlog/*00*/archive/

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

[root@daduxiongpgsql]#rm-rfdata

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

[postgre@daduxiongpgsql]$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

[root@daduxiongpgsql]#mv/archive/usr/local/pgsql/data/usr/local/pgsql
[root@daduxiongdata]#mkdirpg_xlog
[root@daduxiongdata]#chmod0700pg_xlog/
[root@daduxiongdata]#chownpostgre:postgrepg_xlog/
[root@daduxiongdata]#cdpg_xlog/
[root@daduxiongpg_xlog]#mkdirarchive_status
[root@daduxiongpg_xlog]#chmod0700archive_status/
[root@daduxiongpg_xlog]#chownpostgre:postgrearchive_status/
[root@daduxiongpg_xlog]#mv/archive/*00*/usr/local/pgsql/data/pg_xlog
[root@daduxiongpg_xlog]#cd..
[root@daduxiongdata]#ls
backup_labelpg_clogpg_multixactpg_twophasepostgresql.conf
basepg_hba.confpg_subtransPG_VERSIONpostmaster.opts
globalpg_IDent.confpg_tblspcpg_xlogpostmaster.pID

  11,配置恢复配置文件

[root@daduxiongdata]#touchrecovery.conf
[root@daduxiongdata]#echo"restore_command='cp-i/archive/%f%p'">>recovery.conf
[root@daduxiongdata]#chownpostgre:postgrerecovery.conf
[root@daduxiongdata]#chmod0750recovery.conf

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

[root@daduxiongdata]#servicepostgresqlstart
StartingPostgresql:ok

---省略日志部分内容
LOG:selectednewtimelineID:3
LOG:restoredlogfile"00000002.history"fromarchive
LOG:archiverecoverycomplete
LOG:autovacuumlauncherstarted
LOG:databasesystemisreadytoacceptconnections

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


Type:\copyrightfordistributionterms
\hforhelpwithsqlcommands
\?forhelpwithpsqlcommands
\gorterminatewithsemicolontoexecutequery
\qtoquit

postgres=#select*fromabc;
ID
----
1
(1row)

postgres=#\q


[root@daduxiongdata]#ls-l
total80
-rw-------1postgrepostgre147Aug3110:26backup_label.old
drwx------6postgrepostgre4096Aug2711:33base
drwx------2postgrepostgre4096Aug3110:41global
drwx------2postgrepostgre4096Aug1011:06pg_clog
-rwx------1postgrepostgre3429Aug1011:10pg_hba.conf
-rwx------1postgrepostgre1460Aug1011:06pg_IDent.conf
drwx------4postgrepostgre4096Aug1011:06pg_multixact
drwx------2postgrepostgre4096Aug1011:06pg_subtrans
drwx------2postgrepostgre4096Aug1011:06pg_tblspc
drwx------2postgrepostgre4096Aug1011:06pg_twophase
-rwx------1postgrepostgre4Aug1011:06PG_VERSION
drwx------3postgrepostgre4096Aug3110:35pg_xlog
-rwx------1postgrepostgre16727Aug3109:42postgresql.conf
-rwx------1postgrepostgre59Aug3110:35postmaster.opts
-rw-------1postgrepostgre47Aug3110:35postmaster.pID
-rwxr-x---1postgrepostgre39Aug3110:34recovery.done

  说明:

  该测试中采用的是系统安装默认的数据,如果是生产库需要注意备份所有的表空间,不能仅仅备份软连接.

  数据库完成备份后,recovery.conf文件变成recovery.done,backup_label文件变成了backup_lable.old.

  oracle的备份通常采用RMAN工具备份,该工具功能强大,使用方便,得到大家的认可。

  开源项目组也为postgresql开发了一款类似的工具pg-rman,其功能也很强大,使用方式和特点与ORACLE的RMAN非常类似。

总结

以上是内存溢出为你收集整理的Postgresql联机热备全部内容,希望文章能够帮你解决Postgresql联机热备所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1176696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)

保存