2017-03-27 21:16:15 +02:00
|
|
|
#!/bin/bash
|
2021-12-30 18:37:52 +01:00
|
|
|
|
2022-10-15 07:49:01 +02:00
|
|
|
# Get hostname: try read from file, else get from env
|
2022-10-16 21:46:12 +02:00
|
|
|
[ -z "${MYSQL_HOST_FILE}" ] || { MYSQL_HOST=$(head -1 "${MYSQL_HOST_FILE}"); }
|
2022-10-15 07:49:01 +02:00
|
|
|
[ -z "${MYSQL_HOST}" ] && { echo "=> MYSQL_HOST cannot be empty" && exit 1; }
|
|
|
|
# Get username: try read from file, else get from env
|
|
|
|
[ -z "${MYSQL_USER_FILE}" ] || { MYSQL_USER=$(head -1 "${MYSQL_USER_FILE}"); }
|
2017-04-16 11:29:31 +02:00
|
|
|
[ -z "${MYSQL_USER}" ] && { echo "=> MYSQL_USER cannot be empty" && exit 1; }
|
2022-10-15 07:49:01 +02:00
|
|
|
# Get password: try read from file, else get from env, else get from MYSQL_PASSWORD env
|
2021-12-30 18:37:52 +01:00
|
|
|
[ -z "${MYSQL_PASS_FILE}" ] || { MYSQL_PASS=$(head -1 "${MYSQL_PASS_FILE}"); }
|
2022-10-15 07:49:01 +02:00
|
|
|
[ -z "${MYSQL_PASS:=$MYSQL_PASSWORD}" ] && { echo "=> MYSQL_PASS cannot be empty" && exit 1; }
|
2017-04-16 11:29:31 +02:00
|
|
|
|
|
|
|
if [ "$#" -ne 1 ]
|
|
|
|
then
|
|
|
|
echo "You must pass the path of the backup file to restore"
|
2023-09-17 11:01:33 +02:00
|
|
|
exit 1
|
2017-04-16 11:29:31 +02:00
|
|
|
fi
|
|
|
|
|
2021-05-09 17:57:18 +02:00
|
|
|
set -o pipefail
|
|
|
|
|
2022-02-04 20:44:11 +01:00
|
|
|
if [ -z "${USE_PLAIN_SQL}" ]
|
2022-10-15 07:49:01 +02:00
|
|
|
then
|
2022-02-04 20:44:11 +01:00
|
|
|
SQL=$(gunzip -c "$1")
|
|
|
|
else
|
|
|
|
SQL=$(cat "$1")
|
|
|
|
fi
|
|
|
|
|
2021-03-07 12:57:19 +01:00
|
|
|
DB_NAME=${MYSQL_DATABASE:-${MYSQL_DB}}
|
2021-11-07 23:26:56 +01:00
|
|
|
if [ -z "${DB_NAME}" ]
|
2021-05-04 22:59:10 +02:00
|
|
|
then
|
2022-02-04 20:44:11 +01:00
|
|
|
echo "=> Searching database name in $1"
|
2021-05-24 20:11:30 +02:00
|
|
|
DB_NAME=$(echo "$SQL" | grep -oE '(Database: (.+))' | cut -d ' ' -f 2)
|
2021-05-04 22:59:10 +02:00
|
|
|
fi
|
2022-02-04 20:44:11 +01:00
|
|
|
[ -z "${DB_NAME}" ] && { echo "=> Database name not found" && exit 1; }
|
2021-05-04 22:59:10 +02:00
|
|
|
|
|
|
|
echo "=> Restore database $DB_NAME from $1"
|
|
|
|
|
2022-06-18 11:25:46 +02:00
|
|
|
if echo "$SQL" | mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -p"$MYSQL_PASS" $MYSQL_SSL_OPTS "$DB_NAME"
|
2017-04-16 11:29:31 +02:00
|
|
|
then
|
2017-03-27 21:16:15 +02:00
|
|
|
echo "=> Restore succeeded"
|
|
|
|
else
|
|
|
|
echo "=> Restore failed"
|
|
|
|
fi
|