In an ERP environment, data is the lifeblood of the business. A single server failure or a corrupted database can halt operations entirely. Relying on manual backups is a high-risk strategy. To ensure business continuity, a 3-tier automated backup system (Daily, Weekly, and Monthly) is essential. This article provides a production-ready Shell script and configuration guide to secure your Odoo 18 environment.
1. The Strategy: The Grandfather-Father-Son (GFS) Method
Our automation follows a simplified GFS rotation:
- Daily (Son): Retained for 7 days. Fast recovery for recent errors.
- Weekly (Father): Retained for 4 weeks. Captured every Monday.
- Monthly (Grandfather): Retained indefinitely. Captured on the 1st of every month for fiscal auditing.
2. Infrastructure Setup
First, create a dedicated directory to store the backups and ensure the odoo system user has ownership.
sudo mkdir -p /var/odoo_backups/{daily,weekly,monthly}
sudo chown -R odoo:odoo /var/odoo_backups/
3. The Automation Script: odoo_backup.sh
This script captures both the PostgreSQL database (via pg_dump) and the Odoo Filestore (where attachments/images are stored).
Create the file:
sudo nano /usr/local/bin/odoo_backup.sh
Paste the following code:
Bash
#!/bin/bash
# --- CONFIGURATION ---
BACKUP_DIR="/var/odoo_backups"
ODOO_USER="odoo"
DB_NAME="your_database_name" # Replace with your actual Odoo DB name
FILESTORE_PATH="/home/odoo/.local/share/Odoo/filestore/$DB_NAME"
DATE=$(date +%Y-%m-%d_%H%M%S)
DAY_OF_WEEK=$(date +%u) # 1 (Monday) to 7
DAY_OF_MONTH=$(date +%d) # 01 to 31
# --- FILENAMES ---
FILENAME="${DB_NAME}_${DATE}.tar.gz"
TEMP_DIR="/tmp/odoo_backup_$DATE"
# --- EXECUTION ---
echo "Starting Odoo backup for database: $DB_NAME"
mkdir -p $TEMP_DIR
# 1. Exporting Database
sudo -u postgres pg_dump $DB_NAME > $TEMP_DIR/db.sql
# 2. Copying Filestore (Crucial for images and attachments)
if [ -d "$FILESTORE_PATH" ]; then
cp -r $FILESTORE_PATH $TEMP_DIR/filestore
else
echo "Warning: Filestore path not found!"
fi
# 3. Compressing into a single archive
tar -czf $BACKUP_DIR/daily/$FILENAME -C $TEMP_DIR .
# --- ROTATION LOGIC ---
# Weekly: Archive every Monday
if [ "$DAY_OF_WEEK" -eq 1 ]; then
cp $BACKUP_DIR/daily/$FILENAME $BACKUP_DIR/weekly/
echo "Weekly archive created."
fi
# Monthly: Archive every 1st of the month
if [ "$DAY_OF_MONTH" -eq "01" ]; then
cp $BACKUP_DIR/daily/$FILENAME $BACKUP_DIR/monthly/
echo "Monthly archive created."
fi
# --- RETENTION POLICIES (Cleanup) ---
# Remove daily backups older than 7 days
find $BACKUP_DIR/daily/ -type f -mtime +7 -name "*.tar.gz" -exec rm {} \;
# Remove weekly backups older than 31 days
find $BACKUP_DIR/weekly/ -type f -mtime +31 -name "*.tar.gz" -exec rm {} \;
# Note: Monthly backups are kept forever in this script
# Final Cleanup
rm -rf $TEMP_DIR
echo "Backup Process Finished: $BACKUP_DIR/daily/$FILENAME"
Set Permissions:
Bash
sudo chmod +x /usr/local/bin/odoo_backup.sh
4. Scheduling with Cron
To run the backup every night at 2:00 AM (server time), add the script to the system crontab.
- Open the crontab editor:
sudo crontab -e
- Add this line at the end of the file:
0 2 * * * /usr/local/bin/odoo_backup.sh >> /var/log/odoo_backup.log 2>&1
5. Technical Best Practices
- Off-site Storage: Never keep your backups only on the same server. If the disk fails, you lose both the live data and the backups. Use tools like rclone to sync /var/odoo_backups to Amazon S3 or Google Drive.
- Database User: The script uses sudo -u postgres, which is the most reliable way to bypass password prompts in automated scripts while maintaining security.
- Integrity Checks: Periodically test your backups. A backup is only as good as its last successful restore.
Conclusion
Automating your Odoo backups with a tiered retention policy is a hallmark of professional system administration. By separating daily, weekly, and monthly archives, you balance storage efficiency with maximum data safety.
