Odoo自动备份脚本 - xiaohao0576/odoo-doc GitHub Wiki

参考链接

执行原理

通过cURL命令,以POST请求Odoo的 /web/database/backup 接口 参考源码: https://github.com/odoo/odoo/blob/0e7e2cbb3d93edff11ec15ff116246632437873a/addons/web/controllers/database.py#L122 在源码中,可以查看此接口需要传递的参数名称有 master_pwd, name, backup_format, 其中backup_format默认值为zip,表示备份文件包括数据库和附件的文件 如果backup_format不等于zip,Odoo就认为备份文件只是pg_dump导出的数据库备份文件

Curl备份命令

curl -X POST \
  -F "master_pwd=$PASSWORD" \
  -F "name=$DATABASE" \
  -F "backup_format=zip" \
  -o "$FILEPATH" \
  "$ODOO_HOST/web/database/backup"

使用备份脚本

sudo chmod +x /usr/local/bin/odoo-backup
odoo-backup -d nameofyourdatabase -h https://odoo.example.com -p secretmasterpassword

使用crontab和restic进行定时集中备份

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

备份脚本

/usr/local/bin/odoo-backup

#!/bin/bash

# Exit script if command fails
# -u stops the script on unset variables
# -e stops the script on errors
# -o pipefail stops the script if a pipe fails
set -e

# Get script name
SCRIPT=$(basename "$0")

# Display Help
Help() {
  echo
  echo "$SCRIPT"
  echo
  echo "Description: Backup odoo database."
  echo "Syntax: $SCRIPT [-p|-d|-o|-h|help]"
  echo "Example: $SCRIPT -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 with or without name. Defaults to '/var/tmp'"
  echo "  -h    Odoo host. Defaults to 'http://localhost:8069'"
  echo "  help  Show $SCRIPT manual."
  echo
}

# Show help and exit
if [ $1 == 'help' ](/xiaohao0576/odoo-doc/wiki/-$1-==-'help'-); then
    Help
    exit
fi

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

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

# Verify variables
[ -z "$DATABASE" ](/xiaohao0576/odoo-doc/wiki/--z-"$DATABASE"-) && { echo "Parameter -d|database is empty" ; exit 1; }
[ -z "$OUTPUT" ](/xiaohao0576/odoo-doc/wiki/--z-"$OUTPUT"-) && { echo "Parameter -d|dir is empty" ; exit 1; }
[ -z "$ODOO_HOST" ](/xiaohao0576/odoo-doc/wiki/--z-"$ODOO_HOST"-) && { echo "Parameter -h|host is empty" ; exit 1; }

# Check if dir var is file or folder
if [ "${OUTPUT: -4}" = ".zip" ];then
  DIR=`dirname "$OUTPUT"`
  FILE=`basename "$OUTPUT"`
else
  DIR=$OUTPUT
fi

# Ensure output directory exists
mkdir -p $DIR

# Ensure file name is set
if [ -z "$FILE" ];then
  FILE="$DATABASE.zip"
fi

# Set file path
FILEPATH="${DIR}/${FILE}"

echo "Backup database $DATABASE to $FILEPATH"

# Request backup with curl
curl -X POST \
  -F "master_pwd=$PASSWORD" \
  -F "name=$DATABASE" \
  -F "backup_format=zip" \
  -o "$FILEPATH" \
  "$ODOO_HOST/web/database/backup"

# Grep error if is html response
FILETYPE=$(file --mime-type -b "$FILEPATH")
if [ "$FILETYPE" == 'text/html' ](/xiaohao0576/odoo-doc/wiki/-"$FILETYPE"-==-'text/html'-); then
  grep error "$FILEPATH"
fi

# Validate zip file
unzip -q -t "$FILEPATH"

# Notify if backup has finished
echo "The Odoo backup has finished: $FILEPATH"