docker_images/odoo/scripts/backup.py

59 lines
1.9 KiB
Python

# -*- 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_):
""" Dumps a database list """
for dbname in dbnames:
dumpf = odoo.db.dump(password, dbname, format_)
fpath = '{bakpath}/{date}_{dbname}.{fmt}'.format(bakpath=bakpath,
dbname=dbname,
date=datefmt(),
fmt=format_)
with open(fpath, 'wb') as zipf:
zipf.write(dumpf.read())
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')
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
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()