File size: 1,241 Bytes
6b3b61a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/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