Skip to content

Instantly share code, notes, and snippets.

@syedmhashim
Created May 11, 2023 06:43
Show Gist options
  • Save syedmhashim/cfd29d7ecbaf423df34ea7712f10b67b to your computer and use it in GitHub Desktop.
Save syedmhashim/cfd29d7ecbaf423df34ea7712f10b67b to your computer and use it in GitHub Desktop.
DB OPS (DUMP & RESTORE)
#!/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
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"]
@syedmhashim
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment