使用 Python3 和 xmlrpc 失败恢复 Odoo 数据库

Restoring an Odoo Database with Python3 and xmlrpc failing

我正在编写一个 python 脚本来备份和恢复 Odoo 数据库(复制,真的)。

我的备份代码如下:

def download_db() :
    conn = ServerProxy(url + '/xmlrpc/db')
    print ('Dumping database...')

    with open(backup_zip_name, 'wb') as backup_file:
        decode = base64.b64decode(conn.dump(master_password, db_to_copy, 'zip'))
        backup_file.write(decode)

这似乎工作正常,我可以使用 odoo 网络数据库管理器界面手动成功恢复生成的备份文件。

然而,当我尝试通过脚本恢复该备份时:

def restore_db() :
    with open(backup_zip_name, 'rb') as backup_file:
        serv = ServerProxy(url + '/xmlrpc/db')
        print('Creating database ' + new_db_name + ' from ' + backup_zip_name)
        
        serv.restore(master_password, new_db_name, base64.b64encode(backup_file.read()))
        print('done')

我收到以下错误:

Creating database db_copy from ./backup/backup.zip Traceback (most recent call last): File "/usr/bin/anonymisation/odoo-database-copy.py", line 97, in restore_db() File "/usr/bin/anonymisation/odoo-database-copy.py", line 40, in restore_db serv.restore(master_password, new_db_name, encode) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/xmlrpc/client.py", line 1112, in call return self.__send(self.__name, args) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/xmlrpc/client.py", line 1452, in __request verbose=self.__verbose File "/opt/rh/rh-python36/root/usr/lib64/python3.6/xmlrpc/client.py", line 1154, in request return self.single_request(host, handler, request_body, verbose) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/xmlrpc/client.py", line 1170, in single_request return self.parse_response(resp) File "/opt/rh/rh-python36/root/usr/lib64/python3.6/xmlrpc/client.py", line 1342, in parse_response return u.close() File "/opt/rh/rh-python36/root/usr/lib64/python3.6/xmlrpc/client.py", line 656, in close raise Fault(**self._stack[0]) xmlrpc.client.Fault: <Fault object of type 'Binary' has no len(): 'Traceback (most recent call last):\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/addons/base/controllers/rpc.py", line 54, in xmlrpc_1\n response = self._xmlrpc(service)\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/addons/base/controllers/rpc.py", line 43, in _xmlrpc\n result = dispatch_rpc(service, method, params)\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/http.py", line 120, in dispatch_rpc\n result = dispatch(method, params)\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/service/db.py", line 448, in dispatch\n return gexp_method_name\n File "", line 2, in exp_restore\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/service/db.py", line 40, in if_db_mgt_enabled\n return method(self, *args, **kwargs)\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/service/db.py", line 248, in exp_restore\n for chunk in chunks(data):\n File "/opt/rh/rh-python36/root/usr/lib/python3.6/site-packages/odoo-12.0.post20181107-py3.6.egg/odoo/service/db.py", line 244, in chunks\n for i in range(0, len(d), n):\nTypeError: object of type 'Binary' has no len()\n'>

我运行脚本使用python3.6。请问有什么解决办法吗?

预计到达时间:它们都在 python 2.7

上运行良好

base64.b64encode return string in python2.7 and bytes 在 Python3.6.

尝试将字符串而不是字节传递给 restore 函数:

base64.b64encode(backup_file.read()).decode()