#!/bin/bash # Set variables DUMP_FILE="/tmp/backups.dump" DB_NAME="$DB_POSTGRESDB_DATABASE" DB_USER="$DB_POSTGRESDB_USER" DB_PASS="$DB_POSTGRESDB_PASSWORD" WEBDAV_URL="$WEBDAV_URL" WEBDAV_USER="$WEBDAV_USER" WEBDAV_PASSWORD="$WEBDAV_PASSWORD" DB_IMPORT="$DB_IMPORT" # Check if database import is needed if [ "$DB_IMPORT" = "yes" ]; then echo "Starting database import..." # Download dump file echo "Downloading database backup file..." if curl -u "$WEBDAV_USER:$WEBDAV_PASSWORD" -f -o "$DUMP_FILE" "${WEBDAV_URL}backup.dump"; then echo "Download successful." else echo "Download failed. Unable to retrieve database backup file from ${WEBDAV_URL}backup.dump. Authentication failed with username $WEBDAV_USER" echo "Skipping database import process..." exit 1 fi # Check if database exists echo "Checking if database exists..." if psql -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then echo "Database $DB_NAME already exists, preparing to delete existing database..." if dropdb -U "$DB_USER" "$DB_NAME"; then echo "Database $DB_NAME deleted." else echo "Failed to delete database, exiting." exit 1 fi else echo "Database $DB_NAME does not exist, creating new database." fi # Create new database echo "Creating new database $DB_NAME..." if createdb -U "$DB_USER" "$DB_NAME"; then echo "Database $DB_NAME created successfully." else echo "Failed to create database, exiting." exit 1 fi # Import data echo "Importing data into $DB_NAME..." if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then echo "Data import successful." else echo "Data import failed." exit 1 fi # Remove temporary file echo "Removing temporary file..." rm -f "$DUMP_FILE" echo "Database import process complete." else echo "Skipping database import (DB_IMPORT is not 'yes')." fi