summaryrefslogtreecommitdiff
path: root/migrationtoblock.sh
blob: 3cc75feceac8319f473797fa0eb7e4651ac1699e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/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."