-
Notifications
You must be signed in to change notification settings - Fork 9
/
06_initialize_db.sh
executable file
·54 lines (53 loc) · 2.59 KB
/
06_initialize_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
#!/bin/bash
INIT_DB=${DB_PASSWORD:-FALSE}
IDB_HOST=${DB_HOST:-localhost}
IDB_PORT=${DB_PORT:-3306}
IDB_USER=${DB_USER:-root}
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_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
cp /webtrees.sql /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
#Write to mysql database
until mysqladmin ping -h "$IDB_HOST" --silent; do
echo "Waiting for database to be ready ..."
sleep 1
done
echo "Database ready. Writing database."
mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" < /mod_webtrees.sql
#Alternative to set Webtrees admin user:
#echo "UPDATE wt_user SET user_name='$IDB_WT_ADMIN', email='$IDB_WT_ADMINMAIL', real_name='Admin', password='$WTCRYPT' WHERE user_id=1" | mysql -u "$IDB_USER" --password="$IDB_PASSWORD" -h "$IDB_HOST" "$IDB_DB_NAME"
unset RANDOM22
unset WTSALT
unset WTCRYPT
fi
fi