Skip to content

Instantly share code, notes, and snippets.

@juandm
Created July 28, 2020 00:58
Show Gist options
  • Save juandm/92150a0f39b1213d4849525a5ebef165 to your computer and use it in GitHub Desktop.
Save juandm/92150a0f39b1213d4849525a5ebef165 to your computer and use it in GitHub Desktop.

Dump MySQL database from Production and Restore into a local Database in docker container

Reference

https://hub.docker.com/_/mysql/

  1. Dump using mysqlpump.
$ mysqlpump -h your.host.db.com -u user_name -p --skip-definer --set-gtid-purged=OFF  database_name > my_dump_file.sql
  1. Restore
  • If you wanna change the name from Production to Dev environment replace all ocurrence of production database name in dump file.

    • Using nano

      $ nano my_dump_file.sql
      
      1. Press ctrl+\ to open replace menu.
      2. Digit production database name and press enter.
      3. Digit the new database name and press enter.
      4. To replace all ocurrences press A (All)
      5. Save (ctrl+S) and exit (ctrl+X)
  • After that run the restore

$ docker exec -i some-mysql-container sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/dump_file.sql
  1. Verify
$ docker exec -it some-mysql-container mysql -u user_name -pyour_password' -h localhost local_database_name

make some queries to the database and verify that the dump is ok

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