wekan-snap-upgrade-not-working.sh 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/bin/bash
  2. # ==== SETTINGS START ======
  3. MONGODB_PORT=27019
  4. # MongoDB database name.
  5. DATABASE=wekan
  6. # Usually original snap name is wekan. Check with "sudo snap list".
  7. # Migrating changes snap wekan from stable to candidate, and migrates attachments and avatars.
  8. SNAPNAME=wekan
  9. # Backup directory requires a lot of disk space. It will have copy of /var/snap/wekan/common and mongodump.
  10. BACKUPDIR=/var/wekanbackup
  11. # ==== SETTINGS END ======
  12. if [ "$(id -u)" -ne 0 ]; then
  13. echo 'This script must be run by root' >&2
  14. exit 1
  15. fi
  16. #cpuavx=$(cat /proc/cpuinfo | grep avx)
  17. #if [ -z "${cpuavx}" ]; then
  18. # echo "Your CPU does not support AVX. WeKan will add support for AVX soon, by running MongoDB 6 at Qemu."
  19. # exit 1
  20. #fi
  21. function pause(){
  22. read -p "$*"
  23. }
  24. echo "Backups will be stored to $BACKUPDIR . Change it at top of this script, to where is enough disk space."
  25. echo "Backup directory requires a lot of disk space. It will have copy of /var/snap/wekan/common and mongodump."
  26. PS3='Please enter your choice: '
  27. options=("Upgrade WeKan Snap from 6.09 to newest" "Downgrade WeKan Snap from newest to 6.09" "Quit")
  28. select opt in "${options[@]}"
  29. do
  30. case $opt in
  31. "Upgrade WeKan Snap from 6.09 to newest")
  32. echo "STEP 1: BACKUP"
  33. # Original path
  34. export ORIGINAL_LD_LIBRARY_PATH=$LD_LIBRARY_PATH
  35. export ORIGINAL_PATH=$PATH
  36. # Path to old MongoDB
  37. export LD_LIBRARY_PATH=/snap/$SNAPNAME/current/lib/x86_64-linux-gnu:$LD_LIBRARY_PATH
  38. export PATH=/snap/$SNAPNAME/current/bin:/snap/$SNAPNAME/current/usr/bin:$PATH
  39. # Stop Wekan
  40. snap stop $SNAPNAME.wekan
  41. snap start $SNAPNAME.mongodb
  42. sleep 2
  43. # Create backup directory
  44. mkdir -p $BACKUPDIR
  45. # Run MongoDB backup
  46. rm -rf $BACKUPDIR/common $BACKUPDIR/dump
  47. /snap/$SNAPNAME/current/bin/mongodump --port $MONGODB_PORT --out=$BACKUPDIR
  48. snap stop $SNAPNAME.mongodb
  49. sleep 2
  50. cp -pR /var/snap/$SNAPNAME/common $BACKUPDIR/
  51. snap get $SNAPNAME > $BACKUPDIR/snap.sh
  52. echo "STEP 2: UPGRADE WeKan"
  53. # Stop WeKan
  54. snap stop $SNAPNAME
  55. # Remove old files (after backup)
  56. rm -rf /var/snap/$SNAPNAME/common/*
  57. # Upgrade to latest version
  58. snap refresh $SNAPNAME --channel=latest/candidate --amend
  59. mkdir -p /var/snap/$SNAPNAME/common/files/attachments
  60. mkdir -p /var/snap/$SNAPNAME/common/files/avatars
  61. # Stop all services for clean start
  62. echo "STEP 3: RESTORE DATA"
  63. # Start MongoDB
  64. snap stop $SNAPNAME.wekan
  65. snap start $SNAPNAME.mongodb
  66. sleep 2
  67. # Restore database
  68. /snap/$SNAPNAME/current/bin/mongorestore --port $MONGODB_PORT --drop $BACKUPDIR/
  69. echo "STEP 4: MIGRATE ATTACHMENTS"
  70. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  71. --db wekan --eval "db.attachments.files.drop()"
  72. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  73. --db wekan --eval "db.attachments.chunks.drop()"
  74. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  75. --db wekan --eval "db.avatars.files.drop()"
  76. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  77. --db wekan --eval "db.avatars.chunks.drop()"
  78. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  79. --db wekan --eval "db.cfs_gridfs.attachments.chunks.renameCollection('attachments.chunks')"
  80. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  81. --db wekan --eval "db.cfs_gridfs.attachments.files.renameCollection('attachments.files')"
  82. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  83. --db wekan --eval "db.cfs_gridfs.avatars.chunks.renameCollection('avatars.chunks')"
  84. /snap/$SNAPNAME/current/usr/bin/mongosh --port $MONGODB_PORT --host 127.0.0.1 \
  85. --db wekan --eval "db.cfs_gridfs.avatars.files.renameCollection('avatars.files')"
  86. /snap/$SNAPNAME/current/bin/mongoexport --port $MONGODB_PORT --host 127.0.0.1 \
  87. --db wekan --collection cfs.attachments.filerecord > $BACKUPDIR/old_files.json
  88. while IFS= read -r line; do
  89. # Extract key from copies.attachments
  90. key=$(echo "$line" | grep -o '"key"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  91. if [ ! -z "$key" ]; then
  92. echo "Processing file with key: $key"
  93. # Extract all metadata
  94. name=$(echo "$line" | grep -o '"original"[[:space:]]*:[[:space:]]*{[^}]*}' | grep -o '"name"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  95. type=$(echo "$line" | grep -o '"original"[[:space:]]*:[[:space:]]*{[^}]*}' | grep -o '"type"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  96. size=$(echo "$line" | grep -o '"size"[[:space:]]*:[[:space:]]*[0-9]*' | cut -d':' -f2 | tr -d ' ' | head -1)
  97. boardId=$(echo "$line" | grep -o '"boardId"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  98. cardId=$(echo "$line" | grep -o '"cardId"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  99. userId=$(echo "$line" | grep -o '"userId"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  100. swimlaneId=$(echo "$line" | grep -o '"swimlaneId"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  101. listId=$(echo "$line" | grep -o '"listId"[[:space:]]*:[[:space:]]*"[^"]*"' | cut -d'"' -f4)
  102. # Determine file extension
  103. ext=$(echo "$name" | awk -F. '{print $NF}' | tr '[:upper:]' '[:lower:]')
  104. # Determine file type
  105. isPDF=$(echo "$type" | grep -q "pdf" && echo "true" || echo "false")
  106. isImage=$(echo "$type" | grep -E -- "image|png|jpg|jpeg|gif|bmp|tiff|svg|webp|pcx" && echo "true" || echo "false")
  107. isVideo=$(echo "$type" | grep -E -- "video|mp4|m4p|m4v|m4mov|qt|wmv|avi|mpeg|mpg|mp2|mpe|flv|webm|mkv|flv|ogg|mts|m2ts|ts|gifv" && echo "true" || echo "false")
  108. echo "Creating new format for: $name"
  109. # Create new record in new format
  110. echo "{
  111. \"_id\": \"$key\",
  112. \"size\": $size,
  113. \"type\": \"$type\",
  114. \"name\": \"$name\",
  115. \"meta\": {
  116. \"boardId\": \"$boardId\",
  117. \"swimlaneId\": \"$swimlaneId\",
  118. \"listId\": \"$listId\",
  119. \"cardId\": \"$cardId\"
  120. },
  121. \"ext\": \"$ext\",
  122. \"extension\": \"$ext\",
  123. \"extensionWithDot\": \".$ext\",
  124. \"mime\": \"$type\",
  125. \"mime-type\": \"$type\",
  126. \"userId\": \"$userId\",
  127. \"path\": \"/var/snap/$SNAPNAME/common/files/attachments/$key.$ext\",
  128. \"versions\": {
  129. \"original\": {
  130. \"path\": \"/var/snap/$SNAPNAME/common/files/attachments/$key-original-$name\",
  131. \"size\": $size,
  132. \"type\": \"$type\",
  133. \"extension\": \"$ext\",
  134. \"storage\": \"gridfs\",
  135. \"meta\": {
  136. \"gridFsFileId\": \"$key\"
  137. }
  138. }
  139. },
  140. \"_downloadRoute\": \"/cdn/storage\",
  141. \"_collectionName\": \"attachments\",
  142. \"isVideo\": false,
  143. \"isAudio\": false,
  144. \"isImage\": $isImage,
  145. \"isText\": false,
  146. \"isJSON\": false,
  147. \"isPDF\": $isPDF,
  148. \"_storagePath\": \"/var/snap/$SNAPNAME/common/files/attachments\",
  149. \"public\": false
  150. }" > "$BACKUPDIR/new_$key.json"
  151. echo "Importing to new format..."
  152. /snap/$SNAPNAME/current/bin/mongoimport --port $MONGODB_PORT --host 127.0.0.1 \
  153. --db wekan --collection attachments \
  154. --file="$BACKUPDIR/new_$key.json"
  155. echo "Completed migration for: $name"
  156. echo "--------------------------"
  157. fi
  158. done < $BACKUPDIR/old_files.json
  159. snap start $SNAPNAME
  160. echo "Upgrading completed."
  161. break
  162. ;;
  163. "Downgrade WeKan Snap from newest to 6.09")
  164. echo "Downgrading WeKan Snap from newest to 6.09."
  165. snap stop $SNAPNAME
  166. rm -rf /var/snap/$SNAPNAME/common/*
  167. snap refresh $SNAPNAME --channel=latest/stable --amend
  168. snap stop $SNAPNAME
  169. cp -pR $BACKUPDIR/common/* /var/snap/$SNAPNAME/common/
  170. sudo snap start $SNAPNAME
  171. echo "Downgrading completed."
  172. break
  173. ;;
  174. "Quit")
  175. break
  176. ;;
  177. *) echo invalid option;;
  178. esac
  179. done