[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 from datetime import datetime
import argparse import argparse
import httplib
import urllib
import common import common
@ -13,25 +15,36 @@ 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, format_): def dump(odoo, args):
""" Dumps a database list """ """ Dumps a database list from API or HTTP call """
for dbname in dbnames: for dbname in args.names.split(','):
dumpf = odoo.db.dump(password, dbname, format_) if not args.alternative:
fpath = '{bakpath}/{date}_{host}_{dbname}.{fmt}'.format(bakpath=bakpath, dumpf = odoo.db.dump(args.password, dbname, args.format)
host=odoo.host, else:
dbname=dbname, conn = httplib.HTTPConnection(args.host, args.port)
date=datefmt(), if odoo.version != '8.0':
fmt=format_) 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: with open(fpath, 'wb') as zipf:
zipf.write(dumpf.read()) zipf.write(dumpf.read())
if args.alternative:
conn.close()
return True return True
def dump_all(odoo, bakpath, password, format_):
""" Dump all databases """
return dump(odoo, bakpath, password, odoo.db.list(), format_)
def main(): def main():
""" Main function with argument parsing """ """ Main function with argument parsing """
parser = argparse.ArgumentParser(description='Backup Odoo databases') parser = argparse.ArgumentParser(description='Backup Odoo databases')
@ -44,6 +57,8 @@ def main():
help='optional database names (default:all)') help='optional database names (default:all)')
parser.add_argument('-f', '--format', type=str, default='zip', parser.add_argument('-f', '--format', type=str, default='zip',
help='optional database format (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() args = parser.parse_args()
# Connection # Connection
@ -51,9 +66,8 @@ def main():
# Backup actions # Backup actions
if not args.names: if not args.names:
dump_all(odoo, args.directory, args.password, args.format) args.names = odoo.db.list()
else: dump(odoo, args)
dump(odoo, args.directory, args.password, args.names.split(','), args.format)
if __name__ == "__main__": if __name__ == "__main__":
main() main()