From 61df2a4f4df000a6f075553b798112b62ad70127 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Sat, 30 Dec 2017 11:12:29 +0100 Subject: [PATCH] [ADD]Odoo script databases : now able to backup, restore, duplicate on remote host --- odoo/scripts/databases.py | 62 +++++++++++++++++++++++++++++++++------ 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/odoo/scripts/databases.py b/odoo/scripts/databases.py index bc71c1e..8fa837d 100644 --- a/odoo/scripts/databases.py +++ b/odoo/scripts/databases.py @@ -1,12 +1,36 @@ # -*- coding: utf-8 -*- +""" Odoo databases management script """ + import argparse import common +import odoorpc -def str2bool(v): - return v.lower() in ('yes', 'true', '1') +def str2bool(val): + """ Convert sense str to boolean """ + return val.lower() in ('yes', 'true', '1') +def dump(odoo, args): + """ Backup database on path """ + dump_path = u'{}/{}_{}.zip'.format(args.dump_path, args.host, args.database) + dump_file = odoo.db.dump(args.password, args.database) + with open(dump_path, 'wb') as dump_zip: + dump_zip.write(dump_file.read()) + +def restore(odoo, args): + """ Restores database from local file to local or remote host """ + dump_path = u'{}/{}_{}.zip'.format(args.dump_path, args.host, args.database) + with open(dump_path, 'rb') as dump_file: + if args.target_host and args.target_port and args.target_password: + connection = odoorpc.ODOO(args.target_host, port=args.target_port) + connection.config['timeout'] = 600 + password = args.target_password + else: + connection = odoo + password = args.password + connection.db.restore(password, args.database_target, dump_file, + args.restore_copy) def main(): """ Main function with argument parsing """ @@ -15,12 +39,9 @@ def main(): parser = common.parser(parser) parser.add_argument('-a', '--action', type=str, default='create', help='action for the database,' + - 'default to create (others are drop, duplicate)') + 'default to create (others are drop, duplicate, dump)') parser.add_argument('-d', '--database', type=str, required=True, help='the targetted database') - parser.add_argument('-dn', '--duplicate-name', type=str, - help=('name for duplicated database (optional : ' - 'name_copy otherwise')) parser.add_argument('-pw', '--password', type=str, required=True, help='the odoo superadmin password') parser.add_argument('-dm', '--demo', type=str, default='False', @@ -29,6 +50,21 @@ def main(): help='default language') parser.add_argument('-dbpw', '--database_password', type=str, default='admin', help='the database admin password, default to admin') + parser.add_argument('-dp', '--dump-path', type=str, default='/tmp/', + help='the absolute path where to dump the database, ' + 'defaults to /tmp/') + parser.add_argument('-dt', '--database-target', type=str, + help=('name for duplicated or restore targetted database ' + ' (optional : name_copy otherwise')) + parser.add_argument('-rc', '--restore-copy', type=bool, default=False, + help=('Is restore a copy or a move ? Defaults to False, ' + 'then move')) + parser.add_argument('-th', '--target-host', type=str, + help='targetted host, per default equal host') + parser.add_argument('-tpw', '--target-password', type=str, + help='the targeted host odoo superadmin password') + parser.add_argument('-tpp', '--target-port', type=str, + help='the targeted host port') args = parser.parse_args() # Connection @@ -47,9 +83,17 @@ def main(): print 'database not found' elif args.action == 'drop': odoo.db.drop(args.password, args.database) - elif args.action == 'duplicate': - name = args.duplicate_name or args.database + '_copy' - odoo.db.duplicate(args.password, args.database, name) + elif args.action == 'duplicate': # local or remote + name = args.database_target or args.database + '_copy' + if args.target_host and args.target_port and args.target_password: + dump(odoo, args) + restore(odoo, args) + else: + odoo.db.duplicate(args.password, args.database, name) + elif args.action == 'dump': + dump(odoo, args) + elif args.action == 'restore': + restore(odoo, args) if __name__ == "__main__": main()