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.

158 lines
3.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. *** Build new images
  8. Get the last upstream images versions with:
  9. #+begin_src sh
  10. actions/upstream-versions -l 10
  11. #+end_src
  12. Then use these function to help you manage the ~upgrade/build~
  13. directory and create the ~*-myc~ images from upstream images:
  14. #+begin_src sh
  15. MONGO_VERSION=$(
  16. docker-tags-fetch nextcloud -l 10 -f "^[0-9]+\.[0-9]+\.[0-9]+$" |
  17. sort -V |
  18. tail -n 1 |
  19. cut -f 1 -d "-"
  20. )
  21. mk_build_dir() {
  22. local new_version="$1" VLV VV T
  23. (
  24. cd upgrade/build
  25. MONGO_LAST_VERSION=$(
  26. find . -maxdepth 1 -mindepth 1 -type d -regex "./[0-9]+\.[0-9]+\.[0-9]+" -printf "%f\n" |
  27. sort -V | tail -n 1)
  28. for variant in ""; do
  29. if [ -z "$variant" ]; then
  30. VLV="${MONGO_LAST_VERSION}"
  31. VV="${new_version}"
  32. T="${new_version}-myc"
  33. else
  34. VLV="${MONGO_LAST_VERSION}-$variant"
  35. VV="${new_version}-$variant"
  36. T="${new_version}-myc-$variant"
  37. fi
  38. cp -r "${VLV}" "${VV}"
  39. sed -ri "s/FROM mongo:.*/FROM mongo:${new_version}/g" "$VV"/Dockerfile
  40. docker build "$VV" -t docker.0k.io/mongo:${T} || return 1
  41. docker push "docker.0k.io/mongo:${T}" || return 1
  42. done
  43. )
  44. }
  45. mk_build_dir "$MONGO_VERSION"
  46. #+end_src
  47. ** General Access
  48. You can access mondo db, if running, through
  49. #+begin_src sh
  50. SERVICENAME=mongo
  51. compose exec "$SERVICENAME" /usr/bin/mongo --quiet --eval "some js code"
  52. #+end_src
  53. if not running, you might want to run it.
  54. The following could be added to actions
  55. *** list databases
  56. #+begin_src sh
  57. SERVICENAME=mongo
  58. compose exec -T mongo /usr/bin/mongo --quiet \
  59. --eval "JSON.stringify(db.adminCommand({listDatabases: 1}))" | \
  60. jq -r '.databases[] | .name'
  61. #+end_src
  62. *** Dump database
  63. This one will use the probably already builded image from your project on your
  64. host to run a competing mongo with a host directory to store the dump.
  65. #+begin_src sh
  66. PROJECTNAME=myc
  67. SERVICENAME=mongo
  68. docker run --rm --name mongo_client -ti \
  69. -v /tmp/mongo:/tmp/mongo --network "${PROJECTNAME}_default" \
  70. -u root \
  71. --entrypoint /bin/sh "${PROJECTNAME}_$SERVICENAME"
  72. #+end_src
  73. Then inside docker:
  74. #+begin_src sh
  75. apk add mongodb-tools
  76. #+end_src
  77. Then for each databases:
  78. #+begin_src sh
  79. DBNAME="xxx"
  80. cd /tmp/mongo
  81. mongodump -h mongo -d "$DBNAME" -o mdump/
  82. #+end_src
  83. *** Drop database
  84. #+begin_src sh
  85. DBNAME=xxx
  86. PROJECTNAME=myc
  87. SERVICENAME=mongo
  88. docker exec -i "${PROJECTNAME}_${SERVICE}_1" /usr/bin/mongo --quiet < \
  89. <(echo "
  90. use $DBNAME;
  91. db.dropDatabase();
  92. ")
  93. #+end_src
  94. Note: if some connection are active to the database, it might not be
  95. deleted, you should check by relisting the database.
  96. *** Restore database
  97. #+begin_src sh
  98. SOURCE_DIR=/tmp/gogocarto
  99. docker run --rm -ti \
  100. -v "$SOURCE_DIR":/tmp/backups/gogocarto \
  101. -w /tmp/backups \
  102. --entrypoint mongorestore \
  103. --network myc_default \
  104. myc_mongo \
  105. --host rs01/mongo /tmp/backups/
  106. #+end_src
  107. There should not have any errors.
  108. Please note that '/tmp/backups' in the last example should contain
  109. one directory per database.
  110. Note also that you should drop database (see previous section) prior
  111. to restore them.