Skip to content

Instantly share code, notes, and snippets.

@Rene-Roscher
Created June 28, 2023 12:34
Show Gist options
  • Select an option

  • Save Rene-Roscher/786f0fef46b3044e2b828fe5b564db96 to your computer and use it in GitHub Desktop.

Select an option

Save Rene-Roscher/786f0fef46b3044e2b828fe5b564db96 to your computer and use it in GitHub Desktop.
bash livck-db-restore.sh
#!/bin/bash
# Name of the database service defined in the docker-compose.yml
DB_SERVICE="database"
# Path to the SQL dump file for database restoration
DUMP_FILE="/opt/livck/db_dump.sql"
# Name of the database to import
DATABASE_NAME="livck-cp"
# Path to the .env file
ENV_FILE="/opt/livck/.env"
# Load the environment variables from the .env file
export $(grep -v '^#' "$ENV_FILE" | xargs -d '\n')
# Create a temporary directory on the host machine
TMP_DIR=$(mktemp -d)
# Copy the SQL dump file to the temporary directory
cp "$DUMP_FILE" "$TMP_DIR/db_dump.sql"
# Install mysql-client package in the database container
docker-compose exec "$DB_SERVICE" sh -c "apt-get update && apt-get install -y mysql-client"
# Copy the SQL dump file to the database service container
docker compose cp "$TMP_DIR/db_dump.sql" "$DB_SERVICE:/tmp/db_dump.sql"
# Restore the MySQL database inside the container
docker-compose exec -T "$DB_SERVICE" sh -c "mysql -u \"$DB_USERNAME\" -p\"$DB_PASSWORD\" \"$DATABASE_NAME\" < /tmp/db_dump.sql"
# Remove the temporary directory on the host machine
rm -rf "$TMP_DIR"
docker-compose exec app php artisan op:c
echo "MySQL database imported successfully from: $DUMP_FILE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment