614 lines
24 KiB
Bash
614 lines
24 KiB
Bash
#!/bin/bash
|
|
# =============================================================================
|
|
# Nextcloud Backup Script for TrueNAS 27+ App -> Docker Migration
|
|
# =============================================================================
|
|
# This script backs up a Nextcloud 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 27+ with Nextcloud app installed
|
|
# - PostgreSQL client (pg_dump) or MariaDB/MySQL client (mariadb-dump/mysqldump)
|
|
# - Write access to backup destination dataset
|
|
# - Read access to Nextcloud app datasets (configured in app settings)
|
|
# =============================================================================
|
|
|
|
set -euo pipefail
|
|
|
|
# =============================================================================
|
|
# CONFIGURATION - Adjust these values for your environment
|
|
# =============================================================================
|
|
|
|
# Backup destination (TrueNAS dataset path)
|
|
BACKUP_DEST="${BACKUP_DEST:-/mnt/tank/backups/nextcloud}"
|
|
|
|
# ---- Database Configuration ----
|
|
DB_TYPE="${DB_TYPE:-postgresql}" # postgresql or mariadb
|
|
DB_HOST="${DB_HOST:-localhost}"
|
|
DB_PORT="${DB_PORT:-}" # Auto: 5432 for PG, 3306 for MariaDB
|
|
DB_NAME="${DB_NAME:-nextcloud}"
|
|
DB_USER="${DB_USER:-nextcloud}"
|
|
DB_PASS="${DB_PASS:-}" # Required!
|
|
|
|
# ---- Nextcloud Data Paths (Host filesystem paths) ----
|
|
# These are the dataset paths you configured in the Nextcloud app settings
|
|
# Example: /mnt/tank/apps/nextcloud/data
|
|
# Example: /mnt/tank/apps/nextcloud/config
|
|
# Example: /mnt/tank/apps/nextcloud/custom_apps
|
|
NEXTCLOUD_DATA_PATH="${NEXTCLOUD_DATA_PATH:-}"
|
|
NEXTCLOUD_CONFIG_PATH="${NEXTCLOUD_CONFIG_PATH:-}"
|
|
NEXTCLOUD_APPS_PATH="${NEXTCLOUD_APPS_PATH:-}"
|
|
|
|
# Retention: keep last N backups
|
|
RETENTION_COUNT="${RETENTION_COUNT:-7}"
|
|
|
|
# Compression level (1-9, higher = smaller but slower)
|
|
COMPRESSION_LEVEL="${COMPRESSION_LEVEL:-6}"
|
|
|
|
# Put Nextcloud in maintenance mode during backup
|
|
MAINTENANCE_MODE="${MAINTENANCE_MODE:-true}"
|
|
|
|
# =============================================================================
|
|
# INTERNAL VARIABLES
|
|
# =============================================================================
|
|
|
|
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
|
|
BACKUP_DIR="${BACKUP_DEST}/nextcloud_backup_${TIMESTAMP}"
|
|
mkdir -p "${BACKUP_DIR}"
|
|
LOG_FILE="${BACKUP_DIR}/backup.log"
|
|
|
|
# Debug: show config values
|
|
echo "DEBUG: BACKUP_DEST='${BACKUP_DEST}'" >&2
|
|
echo "DEBUG: BACKUP_DIR='${BACKUP_DIR}'" >&2
|
|
|
|
# Colors for output
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
BLUE='\033[0;34m'
|
|
NC='\033[0m'
|
|
|
|
# =============================================================================
|
|
# HELPER FUNCTIONS
|
|
# =============================================================================
|
|
|
|
log() {
|
|
local level="$1"
|
|
shift
|
|
local msg="$*"
|
|
local timestamp=$(date +"%Y-%m-%d %H:%M:%S")
|
|
echo -e "${timestamp} [${level}] ${msg}" | tee -a "${LOG_FILE}"
|
|
}
|
|
|
|
info() { log "INFO" "${BLUE}$*${NC}"; }
|
|
warn() { log "WARN" "${YELLOW}$*${NC}"; }
|
|
error() { log "ERROR" "${RED}$*${NC}"; }
|
|
success() { log "SUCCESS" "${GREEN}$*${NC}"; }
|
|
|
|
die() {
|
|
error "$*"
|
|
exit 1
|
|
}
|
|
|
|
# =============================================================================
|
|
# VALIDATION FUNCTIONS
|
|
# =============================================================================
|
|
|
|
validate_paths() {
|
|
info "Validating data paths..."
|
|
|
|
local has_paths=false
|
|
|
|
if [[ -n "${NEXTCLOUD_DATA_PATH}" ]] && [[ -d "${NEXTCLOUD_DATA_PATH}" ]]; then
|
|
info "Found data path: ${NEXTCLOUD_DATA_PATH}"
|
|
has_paths=true
|
|
elif [[ -n "${NEXTCLOUD_DATA_PATH}" ]]; then
|
|
warn "Data path does not exist: ${NEXTCLOUD_DATA_PATH}"
|
|
fi
|
|
|
|
if [[ -n "${NEXTCLOUD_CONFIG_PATH}" ]] && [[ -d "${NEXTCLOUD_CONFIG_PATH}" ]]; then
|
|
info "Found config path: ${NEXTCLOUD_CONFIG_PATH}"
|
|
has_paths=true
|
|
elif [[ -n "${NEXTCLOUD_CONFIG_PATH}" ]]; then
|
|
warn "Config path does not exist: ${NEXTCLOUD_CONFIG_PATH}"
|
|
fi
|
|
|
|
if [[ -n "${NEXTCLOUD_APPS_PATH}" ]] && [[ -d "${NEXTCLOUD_APPS_PATH}" ]]; then
|
|
info "Found apps path: ${NEXTCLOUD_APPS_PATH}"
|
|
has_paths=true
|
|
elif [[ -n "${NEXTCLOUD_APPS_PATH}" ]]; then
|
|
warn "Apps path does not exist: ${NEXTCLOUD_APPS_PATH}"
|
|
fi
|
|
|
|
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 Nextcloud data paths found. Please set NEXTCLOUD_DATA_PATH, NEXTCLOUD_CONFIG_PATH, NEXTCLOUD_APPS_PATH environment variables to the dataset paths configured in your Nextcloud app."
|
|
fi
|
|
}
|
|
|
|
detect_paths() {
|
|
local common_bases=(
|
|
"/mnt/tank/apps/nextcloud"
|
|
"/mnt/pool/apps/nextcloud"
|
|
"/mnt/data/apps/nextcloud"
|
|
"/mnt/storage/apps/nextcloud"
|
|
"/mnt/apps/nextcloud"
|
|
)
|
|
|
|
for base in "${common_bases[@]}"; do
|
|
if [[ -d "${base}" ]]; then
|
|
info "Found potential base path: ${base}"
|
|
|
|
if [[ -d "${base}/data" ]] && [[ -z "${NEXTCLOUD_DATA_PATH}" ]]; then
|
|
NEXTCLOUD_DATA_PATH="${base}/data"
|
|
info "Auto-detected data: ${NEXTCLOUD_DATA_PATH}"
|
|
fi
|
|
if [[ -d "${base}/config" ]] && [[ -z "${NEXTCLOUD_CONFIG_PATH}" ]]; then
|
|
NEXTCLOUD_CONFIG_PATH="${base}/config"
|
|
info "Auto-detected config: ${NEXTCLOUD_CONFIG_PATH}"
|
|
fi
|
|
if [[ -d "${base}/custom_apps" ]] && [[ -z "${NEXTCLOUD_APPS_PATH}" ]]; then
|
|
NEXTCLOUD_APPS_PATH="${base}/custom_apps"
|
|
info "Auto-detected custom apps: ${NEXTCLOUD_APPS_PATH}"
|
|
fi
|
|
if [[ -d "${base}/apps" ]] && [[ -z "${NEXTCLOUD_APPS_PATH}" ]]; then
|
|
NEXTCLOUD_APPS_PATH="${base}/apps"
|
|
info "Auto-detected apps: ${NEXTCLOUD_APPS_PATH}"
|
|
fi
|
|
|
|
if [[ -n "${NEXTCLOUD_DATA_PATH}" ]] || [[ -n "${NEXTCLOUD_CONFIG_PATH}" ]] || [[ -n "${NEXTCLOUD_APPS_PATH}" ]]; then
|
|
return 0
|
|
fi
|
|
fi
|
|
done
|
|
}
|
|
|
|
validate_database() {
|
|
info "Validating database connection..."
|
|
|
|
if [[ -z "${DB_PORT}" ]]; then
|
|
if [[ "${DB_TYPE}" == "postgresql" ]]; then
|
|
DB_PORT="5432"
|
|
else
|
|
DB_PORT="3306"
|
|
fi
|
|
fi
|
|
|
|
if [[ -z "${DB_PASS}" ]]; then
|
|
warn "DB_PASS not set. Trying to find it..."
|
|
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
|
|
|
|
if [[ "${DB_TYPE}" == "postgresql" ]]; then
|
|
if command -v pg_dump >/dev/null 2>&1; then
|
|
export PGPASSWORD="${DB_PASS}"
|
|
if ! pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists >/dev/null 2>&1; then
|
|
unset PGPASSWORD
|
|
die "Cannot connect to PostgreSQL at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials and ensure database is accessible from host."
|
|
fi
|
|
unset PGPASSWORD
|
|
else
|
|
# No host tools, try docker exec
|
|
info "Host pg_dump not found, trying docker exec..."
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
die "Neither pg_dump nor docker found. Cannot dump database."
|
|
fi
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|nextcloud.*db)' | head -1 || true)
|
|
if [[ -z "${db_container}" ]]; then
|
|
die "No PostgreSQL container found. Cannot dump database."
|
|
fi
|
|
info "Using database container: ${db_container}"
|
|
if ! docker exec -e PGPASSWORD="${DB_PASS}" "${db_container}" pg_isready -U "${DB_USER}" -d "${DB_NAME}" >/dev/null 2>&1; then
|
|
die "Cannot connect to PostgreSQL in container ${db_container}. Check credentials."
|
|
fi
|
|
fi
|
|
else
|
|
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"
|
|
fi
|
|
|
|
if [[ -n "${dump_cmd}" ]]; then
|
|
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 MariaDB/MySQL at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials."
|
|
fi
|
|
unset MYSQL_PWD
|
|
else
|
|
# No host tools, try docker exec
|
|
info "Host mariadb-dump/mysqldump not found, trying docker exec..."
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
|
|
fi
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|nextcloud.*db)' | head -1 || true)
|
|
if [[ -z "${db_container}" ]]; then
|
|
die "No MariaDB/MySQL container found. Cannot dump database."
|
|
fi
|
|
info "Using database container: ${db_container}"
|
|
if ! docker exec "${db_container}" mariadb -u "${DB_USER}" -p"${DB_PASS}" -e "SELECT 1" >/dev/null 2>&1; then
|
|
die "Cannot connect to MariaDB/MySQL in container ${db_container}. Check credentials."
|
|
fi
|
|
fi
|
|
fi
|
|
success "Database connection validated"
|
|
}
|
|
|
|
find_db_password() {
|
|
local possible_files=(
|
|
"/mnt/tank/apps/nextcloud/.env"
|
|
"/mnt/pool/apps/nextcloud/.env"
|
|
"/mnt/data/apps/nextcloud/.env"
|
|
"/mnt/storage/apps/nextcloud/.env"
|
|
"/mnt/apps/nextcloud/.env"
|
|
"/etc/nextcloud/db_password"
|
|
"/root/.nextcloud_db_password"
|
|
)
|
|
|
|
for file in "${possible_files[@]}"; do
|
|
if [[ -f "${file}" ]]; then
|
|
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
|
|
done
|
|
|
|
if command -v docker >/dev/null 2>&1; then
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|mariadb|mysql|nextcloud.*db)' | head -1 || true)
|
|
if [[ -n "${db_container}" ]]; then
|
|
info "Found potential DB container: ${db_container}"
|
|
local pass=$(docker inspect "${db_container}" 2>/dev/null | grep -iE '(POSTGRES_PASSWORD|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
|
|
}
|
|
|
|
# =============================================================================
|
|
# BACKUP FUNCTIONS
|
|
# =============================================================================
|
|
|
|
enable_maintenance_mode() {
|
|
if [[ "${MAINTENANCE_MODE}" != "true" ]]; then
|
|
return 0
|
|
fi
|
|
info "Enabling Nextcloud maintenance mode..."
|
|
|
|
# Try to find Nextcloud container and run occ
|
|
if command -v docker >/dev/null 2>&1; then
|
|
local nc_container=$(docker ps --format '{{.Names}}' | grep -iE 'nextcloud' | grep -vE '(db|redis|cron)' | head -1 || true)
|
|
if [[ -n "${nc_container}" ]]; then
|
|
if docker exec "${nc_container}" php occ maintenance:mode --on 2>>"${LOG_FILE}"; then
|
|
success "Maintenance mode enabled"
|
|
return 0
|
|
else
|
|
warn "Failed to enable maintenance mode via container"
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
# Fallback: write .maintenance file if we have config path
|
|
if [[ -n "${NEXTCLOUD_CONFIG_PATH}" ]] && [[ -f "${NEXTCLOUD_CONFIG_PATH}/config.php" ]]; then
|
|
# Use sed to enable maintenance mode in config.php
|
|
sed -i "s/'maintenance' => false/'maintenance' => true/" "${NEXTCLOUD_CONFIG_PATH}/config.php" 2>/dev/null || true
|
|
info "Attempted to enable maintenance mode via config.php"
|
|
fi
|
|
}
|
|
|
|
disable_maintenance_mode() {
|
|
if [[ "${MAINTENANCE_MODE}" != "true" ]]; then
|
|
return 0
|
|
fi
|
|
info "Disabling Nextcloud maintenance mode..."
|
|
|
|
if command -v docker >/dev/null 2>&1; then
|
|
local nc_container=$(docker ps --format '{{.Names}}' | grep -iE 'nextcloud' | grep -vE '(db|redis|cron)' | head -1 || true)
|
|
if [[ -n "${nc_container}" ]]; then
|
|
docker exec "${nc_container}" php occ maintenance:mode --off 2>>"${LOG_FILE}" || warn "Failed to disable maintenance mode via container"
|
|
return 0
|
|
fi
|
|
fi
|
|
|
|
if [[ -n "${NEXTCLOUD_CONFIG_PATH}" ]] && [[ -f "${NEXTCLOUD_CONFIG_PATH}/config.php" ]]; then
|
|
sed -i "s/'maintenance' => true/'maintenance' => false/" "${NEXTCLOUD_CONFIG_PATH}/config.php" 2>/dev/null || true
|
|
info "Attempted to disable maintenance mode via config.php"
|
|
fi
|
|
}
|
|
|
|
backup_database() {
|
|
info "Backing up ${DB_TYPE} database..."
|
|
|
|
local db_dump="${BACKUP_DIR}/database/nextcloud_${TIMESTAMP}.sql"
|
|
mkdir -p "${BACKUP_DIR}/database"
|
|
|
|
if [[ "${DB_TYPE}" == "postgresql" ]]; then
|
|
if command -v pg_dump >/dev/null 2>&1; then
|
|
info "Using pg_dump for database dump"
|
|
export PGPASSWORD="${DB_PASS}"
|
|
if ! pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" \
|
|
--no-owner --no-privileges --clean --if-exists \
|
|
> "${db_dump}" 2>>"${LOG_FILE}"; then
|
|
unset PGPASSWORD
|
|
die "PostgreSQL dump failed"
|
|
fi
|
|
unset PGPASSWORD
|
|
else
|
|
# Use docker exec
|
|
info "Host pg_dump not found, using docker exec..."
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
die "Neither pg_dump nor docker found. Cannot dump database."
|
|
fi
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|nextcloud.*db)' | head -1 || true)
|
|
if [[ -z "${db_container}" ]]; then
|
|
die "No PostgreSQL container found. Cannot dump database."
|
|
fi
|
|
info "Using docker exec on container: ${db_container}"
|
|
info "Dumping database ${DB_NAME}..."
|
|
if ! docker exec -e PGPASSWORD="${DB_PASS}" "${db_container}" pg_dump -U "${DB_USER}" -d "${DB_NAME}" \
|
|
--no-owner --no-privileges --clean --if-exists \
|
|
> "${db_dump}" 2>>"${LOG_FILE}"; then
|
|
die "PostgreSQL dump failed via docker exec."
|
|
fi
|
|
fi
|
|
else
|
|
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"
|
|
fi
|
|
|
|
if [[ -n "${dump_cmd}" ]]; then
|
|
info "Using ${dump_cmd} for database dump"
|
|
|
|
local conn_params=(
|
|
"-h" "${DB_HOST}"
|
|
"-P" "${DB_PORT}"
|
|
"-u" "${DB_USER}"
|
|
"--single-transaction"
|
|
"--routines"
|
|
"--triggers"
|
|
"--events"
|
|
"--hex-blob"
|
|
"--complete-insert"
|
|
"--extended-insert"
|
|
"--quick"
|
|
"--lock-tables=false"
|
|
)
|
|
|
|
export MYSQL_PWD="${DB_PASS}"
|
|
if ! ${dump_cmd} "${conn_params[@]}" "${DB_NAME}" > "${db_dump}" 2>>"${LOG_FILE}"; then
|
|
unset MYSQL_PWD
|
|
die "MariaDB/MySQL dump failed"
|
|
fi
|
|
unset MYSQL_PWD
|
|
else
|
|
# Use docker exec
|
|
info "Host mariadb-dump/mysqldump not found, using docker exec..."
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
|
|
fi
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|nextcloud.*db)' | head -1 || true)
|
|
if [[ -z "${db_container}" ]]; then
|
|
die "No MariaDB/MySQL container found. Cannot dump database."
|
|
fi
|
|
info "Using docker exec on container: ${db_container}"
|
|
info "Dumping database ${DB_NAME}..."
|
|
if ! docker exec "${db_container}" mariadb-dump -u "${DB_USER}" -p"${DB_PASS}" \
|
|
--single-transaction --routines --triggers --events --hex-blob \
|
|
--complete-insert --extended-insert --quick --lock-tables=false \
|
|
"${DB_NAME}" > "${db_dump}" 2>>"${LOG_FILE}"; then
|
|
die "MariaDB/MySQL dump failed via docker exec."
|
|
fi
|
|
fi
|
|
fi
|
|
|
|
info "Compressing database dump..."
|
|
gzip -${COMPRESSION_LEVEL} "${db_dump}"
|
|
db_dump="${db_dump}.gz"
|
|
|
|
local size=$(du -h "${db_dump}" | cut -f1)
|
|
success "Database backup complete: ${db_dump} (${size})"
|
|
|
|
# Schema-only dump
|
|
if [[ "${DB_TYPE}" == "postgresql" ]]; then
|
|
if command -v pg_dump >/dev/null 2>&1; then
|
|
export PGPASSWORD="${DB_PASS}"
|
|
pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists --schema-only | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
|
|
unset PGPASSWORD
|
|
else
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|nextcloud.*db)' | head -1 || true)
|
|
if [[ -n "${db_container}" ]]; then
|
|
docker exec -e PGPASSWORD="${DB_PASS}" "${db_container}" pg_dump -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists --schema-only | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
|
|
fi
|
|
fi
|
|
else
|
|
if [[ -n "${dump_cmd}" ]]; then
|
|
export MYSQL_PWD="${DB_PASS}"
|
|
${dump_cmd} "${conn_params[@]}" --no-data "${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
|
|
unset MYSQL_PWD
|
|
else
|
|
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|nextcloud.*db)' | head -1 || true)
|
|
if [[ -n "${db_container}" ]]; then
|
|
docker exec "${db_container}" mariadb-dump -u "${DB_USER}" -p"${DB_PASS}" \
|
|
--no-data --single-transaction --routines --triggers --events \
|
|
"${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
|
|
fi
|
|
fi
|
|
fi
|
|
}
|
|
|
|
backup_directory() {
|
|
local src_path="$1"
|
|
local dest_subdir="$2"
|
|
local description="$3"
|
|
|
|
info "Backing up ${description}..."
|
|
|
|
local dest_dir="${BACKUP_DIR}/${dest_subdir}"
|
|
mkdir -p "${dest_dir}"
|
|
|
|
if [[ ! -d "${src_path}" ]]; then
|
|
warn "Source path does not exist: ${src_path}"
|
|
return 0
|
|
fi
|
|
|
|
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
|
|
|
|
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() {
|
|
info "Creating compressed archive..."
|
|
|
|
local archive_name="nextcloud_backup_${TIMESTAMP}.tar.gz"
|
|
local archive_path="${BACKUP_DEST}/${archive_name}"
|
|
|
|
tar -czf "${archive_path}" -C "${BACKUP_DEST}" "nextcloud_backup_${TIMESTAMP}" 2>>"${LOG_FILE}"
|
|
|
|
local size=$(du -h "${archive_path}" | cut -f1)
|
|
success "Archive created: ${archive_path} (${size})"
|
|
|
|
sha256sum "${archive_path}" > "${archive_path}.sha256"
|
|
info "SHA256: $(cat "${archive_path}.sha256")"
|
|
|
|
info "Cleaning up uncompressed backup directory..."
|
|
rm -rf "${BACKUP_DIR}"
|
|
|
|
echo "${archive_path}"
|
|
}
|
|
|
|
cleanup_old_backups() {
|
|
info "Cleaning up old backups (keeping last ${RETENTION_COUNT})..."
|
|
|
|
cd "${BACKUP_DEST}"
|
|
ls -1t nextcloud_backup_*.tar.gz 2>/dev/null | tail -n +$((RETENTION_COUNT + 1)) | while read -r file; do
|
|
info "Removing old backup: ${file}"
|
|
rm -f "${file}" "${file}.sha256"
|
|
done
|
|
|
|
success "Cleanup complete"
|
|
}
|
|
|
|
generate_restore_info() {
|
|
info "Generating restore information..."
|
|
|
|
cat > "${BACKUP_DEST}/RESTORE_INFO_${TIMESTAMP}.txt" <<EOF
|
|
Nextcloud Backup Restore Information
|
|
====================================
|
|
Backup Date: $(date)
|
|
Backup Timestamp: ${TIMESTAMP}
|
|
Source: TrueNAS 27+ App (direct dataset access)
|
|
Database: ${DB_TYPE} ${DB_NAME} on ${DB_HOST}:${DB_PORT} as ${DB_USER}
|
|
|
|
Backup Contents:
|
|
- database/nextcloud_${TIMESTAMP}.sql.gz - Full database dump
|
|
- database/nextcloud_schema_${TIMESTAMP}.sql.gz - Schema only
|
|
- data/ - Nextcloud data directory (user files)
|
|
- config/ - Nextcloud config directory (config.php)
|
|
- apps/ - Nextcloud custom apps (if separate path)
|
|
|
|
To restore to Docker:
|
|
1. Copy the backup archive to your Docker host
|
|
2. Run the restore script: ./restore-nextcloud-docker.sh <backup_archive.tar.gz>
|
|
3. Follow the prompts
|
|
|
|
Database Connection for Docker:
|
|
- Host: <your-docker-db-host> (usually 'db' or 'postgres'/'mariadb' in docker-compose)
|
|
- Port: ${DB_PORT}
|
|
- Database: nextcloud
|
|
- User: nextcloud
|
|
- Password: <set in docker-compose .env>
|
|
|
|
Volume Mounts for Docker:
|
|
- ./nextcloud-data:/var/www/html
|
|
- data/ -> /var/www/html/data/
|
|
- config/ -> /var/www/html/config/
|
|
- apps/ -> /var/www/html/custom_apps/ (if used)
|
|
|
|
Important Nextcloud-specific notes:
|
|
1. Run 'occ maintenance:mode --off' after restore if needed
|
|
2. Run 'occ upgrade' after first startup if version changed
|
|
3. Check config.php for 'trusted_domains' - update for new hostname
|
|
4. If using Redis, configure in docker-compose
|
|
5. If using Cron, set up cron job in Docker
|
|
|
|
Source Paths Used:
|
|
- Data: ${NEXTCLOUD_DATA_PATH:-<not set>}
|
|
- Config: ${NEXTCLOUD_CONFIG_PATH:-<not set>}
|
|
- Apps: ${NEXTCLOUD_APPS_PATH:-<not set>}
|
|
|
|
EOF
|
|
|
|
success "Restore info saved to ${BACKUP_DEST}/RESTORE_INFO_${TIMESTAMP}.txt"
|
|
}
|
|
|
|
# =============================================================================
|
|
# MAIN EXECUTION
|
|
# =============================================================================
|
|
|
|
main() {
|
|
info "======================================"
|
|
info "Nextcloud TrueNAS 27+ App Backup Started"
|
|
info "======================================"
|
|
info "Timestamp: ${TIMESTAMP}"
|
|
info "Backup destination: ${BACKUP_DEST}"
|
|
|
|
info "Backup directory: ${BACKUP_DIR}"
|
|
|
|
validate_paths
|
|
validate_database
|
|
|
|
enable_maintenance_mode
|
|
backup_database
|
|
backup_directory "${NEXTCLOUD_DATA_PATH}" "data" "Data directory"
|
|
backup_directory "${NEXTCLOUD_CONFIG_PATH}" "config" "Configuration"
|
|
backup_directory "${NEXTCLOUD_APPS_PATH}" "apps" "Custom apps"
|
|
disable_maintenance_mode
|
|
|
|
local archive=$(create_archive)
|
|
cleanup_old_backups
|
|
generate_restore_info
|
|
|
|
info "======================================"
|
|
success "Backup completed successfully!"
|
|
info "======================================"
|
|
info "Archive: ${archive}"
|
|
info "Checksum: ${archive}.sha256"
|
|
info "Restore info: ${BACKUP_DEST}/RESTORE_INFO_${TIMESTAMP}.txt"
|
|
info ""
|
|
info "Next steps:"
|
|
info "1. Copy ${archive} to your Docker host"
|
|
info "2. Run the restore script on the Docker host"
|
|
info "3. Verify the restored Nextcloud instance"
|
|
}
|
|
|
|
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
|
|
main "$@"
|
|
fi |