|
| 1 | +from __future__ import absolute_import, unicode_literals |
| 2 | + |
| 3 | +__all__ = ['Backup'] |
| 4 | + |
| 5 | +from arango.api import APIWrapper |
| 6 | +from arango.exceptions import ( |
| 7 | + BackupCreateError, |
| 8 | + BackupDeleteError, |
| 9 | + BackupDownloadError, |
| 10 | + BackupGetError, |
| 11 | + BackupRestoreError, |
| 12 | + BackupUploadError |
| 13 | +) |
| 14 | +from arango.formatter import ( |
| 15 | + format_backup, |
| 16 | + format_backup_restore, |
| 17 | + format_backup_transfer |
| 18 | +) |
| 19 | +from arango.request import Request |
| 20 | + |
| 21 | + |
| 22 | +class Backup(APIWrapper): |
| 23 | + |
| 24 | + def __init__(self, connection, executor): |
| 25 | + super(Backup, self).__init__(connection, executor) |
| 26 | + |
| 27 | + def get(self, backup_id=None): |
| 28 | + """Return backup details. |
| 29 | +
|
| 30 | + :param backup_id: If set, details on only the specified backup is |
| 31 | + returned. Otherwise details on all backups are returned. |
| 32 | + :type backup_id: str |
| 33 | + :return: Backup details. |
| 34 | + :rtype: dict |
| 35 | + :raise arango.exceptions.BackupGetError: If delete fails. |
| 36 | + """ |
| 37 | + request = Request( |
| 38 | + method='post', |
| 39 | + endpoint='/_admin/backup/list', |
| 40 | + data={} if backup_id is None else {'id': backup_id} |
| 41 | + ) |
| 42 | + |
| 43 | + def response_handler(resp): |
| 44 | + if resp.is_success: |
| 45 | + backups = resp.body['result']['list'] |
| 46 | + for key in backups: |
| 47 | + backups[key] = format_backup(backups[key]) |
| 48 | + return resp.body['result'] |
| 49 | + raise BackupGetError(resp, request) |
| 50 | + |
| 51 | + return self._execute(request, response_handler) |
| 52 | + |
| 53 | + def create(self, |
| 54 | + label=None, |
| 55 | + allow_inconsistent=None, |
| 56 | + force=None, |
| 57 | + timeout=None): |
| 58 | + """Create a backup when the global write lock can be obtained. |
| 59 | +
|
| 60 | + :param label: Backup label. If not given, a UUID is used. |
| 61 | + :type label: str |
| 62 | + :param allow_inconsistent: Allow inconsistent backup when the global |
| 63 | + transaction lock cannot be acquired before timeout. Default value |
| 64 | + is False. |
| 65 | + :type allow_inconsistent: bool |
| 66 | + :param force: Forcefully abort all running transactions to ensure a |
| 67 | + consistent backup when the global transaction lock cannot be |
| 68 | + acquired before timeout. Default (and highly recommended) value |
| 69 | + is False. |
| 70 | + :type force: bool |
| 71 | + :param timeout: Timeout in seconds for creating the backup. Default |
| 72 | + value is 120 seconds. |
| 73 | + :type timeout: int |
| 74 | + :return: Result of the create operation. |
| 75 | + :rtype: dict |
| 76 | + :raise arango.exceptions.BackupCreateError: If create fails. |
| 77 | + """ |
| 78 | + data = {'label': label} |
| 79 | + |
| 80 | + if allow_inconsistent is not None: |
| 81 | + data['allowInconsistent'] = allow_inconsistent |
| 82 | + if force is not None: |
| 83 | + data['force'] = force |
| 84 | + if timeout is not None: |
| 85 | + data['timeout'] = timeout |
| 86 | + |
| 87 | + request = Request( |
| 88 | + method='post', |
| 89 | + endpoint='/_admin/backup/create', |
| 90 | + data=data |
| 91 | + ) |
| 92 | + |
| 93 | + def response_handler(resp): |
| 94 | + if resp.is_success: |
| 95 | + return format_backup(resp.body['result']) |
| 96 | + raise BackupCreateError(resp, request) |
| 97 | + |
| 98 | + return self._execute(request, response_handler) |
| 99 | + |
| 100 | + def delete(self, backup_id): |
| 101 | + """Delete a backup. |
| 102 | +
|
| 103 | + :param backup_id: Backup ID. |
| 104 | + :type backup_id: str |
| 105 | + :return: True if the backup was deleted successfully. |
| 106 | + :rtype: bool |
| 107 | + :raise arango.exceptions.BackupDeleteError: If delete fails. |
| 108 | + """ |
| 109 | + request = Request( |
| 110 | + method='post', |
| 111 | + endpoint='/_admin/backup/delete', |
| 112 | + data={'id': backup_id} |
| 113 | + ) |
| 114 | + |
| 115 | + def response_handler(resp): |
| 116 | + if resp.is_success: |
| 117 | + return True |
| 118 | + raise BackupDeleteError(resp, request) |
| 119 | + |
| 120 | + return self._execute(request, response_handler) |
| 121 | + |
| 122 | + def download(self, |
| 123 | + backup_id=None, |
| 124 | + repository=None, |
| 125 | + abort=None, |
| 126 | + config=None, |
| 127 | + download_id=None): |
| 128 | + """Manage backup downloads. |
| 129 | +
|
| 130 | + :param backup_id: Backup ID used for scheduling a download. Mutually |
| 131 | + exclusive with parameter **download_id**. |
| 132 | + :type backup_id: str |
| 133 | + :param repository: Remote repository URL (e.g. "local://tmp/backups"). |
| 134 | + Required for scheduling a download and mutually exclusive with |
| 135 | + parameter **download_id**. |
| 136 | + :type repository: str |
| 137 | + :param config: Remote repository configuration. Required for scheduling |
| 138 | + a download and mutually exclusive with parameter **download_id**. |
| 139 | + :type config: dict |
| 140 | + :param download_id: Download ID. Mutually exclusive with parameters |
| 141 | + **backup_id**, **repository**, and **config**. |
| 142 | + :type download_id: str |
| 143 | + :param abort: If set to True, running download is aborted. Used with |
| 144 | + parameter **download_id**. |
| 145 | + :type abort: bool |
| 146 | + :return: Download details. |
| 147 | + :rtype: dict |
| 148 | + :raise arango.exceptions.BackupDownloadError: If operation fails. |
| 149 | + """ |
| 150 | + data = {} |
| 151 | + if download_id is not None: |
| 152 | + data['downloadId'] = download_id |
| 153 | + if backup_id is not None: |
| 154 | + data['id'] = backup_id |
| 155 | + if repository is not None: |
| 156 | + data['remoteRepository'] = repository |
| 157 | + if abort is not None: |
| 158 | + data['abort'] = abort |
| 159 | + if config is not None: |
| 160 | + data['config'] = config |
| 161 | + |
| 162 | + request = Request( |
| 163 | + method='post', |
| 164 | + endpoint='/_admin/backup/download', |
| 165 | + data=data |
| 166 | + ) |
| 167 | + |
| 168 | + def response_handler(resp): |
| 169 | + if resp.is_success: |
| 170 | + return format_backup_transfer(resp.body['result']) |
| 171 | + raise BackupDownloadError(resp, request) |
| 172 | + |
| 173 | + return self._execute(request, response_handler) |
| 174 | + |
| 175 | + def upload(self, |
| 176 | + backup_id=None, |
| 177 | + repository=None, |
| 178 | + abort=None, |
| 179 | + config=None, |
| 180 | + upload_id=None): |
| 181 | + """Manage backup uploads. |
| 182 | +
|
| 183 | + :param backup_id: Backup ID used for scheduling an upload. Mutually |
| 184 | + exclusive with parameter **upload_id**. |
| 185 | + :type backup_id: str |
| 186 | + :param repository: Remote repository URL (e.g. "local://tmp/backups"). |
| 187 | + Required for scheduling a upload and mutually exclusive with |
| 188 | + parameter **upload_id**. |
| 189 | + :type repository: str |
| 190 | + :param config: Remote repository configuration. Required for scheduling |
| 191 | + an upload and mutually exclusive with parameter **upload_id**. |
| 192 | + :type config: dict |
| 193 | + :param upload_id: Upload ID. Mutually exclusive with parameters |
| 194 | + **backup_id**, **repository**, and **config**. |
| 195 | + :type upload_id: str |
| 196 | + :param abort: If set to True, running upload is aborted. Used with |
| 197 | + parameter **upload_id**. |
| 198 | + :type abort: bool |
| 199 | + :return: Upload details. |
| 200 | + :rtype: dict |
| 201 | + :raise arango.exceptions.BackupUploadError: If upload operation fails. |
| 202 | + """ |
| 203 | + data = {} |
| 204 | + |
| 205 | + if upload_id is not None: |
| 206 | + data['uploadId'] = upload_id |
| 207 | + if backup_id is not None: |
| 208 | + data['id'] = backup_id |
| 209 | + if repository is not None: |
| 210 | + data['remoteRepository'] = repository |
| 211 | + if abort is not None: |
| 212 | + data['abort'] = abort |
| 213 | + if config is not None: |
| 214 | + data['config'] = config |
| 215 | + |
| 216 | + request = Request( |
| 217 | + method='post', |
| 218 | + endpoint='/_admin/backup/upload', |
| 219 | + data=data |
| 220 | + ) |
| 221 | + |
| 222 | + def response_handler(resp): |
| 223 | + if resp.is_success: |
| 224 | + return format_backup_transfer(resp.body['result']) |
| 225 | + raise BackupUploadError(resp, request) |
| 226 | + |
| 227 | + return self._execute(request, response_handler) |
| 228 | + |
| 229 | + def restore(self, backup_id): |
| 230 | + """Restore from a local backup. |
| 231 | +
|
| 232 | + :param backup_id: Backup ID. |
| 233 | + :type backup_id: str |
| 234 | + :return: Result of the restore operation. |
| 235 | + :rtype: dict |
| 236 | + :raise arango.exceptions.BackupRestoreError: If restore fails. |
| 237 | + """ |
| 238 | + request = Request( |
| 239 | + method='post', |
| 240 | + endpoint='/_admin/backup/restore', |
| 241 | + data={'id': backup_id} |
| 242 | + ) |
| 243 | + |
| 244 | + def response_handler(resp): |
| 245 | + if resp.is_success: # pragma: no cover |
| 246 | + return format_backup_restore(resp.body['result']) |
| 247 | + raise BackupRestoreError(resp, request) |
| 248 | + |
| 249 | + return self._execute(request, response_handler) |
0 commit comments