2 min read

Automate Odoo backups with this script

April 2, 2020

Odoo’s database manager provides an simple interface to backup an odoo database (tenant). This interface can be used to run automated backups. I have created a script to easily request odoo backup archives. The script works like every other command line tool.

Create the following script on a server that is running an Odoo instance.

/usr/local/bin/odoo-backup

#!/bin/bash

# Exit script if command fails
set -e

# Display Help
Help() {
  echo
  echo "odoo-backup"
  echo "###########"
  echo
  echo "Description: Backup odoo database."
  echo "Syntax: odoo-backup [-p|-d|-o|-h|help]"
  echo "Example: odoo-backup -p secret -d odoo -o /tmp -h https://odoo.example.com"
  echo "options:"
  echo "  -p    Odoo master password. Defaults to \$ODOO_MASTER_PASSWORD env var."
  echo "  -d    Database name."
  echo "  -o    Output directory. Defaults to '/var/tmp'"
  echo "  -h    Odoo host. Defaults to 'http://localhost:8069'"
  echo "  help  Show odoo-backup manual."
  echo
}

# Show help and exit
if [[ $1 == 'help' ]]; then
    Help
    exit
fi

# Process params
while getopts ":p: :d: :o: :h:" opt; do
  case $opt in
    h) HOST="$OPTARG"
    ;;
    p) PASSWORD="$OPTARG"
    ;;
    d) DATABASE="$OPTARG"
    ;;
    o) DIR="$OPTARG"
    ;;
    \?) echo "Invalid option -$OPTARG" >&2
    Help
    exit;;
  esac
done

# Fallback to environment vars and default values
: ${PASSWORD:=${ODOO_MASTER_PASSWORD:='admin'}}
: ${DIR:='/var/tmp'}
: ${HOST:='http://localhost:8069'}

# Verify variables
[[ -z "$DATABASE" ]] && { echo "Parameter -d|database is empty" ; exit 1; }
[[ -z "$DIR" ]] && { echo "Parameter -d|dir is empty" ; exit 1; }
[[ -z "$HOST" ]] && { echo "Parameter -h|host is empty" ; exit 1; }

# Ensure output directory exists
mkdir -p $DIR

# Request backup with curl
curl -X POST \
  -F "master_pwd=${PASSWORD}" \
  -F "name=${DATABASE}" \
  -F "backup_format=zip" \
  -o ${DIR}/${DATABASE}.zip \
  ${HOST}/web/database/backup

# Validate zip file
unzip -q -t "${DIR}/${DATABASE}.zip"

# Notify if backup has finished
echo "The Odoo backup has finished: ${DIR}/${DATABASE}.zip"

Ensure the script is executable.

$ sudo chmod +x /usr/local/bin/odoo-backup.

Create an odoo backup for your database like this:

$ odoo-backup -d nameofyourdatabase -h https://odoo.example.com -p secretmasterpassword

Assuming you want to automate backups you can create a cron job and provide the master password via environment variable. Here is an example of a cron job:

$ sudo crontab -l
#Ansible: Backup job odoo backup erp
30 1 * * * . /etc/environment; odoo-backup -d erp; restic backup /var/tmp/erp.zip --tag odoo --tag odoo01

The Odoo master password can be declared in /etc/environments.

This example is part of my Ansible Playbook project

Categories:  Odoo

Tags:  odoo , backup , automation , script

comments powered by Disqus