[IMP]Odoo backup script : add alternative method for dump

* Use direct calls with HTTP GET (v8) / POST (v10/12)
This commit is contained in:
Fabien BOURGEOIS 2019-05-30 20:16:09 +02:00
parent 7bbdb5aae2
commit 5bc65957aa

View File

@ -4,6 +4,8 @@
from datetime import datetime
import argparse
import httplib
import urllib
import common
@ -13,25 +15,36 @@ def datefmt():
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}_{host}_{dbname}.{fmt}'.format(bakpath=bakpath,
host=odoo.host,
dbname=dbname,
date=datefmt(),
fmt=format_)
def dump(odoo, args):
""" Dumps a database list from API or HTTP call """
for dbname in args.names.split(','):
if not args.alternative:
dumpf = odoo.db.dump(args.password, dbname, args.format)
else:
conn = httplib.HTTPConnection(args.host, args.port)
if odoo.version != '8.0':
params = urllib.urlencode({'master_pwd': args.password,
'name': dbname,
'backup_format': args.format})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
conn.request('POST', '/web/database/backup', params, headers)
else:
headers = {"Accept": "text/plain"}
url = (u'/web/database/backup?backup_pwd=%s&backup_db=%s'
'&backup_format=%s&token=') % (args.password, dbname, args.format)
conn.request('GET', url, '', headers)
dumpf = conn.getresponse()
fpath = '{bakpath}/{date}_{host}_{dbname}.{fmt}'.format(
bakpath=args.directory, host=args.host, dbname=dbname,
date=datefmt(), fmt=args.format
)
with open(fpath, 'wb') as zipf:
zipf.write(dumpf.read())
if args.alternative:
conn.close()
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')
@ -44,6 +57,8 @@ def main():
help='optional database names (default:all)')
parser.add_argument('-f', '--format', type=str, default='zip',
help='optional database format (default:zip)')
parser.add_argument('-alt', '--alternative', type=bool, default=False,
help='alternative dump through HTTP POST')
args = parser.parse_args()
# Connection
@ -51,9 +66,8 @@ def main():
# 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)
args.names = odoo.db.list()
dump(odoo, args)
if __name__ == "__main__":
main()