From 5e2519c024612f2ad6bf0518b199161afe042b52 Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Thu, 12 Jul 2018 18:42:30 +0200 Subject: [PATCH] [IMP]Odoo backup : handle dump/SQL formats for backups too (default zip) --- odoo/backups/backup.template | 2 +- odoo/backups/launch.sh | 3 ++- odoo/backups/plan.py | 2 +- odoo/scripts/backup.py | 25 +++++++++++++++---------- 4 files changed, 19 insertions(+), 13 deletions(-) diff --git a/odoo/backups/backup.template b/odoo/backups/backup.template index a6e6001..ff5c69e 100644 --- a/odoo/backups/backup.template +++ b/odoo/backups/backup.template @@ -1 +1 @@ -python backup.py -oh ${HOST} -pw ${ADMIN_PASSWORD} -D backups/ -n ${DATABASES} +python backup.py -oh ${HOST} -pw ${ADMIN_PASSWORD} -f ${FMT} -D backups/ -n ${DATABASES} diff --git a/odoo/backups/launch.sh b/odoo/backups/launch.sh index b360acd..abfdf7d 100644 --- a/odoo/backups/launch.sh +++ b/odoo/backups/launch.sh @@ -11,7 +11,8 @@ do HOST=${host} ADMIN_PASSWORD=${ODOO_ADMIN_PASSWORD[$index]} DATABASES=${ODOO_DATABASES[$index]} - export HOST ADMIN_PASSWORD DATABASES + FMT=${ODOO_FMT:='zip'} + export HOST ADMIN_PASSWORD DATABASES FMT envsubst < /home/odoo/backup.template >> /home/odoo/backup.sh ((index++)) done diff --git a/odoo/backups/plan.py b/odoo/backups/plan.py index 5516f96..b24b098 100644 --- a/odoo/backups/plan.py +++ b/odoo/backups/plan.py @@ -40,7 +40,7 @@ def notkeep(fname): # Filters zip files and keeps only legitimate ones # ================================================ -FILES = glob('backups/*.zip') +FILES = glob('backups/*.zip') + glob('backups/*.dump') BACKUPS_TO_REMOVE = filter(notkeep, FILES) if len(BACKUPS_TO_REMOVE) > 0: diff --git a/odoo/scripts/backup.py b/odoo/scripts/backup.py index aba50d7..31d7e60 100644 --- a/odoo/scripts/backup.py +++ b/odoo/scripts/backup.py @@ -1,5 +1,7 @@ # -*- coding: utf-8 -*- +""" Odoo backup script """ + from datetime import datetime import argparse import common @@ -11,21 +13,22 @@ def datefmt(): return today.strftime('%Y-%m-%d_%Hh%Mm%Ss') -def dump(odoo, bakpath, password, dbnames): +def dump(odoo, bakpath, password, dbnames, format_): """ 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()) + 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(dump.read()) + zipf.write(dumpf.read()) return True -def dump_all(odoo, bakpath, password): +def dump_all(odoo, bakpath, password, format_): """ Dump all databases """ - return dump(odoo, bakpath, password, odoo.db.list()) + return dump(odoo, bakpath, password, odoo.db.list(), format_) def main(): @@ -38,6 +41,8 @@ def main(): 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 @@ -45,9 +50,9 @@ def main(): # Backup actions if not args.names: - dump_all(odoo, args.directory, args.password) + dump_all(odoo, args.directory, args.password, args.format) else: - dump(odoo, args.directory, args.password, args.names.split(',')) + dump(odoo, args.directory, args.password, args.names.split(','), args.format) if __name__ == "__main__": main()