Skip to content

Instantly share code, notes, and snippets.

@SvS30
Last active December 4, 2021 17:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SvS30/0a71fd7416c936414346842f16bb0616 to your computer and use it in GitHub Desktop.
Save SvS30/0a71fd7416c936414346842f16bb0616 to your computer and use it in GitHub Desktop.
commands to backup or restore a mysql instance in a docker container
# I use the '$' symbol to indicate that they are variables, change those values.
# Backup
docker exec $name-container mysqldump -u $user --password=$pwd $database > $file-name.sql
# Restore
cat $file-name.sql | docker exec -i $name-container mysql -u $user --password=$pwd $database
# if you don't want to type your password by terminal, or are concerned about mysql warnings.
# You can do this:
# Step 1: enter your container:
docker exec -it $name-container bash
# Step 2: use mysql editor
mysql_config_editor set --login-path=local --host=localhost --user=$username --password
# this will create a file in your container, with your credentials and use it for login.
# Now to log into a restore application:
# if your .sql doesn't create the database, it should specify it in $database.
cat $file-name.sql | docker exec -i $name-container mysql --login-path=local $database
# or to make a backup:
docker exec $name-container mysqldump --login-path=local $database > $file-name.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment