Scripts to backup and restore odoo databases. Typically to restore production database into test environments
This script automates the process of backing up an Odoo database and restoring it to a target database. It:
- Downloads a backup from an Odoo instance.
- Extracts the backup.
- Restores the database.
- Cleans up the database with predefined SQL scripts.
- Restores the filestore.
Ensure that you have the following dependencies installed:
curlunzippsql(PostgreSQL client)pg_dumpdropdbcreatedbchown
The script requires a .env file in the same directory. The file should contain the following variables (see env.j2 template):
# Odoo instance settings
ODOO_HOST_PROTOCOL=http
ODOO_HOST_NAME=localhost
ODOO_HOST_PORT=8069
ODOO_DB_NAME=your_db_name
ODOO_MASTER_PASSWORD=your_master_password
# PostgreSQL settings
POSTGRES_HOST=localhost
POSTGRES_PASSWORD=your_postgres_password
ODOO_TARGET_DB_NAME=your_target_db_name
# File storage path
FILESTORE_PATH=/path/to/filestoreImportant: Ensure that your
.envfile is correctly formatted and does not contain any syntax errors.
Before running the script, ensure that the Odoo service is stopped to prevent conflicts.
Execute the script with the following command:
bash run.sh- The script will create a backup directory (
odoo_backups) if it does not exist. - It will send a request to the Odoo instance to generate a backup.
- The backup file is saved in the
odoo_backupsdirectory.
- The script extracts the backup.
- It checks if the target database exists:
- If it exists, it creates a backup and then drops it.
- It then creates a new database and restores the Odoo database dump.
- It runs predefined SQL scripts (
scripts/default.sqlandscripts/custom.sql) for cleanup. - The filestore is restored and appropriate permissions are set.
Once the script completes successfully, restart the Odoo service:
sudo systemctl start odooThis error may indicate that the backup file is corrupted or incomplete. Try:
- Checking if the backup was fully downloaded.
- Running
file your_backup.zipto verify its format. - Using
unzip -l your_backup.zipto inspect its contents.
If you get an error stating that the database does not exist:
- Ensure that the correct database name is set in
.env. - Confirm that PostgreSQL is running with
systemctl status postgresql.
- Ensure that the script has execution permissions:
chmod +x run.sh
- If running as a different user, make sure they have permission to access the database and filestore.
- Do not commit your
.envfile to version control as it contains sensitive credentials. - The script assumes that the Odoo database is backed up in ZIP format with a PostgreSQL dump and filestore.
This script is provided "as-is" without any warranties. Use at your own risk.
SomIT