n8n / import-db.sh
Amansoni7477030's picture
Create import-db.sh
6b3b61a verified
#!/bin/bash
# Set variables
DUMP_URL="https://mysite.site/backups.dump"
DUMP_FILE="/tmp/backups.dump"
DB_NAME="$POSTGRES_DB"
DB_USER="$POSTGRES_USER"
# Check if database import is required
if [ "$DB_IMPORT" = "yes" ]; then
echo "Starting database import..."
# Download the dump file
echo "Downloading database backup file..."
if curl -f -o "$DUMP_FILE" "$DUMP_URL"; then
echo "Download successful."
else
echo "Download failed. Exiting import process."
exit 1
fi
# Check if the database exists
if psql -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
echo "Database $DB_NAME exists. Deleting existing database..."
dropdb -U "$DB_USER" "$DB_NAME"
fi
# Create a new database
echo "Creating new database $DB_NAME..."
createdb -U "$DB_USER" "$DB_NAME"
# 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
rm -f "$DUMP_FILE"
echo "Database import process completed."
else
echo "Skipping database import (DB_IMPORT is not 'yes')."
fi