2025-01-07 15:08:05 +00:00
#!/bin/bash
2025-01-08 08:57:31 +00:00
# Script to automate Proxmox PVE node backups to a PBS machine.
#
# Author: Xavi Lleó
# Copyright (c) 2025 Verdnatura S.L. All rights reserved.
2025-01-08 12:13:53 +00:00
# Version: 1.0.3
2025-01-13 08:14:23 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
2025-01-08 08:57:31 +00:00
#
# 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
2025-01-08 12:10:56 +00:00
# USER_API="root@pam!api"
# USER="root@pam"
# IP_PBS="192.168.1.250"
2025-01-08 08:57:31 +00:00
# POOL="backup-pool"
# BACKUP_ITEMS="etc-pve.pxar:/etc/pve,interfaces.pxar:/etc/network"
# LOG_FILE="/var/log/proxmox-backup-node-pve.log"
2025-01-08 12:23:49 +00:00
# KEY_FILE="mykeyfile.key" #In case you want encrypted backups
# PBS_PASSWORD='mypass or api token'
2025-01-08 08:57:31 +00:00
# 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.
2025-01-08 12:10:56 +00:00
# Remember to add the port after the IP address when using an API user for authentication.
# Example: IP_PBS="192.168.1.250:8007"
2025-01-07 15:08:05 +00:00
2025-01-08 08:57:31 +00:00
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 "
2025-01-08 12:10:56 +00:00
else
echo " Error: Configuration file not found at $CONFIG_FILE "
exit 1
2025-01-08 08:57:31 +00:00
fi
2025-01-07 15:08:05 +00:00
show_help( ) {
2025-01-08 08:57:31 +00:00
echo " Usage: $0 [options] "
2025-01-07 15:08:05 +00:00
echo
2025-01-08 08:57:31 +00:00
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."
2025-01-08 12:10:56 +00:00
echo " --user-api Specify user API credentials for backup."
echo " --user Specify user credentials for backup."
2025-01-08 08:57:31 +00:00
echo " --help Show this help."
2025-01-07 15:08:05 +00:00
exit 0
}
2025-01-08 09:56:30 +00:00
exit_from_repo( ) {
2025-01-08 12:10:56 +00:00
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 "
2025-01-08 09:56:30 +00:00
}
2025-01-08 12:10:56 +00:00
# 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
2025-01-08 08:57:31 +00:00
while [ [ $# -gt 0 ] ] ; do
case " $1 " in
--standard)
MODE = "standard"
; ;
--encrypt)
MODE = "encrypt"
; ;
--ip)
2025-01-08 12:13:53 +00:00
IP_PBS = " $2 "
2025-01-08 08:57:31 +00:00
shift
; ;
--pool)
POOL = " $2 "
shift
; ;
--items)
BACKUP_ITEMS = " $2 "
shift
; ;
2025-01-08 12:10:56 +00:00
--user-api)
USER_API = " $2 "
shift
; ;
--user)
USER = " $2 "
shift
; ;
2025-01-08 08:57:31 +00:00
--help)
show_help
; ;
*)
2025-01-08 09:56:30 +00:00
echo " Error: Unrecognized option ' $1 ' "
2025-01-08 08:57:31 +00:00
show_help
; ;
esac
shift
done
if [ -z " $MODE " ] ; then
2025-01-08 09:56:30 +00:00
echo "You must specify --standard or --encrypt."
2025-01-07 15:08:05 +00:00
show_help
fi
2025-01-08 12:10:56 +00:00
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 "
2025-01-08 08:57:31 +00:00
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
2025-01-07 15:08:05 +00:00
if [ ! -f " $KEY_FILE " ] ; then
2025-01-08 09:56:30 +00:00
echo " The key file $KEY_FILE does not exist. " | tee -a " $LOG_FILE "
2025-01-07 15:08:05 +00:00
exit 1
fi
2025-01-08 12:10:56 +00:00
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
2025-01-08 08:57:31 +00:00
fi
2025-01-09 08:41:41 +00:00
sleep 5
2025-01-08 08:57:31 +00:00
done
2025-01-07 15:08:05 +00:00
if [ $? -eq 0 ] ; then
2025-01-08 09:56:30 +00:00
exit_from_repo
echo -e " $( date '+%Y-%m-%d %H:%M:%S' ) - Backup completed successfully. You can check the log at $LOG_FILE \n " | tee -a " $LOG_FILE "
2025-01-07 15:08:05 +00:00
else
2025-01-08 09:56:30 +00:00
exit_from_repo
echo -e " $( date '+%Y-%m-%d %H:%M:%S' ) - Error during backup. You can check the log at $LOG_FILE \n " | tee -a " $LOG_FILE "
2025-01-07 15:08:05 +00:00
exit 1
fi