Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Is there a way to backup databases through XMLRPC?

By
César Bustíos Benites
on 11/12/14, 11:46 AM 1,909 views

I'm trying to generate a Python script to backup my database:

import xmlrpclib

sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/common')

uid = sock.login('mydb', 'myuser', 'mypass')

sock = xmlrpclib.ServerProxy('http://localhost:8069/xmlrpc/db')

sock.dump('mydb')

Traceback (most recent call last)

....

Fault: <Fault AccessDenied: 'Access denied.'>

Is this possible?

I'm currently using the scripts dump_db.sh and housekeeping.sh as shown in this question: https://www.odoo.com/forum/help-1/question/how-to-setup-a-regular-postgresql-database-backup-4728 but I can't find a way to restore the database.

Thanks!

While managing database you do not require to use login method like uid = sock.login('mydb', 'myuser', 'mypass') dump method accepts 2arguements example .dump(super_user_password,urdatbase_name)

shashank verma
on 11/14/14, 4:32 AM
2

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 4/7/15, 5:33 AM

César Bustíos Benites,

Here is one more best example to take multiple database backup.

import base64

import xmlrpclib

sock = xmlrpclib.ServerProxy('http://192.168.1.12:8069/xmlrpc/db') #provide IP address of your customer server where all database are running

all_database = sock.list()

for database in all_database:

file_path = "/home/serpentcs/backup/" #Give path of folder where backup file will be store

file_path += database

file_path += ".dump"

backup_db_file = open(file_path, 'wb')

backup_db_file.write(base64.b64decode(sock.dump('admin', database))) #admin is master password in my case

backup_db_file.close()

Hope this will help.

Rgds,

Anil.


1
shashank verma
On 11/14/14, 4:29 AM

While restoring database ,its contents are stored in binary format.
I tested a script and works fine.
U just need to do a little code alteration in ur openerp  file.
Note: Tested for OpenERP 7.0

STEP 1:

 

Find  and open web_services.py in your openerp directory. I found this on /openerp/service/web_services.py


STEP 2:

Find function -

    def exp_dump(self, db_name):

And replace this entire function with -

    def exp_dump(self, db_name):
        logger = logging.getLogger('openerp.service.web_services.db.dump')
        
        with self._set_pg_password_in_environment():
            cmd = ['pg_dump', '--format=c', '--no-owner']
            if tools.config['db_user']:
                cmd.append('--username=' + tools.config['db_user'])
            if tools.config['db_host']:
                cmd.append('--host=' + tools.config['db_host'])
            if tools.config['db_port']:
                cmd.append('--port=' + str(tools.config['db_port']))
            cmd.append(db_name)
    
            stdin, stdout = tools.exec_pg_command_pipe(*tuple(cmd))
            stdin.close()
            data = stdout.read()
            res = stdout.close()
            #store data
            with open(os.getcwd()+'/data1.txt','w') as w:    

                w.write(data)

            if not data or res:
                logger.error(
                        'DUMP DB: %s failed! Please verify the configuration of the database password on the server. '
                        'You may need to create a .pgpass file for authentication, or specify `db_password` in the '
                        'server configuration file.\n %s', db_name, data)
                raise Exception, "Couldn't dump database"
            logger.info('DUMP DB successful: %s', db_name)
    
            return base64.encodestring(data)


#NOTE: with open(os.getcwd()+'/data1.txt','w') as w:    #This line store a dump file in binary format on ur current directory
                                                        #Kindly remember the directory path ,useful when restoring the dump file


STEP 3:
FInd function -
def exp_restore(self, db_name, data):


Replace this fucntion by -


    def exp_restore(self, db_name, data):

        logger = logging.getLogger('openerp.service.web_services.db.restore')

        try:

            with open(data,'r') as w:
                data=w.read()
        except Exception as E:
            pass


        
        
        with self._set_pg_password_in_environment():
            if self.exp_db_exist(db_name):
                logger.warning('RESTORE DB: %s already exists', db_name)
                raise Exception, "Database already exists"

            self._create_empty_database(db_name)

            cmd = ['pg_restore', '--no-owner']
            if tools.config['db_user']:
                cmd.append('--username=' + tools.config['db_user'])
            if tools.config['db_host']:
                cmd.append('--host=' + tools.config['db_host'])
            if tools.config['db_port']:
                cmd.append('--port=' + str(tools.config['db_port']))
            cmd.append('--dbname=' + db_name)
            args2 = tuple(cmd)

            buf=base64.decodestring(data)
            if os.name == "nt":
                tmpfile = (os.environ['TMP'] or 'C:\\') + os.tmpnam()
                file(tmpfile, 'wb').write(buf)
                args2=list(args2)
                args2.append(tmpfile)
                args2=tuple(args2)
            stdin, stdout = tools.exec_pg_command_pipe(*args2)
            if not os.name == "nt":
                stdin.write(base64.decodestring(data))
            stdin.close()
            res = stdout.close()
            if res:
                raise Exception, "Couldn't restore database"
            logger.info('RESTORE DB: %s', db_name)

            return True

 

 

#NOTE:        try:                                # This code reads & restor the dump file path which u provide in ur xmlrpc script

            with open(data,'r') as w:
                data=w.read()
        except Exception as E:
            pass

 

 

STEP 4:
Your xmlrpc script-
import xmlrpclib,os

sock_comm = xmlrpclib.ServerProxy('http://urdomain/xmlrpc/db')
#NOTE: In case if your are taking backup
sock_comm.dump(your_super_user_password,database_name)   #Example -dump('admin','test_db')

#NOTE:In case if your restoring dump file
sock_comm.restore(your_super_user_password,new_database_name,your_dump_file_path_on_disk)   #Example -restore('admin','test_db','/user/erp/my_file.txt)


THATS IT !!!!

 

 

Thank you very much! I'll accept yours answer although, as you mentioned in your comments, it's only required to use "dump(super_user_pass, db_name)"

César Bustíos Benites
on 11/14/14, 10:01 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

3 follower(s)

Stats

Asked: 11/12/14, 11:46 AM
Seen: 1909 times
Last updated: 4/7/15, 5:33 AM