数据库

 首页 > 数据库 > postgresql > Postgresql数据库如何做联机热备

Postgresql数据库如何做联机热备

分享到:
【字体:
导读:
         摘要:Postgresql同样支持在线备份,该备份方式与oracle中用户热备的方式相同,手动拷贝数据文库文件与归档日志。可以根据测试过程将备份直接写成script,通过定制,完成数据库的自动备份。postgresql的恢复支持基于时间戳与事务ID,可以通过时间戳或事务ID的方式,完成...

Postgresql数据库如何做联机热备

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,重新启动数据库

 

[root@daduxiong ~]# service postgresql stop 
Stopping PostgreSQL: server stopped 
ok 
[root@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数据库中创建表并插入记录,作为恢复时的判断。

 

[postgre@daduxiong archive]$ psql postgres 
Welcome to psql 8.3.10, the PostgreSQL interactive terminal. 

Type:  copyright for distribution terms 
       for help with SQL commands 
       for help with psql commands 
       or terminate with semicolon to execute query 
       to quit 

postgres=# create table abc(id integer); 
CREATE TABLE 
postgres=# insert into abc values(1); 
INSERT 
postgres=# q

 

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

 

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

 

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

 

[root@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

 

[root@daduxiong pgsql]# mv /archive/usr/local/pgsql/data /usr/local/pgsql 
[root@daduxiong data]# mkdir pg_xlog 
[root@daduxiong data]# chmod 0700 pg_xlog/ 
[root@daduxiong data]# chown postgre:postgre pg_xlog/ 
[root@daduxiong data]# cd pg_xlog/ 
[root@daduxiong pg_xlog]# mkdir archive_status 
[root@daduxiong pg_xlog]# chmod 0700 archive_status/ 
[root@daduxiong pg_xlog]# chown postgre:postgre archive_status/ 
[root@daduxiong pg_xlog]# mv /archive/*00* /usr/local/pgsql/data/pg_xlog 
[root@daduxiong pg_xlog]# cd .. 
[root@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,配置恢复配置文件


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

 

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


[root@daduxiong data]# service postgresql start 
Starting PostgreSQL: ok 

---省略日志部分内容 
LOG:  selected new timeline ID: 
LOG:  restored log file "00000002.history" from archive 
LOG:  archive recovery complete 
LOG:  autovacuum launcher started 
LOG:  database system is ready to accept connections

 

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


[postgre@daduxiong archive]$ psql postgres 
Welcome to psql 8.3.10, the PostgreSQL interactive terminal. 

Type:  copyright for distribution terms 
       for help with SQL commands 
       for help with psql commands 
       or terminate with semicolon to execute query 
       to quit 

postgres=# select from abc; 
 id 
---- 
  
(1 row) 

postgres=# 

[root@daduxiong data]# ls -l 
total 80 
-rw------- postgre postgre   147 Aug 31 10:26 backup_label.old 
drwx------ postgre postgre  4096 Aug 27 11:33 base 
drwx------ postgre postgre  4096 Aug 31 10:41 global 
drwx------ postgre postgre  4096 Aug 10 11:06 pg_clog 
-rwx------ postgre postgre  3429 Aug 10 11:10 pg_hba.conf 
-rwx------ postgre postgre  1460 Aug 10 11:06 pg_ident.conf 
drwx------ postgre postgre  4096 Aug 10 11:06 pg_multixact 
drwx------ postgre postgre  4096 Aug 10 11:06 pg_subtrans 
drwx------ postgre postgre  4096 Aug 10 11:06 pg_tblspc 
drwx------ postgre postgre  4096 Aug 10 11:06 pg_twophase 
-rwx------ postgre postgre     Aug 10 11:06 PG_VERSION 
drwx------ postgre postgre  4096 Aug 31 10:35 pg_xlog 
-rwx------ postgre postgre 16727 Aug 31 09:42 postgresql.conf 
-rwx------ postgre postgre    59 Aug 31 10:35 postmaster.opts 
-rw------- postgre postgre    47 Aug 31 10:35 postmaster.pid 
-rwxr-x--- 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非常类似。

Postgresql数据库如何做联机热备
分享到:
Linux系统安装PostgreSQL数据库
Linux系统安装PostgreSQL数据库 0.编译环境 Linux: CentOS 5.5 gcc: 4.1.2 1. 安装PostgreSQL 1) 解压postgresql-9.1.7.tar.bz2 #tar jxvf postgresql-9.1.7.tar.bz2   2) 进入解压后的postgresql-9.1.7目录 #cd postgresql-9.1.7 3) 编译postgresql源码 #./configure --prefix=/opt/...
libpq在PostgreSQL客户端编程中的命令执...
libpq在PostgreSQL客户端编程中的命令执行函数 libpq中的命令执行函数有:PQexec,PQexecParams,PQprepare,PQprepared。 PQexec给服务器提交一条命令并且等待结果 定义:PGresult *PQexec(PGconn *conn,const char *command); PQexec返回一个PGresult指针或者NULL。 PGresult *res; const char *command = "INSERT I...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……