diff --git a/incus/backup_incus_volumes.sh b/incus/backup_incus_volumes.sh new file mode 100644 index 0000000..95c2f27 --- /dev/null +++ b/incus/backup_incus_volumes.sh @@ -0,0 +1,70 @@ +#!/usr/bin/env bash +# vim: set tabstop=2 softtabstop=2 shiftwidth=2 expandtab: +# +# Script Name: backup_incus_volumes.sh +# Description: This script backs up all Incus storage volumes +# listed in the 'default' storage pool 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 volumes and check for errors +volumes=$(incus storage volume list default -f csv -c t,n,u 2>/dev/null) +if [ $? -ne 0 ]; then + echo "Error: Failed to list storage volumes." >&2 + exit 1 +fi + +# Check if there are any volumes to back up +if [ -z "$volumes" ]; then + echo "No volumes found to back up." + exit 0 +fi + +# Initialize a success flag +all_backups_successful=true + +# Loop through each volume and back it up +while IFS=, read -r type name used; do + # Ignore images, containers, and unused volumes + if [[ "$type" == "image" || "$type" == "container" \ + || "$type" == "virtual-machine" || "$used" -eq 0 ]]; then + + continue + fi + + echo "Backing up volume: $name" + + if incus storage volume export default "$name" \ + --compression=none --optimized-storage --volume-only \ + "$BACKUP_DIR/${name}.tar" &> /dev/null; then + + echo "Successfully backed up: $name" + else + echo "Error backing up volume: $name" >&2 + all_backups_successful=false + fi +done <<< "$volumes" + +# Final message based on backup success +if $all_backups_successful; then + echo "Backup completed successfully. All volumes have been backed up to $BACKUP_DIR." +else + echo "Backup completed with errors. Some volumes may not have been backed up." +fi +