proxmox-backup-client script notification

bradyloveland

New Member
Sep 18, 2024
1
0
1
I've been digging the web and forums and haven't found a solution for some sort of notification, either email or discord to notify me like regular VM backups when backing up from a host using the proxmox-backup-client. Simply, I need to get a notification via email or discord web hook on success/failure for the following script which is run as a service in system.d and triggered by the second code segment. Any help would be greatly appreciated!

Code:
[Unit]
Description=Run Backups

[Service]
Type=oneshot
#Run as backup
User=root
Group=root
#Allow up to 15% CPU
CPUQuota=15%
#Backup settings
Environment=PBS_REPOSITORY=redacted
Environment=PBS_PASSWORD=redacted
Environment=PBS_FINGERPRINT=redacted
#Run proxmox backup client for the passed directory
ExecStart=proxmox-backup-client backup %i.pxar:/pool/%i --all-file-systems true --backup-id "redacted_%i" --exclude */.zfs/ --change-detection-mode=metadata

[Install]
WantedBy=default.target

Code:
[Unit]
Description=Backup downloads
RefuseManualStart=no
RefuseManualStop=no

[Timer]
#Run at midnight every day
OnCalendar=*-*-* 00:00:00
Unit=pbs-client@downloads.service

[Install]
WantedBy=timers.target
 
not clear if you want the full log or just an alert if any issue occurred?
You can create another systemd service to run a shell script on schedule with something like below after the expected completion of the backup job or in your morning to have it on top of your emails...

Code:
#!/bin/bash

# Define the service to monitor
BACKUP_SERVICE_NAME="your_service_name"

# Define the email to send alerts
ALERT_EMAIL="your_email@example.com"

journalctl -u $BACKUP_SERVICE_NAME -o cat --since "today 00:00" | mail -s "Service Logs" -- $ALERT_EMAIL

If you want only alerts if an error happened you can run continously another script with systemd to monitor the journalctl log stream and filter for errors.

Code:
#!/bin/bash

# Define the service to monitor
BACKUP_SERVICE_NAME="your_service_name"

# Define the email to send alerts
ALERT_EMAIL="your_email@example.com"

# Function to send alert to email, modify with a discord webhook as needed
send_alert() {
    echo "Error detected in $BACKUP_SERVICE_NAME logs!" | mail -s "Alert: Error in $BACKUP_SERVICE_NAME" -- $ALERT_EMAIL
}

# Monitor logs in real-time and filter for errors
journalctl -u $BACKUP_SERVICE_NAME -f -p err | while read line; do
    echo "$line"  # Print the error log line
    send_alert    # Send alert on error
done

hope this helps