From 08f32a6988470b273f21d44d05db40ab42ab8d6d Mon Sep 17 00:00:00 2001 From: tony Date: Wed, 22 Apr 2015 15:50:57 +0200 Subject: [PATCH] Added the --webhook flag to send a post request after the backup is finished. --- README.rst | 1 + requirements.txt | 3 ++- xtrabackup/backup_tools.py | 15 ++++++++++++++- xtrabackup/full_backup.py | 8 ++++++-- xtrabackup/http_manager.py | 7 +++++++ 5 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 xtrabackup/http_manager.py diff --git a/README.rst b/README.rst index fcb2761..6bf6d93 100644 --- a/README.rst +++ b/README.rst @@ -55,6 +55,7 @@ You can also specify the following options: * --out-file: Log file for innobackupex output (default: */var/log/mysql/xtrabackup.out*). * --backup-threads: You can specify more threads in order to backup quicker (default: 1). * --no-compress: Do not compress the backup archive. +* --webhook: URL to send a POST request after the backup is finished. Will send the *archive_path* and *archive_repository* in JSON. Restoration ----------- diff --git a/requirements.txt b/requirements.txt index f64f4e2..0d33a40 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ -docopt \ No newline at end of file +docopt +requests \ No newline at end of file diff --git a/xtrabackup/backup_tools.py b/xtrabackup/backup_tools.py index 77a5a30..24683bf 100644 --- a/xtrabackup/backup_tools.py +++ b/xtrabackup/backup_tools.py @@ -1,5 +1,6 @@ from xtrabackup.command_executor import CommandExecutor from xtrabackup.exception import ProcessError +from xtrabackup.http_manager import HttpManager import xtrabackup.filesystem_utils as filesystem_utils import xtrabackup.log_manager as log_manager import xtrabackup.exception as exception @@ -15,6 +16,7 @@ def __init__(self, log_file, output_file, no_compression): self.setup_logging(log_file) self.command_executor = CommandExecutor(output_file) self.compress = not no_compression + self.http = HttpManager() def setup_logging(self, log_file): self.logger = logging.getLogger(__name__) @@ -146,6 +148,15 @@ def transfer_backup(self, repository): def clean(self): filesystem_utils.delete_directory_if_exists(self.workdir) + def trigger_webhook(self, webhook_url): + postdata = { + 'archive_repository': self.backup_repository, + 'archive_path': self.final_archive_path, + } + self.logger.debug("POST archive_repository: " + self.backup_repository) + self.logger.debug("POST archive_path: " + self.final_archive_path) + self.http.post(webhook_url, postdata) + def save_incremental_data(self, incremental): try: if incremental: @@ -187,7 +198,7 @@ def load_incremental_data(self): raise def start_full_backup(self, repository, workdir, user, - password, threads): + password, threads, webhook): self.check_prerequisites(repository) self.prepare_workdir(workdir) self.prepare_repository(repository, False) @@ -197,6 +208,8 @@ def start_full_backup(self, repository, workdir, user, self.archive_backup() self.transfer_backup(repository) self.clean() + if webhook: + self.trigger_webhook(webhook) def start_incremental_backup(self, repository, incremental, workdir, user, password, threads): diff --git a/xtrabackup/full_backup.py b/xtrabackup/full_backup.py index 63c7d3e..ed7b326 100755 --- a/xtrabackup/full_backup.py +++ b/xtrabackup/full_backup.py @@ -7,7 +7,8 @@ [--log-file=] \ [--out-file=] \ [--backup-threads=] \ -[--no-compress] +[--no-compress] \ +[--webhook=] pyxtrabackup (-h | --help) pyxtrabackup --version @@ -31,6 +32,8 @@ Threads count [default: 1]. --no-compress \ Do not create a compressed archive of the backup. + --webhook= \ + Webhook post backup. If enable will post backup information in JSON format. """ from docopt import docopt @@ -48,7 +51,8 @@ def main(): arguments['--tmp-dir'], arguments['--user'], arguments['--password'], - arguments['--backup-threads']) + arguments['--backup-threads'], + arguments['--webhook']) except Exception: logger = logging.getLogger(__name__) logger.error("pyxtrabackup failed.") diff --git a/xtrabackup/http_manager.py b/xtrabackup/http_manager.py new file mode 100644 index 0000000..9abf21a --- /dev/null +++ b/xtrabackup/http_manager.py @@ -0,0 +1,7 @@ +import requests + + +class HttpManager: + + def post(self, url, json): + requests.post(url, json=json)