Migrate PostgreSQL Server to New Server With Minimal Downtime using PITR (Point In Time Recovery)
Source Server
mkdir /archive; chown postgres.postgres /archive;
vi /usr/local/postgres/data/postgresql.conf
Add the following lines:
archive_mode = on
archive_command = 'cp -i %p /archive%f'
Reload Postgres process
Now goto Psql command mode and run this query statemenet:
postgres=# SELECT pg_start_backup('backup');
postgres=# \q
Now copy the Source Server's /usr/local/postgres/data/ contents to destincation server
rsync --progress -azv --delete /usr/local/postgres/data/ root@:/usr/local/postgres/data/
Now stop Source server postgresql's backup mode;
postgres=# SELECT pg_stop_backup();
postgres=# \q
Destination server
Now on the Destination Server, create the directory, /archive
mkdir /archive; sudo chown postgres.postgres /archive;
vi /usr/local/postgres/data/postgresql.conf
Paste the following line into the newly created file:
restore_command = '/usr/local/postgres/bin/pg_standby -l -d -s 2 -t /tmp/pgsql.trigger /archive %f %p %r 2>>standby.log'
Now sync the /archive directories across, either from the Source server or Destination server
rsync --progress -azv --delete root@:/archive /archive
Now whenever want to switch to new server, just re-sync the /archive and create a trigger file, to start the Postgresql Server in Normal Mode
rsync --progress -azv --delete root@:/archive /archive
su -c 'touch /tmp/pgsql.trigger' postgres
And that;s all, Your New server will be ready as Live DB Server to the point that you created the Trigger File.
No comments:
Post a Comment