Last active
July 27, 2024 18:44
-
-
Save alanfzf/0054c1f442a2c777d12733c8a0ac46b0 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 | |
# Variables | |
DATABASE_NAME="local_finanssoreal" | |
if [ "$#" -ne 2 ]; then | |
echo "Usage: $0 <container-id> <database-file-location>" | |
exit 1 | |
fi | |
# drop database | |
container_id=$1 | |
database_file=$2 | |
if [[ ! $(docker ps -q -f id=$container_id) ]]; then | |
echo "Container with ID $container_id doesn't exist or is not running!" | |
exit 1 | |
fi | |
if [[ ! -e "$database_file" ]]; then | |
echo "Database file at $database_file doesn't exist!" | |
exit 1 | |
fi | |
# drop the database | |
docker exec -i "$container_id" /usr/bin/mysql -u root --password=root -e "DROP DATABASE ${DATABASE_NAME}" | |
docker exec -i "$container_id" /usr/bin/mysql -u root --password=root -e "CREATE DATABASE IF NOT EXISTS ${DATABASE_NAME}" | |
docker exec -i "$container_id" /usr/bin/mysql -u root --password=root ${DATABASE_NAME} < "$database_file" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment