File size: 2,034 Bytes
22ec144
 
4da4818
22ec144
6f6a8bb
 
 
 
e13d45b
 
6f6a8bb
22ec144
4da4818
22ec144
4da4818
22ec144
4da4818
 
0a4cc64
4da4818
22ec144
4da4818
 
22ec144
 
 
4da4818
 
6f6a8bb
4da4818
6f6a8bb
4da4818
6f6a8bb
4da4818
6f6a8bb
 
 
4da4818
22ec144
 
4da4818
 
6f6a8bb
4da4818
6f6a8bb
4da4818
6f6a8bb
 
22ec144
4da4818
 
22ec144
4da4818
22ec144
4da4818
22ec144
 
 
4da4818
 
22ec144
 
4da4818
22ec144
4da4818
 
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/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