Advertisement
obernardovieira

Backup&Restore docker container data (step-by-step)

Mar 21st, 2017
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. 1. CREATE A NETWORK
  2.  
  3. $ docker network create mysql
  4.  
  5.  
  6.  
  7. 2. CREATE A DATA CONTAINER
  8.  
  9. $ docker create \
  10. --name mysql-data \
  11. --volume /var/lib/mysql \
  12. --volume /backups \
  13. mysql:latest
  14.  
  15.  
  16.  
  17. 3. CREATE THE REAL CONTAINER TO USE
  18.  
  19. $ docker run \
  20. --detach \
  21. --env MYSQL_ROOT_PASSWORD=<your-mysql-password> \
  22. --env MYSQL_DATABASE=test \
  23. --name mysql-server \
  24. --net mysql \
  25. --volumes-from mysql-data \
  26. mysql:latest
  27.  
  28.  
  29.  
  30. 4. INSERT SOME DATA
  31.  
  32. $ docker exec -it mysql-server bash
  33.  
  34. # mysql -p
  35.  
  36. > USE test
  37. > CREATE TABLE dog(age INT, name TEXT);
  38. > INSERT INTO dog(age, name) VALUES(5, "bolt");
  39.  
  40. > \q
  41. # CTRL+D
  42.  
  43.  
  44.  
  45.  
  46.  
  47. 5. MAKE A MYSQL BACKUP (THIS IS STILL INSIDE THE CONTAINER)
  48.  
  49. $ docker run \
  50. --rm \
  51. --net mysql \
  52. --volumes-from mysql-data \
  53. mysql \
  54. bash -c "mysqldump -p<your-mysql-password> -h mysql-server --databases test > /backups/test.sql"
  55.  
  56.  
  57.  
  58.  
  59. 6. EXPORT THE BACKUP INSIDE TO THE OUTSIDE
  60.  
  61. $ docker run \
  62. --rm \
  63. --volumes-from mysql-data \
  64. carinamarina/backup \
  65. backup \
  66. --source /backups/ \
  67. --stdout \
  68. --zip > my-local-backup.tar.gz
  69.  
  70.  
  71.  
  72.  
  73. 7. DELETE THOSE CONTAINERS
  74.  
  75. $ docker stop mysql-server
  76. $ docker rm mysql-server mysql-data
  77.  
  78.  
  79.  
  80.  
  81.  
  82. 8. REPEAT SETEP 2 AND 3 TO CREATE THEM AGAIN
  83. IF YOU WANT TO CHECK IF IT'S EMPTY USE:
  84.  
  85. $ docker exec -it mysql-server bash
  86.  
  87. # mysql -p
  88.  
  89. > SHOW DATABASES
  90.  
  91. YOU SHOULDN'T SEE test DATABASE
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101. 9. SEND BACK THE BACKUP (FROM OUTSIDE THE CONTAINER) TO THE CONTAINER (TO INSIDE THE CONTAINER)
  102.  
  103. docker run \
  104. --rm \
  105. --interactive \
  106. --volumes-from mysql-data \
  107. carinamarina/backup \
  108. restore \
  109. --destination /backups/ \
  110. --stdin \
  111. --zip \
  112. < my-local-backup.tar.gz
  113.  
  114.  
  115.  
  116.  
  117.  
  118. 10. IMPORT THE BACKUP DATA DO MYSQL DATABASE (IT HAPPENS INSIDE THE CONTAINER)
  119.  
  120. docker run \
  121. --rm \
  122. --net mysql \
  123. --volumes-from mysql-data \
  124. mysql:latest \
  125. bash -c "mysql -p<your-mysql-password> -h mysql-server < /backups/test.sql"
  126.  
  127.  
  128.  
  129.  
  130. 11. CHECK IF IT'S OK
  131.  
  132. $ docker exec -it mysql-server bash
  133.  
  134. # mysql -p
  135.  
  136. > SHOW DATABASES
  137. > USE test
  138. > SHOW TABLES
  139. > SELECT * FROM dog;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement