Read in 4 minutes

last updated 

How to Setup Automatic Odoo Backup

In this tutorial we will walk you through the process of creating automatic daily backups of your Odoo databases. Odoo is the most popular open source ERP system written in Python and uses PostgreSQL as database back-end.

All the data of your Odoo instance is stored in a PostgreSQL database. Regularly backing up the database will protect you from potentially catastrophic data loss and it is absolutely critical for anyone and everyone who has an Odoo installation.

Odoo database management interface

The Odoo database management interface provides tools to backup, duplicate, delete, create and restore a database. Creating a backup using the database management interface is a no-brainer, simply open your browser and navigate to http://your_server_ip:8069/web/database/manager.

You will be presented with the following screen:

Click on the Backup link and a new popup will be displayed.

Enter your Odoo database Master Password and create a backup by clicking on the blue Backup button.

Depending on the database size, the backup may take some time before being ready.

Create a database backup from the command line

Now that we know how to create a backup through the Odoo database management interface, how can we use the same tool to create a backup from the command line? The answer is simple, we can use wget or curl. Both tools can send data with POST which we can use to pass the necessary variables to the Odoo database tool.

In the example bellow our Odoo Master Password is ADMIN_PASSWORD and we are creating a backup file back_up_filename.zip of a database named DB_NAME which will be saved in the backup_dir directory.

curl -X POST -F 'master_pwd=ADMIN_PASSWORD' -F 'name=DB_NAME' -F 'backup_format=zip' -o /backup_dir/back_up_filename.zip http://localhost:8069/web/database/backup

If you prefer wget over curl, you can use the following command:

wget --post-data 'master_pwd=ADMIN_PASSWORD&name=DB_NAME&backup_format=zip' -O /backup_dir/back_up_filename.zip http://localhost:8069/web/database/backup

If you want to create a backup from a remote location instead of localhost you need to enter the URL to your Odoo instance. In this case it is recommended to use HTTPS because you do not want your password to be send through Internet as a plain text. You can find more information about to configure Odoo with nginx as a reverse-proxy here.

Setup Automatic Odoo Backup

To automate the backup process and backup our Odoo database at regular intervals we can create a cron job.

Let’s say that we want to backup our Odoo database each day at 01:30 am and keep the latest 7 backups.

We will create a simple bash script which you can name it as you want:

~/backup_odoo.sh
#!/bin/bash

# vars
BACKUP_DIR=~/odoo_backups
ODOO_DATABASE=db1
ADMIN_PASSWORD=superadmin_passwd

# create a backup directory
mkdir -p ${BACKUP_DIR}

# create a backup
curl -X POST \
    -F "master_pwd=${ADMIN_PASSWORD}" \
    -F "name=${ODOO_DATABASE}" \
    -F "backup_format=zip" \
    -o ${BACKUP_DIR}/${ODOO_DATABASE}.$(date +%F).zip \
    http://localhost:8069/web/database/backup


# delete old backups
find ${BACKUP_DIR} -type f -mtime +7 -name "${ODOO_DATABASE}.*.zip" -delete
sudo chmod +x ~/backup_odoo.sh

Do not forget to change the BACKUP_DIR, ODOO_DATABASE and ADMIN_PASSWORD variables according to your needs.

The last step is to create a new cron job which will run each day at 01:30 am:

crontab -e
01 30 * * * /home/<yourusername>/backup_odoo.sh

Do not forget to set the correct name and path to the backup script.

You can modify the script and implement a more robust backup solution such as using a remote backup storage, keep weekly and monthly backups ..etc.

Restore an Odoo Database

To restore a database backup using the database management interface, open your browser and navigate to http://your_server_ip:8069/web/database/manager.

Click on the Restore Database button and a new popup will be displayed.

Enter your Odoo database Master Password, select the backup File, enter the new Database Name and restore the database by clicking on the blue Continue button.

Before restoring the database you will need to either delete the database or use another database name.

Depending on the database size and your Internet speed, the restoration process may take some time.

We can also restore the database from the command line:

curl -F 'master_pwd=superadmin_passwd' -F backup_file=@/opt/odoo/odoo_backups/db1.2018-04-14.zip -F 'copy=true' -F 'name=db3' http://localhost:8069/web/database/restore

Of course you will need to adjust the command with your Odoo Master password, the path to the database backup and the database name.

If the restoration is successful the output should look like this:

!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/web/database/manager">/web/database/manager</a>.  If not click the link.

Conclusion

This tutorial walked you through creating automatic daily backups of your Odoo databases using a cronjob.

If you have any question or feedback feel free to leave a comment.