How To Setup Automatic Odoo Backup Using Cronjob


Introduction

Odoo is an open-source Enterprise Resource Planning (ERP) system that utilizes PostgreSQL as the database for its backend. The system is developed in Python, and its source code is open source.

Odoo’s data is archived in PostgreSQL, and it is essential to back up the database regularly to protect it from potential data loss.

In this guide, we will show you how to schedule automated backups of Odoo database using a Cron job. You'll need PostgreSQL hosting (i.e., web hosting that supports PostgreSQL well) through which you'll want to set up a VPS, dedicated server, or cloud hosting account that enables you to have root access to the server. You'll then want to create an ordinary user with sudo access to run commands as root.

Odoo Interface

The database management system for Odoo offers essential tools to help user backup, create, delete, duplicate, and restore any data on their system.

To create the backup file, go to your web browser and type the URL:

http://your_server_ip:8069/web/database/manager.

You should see the screen below:

How To Setup Automatic Odoo Backup Using Cronjob

Select Backup and a popup screen will appear as shown below:

How To Setup Automatic Odoo Backup Using Cronjob

Enter the password for Odoo database and click the blue backup button to create the new backup file. Based on the size of the database, the backup process may take a while before it starts.

Creating A Backup For Database Using Commands

After knowing how to use the database interface to create the backup, we can use another way to create the new backup by running several commands.

To do so, we can either use curl or wget. These tools are useful in handling any type of data using POST and can be utilized to pass important variables to Odoo’s database management tool.

In our example below, we will create a new backup file named back_up_filename.zip. The Master Password for Odoo is ADMIN_PASSWORD and our database will be named and stored in the backup_dirdirectory.

$ 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 want to use wget instead of curl, run the command below:

$ 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

To ensure your files are backed up in the remote location rather than a localhost, you should use the URL that points to the Odoo instance. Since you don’t want the password you entered to be sent online as plain text, it is recommended to use HTTPs.

Setting Up Automatic Backup for Odoo Instance

To set up an automatic backup for Odoo database, we need to generate a cronjob.

For instance, let’s assume you want to perform a daily backup for Odoo database at 03:40 am and store the latest eight backups.

In this case, we will start by creating a bash script with the name of your choice:

Use a script name such as ~/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 +8 -name "${ODOO_DATABASE}.*.zip" -delete

Then run the command below:

$ sudo chmod +x ~/backup_odoo.sh

Remember to change these variables accordingly to fit your needs: BACK_DIR, ADMIN_PASSWORD, and ODOO_DATABASE.

Now, go ahead and create a new file for the cron job to run every day at 03:00 am:

$ crontab -e
0300 * * * /home/<yourusername>/backup_odoo.sh

Note: Remember to set the correct name and path for your backup script.

It is possible to adjust your script and set a strong backup system like a remote storage, for the backed up files, perform regular weekly or monthly backups, and so on.

Restoring Odoo Database

The next step is to restore your database backup in Odoo’s interface. To do so, open your web browser and type the URL http://your_server_ip:8069/web/database/manager.

You should see the following display on your screen:

How To Setup Automatic Odoo Backup Using Cronjob

Press Restore Database. another screen will appear as shown below:

How To Setup Automatic Odoo Backup Using Cronjob

Now, type the Master Password for Odoo database, then select your preferred backup File. Next, enter the name of your new Database and press Continue to start the restoration process of the database.

Note: Make sure that before you restore your database, you have either deleted it or used a new database name.

The restoration process will take a while depending on the size of the database and the Internet speed.

It is also possible to restore your database using the following command:

$ 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

Don’t forget to modify this command line with the Master password for Odoo, the database name, and the path that points to your database backup.

Once the restoration process is complete, you should see the following output:

$ !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

At this point, you have successfully created automatic backups for Odoo databases with the help of cronjob.

Check out these top 3 Ecommerce hosting services:

Was this article helpful?