2016-10-03 17:49:15 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2018-07-12 18:42:30 +02:00
|
|
|
""" Odoo backup script """
|
|
|
|
|
2016-10-03 17:49:15 +02:00
|
|
|
from datetime import datetime
|
|
|
|
import argparse
|
2019-05-30 20:16:09 +02:00
|
|
|
import httplib
|
|
|
|
import urllib
|
2016-10-03 17:49:15 +02:00
|
|
|
import common
|
|
|
|
|
|
|
|
|
|
|
|
def datefmt():
|
|
|
|
""" Get adapted date format """
|
|
|
|
today = datetime.today()
|
|
|
|
return today.strftime('%Y-%m-%d_%Hh%Mm%Ss')
|
|
|
|
|
|
|
|
|
2019-05-30 20:16:09 +02:00
|
|
|
def dump(odoo, args):
|
|
|
|
""" Dumps a database list from API or HTTP call """
|
|
|
|
for dbname in args.names.split(','):
|
2019-05-30 20:21:20 +02:00
|
|
|
if args.alternative == 'no':
|
2019-05-30 20:16:09 +02:00
|
|
|
dumpf = odoo.db.dump(args.password, dbname, args.format)
|
|
|
|
else:
|
|
|
|
conn = httplib.HTTPConnection(args.host, args.port)
|
2022-06-13 07:47:10 +02:00
|
|
|
headers = {'User-Agent': ('Mozilla/5.0 (Windows NT 6.0; WOW64; rv:24.0) '
|
|
|
|
'Gecko/20100101 Firefox/24.0'),
|
|
|
|
'Accept': 'text/plain'}
|
2019-05-30 20:16:09 +02:00
|
|
|
if odoo.version != '8.0':
|
|
|
|
params = urllib.urlencode({'master_pwd': args.password,
|
|
|
|
'name': dbname,
|
|
|
|
'backup_format': args.format})
|
2022-06-13 07:47:10 +02:00
|
|
|
headers['Content-type'] = 'application/x-www-form-urlencoded'
|
2019-05-30 20:16:09 +02:00
|
|
|
conn.request('POST', '/web/database/backup', params, headers)
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
)
|
2016-10-05 08:56:58 +02:00
|
|
|
with open(fpath, 'wb') as zipf:
|
2018-07-12 18:42:30 +02:00
|
|
|
zipf.write(dumpf.read())
|
2019-05-30 20:21:20 +02:00
|
|
|
if args.alternative != 'no':
|
2019-05-30 20:16:09 +02:00
|
|
|
conn.close()
|
2016-10-05 08:56:58 +02:00
|
|
|
return True
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
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')
|
2016-10-05 08:56:58 +02:00
|
|
|
parser.add_argument('-n', '--names', type=str,
|
|
|
|
help='optional database names (default:all)')
|
2018-07-12 18:42:30 +02:00
|
|
|
parser.add_argument('-f', '--format', type=str, default='zip',
|
|
|
|
help='optional database format (default:zip)')
|
2019-05-30 20:21:20 +02:00
|
|
|
parser.add_argument('-alt', '--alternative', type=str, default='no',
|
|
|
|
help='alternative dump through HTTP calls (default:no)')
|
2016-10-03 17:49:15 +02:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Connection
|
|
|
|
odoo = common.connection(args)
|
|
|
|
|
|
|
|
# Backup actions
|
2016-10-05 08:56:58 +02:00
|
|
|
if not args.names:
|
2019-05-30 20:16:09 +02:00
|
|
|
args.names = odoo.db.list()
|
|
|
|
dump(odoo, args)
|
2016-10-03 17:49:15 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|