2016-10-03 17:49:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-07-12 18:42:30 +02:00
|
|
|
""" Odoo backup script """
|
|
|
|
|
2016-10-03 17:49:15 +02:00
|
|
|
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')
|
|
|
|
|
|
|
|
|
2018-07-12 18:42:30 +02:00
|
|
|
def dump(odoo, bakpath, password, dbnames, format_):
|
2016-10-05 08:56:58 +02:00
|
|
|
""" Dumps a database list """
|
|
|
|
for dbname in dbnames:
|
2018-07-12 18:42:30 +02:00
|
|
|
dumpf = odoo.db.dump(password, dbname, format_)
|
2018-09-28 08:14:01 +02:00
|
|
|
fpath = '{bakpath}/{date}_{host}_{dbname}.{fmt}'.format(bakpath=bakpath,
|
|
|
|
host=odoo.host,
|
2018-07-12 18:42:30 +02:00
|
|
|
dbname=dbname,
|
|
|
|
date=datefmt(),
|
|
|
|
fmt=format_)
|
2016-10-05 08:56:58 +02:00
|
|
|
with open(fpath, 'wb') as zipf:
|
2018-07-12 18:42:30 +02:00
|
|
|
zipf.write(dumpf.read())
|
2016-10-05 08:56:58 +02:00
|
|
|
return True
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
|
2018-07-12 18:42:30 +02:00
|
|
|
def dump_all(odoo, bakpath, password, format_):
|
2016-10-03 17:49:15 +02:00
|
|
|
""" Dump all databases """
|
2018-07-12 18:42:30 +02:00
|
|
|
return dump(odoo, bakpath, password, odoo.db.list(), format_)
|
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)')
|
2018-07-12 18:42:30 +02:00
|
|
|
parser.add_argument('-f', '--format', type=str, default='zip',
|
|
|
|
help='optional database format (default:zip)')
|
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:
|
2018-07-12 18:42:30 +02:00
|
|
|
dump_all(odoo, args.directory, args.password, args.format)
|
2016-10-03 17:49:15 +02:00
|
|
|
else:
|
2018-07-12 18:42:30 +02:00
|
|
|
dump(odoo, args.directory, args.password, args.names.split(','), args.format)
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|