save/restore postgresql.conf for WAL-e backups, since WAL-E excludes postgresql.conf from the backup/restore.

This commit is contained in:
Oleksii Kliukin
2015-05-19 12:01:45 +02:00
parent 9fc412a673
commit ae631aae69
+21 -1
View File
@@ -2,6 +2,7 @@ import logging
import os
import psycopg2
import re
import shutil
import subprocess
import sys
import time
@@ -44,6 +45,7 @@ class Postgresql:
self.superuser = config['superuser']
self.admin = config['admin']
self.recovery_conf = os.path.join(self.data_dir, 'recovery.conf')
self.postgresql_conf = os.path.join(self.data_dir, 'postgresql.conf')
self._pg_ctl = 'pg_ctl -w -D ' + self.data_dir
self._wal_e = 'envdir {} wal-e --aws-instance-profile '.format(os.environ.get('WALE_ENV_DIR', '/home/postgres/etc/wal-e.d/env'))
@@ -91,6 +93,7 @@ class Postgresql:
def initialize(self):
if os.system(self._pg_ctl + ' initdb') == 0:
self.save_postgresql_conf()
self.write_pg_hba()
return True
@@ -123,7 +126,10 @@ class Postgresql:
data_dir=self.data_dir, **master_connection))
def create_replica_with_s3(self):
return os.system(self._wal_e + ' backup-fetch {} LATEST'.format(self.data_dir))
ret = os.system(self._wal_e + ' backup-fetch {} LATEST'.format(self.data_dir))
self.restore_postgresql_conf()
return ret
def should_use_s3_to_create_replica(self, master_connurl):
""" determine whether it makes sense to use S3 and not pg_basebackup """
@@ -301,6 +307,20 @@ primary_conninfo = '{}'
self.write_recovery_conf(leader)
self.restart()
def save_posgresql_conf(self):
"""
copy postgresql.conf to postgresql.conf.backup to preserve it in the WAL-e backup.
see http://comments.gmane.org/gmane.comp.db.postgresql.wal-e/239
"""
shutil.copy(self.postgresql_conf, self.postgresql_conf+'.backup')
def restore_postgresql_conf(self):
""" restore a previously saved postgresql.conf """
try:
shutil.copy(self.postgresql_conf+'.backup', self.postgresql_conf)
except Exception as e:
logger.error("unable to restore postgresql.conf from WAL-E backup: {}".format(e))
def promote(self):
return os.system(self._pg_ctl + ' promote') == 0