Skip to content

Instantly share code, notes, and snippets.

@sebastian13
Last active July 28, 2023 20:42
Show Gist options
  • Select an option

  • Save sebastian13/cc58304f7b177ac1d16d7671dc67efb9 to your computer and use it in GitHub Desktop.

Select an option

Save sebastian13/cc58304f7b177ac1d16d7671dc67efb9 to your computer and use it in GitHub Desktop.
curl -O https://gist.githubusercontent.com/sebastian13/cc58304f7b177ac1d16d7671dc67efb9/raw/docker-mysql-dump.sh
curl -O https://gist.githubusercontent.com/sebastian13/cc58304f7b177ac1d16d7671dc67efb9/raw/docker-mysql-restore.sh

.env

MYSQL_DATABASE=example
MYSQL_ROOT_PASSWORD=123456
# Change to the script's directory & create directory
cd $(dirname "$(readlink -f "$0")")
mkdir -p ./dbdumps
# Start mysql service of your compose file
docker-compose up -d mysql
# Load database name + root password
source .env
# Delete old Backups
find ./dbdumps/* -atime +7 -exec rm {} \;
docker-compose exec -T mysql /usr/bin/mysqldump -u root \
--password=$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE \
| gzip --rsyncable > ./dbdumps/`date +\%Y\%m\%d`-$MYSQL_DATABASE.sql.gz
# Start mysql service of your compose file
docker-compose up -d mysql
# Load database name + root password
source .env
# Find latest dump
latest=$(ls -1t ./dbdumps/*.sql.gz | head -1)
# Restore
gunzip $latest | docker-compose exec -T mysql \
/usr/bin/mysql -u root --password=${MYSQL_ROOT_PASSWORD} ${MYSQL_DATABASE}
@sagaban
Copy link

sagaban commented May 20, 2021

I just had a problem with source .env , but replacing this line with . ./.env worked. Thanks for these scripts, amazing work!

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