Skip to content

Instantly share code, notes, and snippets.

@troykelly
Last active March 14, 2024 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troykelly/e7fa34951ec3444689064f99c866b8a2 to your computer and use it in GitHub Desktop.
Save troykelly/e7fa34951ec3444689064f99c866b8a2 to your computer and use it in GitHub Desktop.
Odoo Backup and Restore Scripts

Odoo Backup and Restore Scripts

These scripts provide a simple way to backup and restore Odoo databases and their associated filestores.

Overview

  • backup.sh: Creates a compressed backup of the Odoo database and filestore.
  • restore.sh: Restores an Odoo database and filestore from the created backup.

Prerequisites

  • PostgreSQL client (pg_dump, createdb, dropdb) must be installed and accessible in the path.
  • The user must have permission to access and create/delete the database in PostgreSQL.
  • The scripts assume that Odoo filestore is present at /var/lib/odoo/filestore/.

Setup

Before using the scripts, ensure that the following environment variables are set:

  • HOST: The hostname of the PostgreSQL server.
  • USER: The username used for PostgreSQL connections.
  • PASSWORD: The password used for PostgreSQL connections.

These variables can be set in the environment or directly in the scripts.

Using the Backup Script

To create a backup of your Odoo database and filestore, use the backup_odoo.sh script as follows:

./backup.sh <database_name>

Replace <database_name> with the name of your Odoo database.

The script will create a timestamped backup file in the defined BACKUP_DIR.

Using the Restore Script

To restore an Odoo database and filestore from a backup, use the restore_odoo.sh script as follows:

./restore.sh <database_name>

Replace <database_name> with the name of the Odoo database to restore.

The script will look for the most recent backup in the defined BACKUP_DIR and restore it.

Disclaimer

These scripts are provided "as is", without warranty of any kind. The user assumes all risks associated with the use of these scripts. Always test scripts in a non-production environment before use.

#!/bin/bash
# Check if a parameter is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <database_name>"
exit 1
fi
DATABASE=$1
BACKUP_DIR="/mnt/backup"
FILESTORE_PATH="/var/lib/odoo/filestore/$DATABASE"
TIMESTAMP=$(date +"%Y%m%d%H%M%S")
BACKUP_NAME="${DATABASE}_${TIMESTAMP}"
WORKING_DIR="/tmp/${BACKUP_NAME}"
# Ensure working directory exists
mkdir -p ${WORKING_DIR}
# Backup database
echo "Backing up the PostgreSQL database..."
PGPASSWORD=$PASSWORD pg_dump -h $HOST -U $USER -F c -b -v -f "${WORKING_DIR}/${DATABASE}.sql" ${DATABASE}
# Check if the dump was successful
if [ $? -ne 0 ]; then
echo "Failed to backup the database, exiting."
exit 1
fi
# Backup filestore
echo "Backing up the filestore..."
cp -r ${FILESTORE_PATH} ${WORKING_DIR}/filestore
# Compress backup
echo "Compressing backup..."
tar -czvf ${BACKUP_DIR}/${BACKUP_NAME}.tar.gz -C ${WORKING_DIR} .
# Clean up
echo "Cleaning up..."
rm -rf ${WORKING_DIR}
echo "Backup completed successfully."
#!/bin/bash
# Enforces strict mode
set -euo pipefail
# Function definitions
cleanup() {
echo "Cleaning up..."
rm -rf "${WORKING_DIR}"
}
# Constants
readonly BACKUP_DIR="/mnt/backup"
readonly BASE_FILESTORE_PATH="/var/lib/odoo/filestore"
# Validates input parameters
if [[ "$#" -ne 1 ]]; then
echo "Usage: $0 <database_name>"
exit 1
fi
readonly DATABASE="$1"
readonly FILESTORE_PATH="${BASE_FILESTORE_PATH}/${DATABASE}"
readonly WORKING_DIR="/tmp/odoo_restore_${DATABASE}_$(date +%Y%m%d%H%M%S)"
# Setup trap for cleaning up on error
trap cleanup ERR
# Export PGPASSWORD for non-interactive postgres operations
export PGPASSWORD="${PASSWORD}"
# Find the most recent backup file
readonly LATEST_BACKUP=$(find "${BACKUP_DIR}" -name "${DATABASE}_*.tar.gz" -print | sort -r | head -1)
if [[ -z "${LATEST_BACKUP}" ]]; then
echo "No backup found for database ${DATABASE}."
exit 1
fi
# Prepare working directory
mkdir -p "${WORKING_DIR}"
echo "Restoring from ${LATEST_BACKUP}..."
# Extract backup
tar -xzf "${LATEST_BACKUP}" -C "${WORKING_DIR}"
# Drop the existing database if it exists
echo "Dropping existing database (if it exists)..."
dropdb --if-exists --force -h "${HOST}" -U "${USER}" "${DATABASE}"
# Create the database
echo "Creating the database..."
createdb -h "${HOST}" -U "${USER}" "${DATABASE}"
# Restore database from the sql dump file
echo "Restoring the database..."
pg_restore --exit-on-error --verbose -h "${HOST}" -U "${USER}" -d "${DATABASE}" "${WORKING_DIR}/${DATABASE}.sql"
# Remove existing filestore if it exists
if [[ -d "${FILESTORE_PATH}" ]]; then
echo "Removing existing filestore..."
rm -rf "${FILESTORE_PATH}"
fi
# Restore filestore
echo "Restoring the filestore..."
cp -r "${WORKING_DIR}/filestore" "${FILESTORE_PATH}"
# Cleanup after successful execution
cleanup
echo "Restore completed successfully."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment