mongodb-migrate 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. #!/bin/bash
  2. # MongoDB Migration Script from version 3 to 7
  3. # This script handles migration with disk space checks, progress tracking, and error handling
  4. #
  5. # IMPORTANT: All operations are contained within SNAP_COMMON directory
  6. # This is the only writable directory in a snap environment
  7. set -e
  8. # Source settings
  9. source $SNAP/bin/wekan-read-settings
  10. # Migration configuration
  11. MIGRATION_LOG="${SNAP_COMMON}/mongodb-migration-log.txt"
  12. MIGRATION_STATUS="${SNAP_COMMON}/mongodb-migration-status.json"
  13. MIGRATION_PROGRESS="${SNAP_COMMON}/mongodb-migration-progress.html"
  14. REVERT_FILE="${SNAP_COMMON}/revert-mongodb-migration.txt"
  15. TEMP_DIR="${SNAP_COMMON}/mongodb-migration-temp"
  16. BACKUP_DIR="${SNAP_COMMON}/mongodb-backup-$(date +%Y%m%d-%H%M%S)"
  17. # MongoDB paths
  18. MONGO3_BIN="/snap/${SNAP_NAME}/current/migratemongo/bin"
  19. MONGO7_BIN="/snap/${SNAP_NAME}/current/bin"
  20. MONGO3_LIB="/snap/${SNAP_NAME}/current/migratemongo/lib"
  21. MONGO7_LIB="/snap/${SNAP_NAME}/current/usr/lib"
  22. # Set up environment for MongoDB 3 tools
  23. export LD_LIBRARY_PATH="${MONGO3_LIB}:${MONGO3_LIB}/x86_64-linux-gnu:${LD_LIBRARY_PATH}"
  24. export PATH="${MONGO3_BIN}:${MONGO7_BIN}:${PATH}"
  25. # Set MongoDB log destination to snapcommon for log file detection
  26. export MONGO_LOG_DESTINATION="snapcommon"
  27. # Validate that all operations are within SNAP_COMMON
  28. validate_snap_common_path() {
  29. local path="$1"
  30. local description="$2"
  31. if [[ "$path" != "${SNAP_COMMON}"* ]]; then
  32. log_error "Path outside SNAP_COMMON detected: $path ($description)"
  33. log_error "SNAP_COMMON: $SNAP_COMMON"
  34. return 1
  35. fi
  36. return 0
  37. }
  38. # Validate all critical paths
  39. validate_all_paths() {
  40. log_message "Validating all paths are within SNAP_COMMON"
  41. validate_snap_common_path "$MIGRATION_LOG" "Migration log" || return 1
  42. validate_snap_common_path "$MIGRATION_STATUS" "Migration status" || return 1
  43. validate_snap_common_path "$MIGRATION_PROGRESS" "Migration progress" || return 1
  44. validate_snap_common_path "$REVERT_FILE" "Revert file" || return 1
  45. validate_snap_common_path "$TEMP_DIR" "Temporary directory" || return 1
  46. validate_snap_common_path "$BACKUP_DIR" "Backup directory" || return 1
  47. log_success "All paths validated within SNAP_COMMON"
  48. return 0
  49. }
  50. # Logging functions
  51. log_message() {
  52. local message="$1"
  53. local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
  54. echo "[$timestamp] $message" | tee -a "$MIGRATION_LOG"
  55. }
  56. log_error() {
  57. local message="$1"
  58. log_message "ERROR: $message"
  59. }
  60. log_success() {
  61. local message="$1"
  62. log_message "SUCCESS: $message"
  63. }
  64. # Disk space checking functions
  65. check_disk_space() {
  66. local required_space_gb="$1"
  67. local available_space_gb=$(df "$SNAP_COMMON" | awk 'NR==2 {print int($4/1024/1024)}')
  68. if [ "$available_space_gb" -lt "$required_space_gb" ]; then
  69. log_error "Insufficient disk space. Required: ${required_space_gb}GB, Available: ${available_space_gb}GB"
  70. return 1
  71. fi
  72. log_message "Disk space check passed. Available: ${available_space_gb}GB, Required: ${required_space_gb}GB"
  73. return 0
  74. }
  75. # Progress tracking functions
  76. update_progress() {
  77. local step="$1"
  78. local total_steps="$2"
  79. local description="$3"
  80. local percentage=$((step * 100 / total_steps))
  81. # Update JSON status file
  82. cat > "$MIGRATION_STATUS" << EOF
  83. {
  84. "step": $step,
  85. "total_steps": $total_steps,
  86. "percentage": $percentage,
  87. "description": "$description",
  88. "timestamp": "$(date -Iseconds)",
  89. "status": "running"
  90. }
  91. EOF
  92. # Update HTML progress page
  93. cat > "$MIGRATION_PROGRESS" << EOF
  94. <!DOCTYPE html>
  95. <html>
  96. <head>
  97. <title>MongoDB Migration Progress</title>
  98. <meta http-equiv="refresh" content="5">
  99. <style>
  100. body { font-family: Arial, sans-serif; margin: 40px; }
  101. .progress-bar { width: 100%; background-color: #f0f0f0; border-radius: 5px; }
  102. .progress-fill { height: 30px; background-color: #4CAF50; border-radius: 5px; width: ${percentage}%; }
  103. .status { margin: 20px 0; }
  104. .error { color: red; }
  105. .success { color: green; }
  106. </style>
  107. </head>
  108. <body>
  109. <h1>MongoDB Migration Progress</h1>
  110. <div class="progress-bar">
  111. <div class="progress-fill"></div>
  112. </div>
  113. <div class="status">
  114. <p><strong>Progress:</strong> $step of $total_steps steps ($percentage%)</p>
  115. <p><strong>Current Step:</strong> $description</p>
  116. <p><strong>Last Updated:</strong> $(date)</p>
  117. </div>
  118. <p><em>This page will refresh automatically every 5 seconds.</em></p>
  119. </body>
  120. </html>
  121. EOF
  122. log_message "Progress: $step/$total_steps ($percentage%) - $description"
  123. }
  124. # Estimate completion time
  125. estimate_completion_time() {
  126. local start_time="$1"
  127. local current_step="$2"
  128. local total_steps="$3"
  129. if [ "$current_step" -gt 0 ]; then
  130. local elapsed=$(($(date +%s) - start_time))
  131. local avg_time_per_step=$((elapsed / current_step))
  132. local remaining_steps=$((total_steps - current_step))
  133. local estimated_remaining=$((remaining_steps * avg_time_per_step))
  134. local hours=$((estimated_remaining / 3600))
  135. local minutes=$(((estimated_remaining % 3600) / 60))
  136. local seconds=$((estimated_remaining % 60))
  137. echo "${hours}h ${minutes}m ${seconds}s"
  138. else
  139. echo "Calculating..."
  140. fi
  141. }
  142. # Create backup before migration
  143. create_backup() {
  144. log_message "Creating backup of MongoDB 3 database"
  145. # Check disk space for backup (estimate 2x current database size)
  146. local db_size=$(du -s "${SNAP_COMMON}/wekan" 2>/dev/null | awk '{print $1}' || echo "0")
  147. local required_space=$((db_size * 2 / 1024 / 1024 + 1)) # Convert to GB and add 1GB buffer
  148. if ! check_disk_space "$required_space"; then
  149. log_error "Insufficient disk space for backup"
  150. return 1
  151. fi
  152. # Create backup directory
  153. mkdir -p "$BACKUP_DIR"
  154. # Copy database files
  155. if [ -d "${SNAP_COMMON}/wekan" ]; then
  156. cp -r "${SNAP_COMMON}/wekan" "$BACKUP_DIR/"
  157. log_success "Database backup created at $BACKUP_DIR"
  158. return 0
  159. else
  160. log_error "No database found to backup"
  161. return 1
  162. fi
  163. }
  164. # Check if migration is needed
  165. check_migration_needed() {
  166. if [ -f "$MIGRATION_STATUS" ]; then
  167. local status=$(jq -r '.status' "$MIGRATION_STATUS" 2>/dev/null || echo "unknown")
  168. if [ "$status" = "completed" ]; then
  169. log_message "Migration already completed"
  170. return 1
  171. elif [ "$status" = "running" ]; then
  172. log_message "Migration already in progress"
  173. return 0
  174. fi
  175. fi
  176. # Check if we have MongoDB 3 data (either in wekan directory or raw database files)
  177. if [ -d "${SNAP_COMMON}/wekan" ] && [ ! -f "${SNAP_COMMON}/mongodb-version-7" ]; then
  178. log_message "MongoDB 3 data detected in wekan directory"
  179. return 0
  180. fi
  181. # Check for MongoDB upgrade needed by examining log file
  182. if detect_mongodb_upgrade_needed; then
  183. log_message "MongoDB upgrade needed detected from log file"
  184. return 0
  185. fi
  186. log_message "No migration needed"
  187. return 1
  188. }
  189. # Detect if MongoDB upgrade is needed by checking log file
  190. detect_mongodb_upgrade_needed() {
  191. local mongodb_log="${SNAP_COMMON}/mongodb.log"
  192. # Check if MongoDB log file exists
  193. if [ ! -f "$mongodb_log" ]; then
  194. log_message "MongoDB log file not found: $mongodb_log"
  195. return 1
  196. fi
  197. # Check for the specific error message indicating upgrade is needed
  198. if grep -q "This version of MongoDB is too recent to start up on the existing data files. Try MongoDB 4.2 or earlier." "$mongodb_log"; then
  199. log_message "MongoDB upgrade needed detected in log file"
  200. return 0
  201. fi
  202. # Also check for similar error messages that might indicate upgrade issues
  203. if grep -q "too recent to start up on the existing data files" "$mongodb_log"; then
  204. log_message "MongoDB upgrade needed detected in log file (alternative message)"
  205. return 0
  206. fi
  207. log_message "No MongoDB upgrade needed detected in log file"
  208. return 1
  209. }
  210. # Reset MONGO_LOG_DESTINATION to devnull after successful migration
  211. reset_mongo_log_destination() {
  212. log_message "Resetting MONGO_LOG_DESTINATION to devnull after successful migration"
  213. # Use snap set to change the setting back to devnull
  214. if snap set wekan mongo-log-destination="devnull" 2>/dev/null; then
  215. log_success "MONGO_LOG_DESTINATION reset to devnull successfully"
  216. else
  217. log_error "Failed to reset MONGO_LOG_DESTINATION to devnull"
  218. # Don't fail the migration for this setting issue
  219. fi
  220. }
  221. # Migrate raw MongoDB 3 database files
  222. migrate_raw_database_files() {
  223. log_message "Starting raw MongoDB 3 database files migration"
  224. # Validate paths are within SNAP_COMMON
  225. if ! validate_snap_common_path "${SNAP_COMMON}" "Database path"; then
  226. log_error "Database path validation failed"
  227. return 1
  228. fi
  229. # Stop any running MongoDB processes
  230. log_message "Stopping any running MongoDB processes"
  231. pkill -f mongod || true
  232. sleep 3
  233. # Start MongoDB 3 with raw files
  234. log_message "Starting MongoDB 3 with raw database files"
  235. local mongo3_pid
  236. mongod --dbpath "${SNAP_COMMON}" --port "${MONGODB_PORT:-27019}" --quiet &
  237. mongo3_pid=$!
  238. # Wait for MongoDB 3 to start
  239. local retry_count=0
  240. while [ $retry_count -lt 30 ]; do
  241. if mongosh --quiet --eval "db.adminCommand('ping')" "mongodb://localhost:${MONGODB_PORT:-27019}/admin" >/dev/null 2>&1; then
  242. log_message "MongoDB 3 started successfully"
  243. break
  244. fi
  245. sleep 1
  246. retry_count=$((retry_count + 1))
  247. done
  248. if [ $retry_count -eq 30 ]; then
  249. log_error "MongoDB 3 failed to start"
  250. kill $mongo3_pid 2>/dev/null || true
  251. return 1
  252. fi
  253. # Dump all databases from MongoDB 3
  254. log_message "Dumping databases from MongoDB 3"
  255. if ! mongodump --port "${MONGODB_PORT:-27019}" --out "$TEMP_DIR" --dbpath "${SNAP_COMMON}"; then
  256. log_error "Failed to dump databases from MongoDB 3"
  257. kill $mongo3_pid 2>/dev/null || true
  258. return 1
  259. fi
  260. # Stop MongoDB 3
  261. log_message "Stopping MongoDB 3"
  262. kill $mongo3_pid 2>/dev/null || true
  263. sleep 3
  264. # Start MongoDB 7
  265. log_message "Starting MongoDB 7"
  266. local mongo7_pid
  267. mongod --dbpath "${SNAP_COMMON}" --port "${MONGODB_PORT:-27019}" --quiet &
  268. mongo7_pid=$!
  269. # Wait for MongoDB 7 to start
  270. retry_count=0
  271. while [ $retry_count -lt 30 ]; do
  272. if mongosh --quiet --eval "db.adminCommand('ping')" "mongodb://localhost:${MONGODB_PORT:-27019}/admin" >/dev/null 2>&1; then
  273. log_message "MongoDB 7 started successfully"
  274. break
  275. fi
  276. sleep 1
  277. retry_count=$((retry_count + 1))
  278. done
  279. if [ $retry_count -eq 30 ]; then
  280. log_error "MongoDB 7 failed to start"
  281. kill $mongo7_pid 2>/dev/null || true
  282. return 1
  283. fi
  284. # Restore databases to MongoDB 7
  285. log_message "Restoring databases to MongoDB 7"
  286. if ! mongorestore --port "${MONGODB_PORT:-27019}" --dbpath "${SNAP_COMMON}" "$TEMP_DIR"; then
  287. log_error "Failed to restore databases to MongoDB 7"
  288. kill $mongo7_pid 2>/dev/null || true
  289. return 1
  290. fi
  291. # Stop MongoDB 7
  292. log_message "Stopping MongoDB 7"
  293. kill $mongo7_pid 2>/dev/null || true
  294. sleep 3
  295. # Clean up old MongoDB 3 files
  296. log_message "Cleaning up old MongoDB 3 files"
  297. find "${SNAP_COMMON}" -maxdepth 1 -name "*.0" -o -name "*.1" -o -name "*.ns" -o -name "j._*" -o -name "mongod.lock" | while read -r file; do
  298. if [ -f "$file" ]; then
  299. rm -f "$file"
  300. log_message "Removed old file: $file"
  301. fi
  302. done
  303. # Remove journal directory if it exists
  304. if [ -d "${SNAP_COMMON}/journal" ]; then
  305. rm -rf "${SNAP_COMMON}/journal"
  306. log_message "Removed journal directory"
  307. fi
  308. log_success "Raw database files migration completed successfully"
  309. # Reset MONGO_LOG_DESTINATION to devnull after successful migration
  310. reset_mongo_log_destination
  311. return 0
  312. }
  313. # Snap channel detection and switching functions
  314. get_current_snap_channel() {
  315. local snap_name="$1"
  316. snap list "$snap_name" 2>/dev/null | awk 'NR==2 {print $4}' || echo "unknown"
  317. }
  318. is_wekan_snap() {
  319. local snap_name="$1"
  320. case "$snap_name" in
  321. wekan|wekan-gantt-gpl|wekan-ondra)
  322. return 0
  323. ;;
  324. *)
  325. return 1
  326. ;;
  327. esac
  328. }
  329. switch_to_stable_channel() {
  330. local snap_name="$1"
  331. local current_channel=$(get_current_snap_channel "$snap_name")
  332. if [ "$current_channel" != "stable" ] && [ "$current_channel" != "unknown" ]; then
  333. log_message "Switching $snap_name from $current_channel to stable channel"
  334. if snap refresh "$snap_name" --channel=stable; then
  335. log_success "Successfully switched $snap_name to stable channel"
  336. return 0
  337. else
  338. log_error "Failed to switch $snap_name to stable channel"
  339. return 1
  340. fi
  341. else
  342. log_message "$snap_name is already on stable channel or not installed"
  343. return 0
  344. fi
  345. }
  346. switch_all_wekan_snaps_to_stable() {
  347. log_message "Checking for Wekan-related snaps to switch to stable channel"
  348. local wekan_snaps=("wekan" "wekan-gantt-gpl" "wekan-ondra")
  349. local switched_count=0
  350. local failed_count=0
  351. for snap_name in "${wekan_snaps[@]}"; do
  352. if snap list "$snap_name" >/dev/null 2>&1; then
  353. if switch_to_stable_channel "$snap_name"; then
  354. switched_count=$((switched_count + 1))
  355. else
  356. failed_count=$((failed_count + 1))
  357. fi
  358. fi
  359. done
  360. log_message "Channel switching completed: $switched_count successful, $failed_count failed"
  361. if [ "$failed_count" -gt 0 ]; then
  362. return 1
  363. else
  364. return 0
  365. fi
  366. }
  367. # Get database collections
  368. get_collections() {
  369. local mongo_url="$1"
  370. local collections=$(mongosh --quiet --eval "db.getCollectionNames().join('\n')" "$mongo_url" 2>/dev/null | grep -v "^$" || echo "")
  371. echo "$collections"
  372. }
  373. # Migrate a single collection
  374. migrate_collection() {
  375. local collection="$1"
  376. local mongo3_url="$2"
  377. local mongo7_url="$3"
  378. local step="$4"
  379. local total_steps="$5"
  380. log_message "Migrating collection: $collection"
  381. # Check disk space before each collection (estimate 2x collection size)
  382. local collection_size=$(mongosh --quiet --eval "db.$collection.stats().size" "$mongo3_url" 2>/dev/null || echo "0")
  383. local required_space=$((collection_size * 2 / 1024 / 1024 / 1024 + 1)) # Convert to GB and add 1GB buffer
  384. if ! check_disk_space "$required_space"; then
  385. log_error "Insufficient disk space for collection $collection"
  386. return 1
  387. fi
  388. # Dump collection
  389. local dump_file="${TEMP_DIR}/${collection}.bson"
  390. log_message "Dumping collection $collection to $dump_file"
  391. if ! mongodump --db wekan --collection "$collection" --out "$TEMP_DIR" --port "${MONGODB_PORT:-27019}" --dbpath "${SNAP_COMMON}"; then
  392. log_error "Failed to dump collection $collection"
  393. return 1
  394. fi
  395. # Restore collection
  396. log_message "Restoring collection $collection to MongoDB 7"
  397. if ! mongorestore --db wekan --collection "$collection" "$dump_file" --port "${MONGODB_PORT:-27019}" --dbpath "${SNAP_COMMON}"; then
  398. log_error "Failed to restore collection $collection"
  399. return 1
  400. fi
  401. # Update progress
  402. update_progress "$step" "$total_steps" "Migrated collection: $collection"
  403. # Clean up dump file
  404. rm -f "$dump_file"
  405. log_success "Collection $collection migrated successfully"
  406. return 0
  407. }
  408. # Main migration function
  409. perform_migration() {
  410. local start_time=$(date +%s)
  411. log_message "Starting MongoDB migration from version 3 to 7"
  412. # Create backup before migration
  413. log_message "Creating backup before migration"
  414. if ! create_backup; then
  415. log_error "Failed to create backup, aborting migration"
  416. return 1
  417. fi
  418. # Create temporary directory
  419. mkdir -p "$TEMP_DIR"
  420. # Check if we need to migrate raw database files
  421. if detect_mongodb_upgrade_needed; then
  422. log_message "MongoDB upgrade needed detected, starting raw file migration"
  423. if ! migrate_raw_database_files; then
  424. log_error "Failed to migrate raw database files"
  425. return 1
  426. fi
  427. fi
  428. # Get MongoDB connection details
  429. local mongo3_url="mongodb://localhost:${MONGODB_PORT:-27019}/wekan"
  430. local mongo7_url="mongodb://localhost:${MONGODB_PORT:-27019}/wekan"
  431. # Get collections to migrate
  432. log_message "Getting list of collections to migrate"
  433. local collections=$(get_collections "$mongo3_url")
  434. if [ -z "$collections" ]; then
  435. log_error "No collections found to migrate"
  436. return 1
  437. fi
  438. local collection_count=$(echo "$collections" | wc -l)
  439. log_message "Found $collection_count collections to migrate"
  440. # Migrate each collection
  441. local current_step=0
  442. for collection in $collections; do
  443. current_step=$((current_step + 1))
  444. if ! migrate_collection "$collection" "$mongo3_url" "$mongo7_url" "$current_step" "$collection_count"; then
  445. log_error "Migration failed at collection $collection"
  446. return 1
  447. fi
  448. # Update completion time estimate
  449. local estimated_time=$(estimate_completion_time "$start_time" "$current_step" "$collection_count")
  450. log_message "Estimated completion time: $estimated_time"
  451. done
  452. # Mark migration as completed
  453. cat > "$MIGRATION_STATUS" << EOF
  454. {
  455. "step": $collection_count,
  456. "total_steps": $collection_count,
  457. "percentage": 100,
  458. "description": "Migration completed successfully",
  459. "timestamp": "$(date -Iseconds)",
  460. "status": "completed"
  461. }
  462. EOF
  463. # Create MongoDB 7 version marker
  464. touch "${SNAP_COMMON}/mongodb-version-7"
  465. # Clean up temporary files
  466. rm -rf "$TEMP_DIR"
  467. # Switch Wekan snaps to stable channel after successful migration
  468. log_message "Switching Wekan snaps to stable channel after successful migration"
  469. if switch_all_wekan_snaps_to_stable; then
  470. log_success "All Wekan snaps switched to stable channel successfully"
  471. else
  472. log_error "Some Wekan snaps failed to switch to stable channel"
  473. # Don't fail the migration for channel switching issues
  474. fi
  475. log_success "MongoDB migration completed successfully"
  476. # Reset MONGO_LOG_DESTINATION to devnull after successful migration
  477. reset_mongo_log_destination
  478. return 0
  479. }
  480. # Revert migration
  481. revert_migration() {
  482. log_message "Reverting MongoDB migration"
  483. if [ ! -f "$REVERT_FILE" ]; then
  484. log_error "Revert file not found: $REVERT_FILE"
  485. return 1
  486. fi
  487. # Stop MongoDB 7
  488. log_message "Stopping MongoDB 7"
  489. snapctl stop --disable "${SNAP_NAME}.mongodb"
  490. # Remove MongoDB 7 version marker
  491. rm -f "${SNAP_COMMON}/mongodb-version-7"
  492. # Find the most recent backup directory
  493. local latest_backup=$(ls -td "${SNAP_COMMON}/mongodb-backup-"* 2>/dev/null | head -1)
  494. if [ -n "$latest_backup" ] && [ -d "$latest_backup" ]; then
  495. log_message "Restoring from backup: $latest_backup"
  496. # Stop any running MongoDB processes
  497. pkill -f mongod || true
  498. sleep 2
  499. # Remove current database directory
  500. rm -rf "${SNAP_COMMON}/wekan"
  501. # Restore from backup
  502. cp -r "$latest_backup"/* "${SNAP_COMMON}/"
  503. # Clean up backup directory
  504. rm -rf "$latest_backup"
  505. log_success "Database restored from backup"
  506. else
  507. log_error "No backup found for revert"
  508. return 1
  509. fi
  510. # Remove revert file
  511. rm -f "$REVERT_FILE"
  512. # Clear migration status
  513. rm -f "$MIGRATION_STATUS"
  514. # Start MongoDB 3
  515. log_message "Starting MongoDB 3"
  516. snapctl start --enable "${SNAP_NAME}.mongodb"
  517. log_success "Migration reverted successfully"
  518. return 0
  519. }
  520. # Main execution
  521. main() {
  522. log_message "MongoDB Migration Script started"
  523. # Validate all paths are within SNAP_COMMON
  524. if ! validate_all_paths; then
  525. log_error "Path validation failed - aborting migration"
  526. exit 1
  527. fi
  528. # Check if revert is requested
  529. if [ -f "$REVERT_FILE" ]; then
  530. revert_migration
  531. exit $?
  532. fi
  533. # Check if migration is needed
  534. if ! check_migration_needed; then
  535. exit 0
  536. fi
  537. # Perform migration
  538. if perform_migration; then
  539. log_success "Migration completed successfully"
  540. exit 0
  541. else
  542. log_error "Migration failed"
  543. exit 1
  544. fi
  545. }
  546. # Run main function
  547. main "$@"