Fixed for truenas 27+
This commit is contained in:
+189
-241
@@ -1,17 +1,20 @@
|
||||
#!/bin/bash
|
||||
# =============================================================================
|
||||
# Piwigo Backup Script for TrueNAS App -> Docker Migration
|
||||
# Piwigo Backup Script for TrueNAS 27+ App -> Docker Migration
|
||||
# =============================================================================
|
||||
# This script backs up a Piwigo instance running as a TrueNAS App (k3s/Kubernetes)
|
||||
# This script backs up a Piwigo instance running as a TrueNAS App
|
||||
# to a TrueNAS dataset/share, ready for restoration to a Docker container.
|
||||
#
|
||||
# TrueNAS 27 (Electric Eel) stores app data directly in datasets accessible
|
||||
# from the host filesystem. No kubectl/k3s required.
|
||||
#
|
||||
# Run this script ON the TrueNAS host (via SSH or TrueNAS Shell).
|
||||
#
|
||||
# Requirements:
|
||||
# - TrueNAS with Piwigo app installed
|
||||
# - Access to k3s/kubectl (usually via /usr/local/bin/k3s kubectl)
|
||||
# - MariaDB/MySQL client tools (mariadb-dump/mysqldump)
|
||||
# - TrueNAS 27+ with Piwigo app installed
|
||||
# - MariaDB/MySQL client tools (mariadb-dump/mysqldump) for database dump
|
||||
# - Write access to backup destination dataset
|
||||
# - Read access to Piwigo app datasets (configured in app settings)
|
||||
# =============================================================================
|
||||
|
||||
set -euo pipefail
|
||||
@@ -20,23 +23,29 @@ set -euo pipefail
|
||||
# CONFIGURATION - Adjust these values for your environment
|
||||
# =============================================================================
|
||||
|
||||
# TrueNAS App namespace (usually the app name)
|
||||
APP_NAMESPACE="${APP_NAMESPACE:-piwigo}"
|
||||
|
||||
# Backup destination (TrueNAS dataset path)
|
||||
BACKUP_DEST="${BACKUP_DEST:-/mnt/tank/backups/piwigo}"
|
||||
|
||||
# Database details (auto-detected if not set)
|
||||
DB_HOST="${DB_HOST:-}"
|
||||
# ---- Database Configuration ----
|
||||
# If the database is running as a separate container/app, provide connection details
|
||||
# If using the built-in MariaDB from the Piwigo app chart, it might be accessible via localhost
|
||||
DB_HOST="${DB_HOST:-localhost}"
|
||||
DB_PORT="${DB_PORT:-3306}"
|
||||
DB_NAME="${DB_NAME:-piwigo}"
|
||||
DB_USER="${DB_USER:-piwigo}"
|
||||
DB_PASS="${DB_PASS:-}"
|
||||
DB_PASS="${DB_PASS:-}" # Required! Get from app config or secret
|
||||
|
||||
# Piwigo persistent volume claim names (auto-detected if not set)
|
||||
PVC_UPLOADS="${PVC_UPLOADS:-}"
|
||||
PVC_CONFIG="${PVC_CONFIG:-}"
|
||||
PVC_GALLERIES="${PVC_GALLERIES:-}"
|
||||
# ---- Piwigo Data Paths (Host filesystem paths) ----
|
||||
# These are the dataset paths you configured in the Piwigo app settings
|
||||
# Example: /mnt/tank/apps/piwigo/uploads
|
||||
# Example: /mnt/tank/apps/piwigo/config
|
||||
# Example: /mnt/tank/apps/piwigo/galleries
|
||||
PIWIGO_UPLOADS_PATH="${PIWIGO_UPLOADS_PATH:-}"
|
||||
PIWIGO_CONFIG_PATH="${PIWIGO_CONFIG_PATH:-}"
|
||||
PIWIGO_GALLERIES_PATH="${PIWIGO_GALLERIES_PATH:-}"
|
||||
|
||||
# Alternative: Single base path if all data is under one dataset
|
||||
# PIWIGO_BASE_PATH="${PIWIGO_BASE_PATH:-/mnt/tank/apps/piwigo}"
|
||||
|
||||
# Retention: keep last N backups
|
||||
RETENTION_COUNT="${RETENTION_COUNT:-7}"
|
||||
@@ -51,7 +60,6 @@ COMPRESSION_LEVEL="${COMPRESSION_LEVEL:-6}"
|
||||
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
||||
BACKUP_DIR="${BACKUP_DEST}/piwigo_backup_${TIMESTAMP}"
|
||||
mkdir -p "${BACKUP_DIR}"
|
||||
KUBECTL_CMD="${KUBECTL_CMD:-/usr/local/bin/k3s kubectl}"
|
||||
LOG_FILE="${BACKUP_DIR}/backup.log"
|
||||
|
||||
# Colors for output
|
||||
@@ -91,135 +99,157 @@ run_cmd() {
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
# DETECTION FUNCTIONS
|
||||
# VALIDATION FUNCTIONS
|
||||
# =============================================================================
|
||||
|
||||
detect_kubectl() {
|
||||
if [[ ! -x "${KUBECTL_CMD}" ]]; then
|
||||
# Try common locations
|
||||
for cmd in /usr/local/bin/k3s /usr/bin/k3s /usr/local/bin/kubectl /usr/bin/kubectl; do
|
||||
if [[ -x "${cmd}" ]]; then
|
||||
if [[ "${cmd}" == *k3s ]]; then
|
||||
KUBECTL_CMD="${cmd} kubectl"
|
||||
else
|
||||
KUBECTL_CMD="${cmd}"
|
||||
fi
|
||||
info "Found kubectl at: ${KUBECTL_CMD}"
|
||||
validate_paths() {
|
||||
info "Validating data paths..."
|
||||
|
||||
local has_paths=false
|
||||
|
||||
# Check individual paths
|
||||
if [[ -n "${PIWIGO_UPLOADS_PATH}" ]] && [[ -d "${PIWIGO_UPLOADS_PATH}" ]]; then
|
||||
info "Found uploads path: ${PIWIGO_UPLOADS_PATH}"
|
||||
has_paths=true
|
||||
elif [[ -n "${PIWIGO_UPLOADS_PATH}" ]]; then
|
||||
warn "Uploads path does not exist: ${PIWIGO_UPLOADS_PATH}"
|
||||
fi
|
||||
|
||||
if [[ -n "${PIWIGO_CONFIG_PATH}" ]] && [[ -d "${PIWIGO_CONFIG_PATH}" ]]; then
|
||||
info "Found config path: ${PIWIGO_CONFIG_PATH}"
|
||||
has_paths=true
|
||||
elif [[ -n "${PIWIGO_CONFIG_PATH}" ]]; then
|
||||
warn "Config path does not exist: ${PIWIGO_CONFIG_PATH}"
|
||||
fi
|
||||
|
||||
if [[ -n "${PIWIGO_GALLERIES_PATH}" ]] && [[ -d "${PIWIGO_GALLERIES_PATH}" ]]; then
|
||||
info "Found galleries path: ${PIWIGO_GALLERIES_PATH}"
|
||||
has_paths=true
|
||||
elif [[ -n "${PIWIGO_GALLERIES_PATH}" ]]; then
|
||||
warn "Galleries path does not exist: ${PIWIGO_GALLERIES_PATH}"
|
||||
fi
|
||||
|
||||
# If no individual paths set, try to auto-detect from common locations
|
||||
if [[ "${has_paths}" == "false" ]]; then
|
||||
info "No explicit paths set, trying to auto-detect..."
|
||||
detect_paths
|
||||
fi
|
||||
|
||||
if [[ "${has_paths}" == "false" ]]; then
|
||||
die "No valid Piwigo data paths found. Please set PIWIGO_UPLOADS_PATH, PIWIGO_CONFIG_PATH, PIWIGO_GALLERIES_PATH environment variables to the dataset paths configured in your Piwigo app."
|
||||
fi
|
||||
}
|
||||
|
||||
detect_paths() {
|
||||
# Common TrueNAS app dataset locations
|
||||
local common_bases=(
|
||||
"/mnt/tank/apps/piwigo"
|
||||
"/mnt/pool/apps/piwigo"
|
||||
"/mnt/data/apps/piwigo"
|
||||
"/mnt/storage/apps/piwigo"
|
||||
"/mnt/apps/piwigo"
|
||||
)
|
||||
|
||||
for base in "${common_bases[@]}"; do
|
||||
if [[ -d "${base}" ]]; then
|
||||
info "Found potential base path: ${base}"
|
||||
|
||||
if [[ -d "${base}/uploads" ]] && [[ -z "${PIWIGO_UPLOADS_PATH}" ]]; then
|
||||
PIWIGO_UPLOADS_PATH="${base}/uploads"
|
||||
info "Auto-detected uploads: ${PIWIGO_UPLOADS_PATH}"
|
||||
fi
|
||||
if [[ -d "${base}/config" ]] && [[ -z "${PIWIGO_CONFIG_PATH}" ]]; then
|
||||
PIWIGO_CONFIG_PATH="${base}/config"
|
||||
info "Auto-detected config: ${PIWIGO_CONFIG_PATH}"
|
||||
fi
|
||||
if [[ -d "${base}/galleries" ]] && [[ -z "${PIWIGO_GALLERIES_PATH}" ]]; then
|
||||
PIWIGO_GALLERIES_PATH="${base}/galleries"
|
||||
info "Auto-detected galleries: ${PIWIGO_GALLERIES_PATH}"
|
||||
fi
|
||||
|
||||
# Also check for 'data' as uploads
|
||||
if [[ -d "${base}/data" ]] && [[ -z "${PIWIGO_UPLOADS_PATH}" ]]; then
|
||||
PIWIGO_UPLOADS_PATH="${base}/data"
|
||||
info "Auto-detected uploads (as data): ${PIWIGO_UPLOADS_PATH}"
|
||||
fi
|
||||
|
||||
# If we found at least one, break
|
||||
if [[ -n "${PIWIGO_UPLOADS_PATH}" ]] || [[ -n "${PIWIGO_CONFIG_PATH}" ]] || [[ -n "${PIWIGO_GALLERIES_PATH}" ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
die "kubectl not found. Please install k3s or kubectl."
|
||||
fi
|
||||
info "Using kubectl: ${KUBECTL_CMD}"
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
detect_namespace() {
|
||||
if [[ -z "${APP_NAMESPACE}" ]]; then
|
||||
info "Auto-detecting Piwigo namespace..."
|
||||
local ns=$(${KUBECTL_CMD} get pods --all-namespaces -l app=piwigo -o jsonpath='{.items[0].metadata.namespace}' 2>/dev/null || true)
|
||||
if [[ -n "${ns}" ]]; then
|
||||
APP_NAMESPACE="${ns}"
|
||||
info "Detected namespace: ${APP_NAMESPACE}"
|
||||
else
|
||||
# Try common namespaces
|
||||
for ns in piwigo piwigo-app apps-piwigo; do
|
||||
if ${KUBECTL_CMD} get namespace "${ns}" >/dev/null 2>&1; then
|
||||
APP_NAMESPACE="${ns}"
|
||||
info "Found namespace: ${APP_NAMESPACE}"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
die "Could not detect Piwigo namespace. Set APP_NAMESPACE manually."
|
||||
fi
|
||||
validate_database() {
|
||||
info "Validating database connection..."
|
||||
|
||||
if [[ -z "${DB_PASS}" ]]; then
|
||||
warn "DB_PASS not set. Trying to find it from common locations..."
|
||||
find_db_password
|
||||
fi
|
||||
|
||||
if [[ -z "${DB_PASS}" ]]; then
|
||||
die "Database password (DB_PASS) is required. Please set it or ensure it can be auto-detected."
|
||||
fi
|
||||
|
||||
# Test connection
|
||||
local dump_cmd=""
|
||||
if command -v mariadb-dump >/dev/null 2>&1; then
|
||||
dump_cmd="mariadb-dump"
|
||||
elif command -v mysqldump >/dev/null 2>&1; then
|
||||
dump_cmd="mysqldump"
|
||||
else
|
||||
die "Neither mariadb-dump nor mysqldump found. Install mariadb-client or mysql-client."
|
||||
fi
|
||||
|
||||
export MYSQL_PWD="${DB_PASS}"
|
||||
if ! ${dump_cmd} -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -e "SELECT 1" >/dev/null 2>&1; then
|
||||
unset MYSQL_PWD
|
||||
die "Cannot connect to database at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials and ensure database is accessible from host."
|
||||
fi
|
||||
unset MYSQL_PWD
|
||||
success "Database connection validated"
|
||||
}
|
||||
|
||||
detect_database() {
|
||||
info "Detecting database configuration..."
|
||||
find_db_password() {
|
||||
# Try to find password from common locations
|
||||
local possible_files=(
|
||||
"/mnt/tank/apps/piwigo/.env"
|
||||
"/mnt/pool/apps/piwigo/.env"
|
||||
"/mnt/data/apps/piwigo/.env"
|
||||
"/mnt/storage/apps/piwigo/.env"
|
||||
"/mnt/apps/piwigo/.env"
|
||||
"/etc/piwigo/db_password"
|
||||
"/root/.piwigo_db_password"
|
||||
)
|
||||
|
||||
# Try to get database credentials from Kubernetes secrets
|
||||
local secret_name=$(${KUBECTL_CMD} get secrets -n "${APP_NAMESPACE}" -o name 2>/dev/null | grep -E '(mariadb|mysql|database|piwigo)' | head -1 | cut -d/ -f2 || true)
|
||||
|
||||
if [[ -n "${secret_name}" ]]; then
|
||||
info "Found database secret: ${secret_name}"
|
||||
DB_HOST="${DB_HOST:-$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.host}' 2>/dev/null | base64 -d || echo "")}"
|
||||
DB_PORT="${DB_PORT:-$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.port}' 2>/dev/null | base64 -d || echo "3306")}"
|
||||
DB_NAME="${DB_NAME:-$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.database}' 2>/dev/null | base64 -d || echo "piwigo")}"
|
||||
DB_USER="${DB_USER:-$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.username}' 2>/dev/null | base64 -d || echo "piwigo")}"
|
||||
DB_PASS="${DB_PASS:-$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.password}' 2>/dev/null | base64 -d || echo "")}"
|
||||
|
||||
# Also check for mariadb-root-password or similar
|
||||
if [[ -z "${DB_PASS}" ]]; then
|
||||
DB_PASS=$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.mariadb-root-password}' 2>/dev/null | base64 -d || true)
|
||||
for file in "${possible_files[@]}"; do
|
||||
if [[ -f "${file}" ]]; then
|
||||
# Try to extract password from file
|
||||
local pass=$(grep -iE '(password|passwd)' "${file}" 2>/dev/null | head -1 | sed -E 's/.*[=:]\s*//' | sed -E 's/["\'\'']//g' | xargs)
|
||||
if [[ -n "${pass}" ]]; then
|
||||
DB_PASS="${pass}"
|
||||
info "Found database password in ${file}"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
if [[ -z "${DB_PASS}" ]]; then
|
||||
DB_PASS=$(${KUBECTL_CMD} get secret "${secret_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.data.mysql-root-password}' 2>/dev/null | base64 -d || true)
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
# Try to detect from Piwigo config if running
|
||||
if [[ -z "${DB_HOST}" ]] || [[ -z "${DB_PASS}" ]]; then
|
||||
info "Trying to detect from Piwigo pod config..."
|
||||
local piwigo_pod=$(${KUBECTL_CMD} get pods -n "${APP_NAMESPACE}" -l app=piwigo -o jsonpath='{.items[0].metadata.name}' 2>/dev/null || true)
|
||||
if [[ -n "${piwigo_pod}" ]]; then
|
||||
# Try to read config from the pod
|
||||
local config=$(${KUBECTL_CMD} exec -n "${APP_NAMESPACE}" "${piwigo_pod}" -- cat /var/www/html/local/config/config.inc.php 2>/dev/null || true)
|
||||
if [[ -n "${config}" ]]; then
|
||||
DB_HOST="${DB_HOST:-$(echo "${config}" | grep -oP \"'db_host'\s*=>\s*'\K[^']+\" || true)}"
|
||||
DB_USER="${DB_USER:-$(echo "${config}" | grep -oP \"'db_user'\s*=>\s*'\K[^']+\" || true)}"
|
||||
DB_PASS="${DB_PASS:-$(echo "${config}" | grep -oP \"'db_password'\s*=>\s*'\K[^']+\" || true)}"
|
||||
DB_NAME="${DB_NAME:-$(echo "${config}" | grep -oP \"'db_name'\s*=>\s*'\K[^']+\" || true)}"
|
||||
# Check if there's a MariaDB container running that we can get password from
|
||||
if command -v docker >/dev/null 2>&1; then
|
||||
local mariadb_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|piwigo.*db)' | head -1 || true)
|
||||
if [[ -n "${mariadb_container}" ]]; then
|
||||
info "Found potential MariaDB container: ${mariadb_container}"
|
||||
# Try to get password from container env
|
||||
local pass=$(docker inspect "${mariadb_container}" 2>/dev/null | grep -iE '(MYSQL_PASSWORD|MARIADB_PASSWORD)' | head -1 | sed -E 's/.*"([^"]+)".*/\1/' || true)
|
||||
if [[ -n "${pass}" ]]; then
|
||||
DB_PASS="${pass}"
|
||||
info "Found database password from container env"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
|
||||
# Fallback: check for MariaDB/MySQL pod in same namespace
|
||||
if [[ -z "${DB_HOST}" ]]; then
|
||||
local db_pod=$(${KUBECTL_CMD} get pods -n "${APP_NAMESPACE}" -o name 2>/dev/null | grep -E '(mariadb|mysql)' | head -1 | cut -d/ -f2 || true)
|
||||
if [[ -n "${db_pod}" ]]; then
|
||||
DB_HOST="${db_pod}"
|
||||
info "Detected database pod: ${DB_HOST}"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Final validation
|
||||
if [[ -z "${DB_HOST}" ]] || [[ -z "${DB_PASS}" ]]; then
|
||||
warn "Could not fully auto-detect database credentials."
|
||||
warn "Please set DB_HOST, DB_PORT, DB_NAME, DB_USER, DB_PASS environment variables."
|
||||
warn "Or ensure the database secret is labeled correctly in Kubernetes."
|
||||
else
|
||||
info "Database: ${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
|
||||
fi
|
||||
}
|
||||
|
||||
detect_pvcs() {
|
||||
info "Detecting persistent volume claims..."
|
||||
|
||||
local pvcs=$(${KUBECTL_CMD} get pvc -n "${APP_NAMESPACE}" -o jsonpath='{range .items[*]}{.metadata.name}{" "}{.spec.volumeName}{"\n"}{end}' 2>/dev/null || true)
|
||||
|
||||
if [[ -n "${pvcs}" ]]; then
|
||||
echo "${pvcs}" | while read -r name volume; do
|
||||
info "Found PVC: ${name} -> ${volume}"
|
||||
case "${name}" in
|
||||
*upload*|*gallery*|*image*|*photo*)
|
||||
PVC_UPLOADS="${PVC_UPLOADS:-${name}}"
|
||||
;;
|
||||
*config*|*data*)
|
||||
PVC_CONFIG="${PVC_CONFIG:-${name}}"
|
||||
;;
|
||||
*gallery*)
|
||||
PVC_GALLERIES="${PVC_GALLERIES:-${name}}"
|
||||
;;
|
||||
esac
|
||||
done
|
||||
fi
|
||||
|
||||
# If not detected, try common names
|
||||
PVC_UPLOADS="${PVC_UPLOADS:-piwigo-uploads}"
|
||||
PVC_CONFIG="${PVC_CONFIG:-piwigo-config}"
|
||||
PVC_GALLERIES="${PVC_GALLERIES:-piwigo-galleries}"
|
||||
|
||||
info "Using PVCs: uploads=${PVC_UPLOADS}, config=${PVC_CONFIG}, galleries=${PVC_GALLERIES}"
|
||||
}
|
||||
|
||||
# =============================================================================
|
||||
@@ -233,7 +263,6 @@ backup_database() {
|
||||
|
||||
mkdir -p "${BACKUP_DIR}/database"
|
||||
|
||||
# Check if we have mariadb-dump or mysqldump
|
||||
local dump_cmd=""
|
||||
if command -v mariadb-dump >/dev/null 2>&1; then
|
||||
dump_cmd="mariadb-dump"
|
||||
@@ -245,7 +274,6 @@ backup_database() {
|
||||
|
||||
info "Using ${dump_cmd} for database dump"
|
||||
|
||||
# Build connection parameters
|
||||
local conn_params=(
|
||||
"-h" "${DB_HOST}"
|
||||
"-P" "${DB_PORT}"
|
||||
@@ -261,16 +289,12 @@ backup_database() {
|
||||
"--lock-tables=false"
|
||||
)
|
||||
|
||||
# Use password from environment or prompt
|
||||
if [[ -n "${DB_PASS}" ]]; then
|
||||
export MYSQL_PWD="${DB_PASS}"
|
||||
fi
|
||||
|
||||
export MYSQL_PWD="${DB_PASS}"
|
||||
info "Dumping database ${DB_NAME} from ${DB_HOST}:${DB_PORT}..."
|
||||
if ! ${dump_cmd} "${conn_params[@]}" "${DB_NAME}" > "${db_dump}" 2>>"${LOG_FILE}"; then
|
||||
unset MYSQL_PWD
|
||||
die "Database dump failed. Check credentials and connectivity."
|
||||
fi
|
||||
|
||||
unset MYSQL_PWD
|
||||
|
||||
# Compress the dump
|
||||
@@ -282,112 +306,35 @@ backup_database() {
|
||||
success "Database backup complete: ${db_dump} (${size})"
|
||||
|
||||
# Also create a schema-only dump for reference
|
||||
export MYSQL_PWD="${DB_PASS}"
|
||||
${dump_cmd} "${conn_params[@]}" --no-data "${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/piwigo_schema_${TIMESTAMP}.sql.gz"
|
||||
unset MYSQL_PWD
|
||||
}
|
||||
|
||||
backup_pvc() {
|
||||
local pvc_name="$1"
|
||||
backup_directory() {
|
||||
local src_path="$1"
|
||||
local dest_subdir="$2"
|
||||
local description="$3"
|
||||
|
||||
info "Backing up ${description} (PVC: ${pvc_name})..."
|
||||
info "Backing up ${description}..."
|
||||
|
||||
local dest_dir="${BACKUP_DIR}/${dest_subdir}"
|
||||
mkdir -p "${dest_dir}"
|
||||
|
||||
# Check if PVC exists
|
||||
if ! ${KUBECTL_CMD} get pvc "${pvc_name}" -n "${APP_NAMESPACE}" >/dev/null 2>&1; then
|
||||
warn "PVC ${pvc_name} not found in namespace ${APP_NAMESPACE}, skipping..."
|
||||
if [[ ! -d "${src_path}" ]]; then
|
||||
warn "Source path does not exist: ${src_path}"
|
||||
return 0
|
||||
fi
|
||||
|
||||
# Get the volume name
|
||||
local volume_name=$(${KUBECTL_CMD} get pvc "${pvc_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.spec.volumeName}' 2>/dev/null || true)
|
||||
|
||||
if [[ -z "${volume_name}" ]]; then
|
||||
warn "PVC ${pvc_name} is not bound to a volume, skipping..."
|
||||
return 0
|
||||
fi
|
||||
|
||||
info "PVC ${pvc_name} bound to volume ${volume_name}"
|
||||
|
||||
# Create a temporary pod to access the volume
|
||||
local pod_name="piwigo-backup-${pvc_name}-${TIMESTAMP}"
|
||||
local yaml_file="/tmp/${pod_name}.yaml"
|
||||
|
||||
cat > "${yaml_file}" <<EOF
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: ${pod_name}
|
||||
namespace: ${APP_NAMESPACE}
|
||||
spec:
|
||||
restartPolicy: Never
|
||||
containers:
|
||||
- name: backup
|
||||
image: alpine:latest
|
||||
command: ["/bin/sh", "-c", "sleep 3600"]
|
||||
volumeMounts:
|
||||
- name: data
|
||||
mountPath: /data
|
||||
volumes:
|
||||
- name: data
|
||||
persistentVolumeClaim:
|
||||
claimName: ${pvc_name}
|
||||
EOF
|
||||
|
||||
info "Creating temporary pod to access PVC..."
|
||||
${KUBECTL_CMD} apply -f "${yaml_file}" >>"${LOG_FILE}" 2>&1
|
||||
|
||||
# Wait for pod to be ready
|
||||
local retries=30
|
||||
while [[ ${retries} -gt 0 ]]; do
|
||||
local phase=$(${KUBECTL_CMD} get pod "${pod_name}" -n "${APP_NAMESPACE}" -o jsonpath='{.status.phase}' 2>/dev/null || echo "Unknown")
|
||||
if [[ "${phase}" == "Running" ]]; then
|
||||
break
|
||||
fi
|
||||
sleep 2
|
||||
((retries--))
|
||||
done
|
||||
|
||||
if [[ ${retries} -eq 0 ]]; then
|
||||
warn "Timeout waiting for backup pod, cleaning up..."
|
||||
${KUBECTL_CMD} delete pod "${pod_name}" -n "${APP_NAMESPACE}" --ignore-not-found=true >>"${LOG_FILE}" 2>&1
|
||||
rm -f "${yaml_file}"
|
||||
info "Copying from ${src_path} to ${dest_dir}..."
|
||||
if ! rsync -av "${src_path}/" "${dest_dir}/" 2>&1 | tee -a "${LOG_FILE}"; then
|
||||
warn "Failed to copy data from ${src_path}"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Copy data from pod
|
||||
info "Copying data from PVC..."
|
||||
if ! ${KUBECTL_CMD} cp "${APP_NAMESPACE}/${pod_name}:/data/." "${dest_dir}/" >>"${LOG_FILE}" 2>&1; then
|
||||
warn "Failed to copy data from PVC ${pvc_name}"
|
||||
else
|
||||
local count=$(find "${dest_dir}" -type f | wc -l)
|
||||
local size=$(du -sh "${dest_dir}" | cut -f1)
|
||||
success "${description} backup complete: ${count} files, ${size}"
|
||||
fi
|
||||
|
||||
# Cleanup
|
||||
info "Cleaning up temporary pod..."
|
||||
${KUBECTL_CMD} delete pod "${pod_name}" -n "${APP_NAMESPACE}" --ignore-not-found=true >>"${LOG_FILE}" 2>&1
|
||||
rm -f "${yaml_file}"
|
||||
}
|
||||
|
||||
backup_manifests() {
|
||||
info "Backing up Kubernetes manifests..."
|
||||
|
||||
local manifest_dir="${BACKUP_DIR}/manifests"
|
||||
mkdir -p "${manifest_dir}"
|
||||
|
||||
# Export all resources in the namespace
|
||||
${KUBECTL_CMD} get all,configmap,secret,pvc,ingress,serviceaccount,role,rolebinding -n "${APP_NAMESPACE}" -o yaml > "${manifest_dir}/all-resources.yaml" 2>>"${LOG_FILE}" || true
|
||||
|
||||
# Also export individually for easier reading
|
||||
for resource in deployment statefulset service configmap secret pvc ingress; do
|
||||
${KUBECTL_CMD} get "${resource}" -n "${APP_NAMESPACE}" -o yaml > "${manifest_dir}/${resource}s.yaml" 2>>"${LOG_FILE}" || true
|
||||
done
|
||||
|
||||
success "Manifests backed up to ${manifest_dir}"
|
||||
local count=$(find "${dest_dir}" -type f | wc -l)
|
||||
local size=$(du -sh "${dest_dir}" | cut -f1)
|
||||
success "${description} backup complete: ${count} files, ${size}"
|
||||
}
|
||||
|
||||
create_archive() {
|
||||
@@ -433,7 +380,7 @@ Piwigo Backup Restore Information
|
||||
=================================
|
||||
Backup Date: $(date)
|
||||
Backup Timestamp: ${TIMESTAMP}
|
||||
Source: TrueNAS App (namespace: ${APP_NAMESPACE})
|
||||
Source: TrueNAS 27+ App (direct dataset access)
|
||||
Database: ${DB_NAME} on ${DB_HOST}:${DB_PORT} as ${DB_USER}
|
||||
|
||||
Backup Contents:
|
||||
@@ -441,8 +388,7 @@ Backup Contents:
|
||||
- database/piwigo_schema_${TIMESTAMP}.sql.gz - Schema only
|
||||
- uploads/ - Piwigo uploads directory
|
||||
- config/ - Piwigo configuration (local/config/)
|
||||
- galleries/ - Piwigo galleries (if separate PVC)
|
||||
- manifests/ - Kubernetes resource definitions
|
||||
- galleries/ - Piwigo galleries (if separate path)
|
||||
|
||||
To restore to Docker:
|
||||
1. Copy the backup archive to your Docker host
|
||||
@@ -462,6 +408,11 @@ Volume Mounts for Docker:
|
||||
- config/ -> /var/www/html/local/config/
|
||||
- galleries/ -> /var/www/html/galleries/ (if used)
|
||||
|
||||
Source Paths Used:
|
||||
- Uploads: ${PIWIGO_UPLOADS_PATH:-<not set>}
|
||||
- Config: ${PIWIGO_CONFIG_PATH:-<not set>}
|
||||
- Galleries: ${PIWIGO_GALLERIES_PATH:-<not set>}
|
||||
|
||||
EOF
|
||||
|
||||
success "Restore info saved to ${BACKUP_DEST}/RESTORE_INFO_${TIMESTAMP}.txt"
|
||||
@@ -473,25 +424,22 @@ EOF
|
||||
|
||||
main() {
|
||||
info "======================================"
|
||||
info "Piwigo TrueNAS App Backup Started"
|
||||
info "Piwigo TrueNAS 27+ App Backup Started"
|
||||
info "======================================"
|
||||
info "Timestamp: ${TIMESTAMP}"
|
||||
info "Backup destination: ${BACKUP_DEST}"
|
||||
|
||||
info "Backup directory: ${BACKUP_DIR}"
|
||||
|
||||
# Detection phase
|
||||
detect_kubectl
|
||||
detect_namespace
|
||||
detect_database
|
||||
detect_pvcs
|
||||
# Validate prerequisites
|
||||
validate_paths
|
||||
validate_database
|
||||
|
||||
# Backup phase
|
||||
backup_manifests
|
||||
backup_database
|
||||
backup_pvc "${PVC_UPLOADS}" "uploads" "Uploads"
|
||||
backup_pvc "${PVC_CONFIG}" "config" "Configuration"
|
||||
backup_pvc "${PVC_GALLERIES}" "galleries" "Galleries"
|
||||
backup_directory "${PIWIGO_UPLOADS_PATH}" "uploads" "Uploads"
|
||||
backup_directory "${PIWIGO_CONFIG_PATH}" "config" "Configuration"
|
||||
backup_directory "${PIWIGO_GALLERIES_PATH}" "galleries" "Galleries"
|
||||
|
||||
# Archive and cleanup
|
||||
local archive=$(create_archive)
|
||||
|
||||
Reference in New Issue
Block a user