Last active
December 28, 2021 08:12
-
-
Save QQGoblin/d5c8a262581b43ba94b407f0b9489474 to your computer and use it in GitHub Desktop.
【Patroni源码阅读】使用pg_basebackup创建副本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Bootstrap(object): | |
def basebackup(self, conn_url, env, options): | |
# creates a replica data dir using pg_basebackup. | |
# this is the default, built-in create_replica_methods | |
# tries twice, then returns failure (as 1) | |
# uses "stream" as the xlog-method to avoid sync issues | |
# supports additional user-supplied options, those are not validated | |
maxfailures = 2 | |
ret = 1 | |
# 禁止用户传递的参数 | |
not_allowed_options = ('pgdata', 'format', 'wal-method', 'xlog-method', 'gzip', | |
'version', 'compress', 'dbname', 'host', 'port', 'username', 'password') | |
user_options = self.process_user_options('basebackup', options, not_allowed_options, logger.error) | |
for bbfailures in range(0, maxfailures): | |
if self._postgresql.cancellable.is_cancelled: | |
break | |
# pg_basebackup运行前需要清空数据目录 | |
if not self._postgresql.data_directory_empty(): | |
self._postgresql.remove_data_directory() | |
try: | |
ret = self._postgresql.cancellable.call([self._postgresql.pgcommand('pg_basebackup'), | |
'--pgdata=' + self._postgresql.data_dir, '-X', 'stream', | |
'--dbname=' + conn_url] + user_options, env=env) | |
if ret == 0: | |
break | |
else: | |
logger.error('Error when fetching backup: pg_basebackup exited with code=%s', ret) | |
except Exception as e: | |
logger.error('Error when fetching backup with pg_basebackup: %s', e) | |
if bbfailures < maxfailures - 1: | |
logger.warning('Trying again in 5 seconds') | |
time.sleep(5) | |
return ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment