Refs #8359: 8359-Backup_node_PVE - Final Script

This commit is contained in:
Xavi Lleó 2025-01-08 09:57:31 +01:00
parent c3e9c5cfa8
commit c8afa76994
1 changed files with 83 additions and 35 deletions

View File

@ -1,56 +1,104 @@
#!/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.
# Configuración
IP_MACHINE=10.29.1.11
POOL=backup-pool
TARGET_DIR=/etc/pve
INTERFACES_FILE=/etc/network
BACKUP_NAME=etc-pve.pxar
BACKUP_NAME_INTERFACES=interfaces.pxar
LOG_FILE=/var/log/proxmox-backup-node-pve.log
KEY_FILE="key.key"
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 "Uso: $0 [--standard | --encrypt]"
echo "Usage: $0 [options]"
echo
echo "Opciones:"
echo " --standard Realiza un respaldo estándar."
echo " --encrypt Realiza un respaldo encriptado (requiere un archivo de clave)."
echo " --help Muestra esta ayuda."
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
}
if [ $# -eq 0 ]; then
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
case "$1" in
--standard)
echo "$(date '+%Y-%m-%d %H:%M:%S') - Iniciando respaldo estándar de $TARGET_DIR al repositorio $IP_MACHINE:$POOL" | tee -a "$LOG_FILE"
proxmox-backup-client backup "$BACKUP_NAME:$TARGET_DIR" --repository "$IP_MACHINE:$POOL" 2>>"$LOG_FILE" &&
proxmox-backup-client backup "$BACKUP_NAME_INTERFACES:$INTERFACES_FILE" --repository "$IP_MACHINE:$POOL" 2>>"$LOG_FILE"
;;
--encrypt)
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
echo "$(date '+%Y-%m-%d %H:%M:%S') - Iniciando respaldo encriptado de $TARGET_DIR al repositorio $IP_MACHINE:$POOL" | tee -a "$LOG_FILE"
proxmox-backup-client backup "$BACKUP_NAME:$TARGET_DIR" --repository "$IP_MACHINE:$POOL" --crypt-mode encrypt --keyfile "$KEY_FILE" 2>>"$LOG_FILE" &&
proxmox-backup-client backup "$BACKUP_NAME_INTERFACES:$INTERFACES_FILE" --repository "$IP_MACHINE:$POOL" --crypt-mode encrypt --keyfile "$KEY_FILE" 2>>"$LOG_FILE"
;;
--help)
show_help
;;
*)
echo "Error: Opción no reconocida."
show_help
;;
esac
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
# Verificar el resultado del respaldo
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