Update import-db.sh
Browse files- import-db.sh +27 -27
import-db.sh
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
#!/bin/bash
|
2 |
|
3 |
-
#
|
4 |
DUMP_FILE="/tmp/backups.dump"
|
5 |
DB_NAME="$DB_POSTGRESDB_DATABASE"
|
6 |
DB_USER="$DB_POSTGRESDB_USER"
|
@@ -10,57 +10,57 @@ WEBDAV_USER="$WEBDAV_USER"
|
|
10 |
WEBDAV_PASSWORD="$WEBDAV_PASSWORD"
|
11 |
DB_IMPORT="$DB_IMPORT"
|
12 |
|
13 |
-
#
|
14 |
if [ "$DB_IMPORT" = "yes" ]; then
|
15 |
-
echo "
|
16 |
|
17 |
-
#
|
18 |
-
echo "
|
19 |
if curl -u "$WEBDAV_USER:$WEBDAV_PASSWORD" -f -o "$DUMP_FILE" "${WEBDAV_URL}backup.dump"; then
|
20 |
-
echo "
|
21 |
else
|
22 |
-
echo "
|
23 |
-
echo "
|
24 |
exit 1
|
25 |
fi
|
26 |
|
27 |
-
#
|
28 |
-
echo "
|
29 |
if psql -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
|
30 |
-
echo "
|
31 |
if dropdb -U "$DB_USER" "$DB_NAME"; then
|
32 |
-
echo "
|
33 |
else
|
34 |
-
echo "
|
35 |
exit 1
|
36 |
fi
|
37 |
else
|
38 |
-
echo "
|
39 |
fi
|
40 |
|
41 |
-
#
|
42 |
-
echo "
|
43 |
if createdb -U "$DB_USER" "$DB_NAME"; then
|
44 |
-
echo "
|
45 |
else
|
46 |
-
echo "
|
47 |
exit 1
|
48 |
fi
|
49 |
|
50 |
-
#
|
51 |
-
echo "
|
52 |
if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then
|
53 |
-
echo "
|
54 |
else
|
55 |
-
echo "
|
56 |
exit 1
|
57 |
fi
|
58 |
|
59 |
-
#
|
60 |
-
echo "
|
61 |
rm -f "$DUMP_FILE"
|
62 |
|
63 |
-
echo "
|
64 |
else
|
65 |
-
echo "
|
66 |
-
fi
|
|
|
1 |
#!/bin/bash
|
2 |
|
3 |
+
# Set variables
|
4 |
DUMP_FILE="/tmp/backups.dump"
|
5 |
DB_NAME="$DB_POSTGRESDB_DATABASE"
|
6 |
DB_USER="$DB_POSTGRESDB_USER"
|
|
|
10 |
WEBDAV_PASSWORD="$WEBDAV_PASSWORD"
|
11 |
DB_IMPORT="$DB_IMPORT"
|
12 |
|
13 |
+
# Check if database import is needed
|
14 |
if [ "$DB_IMPORT" = "yes" ]; then
|
15 |
+
echo "Starting database import..."
|
16 |
|
17 |
+
# Download dump file
|
18 |
+
echo "Downloading database backup file..."
|
19 |
if curl -u "$WEBDAV_USER:$WEBDAV_PASSWORD" -f -o "$DUMP_FILE" "${WEBDAV_URL}backup.dump"; then
|
20 |
+
echo "Download successful."
|
21 |
else
|
22 |
+
echo "Download failed. Unable to retrieve database backup file from ${WEBDAV_URL}backup.dump. Authentication failed with username $WEBDAV_USER"
|
23 |
+
echo "Skipping database import process..."
|
24 |
exit 1
|
25 |
fi
|
26 |
|
27 |
+
# Check if database exists
|
28 |
+
echo "Checking if database exists..."
|
29 |
if psql -U "$DB_USER" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
|
30 |
+
echo "Database $DB_NAME already exists, preparing to delete existing database..."
|
31 |
if dropdb -U "$DB_USER" "$DB_NAME"; then
|
32 |
+
echo "Database $DB_NAME deleted."
|
33 |
else
|
34 |
+
echo "Failed to delete database, exiting."
|
35 |
exit 1
|
36 |
fi
|
37 |
else
|
38 |
+
echo "Database $DB_NAME does not exist, creating new database."
|
39 |
fi
|
40 |
|
41 |
+
# Create new database
|
42 |
+
echo "Creating new database $DB_NAME..."
|
43 |
if createdb -U "$DB_USER" "$DB_NAME"; then
|
44 |
+
echo "Database $DB_NAME created successfully."
|
45 |
else
|
46 |
+
echo "Failed to create database, exiting."
|
47 |
exit 1
|
48 |
fi
|
49 |
|
50 |
+
# Import data
|
51 |
+
echo "Importing data into $DB_NAME..."
|
52 |
if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then
|
53 |
+
echo "Data import successful."
|
54 |
else
|
55 |
+
echo "Data import failed."
|
56 |
exit 1
|
57 |
fi
|
58 |
|
59 |
+
# Remove temporary file
|
60 |
+
echo "Removing temporary file..."
|
61 |
rm -f "$DUMP_FILE"
|
62 |
|
63 |
+
echo "Database import process complete."
|
64 |
else
|
65 |
+
echo "Skipping database import (DB_IMPORT is not 'yes')."
|
66 |
+
fi
|