postgresql Point In Time Recovery
Enable archive setting
1 |
|
Origin data
1 |
|
Base backup
1 |
|
Use this command to create a base backup tar file.
Recovery work will start from the last check point of this base backup file.
For more backup methods, please refer to my post.
More data after backup
1 |
|
Emulate server crash
You can use any method to make this happens. Like shutdown your server by power off or kill
.
kill postgresql
Backup latest unarchived xlog
, if you could.
1 |
|
Emulate data files loss.
1 |
|
Now it is time to restore database.
restore base file
What we need to do is just unzip and move all files into $PGDATA
folder.
1 |
|
Now if you start postgresql, you will find data restore to the point of base backup.
But now we need to recover till to most recent valid archieve state.
Setup recovery file
Create a file recovery.conf
under you $PGDATA
.
1 |
|
Since you have your archived WAL, if you start postgresql, database will recovering process till the last
valid segment that was archived
.
But if you want to recover to the last valid moment right before crash, maybe that latest segment has not yet been archived, so you need to copy all pg_xlog
files in original $PGDATA
folder, that is why I said it is better to backup $PGDATA/pg_xlog
even with you enabled archive_mode
1 |
|
If recovery is finished, the recovery.conf
file will be renamed to recovery.done
to avoid further recovery.