Skip to content

Instantly share code, notes, and snippets.

@PieterScheffers
Forked from spalladino/mysql-docker.sh
Created January 31, 2018 22:05
Show Gist options
  • Save PieterScheffers/2a5f46bc1949601d175f66d7c8458c46 to your computer and use it in GitHub Desktop.
Save PieterScheffers/2a5f46bc1949601d175f66d7c8458c46 to your computer and use it in GitHub Desktop.
Backup and restore a mysql database from a running Docker mysql container
# Backup
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
# Restore
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE
@PieterScheffers
Copy link
Author

docker run -it --rm --link my-old-database:mysql mysql sh -c 'echo "[client]\n host=\"$MYSQL_PORT_3306_TCP_ADDR\"\n user=root\n password=\"$MYSQL_ENV_MYSQL_ROOT_PASSWORD\"" > my.cnf && exec mysqldump --defaults-file=my.cnf --databases my_database_name' > db_dump.sql

@PieterScheffers
Copy link
Author

docker exec -i my-new-database mysql -uroot -pnew-db-password --force < db_dump.sql

@PieterScheffers
Copy link
Author

docker run -it --rm --link my-new-database:mysql mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'

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