65 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			65 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
# Copyright 2018-Today datenpol gmbh(<http://www.datenpol.at>)
 | 
						|
# License OPL-1 or later (https://www.odoo.com/documentation/user/11.0/legal/licenses/licenses.html#licenses).
 | 
						|
 | 
						|
import json
 | 
						|
import logging
 | 
						|
import os
 | 
						|
import shutil
 | 
						|
import tempfile
 | 
						|
import base64
 | 
						|
 | 
						|
import odoo
 | 
						|
from odoo.service.db import check_db_management_enabled, dump_db_manifest
 | 
						|
 | 
						|
_logger = logging.getLogger(__name__)
 | 
						|
 | 
						|
 | 
						|
@check_db_management_enabled
 | 
						|
def exp_dump(db_name, backup_format, raw):
 | 
						|
    with tempfile.TemporaryFile(mode='w+b') as t:
 | 
						|
        dump_db(db_name, t, backup_format, raw)
 | 
						|
        t.seek(0)
 | 
						|
        return base64.b64encode(t.read()).decode()
 | 
						|
 | 
						|
 | 
						|
odoo.service.db.exp_dump = exp_dump
 | 
						|
 | 
						|
 | 
						|
@check_db_management_enabled
 | 
						|
def dump_db(db_name, stream, backup_format='zip', raw=None):
 | 
						|
    _logger.info('DUMP DB: %s format %s', db_name, backup_format)
 | 
						|
 | 
						|
    self_dir = os.path.dirname(os.path.realpath(__file__))
 | 
						|
    anon_sql_file = os.path.join(self_dir, '..', 'data', 'anon.sql')
 | 
						|
 | 
						|
    with odoo.tools.osutil.tempdir() as dump_dir:
 | 
						|
 | 
						|
        if backup_format == 'zip':
 | 
						|
            filestore = odoo.tools.config.filestore(db_name)
 | 
						|
            if os.path.exists(filestore):
 | 
						|
                shutil.copytree(filestore, os.path.join(dump_dir, 'filestore'))
 | 
						|
            with open(os.path.join(dump_dir, 'manifest.json'), 'w') as fh:
 | 
						|
                db = odoo.sql_db.db_connect(db_name)
 | 
						|
                with db.cursor() as cr:
 | 
						|
                    json.dump(dump_db_manifest(cr), fh, indent=4)
 | 
						|
 | 
						|
        cmd = ['pg_dump', '--no-owner', db_name, '--file=' + os.path.join(dump_dir, 'dump.sql')]
 | 
						|
        odoo.tools.exec_pg_command(*cmd)
 | 
						|
 | 
						|
        if not raw:
 | 
						|
            with open(os.path.join(dump_dir, 'dump.sql'), 'ab') as dump_file, open(anon_sql_file, 'rb') as anon:
 | 
						|
                shutil.copyfileobj(anon, dump_file)
 | 
						|
 | 
						|
        if stream:
 | 
						|
            odoo.tools.osutil.zip_dir(dump_dir, stream, include_dir=False,
 | 
						|
                                      fnct_sort=lambda file_name: file_name != 'dump.sql')
 | 
						|
        else:
 | 
						|
            t = tempfile.TemporaryFile()
 | 
						|
            odoo.tools.osutil.zip_dir(dump_dir, t, include_dir=False,
 | 
						|
                                      fnct_sort=lambda file_name: file_name != 'dump.sql')
 | 
						|
            t.seek(0)
 | 
						|
            return t
 | 
						|
 | 
						|
 | 
						|
odoo.service.db.dump_db = dump_db
 |