Files
media-server/nextcloud_backup.sh
T

92 lines
4.7 KiB
Bash

#!/bin/bash
# ==============================================================================
# Script: nextcloud_backup.sh
# Description: This script securely backs up NextCloud to a designated
# mounted drive using rsync. It includes pre-run checks to ensure
# the source files and destination are valid, and sends ntfy
# notifications for each task.
# Usage: sudo ./nextcloud_backup.sh
# Notes: This script may require sudo/root privileges to access files.
# ==============================================================================
# ==============================================================================
# Define Variables for Source and Destination
# ==============================================================================
# This makes the script easier to read and modify.
BACKUP_MOUNT_POINT="/mnt/5TB-Disk1"
BACKUP_DESTINATION="$BACKUP_MOUNT_POINT/backup"
# NextCloud backup directories
NEXTCLOUD_BACKUP_SOURCE="/mnt/26TB-Disk1/ncdata/borg"
NEXTCLOUD_DESTINATION="$BACKUP_MOUNT_POINT/backup/services/nextcloud"
# ntfy Notification Configuration
# To use this feature, you must have the 'curl' command installed.
# NTFY_TOPIC is the topic you wish to send notifications to.
# NTFY_SERVER is the URL of your ntfy server.
# NTFY_TOKEN is the authorization token for your ntfy server.
NTFY_TOPIC="Server"
NTFY_SERVER="https://ntfy.speerfam.net"
NTFY_TOKEN="tk_3xo9mejjtgemyowzckqw2souo3mux"
# ==============================================================================
# Pre-flight Checks (for the destination drive)
# ==============================================================================
# Check if the mount point directory exists and is a mounted filesystem.
# This is a critical check to ensure we are writing to the intended air-gapped
# drive and not to a local directory if the mount failed.
if ! mountpoint -q "$BACKUP_MOUNT_POINT"; then
echo "Error: '$BACKUP_MOUNT_POINT' is not a mount point. Please ensure the drive is mounted." >&2
# Send a failure notification
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Destination '$BACKUP_MOUNT_POINT' is not mounted." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# Check if the destination directories exist and create them if they don't.
# This prevents rsync from failing on the first run.
# mkdir -p "$BACKUP_DESTINATION"
# mkdir -p "$PHOTOS_DESTINATION"
# ==============================================================================
# Task 8: Sync Nextcloud backup
# ==============================================================================
echo "Starting Nextcloud backup process..."
# This section is commented out because nextcloud is configured to perform automated daily backups
# Step 1: Execute the Docker command to create the backup.
# echo "Running Nextcloud AIO daily backup script via Docker..."
# sudo docker exec --env DAILY_BACKUP=1 nextcloud-aio-mastercontainer /daily-backup.sh
# if [ $? -ne 0 ]; then
# echo "Error: Docker command failed to create Nextcloud backup." >&2
# curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Docker command failed to create Nextcloud backup." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# exit 1
# fi
# echo "Nextcloud backup script completed successfully."
# curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Nextcloud daily backup created. Starting rsync." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
# Step 1: Rsync the Nextcloud backup directory to the destination.
echo "Starting rsync of '$NEXTCLOUD_BACKUP_SOURCE' to '$NEXTCLOUD_DESTINATION'..."
# Check if the Nextcloud backup source directory exists.
if [ ! -d "$NEXTCLOUD_BACKUP_SOURCE" ]; then
echo "Error: Nextcloud backup source directory '$NEXTCLOUD_BACKUP_SOURCE' not found." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: Script failed. Nextcloud backup source not found." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi
# The trailing slash on NEXTCLOUD_BACKUP_SOURCE is crucial. It tells rsync to
# copy the *contents* of the directory, not the directory itself.
rsync -av --progress "$NEXTCLOUD_BACKUP_SOURCE" "$NEXTCLOUD_DESTINATION"
# Post-rsync status check for the Nextcloud backup directory.
if [ $? -eq 0 ]; then
echo "Success: Nextcloud backup directory has been successfully copied to '$NEXTCLOUD_DESTINATION'."
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Success: Nextcloud backup copied to $NEXTCLOUD_DESTINATION" "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
else
echo "Error: rsync failed to copy the Nextcloud backup directory. Please check permissions." >&2
curl -H "Authorization: Bearer $NTFY_TOKEN" -d "Error: rsync failed to copy Nextcloud backup directory." "$NTFY_SERVER/$NTFY_TOPIC" > /dev/null 2>&1
exit 1
fi