#!/bin/bash # Script to automate Proxmox PVE node backups to a PBS machine. # # Author: Xavi Lleó # Copyright (c) 2025 Verdnatura S.L. All rights reserved. # Version: 1.0.0 # ¿Juan Wants add GPL License? # # A configuration file is required in the user's home directory who runs this command. # The file should be sourced using CONFIG_FILE before execution. # # Example of a configuration file: # # Default values # IP_MACHINE="root@pam@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" # KEY_FILE="mykeyfile.key" # PBS_PASSWORD='mypass' # 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. CONFIG_FILE="$HOME/.backup_config.conf" # https://pbs.proxmox.com/docs/backup-client.html#environment-variables export PBS_PASSWORD export PBS_FINGERPRINT if [ -f "$CONFIG_FILE" ]; then source "$CONFIG_FILE" fi show_help() { echo "Usage: $0 [options]" echo echo "Options:" echo " --standard Perform a standard backup." echo " --encrypt Perform an encrypted backup (requires a key file)." 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 " --help Show this help." exit 0 } while [[ $# -gt 0 ]]; do case "$1" in --standard) MODE="standard" ;; --encrypt) MODE="encrypt" ;; --ip) IP_MACHINE="$2" shift ;; --pool) POOL="$2" shift ;; --items) BACKUP_ITEMS="$2" shift ;; --help) show_help ;; *) echo "Error: Opción no reconocida '$1'" show_help ;; esac shift done if [ -z "$MODE" ]; then echo "Error: Debes especificar --standard o --encrypt." show_help fi echo "$(date '+%Y-%m-%d %H:%M:%S') - Iniciando respaldo al repositorio $IP_MACHINE:$POOL" | 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) if [ "$MODE" == "encrypt" ]; then # Comprobar si el archivo de clave existe if [ ! -f "$KEY_FILE" ]; then echo "Error: El archivo de clave $KEY_FILE no existe." | tee -a "$LOG_FILE" 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" fi done if [ $? -eq 0 ]; then echo -e "$(date '+%Y-%m-%d %H:%M:%S') - Respaldo completado con éxito. Puede revisar el log en $LOG_FILE\n" | tee -a "$LOG_FILE" else echo -e "$(date '+%Y-%m-%d %H:%M:%S') - Error durante el respaldo. Puede revisar el log en $LOG_FILE\n" | tee -a "$LOG_FILE" exit 1 fi