[ADD]Odoo script databases : now able to backup, restore, duplicate on remote host

This commit is contained in:
Fabien BOURGEOIS 2017-12-30 11:12:29 +01:00
parent 58e94618d6
commit 61df2a4f4d

View File

@ -1,12 +1,36 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" Odoo databases management script """
import argparse import argparse
import common import common
import odoorpc
def str2bool(v): def str2bool(val):
return v.lower() in ('yes', 'true', '1') """ 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(): def main():
""" Main function with argument parsing """ """ Main function with argument parsing """
@ -15,12 +39,9 @@ def main():
parser = common.parser(parser) parser = common.parser(parser)
parser.add_argument('-a', '--action', type=str, default='create', parser.add_argument('-a', '--action', type=str, default='create',
help='action for the database,' + 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, parser.add_argument('-d', '--database', type=str, required=True,
help='the targetted database') 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, parser.add_argument('-pw', '--password', type=str, required=True,
help='the odoo superadmin password') help='the odoo superadmin password')
parser.add_argument('-dm', '--demo', type=str, default='False', parser.add_argument('-dm', '--demo', type=str, default='False',
@ -29,6 +50,21 @@ def main():
help='default language') help='default language')
parser.add_argument('-dbpw', '--database_password', type=str, default='admin', parser.add_argument('-dbpw', '--database_password', type=str, default='admin',
help='the database admin password, default to 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() args = parser.parse_args()
# Connection # Connection
@ -47,9 +83,17 @@ def main():
print 'database not found' print 'database not found'
elif args.action == 'drop': elif args.action == 'drop':
odoo.db.drop(args.password, args.database) odoo.db.drop(args.password, args.database)
elif args.action == 'duplicate': elif args.action == 'duplicate': # local or remote
name = args.duplicate_name or args.database + '_copy' name = args.database_target or args.database + '_copy'
odoo.db.duplicate(args.password, args.database, name) 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__": if __name__ == "__main__":
main() main()