Odoo Database Auto Backup

Odoo Database Auto Backup

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.

sudo nano /var/scripts/odoo_backup.sh

then copy and paste below code

#!/bin/sh hostname=localhost
####################################
## OpenERP Backup
## Backup databases:odoo_live_db1,odoo_live_db2
##########################################
# Stop OpenERP Server
/etc/init.d/odoo-server stop
# name of the database to be backed up
for db in odoo_live_db1 odoo_live_db2
do
date= date +"%Y%m%d_%H%M%N"
filename="/var/pgdump/${hostname}_${db}_${date}.sql"
pg_dump -E UTF-8 -p 5433 -F p -b -f $filename $db
gzip $filename
done
# Start OpenERP Server
/etc/init.d/odoo-server start
exit 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.

sudo nano /var/scripts/clean_backup.sh

Then copy and paste below code

#!/bin/sh
path=/var/pgdump
logfile=/var/log/$0
rm -f $logfile
for file in
find /var/pgdump/ -mtime +30 -type f -name *.sql.gz
do
echo "deleting: " $file >> $logfile
rm $file
done
exit 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:

crontab -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.

# m h dom mon dow user command
0 2 * * * postgres /var/scripts/odoo_backup.sh
0 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.

  • Share On: