You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.4 KiB

  1. #+TITLE: Mongo Charm
  2. * Quick information
  3. ** Upgrade
  4. Since 4.0.6 based on alpine 3.9, alpine is not packaging mongo
  5. anymore. So we use the official version (that is huge). We still
  6. must change entrypoint to enforce proper directory paths.
  7. A ``Dockerfile`` is provided for this mean.
  8. You can fetch latest official tags:
  9. #+begin_quote
  10. docker-tags-fetch mongo -l 10 -f '^[0-9]+\.[0-9]+\.[0-9]+$'
  11. #+end_quote
  12. change it in docker file, then
  13. #+begin_quote
  14. docker build . -t docker.0k.io/mongo:"$TAG"-myc
  15. #+end_quote
  16. ** General Access
  17. You can access mondo db, if running, through
  18. #+begin_src sh
  19. SERVICENAME=mongo
  20. compose exec "$SERVICENAME" /usr/bin/mongo --quiet --eval "some js code"
  21. #+end_src
  22. if not running, you might want to run it.
  23. The following could be added to actions
  24. *** list databases
  25. #+begin_src sh
  26. SERVICENAME=mongo
  27. compose exec -T mongo /usr/bin/mongo --quiet \
  28. --eval "JSON.stringify(db.adminCommand({listDatabases: 1}))" | \
  29. jq -r '.databases[] | .name'
  30. #+end_src
  31. *** Dump database
  32. This one will use the probably already builded image from your project on your
  33. host to run a competing mongo with a host directory to store the dump.
  34. #+begin_src sh
  35. PROJECTNAME=myc
  36. SERVICENAME=mongo
  37. docker run --rm --name mongo_client -ti \
  38. -v /tmp/mongo:/tmp/mongo --network "${PROJECTNAME}_default" \
  39. -u root \
  40. --entrypoint /bin/sh "${PROJECTNAME}_$SERVICENAME"
  41. #+end_src
  42. Then inside docker:
  43. #+begin_src sh
  44. apk add mongodb-tools
  45. #+end_src
  46. Then for each databases:
  47. #+begin_src sh
  48. DBNAME="xxx"
  49. cd /tmp/mongo
  50. mongodump -h mongo -d "$DBNAME" -o mdump/
  51. #+end_src
  52. *** Drop database
  53. #+begin_src sh
  54. DBNAME=xxx
  55. PROJECTNAME=myc
  56. SERVICENAME=mongo
  57. docker exec -i "${PROJECTNAME}_${SERVICE}_1" /usr/bin/mongo --quiet < \
  58. <(echo "
  59. use $DBNAME;
  60. db.dropDatabase();
  61. ")
  62. #+end_src
  63. Note: if some connection are active to the database, it might not be
  64. deleted, you should check by relisting the database.
  65. *** Restore database
  66. #+begin_src sh
  67. SOURCE_DIR=/tmp/gogocarto
  68. docker run --rm -ti \
  69. -v "$SOURCE_DIR":/tmp/backups/gogocarto \
  70. -w /tmp/backups \
  71. --entrypoint mongorestore \
  72. --network myc_default \
  73. myc_mongo \
  74. --host rs01/mongo /tmp/backups/
  75. #+end_src
  76. There should not have any errors.
  77. Please note that '/tmp/backups' in the last example should contain
  78. one directory per database.
  79. Note also that you should drop database (see previous section) prior
  80. to restore them.