[Odoo] A script to autobackup Postgres Databases

If you don’t want to explore the Odoo modules in the market and you are rather looking to customize your own script. You may use this, as one of our client devs asked for a fast way to do it.



First of all create a folder for your backups, you may also give postgres user the ownership:

mkdir /backup-odoo/archive -pchown postgres.postgres /backup -R

You can list your databases using the command sudo -u postgres psql -l.

Create then your script /backups/db-backups.sh that rotates the backup every BACKUP_KEEP_DAYS which is here defined as 30 days:

#!/bin/bash# Define paths and parametersBACKUP_DIR="/backup-odoo/archive"BACKUP_KEEP_DAYS=30  # Number of days to keep backupsfor db in database1 database2 database3 database3; do  # Generate backup filename with database name  BACKUP_FILE="${BACKUP_DIR}/${db}_$(date +%Y-%m-%d).sql"  # Use dynamic database name in pg_dump command  pg_dump -E UTF-8 -f "$BACKUP_FILE" "$db"  gzip $BACKUP_FILEdone# Rotate monthly backupsfind "$BACKUP_DIR" -type f -mtime +"$BACKUP_KEEP_DAYS" -delete# Log backup detailsecho "Backup of Odoo databases completed at $(date +%Y-%m-%d)" >> "backup_log.txt"exit 0

Then add it to your cron jobs. For example every day at 23:00

crontab -e0 23 * * * /backup-odoo/archive/db-backup.sh

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *