docker_images/odoo/scripts/backup.py

60 lines
2.0 KiB
Python
Raw Normal View History

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