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=localhost2####################################3## OpenERP Backup4## Backup databases:odoo_live_db1,odoo_live_db25##########################################6# Stop OpenERP Server7/etc/init.d/odoo-server stop8# name of the database to be backed up9for db in odoo_live_db1 odoo_live_db210do11date= 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 $db14gzip $filename15done16# Start OpenERP Server17/etc/init.d/odoo-server start18exit 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/sh2path=/var/pgdump3logfile=/var/log/$04rm -f $logfile5for file in6find /var/pgdump/ -mtime +30 -type f -name *.sql.gz7do8echo "deleting: " $file >> $logfile9rm $file10done11exit 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 command20 2 * * * postgres /var/scripts/odoo_backup.sh30 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.