From cd9c0f004412b410c2eb70d153e1e7aeac676fec Mon Sep 17 00:00:00 2001 From: dave Date: Mon, 28 Dec 2015 21:20:06 -0800 Subject: [PATCH] Add no exec flag to disable post/pre exec functionality. Allow use of env variable to override config path. --- .gitignore | 1 + README.md | 21 ++++++++++++++------- datadb/datadb.py | 24 +++++++++++++++++------- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 496bc8e..1870d6f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ testenv build datadb.egg-info dist +test.ini diff --git a/README.md b/README.md index 3db0913..5aa804c 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,9 @@ Just python3 and [requests](http://python-requests.org/). For one, this is beta and some things are hard-coded. In datadb.py it is recommended to change the DATADB_HTTP_API URL. This URL should be the cgi-bin path of an http server running [datadb-scripts](http://gitlab.xmopx.net/dave/datadb-scripts). -Next, a config file must be created for each directory to be restored/backed up. It lives at /etc/datadb.ini and contains -many entires of this format: +Next, a config file must be created with entries for each directory to be restored/backed up. The config at `/etc/datadb.ini` +is read by default, however this path can be overriden by setting the environmental variable `DATADB_CONF`. The file should +contain many entires of this format: ``` [profile_name] @@ -68,12 +69,20 @@ Datadb makes some assumptions about it's environment. ### CLI Usage -* Restore from backup: `datadb [--force] restore` +`datadb [--no-exec] [--no-pre-exec] [--no-post-exec] [--force] profile_name restore|backup|status` + +* `--no-exec`: Do not execute restore and export pre and post exec commmands +* `--no-pre-exec`: Do not execute restore and export preexec commmands +* `--no-post-exec`: Do not execute restore and export postexec commmands +* `--force`: Allow overwriting existing data or existing backups + +**Restore from backup:** `datadb [options] restore` Restore operations have a degree of sanity checking. Upon a successful restore, a file named *.datadb.lock* will be created in the local dir. Datadb checks for this file before doing restore operations, to prevent overwriting live data with an old backup. This check can be overridden with the `--force` command line option. -* Backup to remote server: `datadb backup` -* Check status: `datadb status` +**Backup to remote server:** `datadb backup` + +**Check status:** `datadb status` Command line usage is agnostic to the underlying transport protocol used. @@ -82,7 +91,5 @@ Command line usage is agnostic to the underlying transport protocol used. * Fix hard coded stuff mentioned above * Support config file-less usage * Sync all command -* Option to override config path * Nicer config parsing * Implement security -* Implement pre/post exec functions \ No newline at end of file diff --git a/datadb/datadb.py b/datadb/datadb.py index 7d35fa9..cb08e36 100755 --- a/datadb/datadb.py +++ b/datadb/datadb.py @@ -4,7 +4,7 @@ import argparse from configparser import ConfigParser from urllib.parse import urlparse from os.path import normpath, join, exists -from os import chmod, chown, stat +from os import chmod, chown, stat, environ from enum import Enum import subprocess from requests import get,put @@ -184,15 +184,21 @@ def main(): *export_postexec*: Shell command to exec after pushing data """ + + conf_path = environ["DATADB_CONF"] if "DATADB_CONF" in environ else "/etc/datadb.ini" + # Load profiles config = ConfigParser() - config.read("/etc/datadb.ini") + config.read(conf_path) config = {section:{k:config[section][k] for k in config[section]} for section in config.sections()} parser = argparse.ArgumentParser(description="Backupdb Agent depends on config: /etc/datadb.ini") - parser.add_argument('--force', default=False, action='store_true', help='force restore operation if destination data already exists') + parser.add_argument('-f', '--force', default=False, action='store_true', help='force restore operation if destination data already exists') + parser.add_argument('-n', '--no-exec', default=False, action='store_true', help='don\'t run pre/post-exec commands') + parser.add_argument('-b', '--no-pre-exec', default=False, action='store_true', help='don\'t run pre-exec commands') + parser.add_argument('-m', '--no-post-exec', default=False, action='store_true', help='don\'t run post-exec commands') parser.add_argument('profile', type=str, choices=config.keys(), help='Profile to restore') @@ -213,22 +219,26 @@ def main(): args = parser.parse_args() + if args.no_exec: + args.no_pre_exec = True + args.no_post_exec = True + if args.mode == 'restore': - if config[args.profile]['restore_preexec']: + if not args.no_pre_exec and config[args.profile]['restore_preexec']: shell_exec(config[args.profile]['restore_preexec']) restore(args.profile, config[args.profile], force=args.force) - if config[args.profile]['restore_postexec']: + if not args.no_post_exec and config[args.profile]['restore_postexec']: shell_exec(config[args.profile]['restore_postexec']) elif args.mode == 'backup': - if config[args.profile]['export_preexec']: + if not args.no_pre_exec and config[args.profile]['export_preexec']: shell_exec(config[args.profile]['export_preexec']) backup(args.profile, config[args.profile]) - if config[args.profile]['export_postexec']: + if not args.no_post_exec and config[args.profile]['export_postexec']: shell_exec(config[args.profile]['export_postexec']) elif args.mode == 'status':