Created
May 11, 2023 06:43
-
-
Save syedmhashim/cfd29d7ecbaf423df34ea7712f10b67b to your computer and use it in GitHub Desktop.
DB OPS (DUMP & RESTORE)
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 | |
VALID_MODES=("dump" "restore") | |
if [[ ! " ${VALID_MODES[*]} " =~ " ${MODE} " ]]; then | |
printf "\nError: Invalid value for \`MODE\`" | |
exit 1 | |
fi | |
if [[ -z "$MYSQL_DUMP_FILE" ]]; then | |
printf "\nError: Required environment variable not set: \`MYSQL_DUMP_FILE\`" | |
exit 1 | |
fi | |
if [[ "$MODE" == "dump" ]]; then | |
mysqldump -u $MYSQL_USER -h $MYSQL_HOST -p $MYSQL_DATABASE --no-tablespaces > $MYSQL_DUMP_FILE | |
else | |
mysql -u $MYSQL_USER -h $MYSQL_HOST $MYSQL_DATABASE < $MYSQL_DUMP_FILE | |
fi |
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
FROM mysql:8.0 | |
# MODE can be restore or dump | |
ENV MODE=dump | |
ENV MYSQL_DATABASE=ghostdb | |
ENV MYSQL_PWD=password | |
ENV MYSQL_USER=ghost_admin | |
ENV MYSQL_HOST=127.0.0.1 | |
COPY docker-entrypoint.sh ./docker-entrypoint.sh | |
RUN chmod +x ./docker-entrypoint.sh | |
ENTRYPOINT ["./docker-entrypoint.sh"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The Dockerfile can be used in a containerized environment to take database backup and to do database restore. The
docker-entrypoint.sh
file can also be independently used to do backup and restores as well.