2016-10-03 17:49:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
import argparse
|
|
|
|
import common
|
|
|
|
|
|
|
|
|
|
|
|
def datefmt():
|
|
|
|
""" Get adapted date format """
|
|
|
|
today = datetime.today()
|
|
|
|
return today.strftime('%Y-%m-%d_%Hh%Mm%Ss')
|
|
|
|
|
|
|
|
|
2016-10-05 08:56:58 +02:00
|
|
|
def dump(odoo, bakpath, password, dbnames):
|
|
|
|
""" Dumps a database list """
|
|
|
|
for dbname in dbnames:
|
|
|
|
dump = odoo.db.dump(password, dbname)
|
|
|
|
fpath = '{bakpath}/{date}_{dbname}.zip'.format(bakpath=bakpath,
|
|
|
|
dbname=dbname,
|
|
|
|
date=datefmt())
|
|
|
|
with open(fpath, 'wb') as zipf:
|
|
|
|
zipf.write(dump.read())
|
|
|
|
return True
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def dump_all(odoo, bakpath, password):
|
|
|
|
""" Dump all databases """
|
2016-10-05 08:56:58 +02:00
|
|
|
return dump(odoo, bakpath, password, odoo.db.list())
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" Main function with argument parsing """
|
|
|
|
parser = argparse.ArgumentParser(description='Backup Odoo databases')
|
|
|
|
parser = common.parser(parser)
|
|
|
|
parser.add_argument('-pw', '--password', type=str, required=True,
|
|
|
|
help='the odoo master password')
|
|
|
|
parser.add_argument('-D', '--directory', type=str, required=True,
|
|
|
|
help='the directory path where to save backups')
|
2016-10-05 08:56:58 +02:00
|
|
|
parser.add_argument('-n', '--names', type=str,
|
|
|
|
help='optional database names (default:all)')
|
2016-10-03 17:49:15 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Connection
|
|
|
|
odoo = common.connection(args)
|
|
|
|
|
|
|
|
# Backup actions
|
2016-10-05 08:56:58 +02:00
|
|
|
if not args.names:
|
2016-10-03 17:49:15 +02:00
|
|
|
dump_all(odoo, args.directory, args.password)
|
|
|
|
else:
|
2016-10-05 08:56:58 +02:00
|
|
|
dump(odoo, args.directory, args.password, args.names.split(','))
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|