PostgreSQLdata同步工具【pg_rewind】(二)

2015-02-03 11:52:39 · 作者: · 浏览: 64
node2 ~]$ createdb pgbench

[postgres@node2 ~]$ pgbench -i -s 10 pgbench

4.4将node1恢复为standby

同步数据:

[postgres@node1 ~]$ pg_rewind -D /opt/pgsql/data/--source-server='host=node2 user=postgres port=5432'

The serversdiverged at WAL position 0/30000C8 on timeline 1.

No rewindrequired.

[postgres@node1 ~]$ vi /opt/pgsql/data/recovery.conf

standby_mode ='on'

primary_conninfo= 'host=node2 user=postgres port=5432'

recovery_target_timeline= 'latest'

启动node1上的数据库

[postgres@node1 ~]$ pg_ctl start

4.4将node1恢复为master

停止node2:

[postgres@node2 ~]$ pg_ctl stop -m f

提升node1状态:

[postgres@node1 ~]$ pg_ctl promote

在node1上做一些更新:

[postgres@node1 ~]$ pgbench -s 10 -T 60 pgbench

恢复node2为standby:

[postgres@node2 ~]$ pg_rewind -D /opt/pgsql/data/--source-server='host=node1 user=postgres port=5432'

The serversdiverged at WAL position 0/12ACCC30 on timeline 2.

No rewindrequired.?

[postgres@node2 ~]$ mv /opt/pgsql/data/recovery.done/opt/pgsql/data/recovery.conf

[postgres@node2 ~]$ vi /opt/pgsql/data/recovery.conf

(修改node2为node1)?

启动node2上的数据库

[postgres@node2 ~]$ pg_ctl start

server starting

五、基本原理

The basic idea is to copy everything fromthe new cluster to the old cluster,

except for the blocks that we know to bethe same.

1. Scan the WAL log of the old cluster,starting from the last checkpoint before

the point where the new cluster's timelinehistory forked off from the old cluster.

For each WAL record, make a note of thedata blocks that were touched. This yields

a list of all the data blocks that werechanged in the old cluster, after the new

cluster forked off.

2. Copy all those changed blocks from thenew cluster to the old cluster.

3. Copy all other files like clog, conffiles etc. from the new cluster to old cluster.

Everything except the relation files.

4. Apply the WAL from the new cluster,starting from the checkpoint created at

failover. (pg_rewind doesn't actually apply the WAL, it just creates abackup

label file indicating that when PostgreSQLis started, it will start replay

from that checkpoint and apply all the requiredWAL)

六、参考文献

https://github.com/vmware/pg_rewind

http://michael.otacoo.com/postgresql-2/postgres-module-highlight-pg_rewind-to-recycle-a-postgres-master-into-a-slave/

七、license

pg_rewind can be distributed under theBSD-style PostgreSQL license