Last active
October 25, 2024 13:59
-
-
Save ekanant/823b5320dd03d8a8cea46a4c99e9f1a9 to your computer and use it in GitHub Desktop.
Backup and restore MySQL in docker-compose
This file contains hidden or 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
#reference https://medium.com/@siriphonnot/backup-and-restore-a-mysql-database-from-a-running-docker-mysql-container-6c932907e21f | |
#reference https://medium.com/@vineetcic/script-to-take-mysql-database-backup-for-last-7-days-45f97ed067a9 | |
mysqlUser="root" | |
mysqlPassword="xxxxx" | |
mysqlHost="127.0.0.1" | |
DB="my_db" | |
credentialsFile="/mysql-credentials.cnf" | |
/usr/local/bin/docker-compose exec -T mysql bash -c " | |
echo '[client]' > ${credentialsFile} \ | |
&& echo 'user=${mysqlUser}' >> ${credentialsFile} \ | |
&& echo 'password=${mysqlPassword}' >> ${credentialsFile} \ | |
&& echo 'host=${mysqlHost}' >> ${credentialsFile} \ | |
&& echo '`date +%Y-%m-%d_%H:%M:%S` :: create credential file finish' \ | |
" | |
BACKUP_DIR="./backup" | |
DATESTAMP=$(date +%Y-%m-%d_%H:%M:%S) | |
# remove backups older than $DAYS_KEEP | |
DAYS_KEEP=7 | |
find ${BACKUP_DIR}* -mtime +$DAYS_KEEP -exec rm -f {} \; 2> /dev/null | |
# create backups securely | |
umask 006 | |
FILEPATH="${BACKUP_DIR}/${DB}-${DATESTAMP}.sql.gz" | |
echo "`date +%Y-%m-%d_%H:%M:%S` :: backup to ${FILEPATH}" | |
#backup with gzip | |
/usr/local/bin/docker-compose exec -T mysql /usr/bin/mysqldump --defaults-extra-file="${credentialsFile}" --databases ${DB} | gzip > ${FILEPATH} | |
#backup with normal sql | |
#/usr/local/bin/docker-compose exec -T mysql /usr/bin/mysqldump --defaults-extra-file="${credentialsFile}" --databases ${DB} > ${FILEPATH} | |
echo "`date +%Y-%m-%d_%H:%M:%S` :: Finish backup" |
This file contains hidden or 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
mysqlUser="root" | |
mysqlPassword="xxxxx" | |
mysqlHost="127.0.0.1" | |
DB="my_db" | |
credentialsFile="/mysql-credentials.cnf" | |
backup_file="backup.sql.gz" | |
/usr/local/bin/docker-compose exec -T mysql bash -c " | |
echo '[client]' > ${credentialsFile} \ | |
&& echo 'user=${mysqlUser}' >> ${credentialsFile} \ | |
&& echo 'password=${mysqlPassword}' >> ${credentialsFile} \ | |
&& echo 'host=${mysqlHost}' >> ${credentialsFile} \ | |
&& echo '`date +%Y-%m-%d_%H:%M:%S` :: create credential file finish' \ | |
" | |
#restore with gzip file | |
gunzip < ${backup_file} | /usr/local/bin/docker-compose exec -T mysql /usr/bin/mysql --defaults-extra-file="${credentialsFile}" | |
#restore with not gzip file | |
#docker-compose exec -T mysql /usr/bin/mysql --defaults-extra-file="${credentialsFile}" < ${backup_file} | |
echo "`date +%Y-%m-%d_%H:%M:%S` :: Finish restore" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment