After you get the Odoo docker running on your VPS or any other ubuntu server, you need a reliable backup strategy. A single misstep—like an accidental deletion or a corrupted container—can cost you hours of recovery time. In this guide, I’ll show you how to automatically back up your PostgreSQL database and custom modules on a daily schedule using a bash script and cron.
This setup is ideal for developers and system administrators who want a minimal, CLI-based solution without relying on external services.
Step 1: Create a Backup Directory
Start by creating a dedicated folder to store your backups:
mkdir -p /root/backups
You can check your disk space to make sure you have enough room:
df -h
Step 2: Write the Backup Script
Create a backup script at /root/scripts/backup_odoo.sh:
nano /root/scripts/backup_odoo.sh
Then add the following code:
#!/bin/bash TIMESTAMP=$(date +"%F-%H-%M") BACKUP_DIR="/root/backups" DB_CONTAINER="root-db-1" DB_NAME="postgres" DB_USER="odoo" # Backup PostgreSQL database docker exec $DB_CONTAINER pg_dump -U $DB_USER $DB_NAME > "$BACKUP_DIR/db-$TIMESTAMP.sql" # Optional: Backup custom addons tar -czf "$BACKUP_DIR/custom-addons-$TIMESTAMP.tar.gz" /root/custom-addons/ # Cleanup: remove backups older than 7 days find $BACKUP_DIR -type f -mtime +7 -exec rm {} \; echo "Backup completed at $TIMESTAMP"
Make the script executable:
chmod +x /root/scripts/backup_odoo.sh
Step 3: Schedule the Backup with Cron
To automate the backup, open your root crontab:
crontab -e
Add the following line to schedule the script to run every day at 2:00 AM:
0 2 * * * /root/scripts/backup_odoo.sh >> /root/backup.log 2>&1
This will also log any output or errors into /root/backup.log.
Step 4: Test the Backup
To verify everything is working, run the script manually:
/root/scripts/backup_odoo.sh
Then check your backup directory:
ls -lah /root/backups
You should see output files like:
db-2025-04-20-02-00.sql custom-addons-2025-04-20-02-00.tar.gz
These files include your full database dump and a compressed archive of your custom modules.
Conclusion
With this simple cron-based setup, you’ll have daily backups of your Odoo environment—PostgreSQL data and custom modules alike. The script also includes a cleanup step to automatically remove backups older than 7 days, helping you manage disk space without manual intervention.
This method is flexible, easy to maintain, and entirely self-contained. If you want to extend it further, consider adding remote syncing via rsync, S3 uploads, or even container volume snapshots.
For a Dockerized Odoo setup on Hetzner, this solution offers just the right balance of simplicity and reliability.