curl
If the size of the backup is too big, NGINX will refuse (throwing a timeout) to upload/download the file due to client_max_body_size
directive.
We still can manage the Odoo databases bypassing NGINX from the server itself, doing a curl request to localhost:8069
.
curl -X POST -F 'master_pwd=ADMIN_PASSWORD' -F 'name=DB_NAME' -F 'backup_format=zip' -o /backup_dir/back_up_filename.zip http://localhost:8069/web/database/backup
curl -F 'master_pwd=superadmin_passwd' -F backup_file=@/opt/odoo/odoo_backups/db1.2021-09-14.zip -F 'copy=true' -F 'name=db3' http://localhost:8069/web/database/restore
In case of successful restore, you will see the following output.
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>Redirecting...</title>
<h1>Redirecting...</h1>
<p>You should be redirected automatically to target URL: <a href="/web/database/manager">/web/database/manager</a>. If not click the link.
From janikvonrotz.ch
#!/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' ]]; 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" ]] && { echo "Parameter -d|database is empty" ; exit 1; }
[[ -z "$OUTPUT" ]] && { echo "Parameter -d|dir is empty" ; exit 1; }
[[ -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' ]]; then
grep error "$FILEPATH"
fi
# Validate zip file
unzip -q -t "$FILEPATH"
# Notify if backup has finished
echo "The Odoo backup has finished: $FILEPATH"
From janikvonrotz.ch
#!/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: Restore odoo database."
echo "Syntax: $SCRIPT [-p|-d|-f|-h|-r|help]"
echo "Example: $SCRIPT -p secret -d odoo -f /tmp/odoo.zip -h https://odoo.example.org"
echo "options:"
echo " -p Odoo master password. Defaults to \$ODOO_MASTER_PASSWORD env var and 'admin'."
echo " -d Database name. Defaults to filename."
echo " -f Odoo database backup file. Defaults to '/var/tmp/odoo.zip'"
echo " -h Odoo host. Defaults to 'http://localhost:8069'"
echo " -r Replace existing database."
echo " help Show $SCRIPT manual."
echo
}
# Show help and exit
if [[ $1 == 'help' ]]; then
Help
exit
fi
# Initialise option flag with a false value
REPLACE='false'
# Process params
while getopts ":r :p: :d: :f: :h:" opt; do
case $opt in
r) REPLACE='true'
;;
h) ODOO_HOST="$OPTARG"
;;
p) PASSWORD="$OPTARG"
;;
d) DATABASE="$OPTARG"
;;
f) FILE="$OPTARG"
;;
\?) echo "Invalid option -$OPTARG" >&2
Help
;;
esac
done
# Fallback to environment vars and default values
: ${PASSWORD:=${ODOO_MASTER_PASSWORD:='admin'}}
: ${FILE:='/var/tmp/odoo.zip'}
: ${ODOO_HOST:='http://localhost:8069'}
FILENAME=$(basename -- "$FILE")
: ${DATABASE:="${FILENAME%%.*}"}
# Verify variables
[[ "$ODOO_HOST" == http* ]] || { echo "Parameter -h|host must start with 'http/s'" ; exit 1; }
[[ "${FILENAME##*.}" != "zip" ]] && { echo "Parameter -f|filename must end with '.zip'" ; exit 1; }
# Validate zip file
unzip -q -t $FILE
if $REPLACE; then
echo "Deleting Odoo database $DATABASE ..."
curl \
--silent \
-F "master_pwd=${PASSWORD}" \
-F "name=${DATABASE}" \
${ODOO_HOST%/}/web/database/drop | grep -q -E 'Internal Server Error|Redirecting...'
fi
# Start restore
echo "Requesting restore for Odoo database $DATABASE ..."
# Request restore with curl
CURL=$(curl \
-F "master_pwd=$PASSWORD" \
-F "name=$DATABASE" \
-F backup_file=@$FILE \
-F 'copy=true' \
"${ODOO_HOST%/}/web/database/restore")
(echo $CURL | grep -q 'Redirecting...') || (echo "The restore failed:"; echo $CURL | grep error; exit 1)
echo "The restore for Odoo database $DATABASE has finished."