Last active
April 28, 2024 18:19
-
-
Save stefanpejcic/f38e025aa5a2a5013e759a186ddf64a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if Docker is running | |
if ! docker info &>/dev/null; then | |
echo "Docker is not running. Please start Docker and try again." | |
exit 1 | |
fi | |
# Check if the Docker container exists | |
if docker ps -a --format '{{.Names}}' | grep -q "openpanel_mysql"; then | |
# Generate a random password | |
MYSQL_ROOT_PASSWORD=$(docker logs openpanel_mysql 2>/dev/null | grep "GENERATED ROOT PASSWORD" | awk '{print $NF}') | |
echo "Existing MySQL password: $MYSQL_ROOT_PASSWORD" | |
# Execute MySQL commands inside the container | |
docker exec openpanel_mysql bash -c "mysql -u root -p'$MYSQL_ROOT_PASSWORD' -e 'CREATE DATABASE IF NOT EXISTS panel;'" | |
# Check if the "panel" database has tables | |
TABLES=$(docker exec openpanel_mysql bash -c "mysql -u root -p'$MYSQL_ROOT_PASSWORD' -e 'USE panel; SHOW TABLES;'") | |
if [ -z "$TABLES" ]; then | |
echo "The 'panel' database is empty. Importing SQL file..." | |
# Import .sql file | |
wget -O panel.sql https://gist.githubusercontent.com/stefanpejcic/a5ae3999ae0cf3e9ebf0853433f70e98/raw/2ea067aecfeff6eb40c2bfbf652e61af0735bdc7/initialise.sql | |
docker exec -i openpanel_mysql mysql -u root -p"$MYSQL_ROOT_PASSWORD" panel < panel.sql | |
rm panel.sql | |
echo "SQL file imported successfully, database is ready." | |
else | |
echo "The 'panel' database already contains tables. Skipping import.." | |
fi | |
# Update configuration files with new password | |
sed -i "s/\"mysql_password\": \".*\"/\"mysql_password\": \"${MYSQL_ROOT_PASSWORD}\"/g" /usr/local/admin/config.json | |
sed -i "s/password = \".*\"/password = \"${MYSQL_ROOT_PASSWORD}\"/g" /usr/local/admin/db.cnf | |
else | |
echo "Docker container 'openpanel_mysql' does not exist. Please make sure the container is running." | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment