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