| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 | #!/bin/bash# get wekan/mongo settingssource $SNAP/bin/wekan-read-settingsif [ "true" == "${DISABLE_MONGODB}" ]; then    echo "mongodb is disabled. Stop service"    snapctl stop --disable ${SNAP_NAME}.mongodb    exit 0fi# make sure we have set minimum env variables for localeif [ -z "$LANG" ]; then    export LANG=en_US.UTF-8fiexport LC_ALL=Cexport LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/snap/${SNAP_NAME}/current/lib/x86_64-linux-gnuif test -f "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"; then   touch "$SNAP_COMMON/01-migrate-mongo3-to-mongo5.txt"   # Stop MongoDB   touch "$SNAP_COMMON/02-disable-mongo.txt"   snapctl stop --disable ${SNAP_NAME}.mongodb   touch "$SNAP_COMMON/03-eval-stop-mongo.txt"   mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS   # Start MongoDB 4.4   touch "$SNAP_COMMON/04-start-mongo44.txt"   $SNAP/mongo44bin/mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/02_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet   # Wait MongoDB 4.4 to start   touch "$SNAP_COMMON/05-wait-2s-mongo44-start.txt"   sleep 2s   # Dump Old MongoDB 3.x database   touch "$SNAP_COMMON/06-dump-database.txt"   (cd $SNAP_COMMON && mongodump --port ${MONGODB_PORT})   # Stop MongoDB 4.4   touch "$SNAP_COMMON/07-stop-mongo44.txt"   $SNAP/mongo44bin/mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS   # Wait MongoDB 4.4 to stop   touch "$SNAP_COMMON/08-wait-2s-mongo44-stop.txt"   sleep 2s   # Start MongoDB 5   touch "$SNAP_COMMON/09-start-mongo5.txt"   mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/10_mongodb_log_while_migrate.txt --logappend --journal $MONGO_URL --quiet   # Restore database   touch "$SNAP_COMMON/11-mongorestore-to-mongo5.txt"   (cd $SNAP_COMMON && mongorestore --port ${MONGODB_PORT})   # Wait 5s   touch "$SNAP_COMMON/12-wait-5s-after-restore.txt"   sleep 5s   # Shutdown mongodb   touch "$SNAP_COMMON/13-shutdown-mongodb.txt"   mongo wekan --eval "db.getSiblingDB('admin').shutdownServer()" $BIND_OPTIONS   touch "$SNAP_COMMON/14-wait-5s-after-mongo5-shutdown.txt"   sleep 5s   # Enable MongoDB 5   touch "$SNAP_COMMON/15-enable-mongo-5.txt"   snapctl start --enable ${SNAP_NAME}.mongodbfi# When starting MongoDB, if logfile exist, delete it, because now uses syslog instead of logfile,# because syslog usually already has log rotation.# https://github.com/wekan/wekan-snap/issues/92if test -f "$SNAP_COMMON/mongodb.log"; then   rm -f "$SNAP_COMMON/mongodb.log"fi# Alternative: When starting MongoDB, and using logfile, truncate log to last 1000 lines of text.# 1) If file exists:#if test -f "$SNAP_COMMON/mongodb.log"; then#    # 2) Copy last 1000 lines to variable loglast1000lines.#    loglast1000lines=$(tail -1000 "$SNAP_COMMON/mongodb.log")#    # 3) Copy variable to replace original MongoDB log.#    echo "$loglast1000lines" > "$SNAP_COMMON/mongodb.log"#    # 4) Set variable to be empty.#    loglast1000lines=""#fiif [ -z "$MONGO_URL" ]; then    # Disable MongoDB telemetry and free monitoring    mongo --eval "disableTelemetry()"    mongo --eval "db.disableFreeMonitoring()"    # Snap: Disable apparmor="DENIED" at syslog    # https://github.com/wekan/wekan/issues/4855    mongo wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' $BIND_OPTIONS    # start mongo deamon    BIND_OPTIONS=""    if [ "nill" != "$MONGODB_BIND_UNIX_SOCKET" ] && [ "x" != "x${MONGODB_BIND_UNIX_SOCKET}" ]; then        BIND_OPTIONS+=" --unixSocketPrefix  $MONGODB_BIND_UNIX_SOCKET"    fi    if [ "x" != "x${MONGODB_BIND_IP}" ]; then        BIND_OPTIONS+=" --bind_ip $MONGODB_BIND_IP"    fi    if [ "x" != "x${MONGODB_PORT}" ]; then        BIND_OPTIONS+=" --port $MONGODB_PORT"    fi    echo "mongodb bind options: $BIND_OPTIONS"    if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then        echo "Sending mongodb logs to syslog"        mongod --dbpath $SNAP_COMMON --syslog --journal $BIND_OPTIONS --quiet        exit 0    fi    if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then        echo "Sending mongodb logs to $SNAP_COMMON"        mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $BIND_OPTIONS --quiet    fi    if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then        echo "Sending mongodb logs to /dev/null"        mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $BIND_OPTIONS --quiet    fi    # Drop indexes on database upgrade, when starting MongoDB    #mongo wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS    mongo wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.4" });' $BIND_OPTIONS    # Delete incomplete uploads so that they would not prevent starting WeKan    mongo wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' $BIND_OPTIONSelse    # Disable MongoDB telemetry and free monitoring    mongo --eval "disableTelemetry()"    mongo --eval "db.disableFreeMonitoring()"    # Snap: Disable apparmor="DENIED" at syslog    # https://github.com/wekan/wekan/issues/4855    mongo wekan --eval 'db.adminCommand({ setParameter: 1, diagnosticDataCollectionEnabled: false});' $BIND_OPTIONS    if [ "syslog" == "${MONGO_LOG_DESTINATION}" ]; then        echo "Sending mongodb logs to syslog"        mongod --dbpath $SNAP_COMMON --syslog --journal $MONGO_URL --quiet    fi    if [ "snapcommon" == "${MONGO_LOG_DESTINATION}" ]; then        echo "Sending mongodb logs to $SNAP_COMMON"        mongod --dbpath $SNAP_COMMON --logpath $SNAP_COMMON/mongodb.log --logappend --journal $MONGO_URL --quiet    fi    if [ "devnull" == "${MONGO_LOG_DESTINATION}" ]; then        echo "Sending mongodb logs to /dev/null"        mongod --dbpath $SNAP_COMMON --logpath /dev/null --journal $MONGO_URL --quiet    fi    # Drop indexes on database upgrade, when starting MongoDB    #mongo wekan --eval "db.getCollectionNames().forEach(function(col_name) { var coll = db.getCollection(col_name); coll.dropIndexes(); });" $BIND_OPTIONS    mongo wekan --eval 'db.adminCommand({ setFeatureCompatibilityVersion: "4.2" });' $BIND_OPTIONS   # Delete incomplete uploads so that they would not prevent starting WeKan    mongo wekan --eval 'db.getCollection("cfs.attachments.filerecord").find( { "uploadedAt": { "$exists": true }, "copies.attachments" : null,"failures.copies.attachments.doneTrying" : {"$ne" : true}});' $BIND_OPTIONSfi
 |