#!/bin/bash # === CONFIG === SRC="/mnt/raid5/minio-data/linodeassets" DST="/mnt/mastodon-assets" MOUNTPOINT="/home/mastodon/live/public/system" LOGFILE="/var/log/mastodon_asset_migration_$(date +%Y%m%d_%H%M%S).log" log() { echo "[$(date '+%F %T')] $*" | tee -a "$LOGFILE" } verify_sync() { local src_count=$(find "$SRC" -type f | wc -l) local dst_count=$(find "$DST" -type f | wc -l) local src_bytes=$(du -sb "$SRC" | awk '{print $1}') local dst_bytes=$(du -sb "$DST" | awk '{print $1}') echo "--- Verification Results ---" | tee -a "$LOGFILE" echo "Files: $src_count โ†’ $dst_count" | tee -a "$LOGFILE" echo "Bytes: $src_bytes โ†’ $dst_bytes" | tee -a "$LOGFILE" if [[ "$src_count" -ne "$dst_count" || "$src_bytes" -ne "$dst_bytes" ]]; then echo "โŒ MISMATCH detected. Please review the rsync log." | tee -a "$LOGFILE" else echo "โœ… Verified: source and destination match." | tee -a "$LOGFILE" fi echo "---------------------------" | tee -a "$LOGFILE" } # === PHASE 1: Live Sync === log "๐Ÿš€ Starting Phase 1: Live rsync" rsync -aAXv --progress "$SRC/" "$DST/" | tee -a "$LOGFILE" # === Stop Mastodon === log "๐Ÿ›‘ Stopping Mastodon services..." systemctl stop mastodon-web mastodon-sidekiq mastodon-streaming || { log "โŒ Failed to stop Mastodon services"; exit 1; } # === PHASE 2: Final Sync === log "๐Ÿ” Starting Phase 2: Final rsync with --delete" rsync -aAXv --delete "$SRC/" "$DST/" | tee -a "$LOGFILE" # === Bind Mount Cutover === log "๐Ÿ”— Swapping in block storage as $MOUNTPOINT" if [[ -d "$MOUNTPOINT" ]]; then mv "$MOUNTPOINT" "${MOUNTPOINT}.bak" || { log "โŒ Could not move existing mountpoint"; exit 1; } fi mkdir -p "$MOUNTPOINT" mount --bind "$DST" "$MOUNTPOINT" grep -q "$MOUNTPOINT" /etc/fstab || echo "$DST $MOUNTPOINT none bind 0 0" >> /etc/fstab log "[โœ“] Bind mount active and persisted" # === Permissions === log "๐Ÿ”ง Fixing permissions on $DST" chown -R mastodon:mastodon "$DST" # === Restart Mastodon === log "๐Ÿš€ Restarting Mastodon services..." systemctl start mastodon-web mastodon-sidekiq mastodon-streaming || { log "โŒ Failed to restart Mastodon services"; exit 1; } # === VERIFY === log "๐Ÿงช Verifying file count and byte totals" verify_sync log "๐ŸŽ‰ Migration completed successfully. Mastodon is live on block storage."