diff --git a/incus/backup_incus_instances.sh b/incus/backup_incus_instances.sh new file mode 100644 index 0000000..3789e8f --- /dev/null +++ b/incus/backup_incus_instances.sh @@ -0,0 +1,63 @@ +#!/usr/bin/env bash +# vim: set tabstop=2 softtabstop=2 shiftwidth=2 expandtab: +# +# Script Name: backup_incus_instances.sh +# Description: This script backs up all Incus instances +# to a specified backup directory. It is designed +# to be invoked by Borgmatic hooks. +# +# Author: Benoit +# Website: benoit.jp.net +# License: MIT +# +# Dependencies: incus +# Written with the help of ChatGPT +# + +# Strict mode +set -euo pipefail + +# Set the backup directory +BACKUP_DIR="/var/backups/incus" + +# Create the backup directory if it doesn't exist +mkdir -p "$BACKUP_DIR" + +# Get the list of instances and check for errors +instances=$(incus ls -f csv -c n 2>/dev/null) +if [ $? -ne 0 ]; then + echo "Error: Failed to list storage instances." >&2 + exit 1 +fi + +# Check if there are any instances to back up +if [ -z "$instances" ]; then + echo "No instances found to back up." + exit 0 +fi + +# Initialize a success flag +all_backups_successful=true + +# Loop through each instance and back it up +while IFS=, read -r name; do + echo "Backing up instance: $name" + + if incus export "$name" \ + --compression=none --optimized-storage --instance-only \ + "$BACKUP_DIR/${name}.tar" &> /dev/null; then + + echo "Successfully backed up: $name" + else + echo "Error backing up instance: $name" >&2 + all_backups_successful=false + fi +done <<< "$instances" + +# Final message based on backup success +if $all_backups_successful; then + echo "Backup completed successfully. All instances have been backed up to $BACKUP_DIR." +else + echo "Backup completed with errors. Some instances may not have been backed up." +fi +