blog-odoo-database-auto-backup
Odoo,  How To

Odoo Database Auto Backup

Date Published

Share This

Think about how much money a big company could lose if their main server data suddenly went away, even just for a short time like 5 or 10 minutes. Or imagine if important data disappeared because of a server problem, and there was no way to get it back because there wasn't a backup copy. From a business point of view, these situations could be really bad. The key to dealing with such problems successfully is to have a clear plan for making backups (copies) of your data and being able to recover it when needed.

In big companies, it's essential to create a backup plan that fits the specific needs of the business. This plan can involve making backups every hour, every day, every week, or every month, depending on what the company requires.

Here, we're going to explain an easy and effective way to automatically backup an Odoo database using the default settings on a Linux server. This method ensures that backups are done regularly without taking up too much space on the server.

Create PostgreSQL Backup Script for Odoo

Create a script file called odoo_backup.sh on /var/scripts/ path. you can create the file using a nano text editor on the terminal. Here is the command.

1sudo nano /var/scripts/odoo_backup.sh

then copy and paste below code

1#!/bin/sh hostname=localhost
2####################################
3## OpenERP Backup
4## Backup databases:odoo_live_db1,odoo_live_db2
5##########################################
6# Stop OpenERP Server
7/etc/init.d/odoo-server stop
8# name of the database to be backed up
9for db in odoo_live_db1 odoo_live_db2
10do
11date= date +"%Y%m%d_%H%M%N"
12filename="/var/pgdump/${hostname}_${db}_${date}.sql"
13pg_dump -E UTF-8 -p 5433 -F p -b -f $filename $db
14gzip $filename
15done
16# Start OpenERP Server
17/etc/init.d/odoo-server start
18exit 0

Remove Old Odoo Database backup

Now The above file will create the backup, we will need a script to remove db backup older than 30 days,

Create a file named clean_backup.sh on /var/scripts/ path. Here is the command.

1sudo nano /var/scripts/clean_backup.sh

Then copy and paste below code

1#!/bin/sh
2path=/var/pgdump
3logfile=/var/log/$0
4rm -f $logfile
5for file in
6find /var/pgdump/ -mtime +30 -type f -name *.sql.gz
7do
8echo "deleting: " $file >> $logfile
9rm $file
10done
11exit 0

After creating both the file we are ready to create cron(schedule) jobs using crontab. In below example backup runs daily at 2 AM and the old database cleanup job runs daily at 5 AM.To edit or create your crontab file, type the following command at the Linux shell prompt:

1crontab -e

Select default editor if you are using command first time. and paste the below line. for more information on Linux crontab, you can visit this website.

1# m h dom mon dow user command
20 2 * * * postgres /var/scripts/odoo_backup.sh
30 5 * * * postgres /var/scripts/clean_backup.sh

Now the crontab will take the backup of your odoo database using the odoo_backup.sh script everyday 2 AM in the morning and store the odoo database backup under /var/pgdump/.

It will work for all odoo database version.