后援很容易。
backwork的Python项目详细描述
后台工作
备份简化。
backwork
是一个简化备份和还原数据库过程的工具包。它
处理备份过程本身以及上载、下载、还原和错误通知。
先决条件
- python 2.7版
安装
您可以使用pip
:
backwork
$ pip install backwork
运行
安装之后,应该有一个backwork
命令可用。
$ backwork --help
usage: backwork [-h] [-n NOTIFIERS] {backup,restore,upload,download} ...
positional arguments:
{backup,restore,upload,download}
optional arguments:
-h, --help show this help message and exit
-n NOTIFIERS, --notify NOTIFIERS
enable a notifier, it can be used multiple times
插件
仅仅拥有backwork
是不够的。你需要的插件
适合你的需要。您可以通过运行以下命令安装插件:
$ pip install <plug-in_name>
您可以通过viewing the ^{
插件分为三类:
备份
备份插件负责连接到数据库并执行
实际备份过程,以及备份的还原过程。
您可以通过viewing the ^{
安装备份插件后,它将通过backwork backup
和backwork restore
命令可用:
$ backwork backup --help usage: backwork backup [-h][-U]{mongo} ... Perform database backups. Run `backwork backup {database_type} -h`for more details on each supported database. positional arguments: {mongo} optional arguments: -h, --help show this help message and exit -U, --upload output backup data to stdout to allow piping it to an upload command
usage: backwork restore [-h] [-U] {mongo} ...
Perform database restores. Run `backwork restore {database_type} -h` for more
details on each supported database.
positional arguments:
{mongo}
optional arguments:
-h, --help show this help message and exit
可用插件:
backwork-backup-mongo
上传
上载插件存储并从远程存储中安全地检索备份文件。
您可以通过viewing the ^{
您可以将它们与backwork upload
、backwork show
和backwork download
命令一起使用:
$ backwork upload --help usage: backwork upload [-h]{softlayer} ... Upload a file to remote service. Run `backwork upload {service} -h`for more details on each supported service. positional arguments: {softlayer} optional arguments: -h, --help show this help message and exit
$ backwork show --help usage: backwork show [-h]{cos} ... Shows available backups on a remote service. Run `backwork show {service} -h`for more details on each supported service. positional arguments: {cos} optional arguments: -h, --help show this help message and exit
$ backwork download --help usage: backwork download [-h]{softlayer} ... Download a file from a remote service. Run `backwork upload {service} -h`for more details on each supported service. positional arguments: {softlayer} optional arguments: -h, --help show this help message and exit
可用插件:
backwork-upload-softlayer
通知
当事情出错时,通知者会告诉你。比备份更重要
配置的进程知道此进程何时失败。
您可以通过viewing the ^{
使用-n
或--notify
在backwork
命令上启用通知程序。
论据。它们还可能需要一些额外的值,比如api键。
$ backwork --help usage: backwork [-h][-n NOTIFIERS][--sentry-dsn SENTRY_DSN]{backup,upload} ... positional arguments: {backup,upload} optional arguments: -h, --help show this help message and exit -n NOTIFIERS, --notify NOTIFIERS enable a notifier, it can be used multiple times --sentry-dsn SENTRY_DSN Sentry DSN to be used for notifications. It can also be set with the evironment variable $SENTRY_DSN.
您可以在命令上启用任意数量的通知程序。
可用插件:
backwork-notify-sentry
示例
备份本地运行的MongoDB数据库
$ backwork backup mongo
2017-01-15 03:58:15,270 backup.mongo INFO starting mongo backup...
2017-01-15 03:58:15,270 backup.mongo INFO saving file to /Users/laoqui/Projects/backwork/dumps/mongo_backup_20170115-035815.archive.gz
2017-01-15 03:58:15,350 backup.mongo INFO output:
2017-01-15T03:58:15.342-0500 writing app.products to archive '/Users/laoqui/Projects/backwork/dumps/mongo_backup_20170115-035815.archive.gz'2017-01-15T03:58:15.347-0500 done dumping app.products (1 document)2017-01-15 03:58:15,350 backup.mongo INFO backup complete
这将创建一个存档的备份,压缩并加上时间戳,并存储在
当前目录中名为dumps
的文件夹。
备份远程MongoDB数据库
$ backwork backup mongo -h <HOST IP>:<PORT> -u <USER> -p<PASSWORD>
将MongoDB备份到特定的文件夹和文件名
$ backwork backup mongo -o /var/backups --archive=mongo_backup.archive
将备份文件上载到Softlayer对象存储
$ backwork upload softayer -u <USERNAME> -p <API KEY> -d <DATACENTER> -c <CONTAINER> /path/to/file /remote/path/location
接收错误消息的用户哨兵
$ backwork -n sentry --sentry-dsn <SENTRY DSN> backup mongo -o /var/backups --archive=mongo_backup.archive
更多信息
有关详细信息,请检查每个命令的--help
信息。
延伸
扩展backwork
的最佳方法是创建新的插件。它们很简单
实现一些方法集的python包。下面是一些基类
您可以使用作为起点:
classBackupBase(object):"""Base class that describes the interface a backup command must implement. Attributes: command the value used in the command line to invoke this command, usually is the name of the database (e.g.: mongo, mysql) """command=""def__init__(self,args,extra):"""Initialize a backup command given the arguments passed to CLI"""self.args=argsself.extra=extra@classmethoddefparse_args(cls,subparsers):"""Parse CLI arguments specific to this subcommand"""raiseNotImplementedError("Base method not overriden.")defbackup(self):"""Backup a database given the arguments specified. All the values passed via the CLI are available at `self.args` and `self.extra`. The first object stores known arguments that have been explicitly parsed. The second object is a list of arguments that are unkown to the parser and can be useful for invoking other commands withouht having to re-define its parser's arguments. This method should raise exceptions in case of errors so any active notifier can hadle it. """raiseNotImplementedError("Base method not overriden.")classUploadBase(object):"""Base class that describes the interface an upload command must implement. Attributes: command the value used in the command line to invoke this command, usually is the name of the service (e.g.: softlayer, s3) """command=""def__init__(self,args,extra):"""Initialize an upload command given the arguments passed to CLI"""self.args=argsself.extra=extra@classmethoddefparse_args(cls,subparsers):"""Parse CLI arguments specific to this subcommand"""raiseNotImplementedError("Base method not overriden.")defupload(self):"""Upload a file to the remote service. All the values passed via the CLI are available at `self.args` and `self.extra`. The first object stores known arguments that have been explicitly parsed. The second object is a list of arguments that are unkown to the parser and can be useful for invoking other commands withouht having to re-define its parser's arguments. This method should raise exceptions in case of errors so any active notifier can hadle it. """raiseNotImplementedError("Base method not overriden.")classNotifierBase(object):"""Base class for notifiers Notifiers are responsible for sending messages to external services to report relevant events that may happen during the execution of a command. Attributes: command the value used to enable a notifier in the command line, e.g.: `backwork -n {command}` """command=""def__init__(self,args,extra):self.args=argsself.extra=extra@classmethoddefparse_args(cls,parser):"""Add command line argument parsing rules relevant to the notifier. This method is not required to be implemented as it might not be necessary to add more arguments to the parser in some cases. """passdefnotify(self,msg=""):"""Handle an incoming message. The `msg` object could be either a `string` or an `Exception`. You may want to handle them differently with something like: if issubclass(msg.__class__, Exception): handle_exception(msg) else: handle_string(msg) If `msg` is an exception, the call to this method will be in the context of an `except`, meaning you will be able to access `sys.exc_info()`. """raiseNotImplementedError("Base method not overriden.")
要使包对backwork
可见,还需要声明
^{setup.py
文件中。
每个插件类型都有不同的entry_point
键:
备份:
setup(...entry_points={"backwork.backups":["<COMMAND NAME>":"module:BackupClass"],"backwork.restores":["<COMMAND NAME>":"module:RestoreClass"]},...
上传:
setup(...entry_points={"backwork.uploads":["<COMMAND NAME>":"module:UploadClass"],"backwork.shows":["<COMMAND NAME>":"module:ShowClass"]"backwork.downloads":["<COMMAND NAME>":"module:DownloadClass"]},...
通知者:
setup(...entry_points={"backwork.notifiers":["<COMMAND NAME>":"module:NotifierClass"]},...
一旦你的插件准备好了,你可以使用pip
来安装它,它应该
可用于backwork
。
未来工作
- 添加对更多数据库、存储服务和通知程序的支持
- 处理备份计划
- 支持更多环境变量