Init backup_incus_instances.sh

This commit is contained in:
Benoit 2024-09-28 01:43:51 +09:00
parent 5c9da26aa5
commit 781720989c
Signed by: Benoit
SSH key fingerprint: SHA256:kFsX94Kq6z/6CY0dX+7/FpAeJC0QlMhJVY+B7NYrOmA

View file

@ -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 <forgejo@benoit.jp.net>
# 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