| 1 | #!/usr/bin/env bash
|
|---|
| 2 | #
|
|---|
| 3 | # Move an existing Klonkt install to the split layout:
|
|---|
| 4 | #
|
|---|
| 5 | # /opt/klonkt/ shared code, read-only at runtime
|
|---|
| 6 | # /var/lib/klonkt/<slug>/ this instance's data and .env
|
|---|
| 7 | #
|
|---|
| 8 | # Before, an instance kept its database, uploads and .env inside the checkout.
|
|---|
| 9 | # That made the code directory undeletable (it held live user data), made
|
|---|
| 10 | # backups awkward, and meant a second instance needed a second copy of the code.
|
|---|
| 11 | #
|
|---|
| 12 | # Run as root on the server. Safe to re-run: it stops at the first step that is
|
|---|
| 13 | # already done rather than moving anything twice.
|
|---|
| 14 | #
|
|---|
| 15 | # sudo bash scripts/klonkt-migrate-data.sh <slug>
|
|---|
| 16 | # sudo bash scripts/klonkt-migrate-data.sh <slug> --dry-run
|
|---|
| 17 | #
|
|---|
| 18 | # The slug names the instance and nothing else: it is the directory under
|
|---|
| 19 | # /var/lib/klonkt and the systemd instance name (klonkt@<slug>).
|
|---|
| 20 |
|
|---|
| 21 | set -euo pipefail
|
|---|
| 22 |
|
|---|
| 23 | KLONKT_DIR="${KLONKT_DIR:-/opt/klonkt}"
|
|---|
| 24 | KLONKT_USER="${KLONKT_USER:-klonkt}"
|
|---|
| 25 | DATA_ROOT="${KLONKT_DATA_ROOT:-/var/lib/klonkt}"
|
|---|
| 26 | OLD_UNIT="klonkt.service"
|
|---|
| 27 |
|
|---|
| 28 | SLUG=""
|
|---|
| 29 | DRY=0
|
|---|
| 30 | for arg in "$@"; do
|
|---|
| 31 | case "$arg" in
|
|---|
| 32 | --dry-run) DRY=1 ;;
|
|---|
| 33 | -*) echo "unknown option: $arg" >&2; exit 2 ;;
|
|---|
| 34 | *) SLUG="$arg" ;;
|
|---|
| 35 | esac
|
|---|
| 36 | done
|
|---|
| 37 |
|
|---|
| 38 | say() { printf ' %s\n' "$*"; }
|
|---|
| 39 | step() { printf '\n== %s\n' "$*"; }
|
|---|
| 40 | die() { printf '\nERROR: %s\n' "$*" >&2; exit 1; }
|
|---|
| 41 | run() { if [ "$DRY" = 1 ]; then printf ' [dry-run] %s\n' "$*"; else eval "$@"; fi; }
|
|---|
| 42 |
|
|---|
| 43 | [ "$(id -u)" = 0 ] || die "run this as root (sudo)."
|
|---|
| 44 | [ -n "$SLUG" ] || die "usage: $0 <slug> [--dry-run] e.g. $0 boiert"
|
|---|
| 45 | [[ "$SLUG" =~ ^[a-z0-9][a-z0-9._-]*$ ]] || die "slug must be lowercase letters, digits, dot, dash or underscore."
|
|---|
| 46 |
|
|---|
| 47 | DATA_DIR="$DATA_ROOT/$SLUG"
|
|---|
| 48 | ENV_OLD="$KLONKT_DIR/.env"
|
|---|
| 49 | ENV_NEW="$DATA_DIR/.env"
|
|---|
| 50 |
|
|---|
| 51 | step "Preflight"
|
|---|
| 52 | [ -d "$KLONKT_DIR" ] || die "no install at $KLONKT_DIR"
|
|---|
| 53 | [ -f "$ENV_OLD" ] || [ -f "$ENV_NEW" ] || die "no .env at $ENV_OLD (already migrated elsewhere?)"
|
|---|
| 54 | id -u "$KLONKT_USER" >/dev/null 2>&1 || die "user $KLONKT_USER does not exist"
|
|---|
| 55 |
|
|---|
| 56 | # The split only works on code where every media subdirectory derives from
|
|---|
| 57 | # MEDIA_PATH. On older code the subdirectories fall back into the checkout, so
|
|---|
| 58 | # the app would quietly recreate storage/ next to the code and uploads would
|
|---|
| 59 | # land there.
|
|---|
| 60 | [ -f "$KLONKT_DIR/src/config/paths.js" ] || die \
|
|---|
| 61 | "this build is too old for the split layout: src/config/paths.js is missing.
|
|---|
| 62 | Update first (git pull in $KLONKT_DIR), then run this again."
|
|---|
| 63 | say "code at $KLONKT_DIR supports MEDIA_PATH-derived subdirectories"
|
|---|
| 64 |
|
|---|
| 65 | if [ -d "$DATA_DIR" ] && [ -n "$(ls -A "$DATA_DIR" 2>/dev/null)" ]; then
|
|---|
| 66 | die "$DATA_DIR already exists and is not empty. Remove it or pick another slug."
|
|---|
| 67 | fi
|
|---|
| 68 | say "target $DATA_DIR is free"
|
|---|
| 69 | [ "$DRY" = 1 ] && say "DRY RUN: nothing will be changed"
|
|---|
| 70 |
|
|---|
| 71 | step "Stopping the service"
|
|---|
| 72 | if systemctl is-active --quiet "$OLD_UNIT"; then
|
|---|
| 73 | run "systemctl stop $OLD_UNIT"
|
|---|
| 74 | say "stopped $OLD_UNIT (SQLite checkpoints its write-ahead log on shutdown)"
|
|---|
| 75 | else
|
|---|
| 76 | say "$OLD_UNIT was not running"
|
|---|
| 77 | fi
|
|---|
| 78 |
|
|---|
| 79 | step "Creating the data directory"
|
|---|
| 80 | run "mkdir -p '$DATA_DIR'"
|
|---|
| 81 |
|
|---|
| 82 | step "Moving data out of the checkout"
|
|---|
| 83 | if [ -d "$KLONKT_DIR/storage" ]; then
|
|---|
| 84 | say "found $(find "$KLONKT_DIR/storage" -type f 2>/dev/null | wc -l) files in storage/ ($(du -sh "$KLONKT_DIR/storage" 2>/dev/null | cut -f1))"
|
|---|
| 85 | # Everything, including database.sqlite plus its -wal and -shm siblings.
|
|---|
| 86 | run "shopt -s dotglob nullglob; for f in '$KLONKT_DIR/storage/'*; do mv \"\$f\" '$DATA_DIR/'; done"
|
|---|
| 87 | run "rmdir '$KLONKT_DIR/storage' 2>/dev/null || true"
|
|---|
| 88 | say "moved to $DATA_DIR"
|
|---|
| 89 | else
|
|---|
| 90 | say "no storage/ directory (already moved?)"
|
|---|
| 91 | fi
|
|---|
| 92 |
|
|---|
| 93 | if [ -f "$ENV_OLD" ]; then
|
|---|
| 94 | run "mv '$ENV_OLD' '$ENV_NEW'"
|
|---|
| 95 | say "moved .env to $ENV_NEW"
|
|---|
| 96 | fi
|
|---|
| 97 |
|
|---|
| 98 | step "Pointing the data paths at the new location"
|
|---|
| 99 | # Replace when present, append when absent, so this works regardless of which
|
|---|
| 100 | # variables the original install wrote.
|
|---|
| 101 | set_env() {
|
|---|
| 102 | local key="$1" val="$2"
|
|---|
| 103 | if [ "$DRY" = 1 ]; then printf ' [dry-run] %s=%s\n' "$key" "$val"; return; fi
|
|---|
| 104 | if grep -q "^${key}=" "$ENV_NEW" 2>/dev/null; then
|
|---|
| 105 | sed -i "s#^${key}=.*#${key}=${val}#" "$ENV_NEW"
|
|---|
| 106 | else
|
|---|
| 107 | printf '%s=%s\n' "$key" "$val" >> "$ENV_NEW"
|
|---|
| 108 | fi
|
|---|
| 109 | printf ' %s=%s\n' "$key" "$val"
|
|---|
| 110 | }
|
|---|
| 111 | set_env DATABASE_PATH "$DATA_DIR/database.sqlite"
|
|---|
| 112 | set_env MEDIA_PATH "$DATA_DIR/media"
|
|---|
| 113 | set_env AUDIO_PATH "$DATA_DIR/audio"
|
|---|
| 114 |
|
|---|
| 115 | step "Ownership and permissions"
|
|---|
| 116 | run "chown -R '$KLONKT_USER:$KLONKT_USER' '$DATA_DIR'"
|
|---|
| 117 | run "chmod 750 '$DATA_DIR'"
|
|---|
| 118 | run "chmod 600 '$ENV_NEW'"
|
|---|
| 119 | say "data owned by $KLONKT_USER, .env readable only by that user"
|
|---|
| 120 |
|
|---|
| 121 | step "Installing the systemd template"
|
|---|
| 122 | if [ -f "$KLONKT_DIR/deploy/klonkt@.service" ]; then
|
|---|
| 123 | run "install -m 0644 '$KLONKT_DIR/deploy/klonkt@.service' /etc/systemd/system/klonkt@.service"
|
|---|
| 124 | say "installed /etc/systemd/system/klonkt@.service"
|
|---|
| 125 | else
|
|---|
| 126 | die "template not found at $KLONKT_DIR/deploy/klonkt@.service"
|
|---|
| 127 | fi
|
|---|
| 128 | run "systemctl daemon-reload"
|
|---|
| 129 |
|
|---|
| 130 | step "Switching to klonkt@$SLUG"
|
|---|
| 131 | if systemctl list-unit-files "$OLD_UNIT" >/dev/null 2>&1; then
|
|---|
| 132 | run "systemctl disable --now $OLD_UNIT"
|
|---|
| 133 | # Disable only removes the autostart link: `systemctl restart klonkt` would
|
|---|
| 134 | # still START it. That is not theoretical — an updater generated before the
|
|---|
| 135 | # split does exactly that, and the resurrected unit finds no .env (it moved
|
|---|
| 136 | # with the data), falls back to the built-in defaults and creates a FRESH
|
|---|
| 137 | # EMPTY database in the checkout. Masking makes any such call fail loudly.
|
|---|
| 138 | # Reversible: systemctl unmask klonkt.
|
|---|
| 139 | run "systemctl mask $OLD_UNIT"
|
|---|
| 140 | say "disabled and masked $OLD_UNIT (unmask to roll back)"
|
|---|
| 141 | fi
|
|---|
| 142 | run "systemctl enable --now 'klonkt@$SLUG'"
|
|---|
| 143 |
|
|---|
| 144 | step "Rewriting klonkt-update for the new layout"
|
|---|
| 145 | # The installer generated an updater that restarts klonkt.service — which we
|
|---|
| 146 | # just retired. Left alone it would keep updating the code while never
|
|---|
| 147 | # restarting the real process: half old, half new, and a 500 with no obvious
|
|---|
| 148 | # cause. Rewrite it so it restarts every klonkt@<slug> instead.
|
|---|
| 149 | if [ -f "$KLONKT_DIR/scripts/klonkt-refresh-updater.sh" ]; then
|
|---|
| 150 | run "KLONKT_DIR='$KLONKT_DIR' KLONKT_USER='$KLONKT_USER' KLONKT_DATA_ROOT='$DATA_ROOT' bash '$KLONKT_DIR/scripts/klonkt-refresh-updater.sh'"
|
|---|
| 151 | else
|
|---|
| 152 | say "WARNING: scripts/klonkt-refresh-updater.sh missing in this checkout."
|
|---|
| 153 | say " Update the code and run it once by hand, or every klonkt-update"
|
|---|
| 154 | say " from now on will update code WITHOUT restarting the process."
|
|---|
| 155 | fi
|
|---|
| 156 |
|
|---|
| 157 | step "Verifying"
|
|---|
| 158 | if [ "$DRY" = 1 ]; then
|
|---|
| 159 | say "dry run: skipping verification"
|
|---|
| 160 | exit 0
|
|---|
| 161 | fi
|
|---|
| 162 | sleep 3
|
|---|
| 163 | systemctl is-active --quiet "klonkt@$SLUG" || {
|
|---|
| 164 | echo
|
|---|
| 165 | journalctl -u "klonkt@$SLUG" -n 30 --no-pager || true
|
|---|
| 166 | die "klonkt@$SLUG did not start. Roll back with: systemctl enable --now $OLD_UNIT"
|
|---|
| 167 | }
|
|---|
| 168 | say "klonkt@$SLUG is running"
|
|---|
| 169 |
|
|---|
| 170 | PORT="$(grep -m1 '^PORT=' "$ENV_NEW" | cut -d= -f2- | tr -d '\r')"
|
|---|
| 171 | if [ -n "$PORT" ]; then
|
|---|
| 172 | if curl -fsS --max-time 8 -o /dev/null "http://127.0.0.1:${PORT}/"; then
|
|---|
| 173 | say "responding on 127.0.0.1:${PORT}"
|
|---|
| 174 | else
|
|---|
| 175 | say "WARNING: no answer on 127.0.0.1:${PORT} yet; check: journalctl -u klonkt@$SLUG -f"
|
|---|
| 176 | fi
|
|---|
| 177 | fi
|
|---|
| 178 |
|
|---|
| 179 | if [ -e "$KLONKT_DIR/storage" ]; then
|
|---|
| 180 | say "WARNING: $KLONKT_DIR/storage came back. That means this build still writes"
|
|---|
| 181 | say " next to its code. Report it; do not delete the directory."
|
|---|
| 182 | else
|
|---|
| 183 | say "the checkout no longer holds user data"
|
|---|
| 184 | fi
|
|---|
| 185 |
|
|---|
| 186 | cat <<EOF
|
|---|
| 187 |
|
|---|
| 188 | Done. This instance now looks like:
|
|---|
| 189 |
|
|---|
| 190 | code $KLONKT_DIR shared, replaceable, no user data
|
|---|
| 191 | data $DATA_DIR database, uploads and .env
|
|---|
| 192 | unit klonkt@$SLUG
|
|---|
| 193 |
|
|---|
| 194 | Back up $DATA_DIR and you have the whole instance.
|
|---|
| 195 | Add another instance with: klonkt-add-instance.sh <slug> <domain> <port>
|
|---|
| 196 | EOF
|
|---|