Add no exec flag to disable post/pre exec functionality. Allow use of env variable to override config path.

This commit is contained in:
dave 2015-12-28 21:20:06 -08:00
parent 94151d5fc9
commit cd9c0f0044
3 changed files with 32 additions and 14 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ testenv
build
datadb.egg-info
dist
test.ini

View File

@ -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] <profile_name> 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] <profile_name> 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 <profile_name> backup`
* Check status: `datadb <profile_name> status`
**Backup to remote server:** `datadb <profile_name> backup`
**Check status:** `datadb <profile_name> 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

View File

@ -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':