-
Notifications
You must be signed in to change notification settings - Fork 0
/
import-db.sh
70 lines (56 loc) · 1.77 KB
/
import-db.sh
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
67
68
69
70
#!/bin/bash
# Check if the SQL file is passed as a parameter
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <path_to_sql_file>"
exit 1
fi
SQL_FILE="$1"
# Check if the SQL file exists
if [ ! -f "$SQL_FILE" ]; then
echo "Error: The file $SQL_FILE does not exist."
exit 1
fi
# MariaDB container name
MARIADB_CONTAINER="humhub-mariadb"
# Check if the container is running
if ! docker ps --format "{{.Names}}" | grep -q "^$MARIADB_CONTAINER$"; then
echo "Error: The container $MARIADB_CONTAINER is not running."
exit 1
fi
# Read .env file
if [ ! -f ".env" ]; then
echo "Error: The .env file was not found."
exit 1
fi
source .env
DB_USER="$HUMHUB_DOCKER_DB_USER"
DB_PASSWORD="$HUMHUB_DOCKER_DB_PASSWORD"
DB_NAME="humhub"
# Check if variables are set
if [ -z "$DB_USER" ] || [ -z "$DB_PASSWORD" ]; then
echo "Error: Database user or password is not defined in the .env file."
exit 1
fi
# Get user confirmation
echo "WARNING: The database \"$DB_NAME\" will be dropped and re-imported. Continue? (y/n)"
read -r CONFIRM
if [[ "$CONFIRM" != "y" ]]; then
echo "Action aborted."
exit 0
fi
# Drop the database
echo "Dropping the database \"$DB_NAME\" in container \"$MARIADB_CONTAINER\" ..."
docker exec -i "$MARIADB_CONTAINER" mysql -u "$DB_USER" -p"$DB_PASSWORD" -e "DROP DATABASE IF EXISTS \`$DB_NAME\`; CREATE DATABASE \`$DB_NAME\` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;"
if [ $? -ne 0 ]; then
echo "Error: Dropping or creating the database failed."
exit 1
fi
# Import the SQL file
echo "Starting import into the container $MARIADB_CONTAINER ..."
cat "$SQL_FILE" | docker exec -i "$MARIADB_CONTAINER" mysql -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME"
if [ $? -eq 0 ]; then
echo "Database import completed successfully."
else
echo "Error: Database import failed."
exit 1
fi