|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import tempfile |
| 6 | + |
| 7 | +from .consts import \ |
| 8 | + DATA_DIR as _DATA_DIR, \ |
| 9 | + BACKUP_LOG_FILE as _BACKUP_LOG_FILE, \ |
| 10 | + DEFAULT_XLOG_METHOD as _DEFAULT_XLOG_METHOD |
| 11 | + |
| 12 | +from .exceptions import \ |
| 13 | + BackupException |
| 14 | + |
| 15 | +from .utils import \ |
| 16 | + default_username as _default_username, \ |
| 17 | + execute_utility as _execute_utility, \ |
| 18 | + explain_exception as _explain_exception |
| 19 | + |
| 20 | + |
| 21 | +class NodeBackup(object): |
| 22 | + """ |
| 23 | + Smart object responsible for backups |
| 24 | + """ |
| 25 | + |
| 26 | + @property |
| 27 | + def log_file(self): |
| 28 | + return os.path.join(self.base_dir, _BACKUP_LOG_FILE) |
| 29 | + |
| 30 | + def __init__(self, |
| 31 | + node, |
| 32 | + base_dir=None, |
| 33 | + username=None, |
| 34 | + xlog_method=_DEFAULT_XLOG_METHOD): |
| 35 | + |
| 36 | + if not node.status(): |
| 37 | + raise BackupException('Node must be running') |
| 38 | + |
| 39 | + # Set default arguments |
| 40 | + username = username or _default_username() |
| 41 | + base_dir = base_dir or tempfile.mkdtemp() |
| 42 | + |
| 43 | + # public |
| 44 | + self.original_node = node |
| 45 | + self.base_dir = base_dir |
| 46 | + |
| 47 | + # private |
| 48 | + self._available = True |
| 49 | + |
| 50 | + data_dir = os.path.join(self.base_dir, _DATA_DIR) |
| 51 | + _params = [ |
| 52 | + "-D{}".format(data_dir), "-p{}".format(node.port), |
| 53 | + "-U{}".format(username), "-X{}".format(xlog_method) |
| 54 | + ] |
| 55 | + _execute_utility("pg_basebackup", _params, self.log_file) |
| 56 | + |
| 57 | + def __enter__(self): |
| 58 | + return self |
| 59 | + |
| 60 | + def __exit__(self, type, value, traceback): |
| 61 | + self.cleanup() |
| 62 | + |
| 63 | + def _prepare_dir(self, destroy): |
| 64 | + """ |
| 65 | + Provide a data directory for a copy of node. |
| 66 | +
|
| 67 | + Args: |
| 68 | + destroy: should we convert this backup into a node? |
| 69 | +
|
| 70 | + Returns: |
| 71 | + Path to data directory. |
| 72 | + """ |
| 73 | + |
| 74 | + if not self._available: |
| 75 | + raise BackupException('Backup is exhausted') |
| 76 | + |
| 77 | + # Do we want to use this backup several times? |
| 78 | + available = not destroy |
| 79 | + |
| 80 | + if available: |
| 81 | + dest_base_dir = tempfile.mkdtemp() |
| 82 | + |
| 83 | + data1 = os.path.join(self.base_dir, _DATA_DIR) |
| 84 | + data2 = os.path.join(dest_base_dir, _DATA_DIR) |
| 85 | + |
| 86 | + try: |
| 87 | + # Copy backup to new data dir |
| 88 | + shutil.copytree(data1, data2) |
| 89 | + except Exception as e: |
| 90 | + raise BackupException(_explain_exception(e)) |
| 91 | + else: |
| 92 | + dest_base_dir = self.base_dir |
| 93 | + |
| 94 | + # Is this backup exhausted? |
| 95 | + self._available = available |
| 96 | + |
| 97 | + # Return path to new node |
| 98 | + return dest_base_dir |
| 99 | + |
| 100 | + def spawn_primary(self, name, destroy=True, use_logging=False): |
| 101 | + """ |
| 102 | + Create a primary node from a backup. |
| 103 | +
|
| 104 | + Args: |
| 105 | + name: name for a new node. |
| 106 | + destroy: should we convert this backup into a node? |
| 107 | + use_logging: enable python logging. |
| 108 | +
|
| 109 | + Returns: |
| 110 | + New instance of PostgresNode. |
| 111 | + """ |
| 112 | + |
| 113 | + base_dir = self._prepare_dir(destroy) |
| 114 | + |
| 115 | + # Build a new PostgresNode |
| 116 | + from .node import PostgresNode |
| 117 | + node = PostgresNode( |
| 118 | + name=name, |
| 119 | + base_dir=base_dir, |
| 120 | + master=self.original_node, |
| 121 | + use_logging=use_logging) |
| 122 | + |
| 123 | + # New nodes should always remove dir tree |
| 124 | + node._should_rm_dirs = True |
| 125 | + |
| 126 | + node.append_conf("postgresql.conf", "\n") |
| 127 | + node.append_conf("postgresql.conf", "port = {}".format(node.port)) |
| 128 | + |
| 129 | + return node |
| 130 | + |
| 131 | + def spawn_replica(self, name, destroy=True, use_logging=False): |
| 132 | + """ |
| 133 | + Create a replica of the original node from a backup. |
| 134 | +
|
| 135 | + Args: |
| 136 | + name: name for a new node. |
| 137 | + destroy: should we convert this backup into a node? |
| 138 | + use_logging: enable python logging. |
| 139 | +
|
| 140 | + Returns: |
| 141 | + New instance of PostgresNode. |
| 142 | + """ |
| 143 | + |
| 144 | + node = self.spawn_primary(name, destroy, use_logging=use_logging) |
| 145 | + node._create_recovery_conf(self.original_node) |
| 146 | + |
| 147 | + return node |
| 148 | + |
| 149 | + def cleanup(self): |
| 150 | + if self._available: |
| 151 | + shutil.rmtree(self.base_dir, ignore_errors=True) |
| 152 | + self._available = False |
0 commit comments