-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy path06_initialize_db.sh
More file actions
executable file
·83 lines (76 loc) · 3.39 KB
/
06_initialize_db.sh
File metadata and controls
executable file
·83 lines (76 loc) · 3.39 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/bin/bash
INIT_DB=${DB_PASSWORD:-FALSE}
IDB_TYPE=${DB_TYPE:-mysql}
IDB_HOST=${DB_HOST:-localhost}
IDB_PORT=${DB_PORT:-3306}
IDB_USER=${DB_USER:-root}
# Change default port and database user if using postgres
if [ "$IDB_TYPE" = "postgres" ]; then
IDB_PORT=${DB_PORT:-5432}
IDB_USER=${DB_USER:-postgres}
fi
IDB_PASSWORD=${DB_PASSWORD:-password}
IDB_DB_NAME=${DB_NAME:-webtrees}
IDB_DB_PREFIX="wt_"
IDB_WT_ADMIN=${WT_ADMIN:-admin}
IDB_WT_ADMINPW=${WT_ADMINPW:-admin123}
IDB_WT_ADMINMAIL=${WT_ADMINMAIL:-noreply@webtrees.net}
#Check if initial database configuration should be set
if [ "$INIT_DB" != "FALSE" ]
then
#Check if not yet set, if file exists, do nothing
CONFIG_FILE=/var/www/html/data/config.ini.php
if [ -f "$CONFIG_FILE" ]; then
echo "Configuration file $CONFIG_FILE yet exist. No settings will be modified."
else
echo "Creating the initial database settings in configuration file $CONFIG_FILE and creating database."
cp /config.ini.php "$CONFIG_FILE"
sed -i 's/<DB_TYPE>/'"$IDB_TYPE"'/g' "$CONFIG_FILE"
sed -i 's/<DB_HOST>/'"$IDB_HOST"'/g' "$CONFIG_FILE"
sed -i 's/<DB_PORT>/'"$IDB_PORT"'/g' "$CONFIG_FILE"
sed -i 's/<DB_USER>/'"$IDB_USER"'/g' "$CONFIG_FILE"
sed -i 's/<DB_PASSWORD>/'"$IDB_PASSWORD"'/g' "$CONFIG_FILE"
sed -i 's/<DB_NAME>/'"$IDB_DB_NAME"'/g' "$CONFIG_FILE"
sed -i 's/<DB_PREFIX>/'"$IDB_DB_PREFIX"'/g' "$CONFIG_FILE"
chown www-data:docker-data "$CONFIG_FILE"
chmod 660 "$CONFIG_FILE"
#Create database structure and add admin user
if [ "$IDB_TYPE" = "postgres" ]; then
SQL_FILE="/webtrees-postgres.sql"
else
SQL_FILE="/webtrees.sql"
fi
cp "$SQL_FILE" /mod_webtrees.sql
sed -i 's/<DB_NAME>/'"$IDB_DB_NAME"'/g' /mod_webtrees.sql
sed -i 's/<WT_ADMIN_NAME>/'"$IDB_WT_ADMIN"'/g' /mod_webtrees.sql
#Encode password and escape for sed
RANDOM22=$(php -r "echo substr(base64_encode(openssl_random_pseudo_bytes(17)),0,22);")
WTSALT=$(php -r "echo '\$2y\$10\$'.str_replace('+','.','$RANDOM22');")
WTCRYPT=$(php -r "echo crypt('$IDB_WT_ADMINPW', '$WTSALT');")
sed -i 's/<WT_ADMIN_PW>/'"$(echo $WTCRYPT | sed -e 's/[]\/$*.^[]/\\&/g')"'/g' /mod_webtrees.sql
sed -i 's/<WT_ADMIN_MAIL>/'"$IDB_WT_ADMINMAIL"'/g' /mod_webtrees.sql
# Wait for database and write schema based on type
if [ "$IDB_TYPE" = "postgres" ]; then
echo "Using PostgreSQL database"
until pg_isready -h "$IDB_HOST" -p "$IDB_PORT" -U "$IDB_USER" > /dev/null 2>&1; do
echo "Waiting for PostgreSQL database to be ready ..."
sleep 1
done
echo "PostgreSQL database ready. Writing database."
export PGPASSWORD="$IDB_PASSWORD"
psql -h "$IDB_HOST" -p "$IDB_PORT" -U "$IDB_USER" -d postgres -f /mod_webtrees.sql
unset PGPASSWORD
else
echo "Using MySQL/MariaDB database"
until mysqladmin ping -h "$IDB_HOST" --silent; do
echo "Waiting for MySQL/MariaDB database to be ready ..."
sleep 1
done
echo "MySQL/MariaDB database ready. Writing database."
mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" < /mod_webtrees.sql
fi
unset RANDOM22
unset WTSALT
unset WTCRYPT
fi
fi