Spaces:
Sleeping
Sleeping
Amansoni7477030
commited on
Create import-db.sh
Browse files- import-db.sh +47 -0
import-db.sh
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
|
3 |
+
# Set variables
|
4 |
+
DUMP_URL="https://mysite.site/backups.dump"
|
5 |
+
DUMP_FILE="/tmp/backups.dump"
|
6 |
+
DB_NAME="$POSTGRES_DB"
|
7 |
+
DB_USER="$POSTGRES_USER"
|
8 |
+
|
9 |
+
# Check if database import is required
|
10 |
+
if [ "$DB_IMPORT" = "yes" ]; then
|
11 |
+
echo "Starting database import..."
|
12 |
+
|
13 |
+
# Download the dump file
|
14 |
+
echo "Downloading database backup file..."
|
15 |
+
if curl -f -o "$DUMP_FILE" "$DUMP_URL"; then
|
16 |
+
echo "Download successful."
|
17 |
+
else
|
18 |
+
echo "Download failed. Exiting import process."
|
19 |
+
exit 1
|
20 |
+
fi
|
21 |
+
|
22 |
+
# Check if the database exists
|
23 |
+
if psql -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then
|
24 |
+
echo "Database $DB_NAME exists. Deleting existing database..."
|
25 |
+
dropdb -U "$DB_USER" "$DB_NAME"
|
26 |
+
fi
|
27 |
+
|
28 |
+
# Create a new database
|
29 |
+
echo "Creating new database $DB_NAME..."
|
30 |
+
createdb -U "$DB_USER" "$DB_NAME"
|
31 |
+
|
32 |
+
# Import data
|
33 |
+
echo "Importing data into $DB_NAME..."
|
34 |
+
if pg_restore -U "$DB_USER" -d "$DB_NAME" "$DUMP_FILE"; then
|
35 |
+
echo "Data import successful."
|
36 |
+
else
|
37 |
+
echo "Data import failed."
|
38 |
+
exit 1
|
39 |
+
fi
|
40 |
+
|
41 |
+
# Remove temporary file
|
42 |
+
rm -f "$DUMP_FILE"
|
43 |
+
|
44 |
+
echo "Database import process completed."
|
45 |
+
else
|
46 |
+
echo "Skipping database import (DB_IMPORT is not 'yes')."
|
47 |
+
fi
|