Skip to content

Instantly share code, notes, and snippets.

@jberkus
Created October 7, 2015 18:05
Show Gist options
  • Save jberkus/4b6a949f569f0f435824 to your computer and use it in GitHub Desktop.
Save jberkus/4b6a949f569f0f435824 to your computer and use it in GitHub Desktop.
sample restore code
def create_replica(self, master_connection, env):
# create the replica according to the replica_method
# defined by the user. this is a list, so we need to
# loop through all methods the user supplies
connstring = self.build_connstring(master_connection)
# get list of replica methods from config
replica_method = config.get('create_replica_method', 'basebackup')
self.replica_methods = [rm.strip() for rm in replica_method.split(',')]
# go through them in priority order
for replica_method in replica_methods:
# if the method is basebackup, then use the built-in
if replica_method == "basebackup":
ret = self.basebackup()
if ret == 0:
# if basebackup succeeds, exit with success
return 0
else:
# user-defined method; check for configuration
# not required, actually
if replica_method in self.config:
# look to see if the user has supplied a full command path
# if not, use the method name as the command
if "command" in self.config[replica_method]:
cmd = self.config[replica_method]["command"]
else:
cmd = replica_method
# get the rest of the replica config
method_config = self.config[replica_method].copy()
# remove the command and turn it into a shlex set
del method_config["command"]
# add the default parameters
method_config.update({"scope": self.scope,
"role" : "replica",
"datadir" : self.data_dir
"connstring" : self.connstring})
params = ["--{0}={1}".format(arg, val) for arg, val in method_config.iteritems()]
try:
# call script with the full set of parameters
ret = subprocess.call(shlex.split(cmd) + shlex.split(method_config), env=env)
except Exception as e:
logger.exception('Error creating replica using method {0}: {1}'.format(replica_method, e.str))
ret = 1
return ret
def basebackup(self):
# creates a replica data dir using pg_basebackup.
# this is the default, built-in create_replica_method
# tries twice, then returns failure (as 1)
# uses "stream" as the xlog-method to avoid sync issues
bbfailures = 0;
maxfailures = 2;
while bbfailures < maxfailures:
try:
ret = subprocess.call(['pg_basebackup', '-R', '-D', '--xlog-method=stream',
self.data_dir, '--host=' + self.master_connection['host'],
'--port=' + str(self.master_connection['port']),
'-U', self.master_connection['user']],
env=self.env)
except Exception as e:
logger.error('Error when fetching backup with pg_basebackup: {0}'.format(e))
ret = 1
bbfailures += 1
if bbfailures < maxfailures:
logger.error('Trying again in 5 seconds')
sleep 5
else:
break
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment