Last active
February 22, 2025 09:03
-
-
Save lorenzogirardi/2337bdc557f9ec5e0cce7877b022fd45 to your computer and use it in GitHub Desktop.
proxmox_backup.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Configuration | |
NFS_SERVER="x.x.x.x" # Replace with your nfs server ip | |
NFS_SHARE="/volume1/backup-vm" | |
MOUNT_POINT="/mnt/proxmox-backup" | |
STORAGE_ID="syn" | |
LOG_FILE="/var/log/proxmox_backup.log" | |
DATE=$(date +"%Y-%m-%d") | |
BACKUP_DIR="$MOUNT_POINT/servername" # Replace with your server name | |
EMAIL="email" # Replace with your email address | |
SUBJECT="Proxmox Backup Report - $(date +%Y-%m-%d)" | |
BACKUP_RETENTION=3 | |
# Function to log messages | |
log_message() { | |
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> $LOG_FILE | |
} | |
# Function to send email | |
send_email() { | |
local body="$1" | |
cat "$LOG_FILE" | mail -s "$SUBJECT" "$EMAIL" | |
} | |
# Clear the log file | |
> $LOG_FILE | |
# Create mount point if it doesn't exist | |
mkdir -p $MOUNT_POINT | |
# Mount the NFS share | |
log_message "Mounting NFS share..." | |
mount -t nfs -o rw,nosuid,noexec $NFS_SERVER:$NFS_SHARE $MOUNT_POINT | |
if [ $? -ne 0 ]; then | |
log_message "Failed to mount NFS share. Exiting." | |
send_email "Failed to mount NFS share. Backup process aborted." | |
exit 1 | |
fi | |
# Create backup directory if it doesn't exist | |
mkdir -p $BACKUP_DIR | |
# Backup all VMs | |
log_message "Starting VM backups..." | |
for VM in $(/usr/sbin/qm list | awk '{if(NR>1) print $1}') | |
do | |
log_message "Backing up VM $VM..." | |
vzdump $VM --mode snapshot --compress zstd --dumpdir $BACKUP_DIR >> $LOG_FILE 2>&1 | |
if [ $? -ne 0 ]; then | |
log_message "Failed to backup VM $VM." | |
else | |
log_message "Successfully backed up VM $VM." | |
fi | |
# Clean up old backups for this VM (keep only the last 3) | |
log_message "Cleaning up old backups for VM $VM..." | |
ls -t $BACKUP_DIR/vzdump-qemu-$VM-*.vma.zst | tail -n +$(($BACKUP_RETENTION + 1)) | xargs rm -f | |
done | |
# Backup all containers | |
log_message "Starting container backups..." | |
for CT in $(/usr/sbin/pct list | awk '{if(NR>1) print $1}') | |
do | |
log_message "Backing up container $CT..." | |
vzdump $CT --mode snapshot --compress zstd --dumpdir $BACKUP_DIR >> $LOG_FILE 2>&1 | |
if [ $? -ne 0 ]; then | |
log_message "Failed to backup container $CT." | |
else | |
log_message "Successfully backed up container $CT." | |
fi | |
# Clean up old backups for this container (keep only the last 3) | |
log_message "Cleaning up old backups for container $CT..." | |
ls -t $BACKUP_DIR/vzdump-lxc-$CT-*.tar.zst | tail -n +$(($BACKUP_RETENTION + 1)) | xargs rm -f | |
done | |
# Unmount the NFS share | |
log_message "Unmounting NFS share..." | |
umount $MOUNT_POINT | |
if [ $? -ne 0 ]; then | |
log_message "Failed to unmount NFS share." | |
send_email "Backup process completed with errors. Failed to unmount NFS share." | |
exit 1 | |
fi | |
# Send email report | |
log_message "Backup process completed successfully." | |
send_email "Backup process completed successfully. Logs attached." < $LOG_FILE | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment