Back to Top

How to backup and restore OpenERP database automatically on linux ?

Updated 17 March 2017

In my last article, I described you how to take backup and restore OpenERP database using Openerp GUI or web-interface method method. Now, In this article , I`ll describe you how to take backup of your OpenERP database automatically and regularly too after certain time of period, using cron on linux.

1) Create 2 directories, one for placing the backup script (/var/scripts) and other for placing your database dump files (/var/pgdump).

NOTE – both directories should be owned by user = root

2) Create a script file as – /var/scripts/dump_db.sh, set permission as 700,  owned by user = root and meant to run as root. I`ll set a cron job. The content of this script will be –

#!/bin/bash

#fixed parameters:
date=$(date +"%Y-%m-%d_%H:%M:%S")
db='name_of_the_db_u_want_to_take_backup'
backup_path="/var/pgdump"
filename="$backup_path/${db}_$date"

# Stop OpenERP Server
 /etc/init.d/openerp-server stop

# Taking Dump of selected DB
sudo -H -u postgres bash -c "pg_dump --format=c --no-owner $db>$filename"


# Start OpenERP Server
/etc/init.d/openerp-server start

exit 0

NOTE: starting/stopping openerp server is needed only when the database is heavy

Start your headless eCommerce
now.
Find out More

You can test whether this script is working or not by manually running it, like this

/var/scripts/./dump_db.sh

3) If everything goes correct , create a cron to run this script regularly after certain time of period –

sudo crontab -e

# add this line
0 */12 * * * /var/scripts/dump_db.sh
# this would run after every 12 hours i.e twice a day

4) In order to restore using these dump file we can use Openerp GUI or can run following command:

pg_restore --no-owner --dbname=<DUMP_FILE>

5) Remove old dump files ,

– create another script (deletes backups which are older than 30 days) as
/var/scripts/rm_db.sh

#!/bin/bash

sudo find /var/pgdump/* -mtime +30 -exec rm {} \;

exit 0

– add this to cron as –

sudo crontab -e

# add this line
5 8 * * 6 /var/scripts/rm_db.sh
# this would run 8:05 every saturday

That`s it !!!
I hope it will help someone.

[stextbox id=”info”]Your opinions, comments and suggestions are important to keep the page updated and interesting. [/stextbox]

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home