mongodb-control 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/bin/bash
  2. # get wekan/mongo settings
  3. source $SNAP/bin/wekan-read-settings
  4. if [ "true" == "${DISABLE_MONGODB}" ]; then
  5. echo "mongodb is disabled. Stop service"
  6. snapctl stop --disable ${SNAP_NAME}.mongodb
  7. exit 0
  8. fi
  9. # make sure we have set minimum env variables for locale
  10. if [ -z "$LANG" ]; then
  11. export LANG=en_US.UTF-8
  12. fi
  13. export LC_ALL=C
  14. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnu
  15. if test -f "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"; then
  16. touch "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"
  17. # Stop MongoDB
  18. touch "$SNAP_COMMON/02-disable-mongo.txt"
  19. snapctl stop --disable ${SNAP_NAME}.mongodb
  20. touch "$SNAP_COMMON/03-eval-stop-mongo.txt"
  21. mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  22. # Start MongoDB 4.4
  23. touch "$SNAP_COMMON/04-start-mongo44.txt"
  24. $SNAP/mongo44bin/mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/02_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet
  25. # Wait MongoDB 4.4 to start
  26. touch "$SNAP_COMMON/05-wait-2s-mongo44-start.txt"
  27. sleep 2s
  28. # Dump Old MongoDB 3.x database
  29. touch "$SNAP_COMMON/06-dump-database.txt"
  30. (cd $SNAP_COMMON && mongodump --port ${MONGODB_PORT})
  31. # Stop MongoDB 4.4
  32. touch "$SNAP_COMMON/07-stop-mongo44.txt"
  33. $SNAP/mongo44bin/mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  34. # Wait MongoDB 4.4 to stop
  35. touch "$SNAP_COMMON/08-wait-2s-mongo44-stop.txt"
  36. sleep 2s
  37. # Start MongoDB 5
  38. touch "$SNAP_COMMON/09-start-mongo5.txt"
  39. mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/10_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet
  40. # Restore database
  41. touch "$SNAP_COMMON/11-mongorestore-to-mongo5.txt"
  42. (cd $SNAP_COMMON && mongorestore --port ${MONGODB_PORT})
  43. # Wait 5s
  44. touch "$SNAP_COMMON/12-wait-5s-after-restore.txt"
  45. sleep 5s
  46. # Shutdown mongodb
  47. touch "$SNAP_COMMON/13-shutdown-mongodb.txt"
  48. mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS
  49. touch "$SNAP_COMMON/14-wait-5s-after-mongo5-shutdown.txt"
  50. sleep 5s
  51. # Enable MongoDB 5
  52. touch "$SNAP_COMMON/15-enable-mongo-5.txt"
  53. snapctl start --enable ${SNAP_NAME}.mongodb
  54. fi
  55. # When starting MongoDB, if logfile exist, delete it, because now uses syslog instead of logfile,
  56. # because syslog usually already has log rotation.
  57. # https://github.com/wekan/wekan-snap/issues/92
  58. if test -f "$SNAP_COMMON/mongodb.log"; then
  59. rm -f "$SNAP_COMMON/mongodb.log"
  60. fi
  61. # Alternative: When starting MongoDB, and using logfile, truncate log to last 1000 lines of text.
  62. # 1) If file exists:
  63. #if test -f "$SNAP_COMMON/mongodb.log"; then
  64. # # 2) Copy last 1000 lines to variable loglast1000lines.
  65. # loglast1000lines=$(tail -1000 "$SNAP_COMMON/mongodb.log")
  66. # # 3) Copy variable to replace original MongoDB log.
  67. # echo "$loglast1000lines" > "$SNAP_COMMON/mongodb.log"
  68. # # 4) Set variable to be empty.
  69. # loglast1000lines=""
  70. #fi
  71. if [ -z "$MONGO_URL" ]; then
  72. # start mongo deamon
  73. BIND_OPTIONS=""
  74. if [ "nill" != "$MONGODB_BIND_UNIX_SOCKET" ] && [ "x" != "x${MONGODB_BIND_UNIX_SOCKET}" ]; then
  75. BIND_OPTIONS+=" --unixSocketPrefix $MONGODB_BIND_UNIX_SOCKET"
  76. fi
  77. if [ "x" != "x${MONGODB_BIND_IP}" ]; then
  78. BIND_OPTIONS+=" --bind_ip $MONGODB_BIND_IP"
  79. fi
  80. if [ "x" != "x${MONGODB_PORT}" ]; then
  81. BIND_OPTIONS+=" --port $MONGODB_PORT"
  82. fi
  83. echo "mongodb bind options: $BIND_OPTIONS"
  84. if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
  85. echo "Sending mongodb logs to syslog"
  86. mongod --dbpath $SNAP_COMMON --syslog --journal $BIND_OPTIONS --quiet
  87. exit 0
  88. fi
  89. if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
  90. echo "Sending mongodb logs to $SNAP_COMMON"
  91. mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $BIND_OPTIONS --quiet
  92. fi
  93. if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
  94. echo "Sending mongodb logs to /dev/null"
  95. mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $BIND_OPTIONS --quiet
  96. fi
  97. # Drop indexes on database upgrade, when starting MongoDB
  98. #mongo wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
  99. mongo wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' $BIND_OPTIONS
  100. # Delete incomplete uploads so that they would not prevent starting WeKan
  101. mongo wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' $BIND_OPTIONS
  102. else
  103. if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then
  104. echo "Sending mongodb logs to syslog"
  105. mongod --dbpath $SNAP_COMMON --syslog --journal $MONGO_URL --quiet
  106. fi
  107. if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then
  108. echo "Sending mongodb logs to $SNAP_COMMON"
  109. mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $MONGO_URL --quiet
  110. fi
  111. if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then
  112. echo "Sending mongodb logs to /dev/null"
  113. mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $MONGO_URL --quiet
  114. fi
  115. # Drop indexes on database upgrade, when starting MongoDB
  116. #mongo wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS
  117. mongo wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.2" });' $BIND_OPTIONS
  118. # Delete incomplete uploads so that they would not prevent starting WeKan
  119. mongo wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' $BIND_OPTIONS
  120. fi