[IMP]Odoo backup : handle dump/SQL formats for backups too (default zip)

This commit is contained in:
Fabien BOURGEOIS 2018-07-12 18:42:30 +02:00
parent d97cd98130
commit 5e2519c024
4 changed files with 19 additions and 13 deletions

View File

@ -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}

View File

@ -11,7 +11,8 @@ do
HOST=${host} HOST=${host}
ADMIN_PASSWORD=${ODOO_ADMIN_PASSWORD[$index]} ADMIN_PASSWORD=${ODOO_ADMIN_PASSWORD[$index]}
DATABASES=${ODOO_DATABASES[$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 envsubst < /home/odoo/backup.template >> /home/odoo/backup.sh
((index++)) ((index++))
done done

View File

@ -40,7 +40,7 @@ def notkeep(fname):
# Filters zip files and keeps only legitimate ones # 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) BACKUPS_TO_REMOVE = filter(notkeep, FILES)
if len(BACKUPS_TO_REMOVE) > 0: if len(BACKUPS_TO_REMOVE) > 0:

View File

@ -1,5 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" Odoo backup script """
from datetime import datetime from datetime import datetime
import argparse import argparse
import common import common
@ -11,21 +13,22 @@ def datefmt():
return today.strftime('%Y-%m-%d_%Hh%Mm%Ss') 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 """ """ Dumps a database list """
for dbname in dbnames: for dbname in dbnames:
dump = odoo.db.dump(password, dbname) dumpf = odoo.db.dump(password, dbname, format_)
fpath = '{bakpath}/{date}_{dbname}.zip'.format(bakpath=bakpath, fpath = '{bakpath}/{date}_{dbname}.{fmt}'.format(bakpath=bakpath,
dbname=dbname, dbname=dbname,
date=datefmt()) date=datefmt(),
fmt=format_)
with open(fpath, 'wb') as zipf: with open(fpath, 'wb') as zipf:
zipf.write(dump.read()) zipf.write(dumpf.read())
return True return True
def dump_all(odoo, bakpath, password): def dump_all(odoo, bakpath, password, format_):
""" Dump all databases """ """ Dump all databases """
return dump(odoo, bakpath, password, odoo.db.list()) return dump(odoo, bakpath, password, odoo.db.list(), format_)
def main(): def main():
@ -38,6 +41,8 @@ def main():
help='the directory path where to save backups') help='the directory path where to save backups')
parser.add_argument('-n', '--names', type=str, parser.add_argument('-n', '--names', type=str,
help='optional database names (default:all)') 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() args = parser.parse_args()
# Connection # Connection
@ -45,9 +50,9 @@ def main():
# Backup actions # Backup actions
if not args.names: if not args.names:
dump_all(odoo, args.directory, args.password) dump_all(odoo, args.directory, args.password, args.format)
else: else:
dump(odoo, args.directory, args.password, args.names.split(',')) dump(odoo, args.directory, args.password, args.names.split(','), args.format)
if __name__ == "__main__": if __name__ == "__main__":
main() main()