From 5951d626ca314732df6ad861ae3a853550bc2f63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xavi=20Lle=C3=B3=20Tom=C3=A1s?= Date: Wed, 8 Jan 2025 13:10:56 +0100 Subject: [PATCH] Refs #8359: 8359-Backup_node_PVE - Final Script - Adding support for API authentication --- scripts/backup_pve.sh | 51 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/scripts/backup_pve.sh b/scripts/backup_pve.sh index f03200f..6204903 100644 --- a/scripts/backup_pve.sh +++ b/scripts/backup_pve.sh @@ -3,7 +3,7 @@ # # Author: Xavi Lleó # Copyright (c) 2025 Verdnatura S.L. All rights reserved. -# Version: 1.0.0 +# Version: 1.0.2 # ¿Juan Wants add GPL License? # # A configuration file is required in the user's home directory who runs this command. @@ -12,7 +12,9 @@ # Example of a configuration file: # # Default values -# IP_MACHINE="root@pam@192.168.1.250" +# USER_API="root@pam!api" +# USER="root@pam" +# IP_PBS="192.168.1.250" # POOL="backup-pool" # BACKUP_ITEMS="etc-pve.pxar:/etc/pve,interfaces.pxar:/etc/network" # LOG_FILE="/var/log/proxmox-backup-node-pve.log" @@ -21,6 +23,9 @@ # PBS_FINGERPRINT='b0:69:24:75:f0:92:a2:72:37:7c:c1:cb:0d:ba:8e:14:EE:XX:AA:MM:PP:LL:EE:e4:2b:07:02:18:86:9a:df:45' # # If you prefer to use switches in a one-liner, refer to the help section (--help) for available options. +# Remember to add the port after the IP address when using an API user for authentication. +# Example: IP_PBS="192.168.1.250:8007" + CONFIG_FILE="$HOME/.backup_config.conf" @@ -30,6 +35,9 @@ export PBS_FINGERPRINT if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE" +else + echo "Error: Configuration file not found at $CONFIG_FILE" + exit 1 fi show_help() { @@ -41,14 +49,22 @@ show_help() { echo " --ip Repository IP address (overrides configuration)." echo " --pool Name of the backup pool (overrides configuration)." echo " --items List of backup items in 'name1:source1,name2:source2' format." + echo " --user-api Specify user API credentials for backup." + echo " --user Specify user credentials for backup." echo " --help Show this help." exit 0 } exit_from_repo() { - proxmox-backup-client logout --repository "$IP_MACHINE:$POOL" 2>>"$LOG_FILE" && echo "$(date '+%Y-%m-%d %H:%M:%S') - Logged out from repository $IP_MACHINE:$POOL" | tee -a "$LOG_FILE" + proxmox-backup-client logout --repository "$REPOSITORY" 2>>"$LOG_FILE" && echo "$(date '+%Y-%m-%d %H:%M:%S') - Logged out from repository $REPOSITORY" | tee -a "$LOG_FILE" } +# Check if PBS_PASSWORD and PBS_FINGERPRINT are set +if [ -z "$PBS_PASSWORD" ] || [ -z "$PBS_FINGERPRINT" ]; then + echo "Error: PBS_PASSWORD or PBS_FINGERPRINT is not set." + exit 1 +fi + while [[ $# -gt 0 ]]; do case "$1" in --standard) @@ -58,7 +74,7 @@ while [[ $# -gt 0 ]]; do MODE="encrypt" ;; --ip) - IP_MACHINE="$2" + IP_ADDRESS="$2" shift ;; --pool) @@ -69,6 +85,14 @@ while [[ $# -gt 0 ]]; do BACKUP_ITEMS="$2" shift ;; + --user-api) + USER_API="$2" + shift + ;; + --user) + USER="$2" + shift + ;; --help) show_help ;; @@ -85,7 +109,13 @@ if [ -z "$MODE" ]; then show_help fi -echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting backup to repository $IP_MACHINE:$POOL" | tee -a "$LOG_FILE" +if [ -n "$USER_API" ]; then + REPOSITORY="$USER_API@$IP_PBS:$POOL" +else + REPOSITORY="$USER@$IP_PBS:$POOL" +fi + +echo "$(date '+%Y-%m-%d %H:%M:%S') - Starting backup to repository $REPOSITORY" | tee -a "$LOG_FILE" for item in $(echo "$BACKUP_ITEMS" | tr ',' '\n'); do BACKUP_NAME=$(echo "$item" | cut -d':' -f1) TARGET_DIR=$(echo "$item" | cut -d':' -f2) @@ -96,9 +126,14 @@ for item in $(echo "$BACKUP_ITEMS" | tr ',' '\n'); do exit 1 fi - proxmox-backup-client backup "$BACKUP_NAME:$TARGET_DIR" --repository "$IP_MACHINE:$POOL" --crypt-mode encrypt --keyfile "$KEY_FILE" --backup-type "host" 2>>"$LOG_FILE" -else - proxmox-backup-client backup "$BACKUP_NAME:$TARGET_DIR" --repository "$IP_MACHINE:$POOL" --backup-type "host" 2>>"$LOG_FILE" + proxmox-backup-client backup "$BACKUP_NAME:$TARGET_DIR" --repository "$REPOSITORY" --crypt-mode encrypt --keyfile "$KEY_FILE" --backup-type 'host' 2>>"$LOG_FILE" + else + proxmox-backup-client backup "$BACKUP_NAME:$TARGET_DIR" --repository "$REPOSITORY" --backup-type 'host' 2>>"$LOG_FILE" + fi + + if [ $? -ne 0 ]; then + echo "Backup failed for $BACKUP_NAME" | tee -a "$LOG_FILE" + exit 1 fi done