Update backup script to support multiple remote mountpoints

This commit is contained in:
Keannu Christian Bernasol 2025-07-05 21:35:17 +08:00
parent 852cbebb68
commit 38f2eeddfd

View file

@ -2,47 +2,73 @@
current_date=$(date "+%B %-d %Y%l:%M %p") current_date=$(date "+%B %-d %Y%l:%M %p")
env BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes # Export Borg environment variables
env BORG_RELOCATED_REPO_ACCESS_IS_OK=yes export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes
export BORG_RELOCATED_REPO_ACCESS_IS_OK=yes
echo "Timestamp: $current_date" echo "Timestamp: $current_date"
# Check if the required mount point exists # Define required mount points as key-value pairs (remote:local)
if ! df -h | grep -q "^//255\.255\.255\.255/SAMBA-MOUNT.*\/mnt/backups$"; then declare -A mount_points=(
echo "Error: Required mount point //255.255.255.255/SAMBA-MOUNT not found or not mounted at /mnt/backups." ["//10.0.10.169/pc-2"]="/mnt/backups_a"
["//10.0.10.115/pc-2"]="/mnt/backups_b"
)
# Function to check if a mount point is mounted
is_mounted() {
local remote="$1"
local local_path="$2"
if ! findmnt --target "$local_path" --source "$remote" &>/dev/null; then
echo "Error: $remote not mounted at $local_path"
return 1
fi
return 0
}
# Verify all required mount points
for remote in "${!mount_points[@]}"; do
local_path="${mount_points[$remote]}"
if ! is_mounted "$remote" "$local_path"; then
exit 1 exit 1
fi fi
done
# Backup function
function backup() { function backup() {
# $1 - Backup Name [[ -z "$1" || -z "$2" ]] && { echo "Missing arguments!"; exit 1; }
# $2 - Directory
# $3 - Extras
if [ "$1" == "" ] || [ "$2" == "" ]; then local backup_name="$1"
echo "Missing arguments!" local source_dir="$2"
exit shift 2
local extras=("$@")
# Iterate through all mount points
for local_mount in "${mount_points[@]}"; do
local repo_path="${local_mount}/${backup_name}"
echo "Starting backups for '$backup_name' at $local_mount"
# Initialize repository if missing
if [[ ! -d "$repo_path" ]]; then
echo "Initializing new repository: $repo_path"
borg init --encryption=none "$repo_path"
fi fi
# Root backup directory # Perform backup
root_directory="/mnt/backups/" echo "Backing up $source_dir to $repo_path"
borg create --stats --progress --compression lz4 "$repo_path"::"$current_date" \
"$source_dir" "${extras[@]}"
echo "Starting backups for: $1" # Cleanup old backups
echo "Cleaning old backups at $repo_path"
borg prune --stats "$repo_path" -d 6
borg compact "$repo_path"
# Check if the backup directory exists echo "Backup for '$backup_name' completed at $local_mount"
if [ ! -d "$root_directory/$1" ]; then done
echo "Backup directory does not exist for $1. Initializing one"
borg init --encryption=none "$root_directory/$1"
else
echo "Backup directory already exists for $1"
fi
borg create --stats --progress --compression lz4 "$root_directory/$1"::"$current_date" "$2" $3
echo "Cleaning old backups"
borg prune --stats "$root_directory/$1" -d 6
borg compact "$root_directory/$1"
echo "Backup for $1 finished"
} }
## Docker Projects ## Docker Projects
## Root Docker Projects Directory ## Root Docker Projects Directory