#!/usr/bin/env bash set -euo pipefail project_dir=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd) expected_context=kubernetes-admin@kubernetes context=$(kubectl config current-context) if [[ "${context}" != "${expected_context}" ]]; then echo "Expected Kubernetes context ${expected_context}, found ${context}." >&2 exit 1 fi sudo systemctl stop plexmediaserver.service if sudo systemctl is-active --quiet plexmediaserver.service; then echo 'plexmediaserver.service is still active; refusing to copy its database.' >&2 exit 1 fi if pgrep -u plex >/dev/null; then echo 'A process owned by plex is still running; refusing to copy its database.' >&2 exit 1 fi database_root='/var/lib/plex/Plex Media Server/Plug-in Support/Databases' for database in \ "${database_root}/com.plexapp.plugins.library.db" \ "${database_root}/com.plexapp.plugins.library.blobs.db"; do result=$(sudo -u plex '/usr/lib/plexmediaserver/Plex SQLite' "${database}" 'PRAGMA quick_check;') if [[ "${result}" != ok ]]; then echo "SQLite quick_check failed for ${database}: ${result}" >&2 exit 1 fi done kubectl apply --filename "${project_dir}/namespace.yaml" kubectl apply --filename "${project_dir}/plex.yaml" \ --selector=app.kubernetes.io/component=state kubectl --namespace plex wait \ --for=jsonpath='{.status.phase}'=Bound \ persistentvolumeclaim/plex-config \ --timeout=10m if kubectl --namespace plex get job plex-archlinux-migration >/dev/null 2>&1; then echo 'Job plex-archlinux-migration already exists; inspect it instead of overwriting it.' >&2 exit 1 fi kubectl apply --filename "${project_dir}/migration-job.yaml" echo 'Migration started. Follow it with:' echo ' kubectl -n plex logs -f job/plex-archlinux-migration' echo ' kubectl -n plex wait --for=condition=complete job/plex-archlinux-migration --timeout=12h'