Thanks a lot @rodluz !
I took your script and adapted it. I use it directly on my FOG server, which simplifies things a bit. I also changed the mail to a notification sent to my smartphone with ntfy, which I find easy to use.
I also mounted my NAS share in NFS directly on /mnt/FOGBackups.
Here’s my script:
#!/bin/bash
fogServerAddress="172.X.Y.Z" # Change to actual FOG server address
backupDate=$(date +"%Y%m%d")
backupDir="/mnt/FOGBackups/$backupDate"
backupDirImages="/mnt/FOGBackups/images"
snapinLocation="/opt/fog/snapins"
reportLocation="/var/www/fog/lib/reports"
imageLocation="/images"
failedBackupDB=0
failedBackupSnapins=0
failedBackupReports=0
failedBackupImages=0
[[ ! -d $backupDir ]] && mkdir -p $backupDir/{mysql,snapins,reports,logs} >/dev/null 2>&1
[[ ! -d $backupDir/mysql || $backupDir/snapins || $backupDir/reports || $backupDir/logs ]] && mkdir -p $backupDir/{mysql,snapins,reports,logs} >/dev/null 2>&1
backupDB() {
wget --no-check-certificate --post-data="nojson=1" -O $backupDir/mysql/fog.sql "http://$fogServerAddress/fog/management/export.php?type=sql" 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1
stat=$?
if [[ ! $stat -eq 0 ]]; then
echo "Failed to backup database!"
failedBackupDB=1
else
echo "Database backed up."
fi
}
backupSnapins() {
cp -r $snapinLocation $backupDir/snapins/ 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1
stat=$?
if [[ ! $stat -eq 0 ]]; then
echo "Failed to backup snapins!"
failedBackupSnapins=1
else
echo "Snapins backed up."
fi
}
backupReports() {
cp -r $reportLocation $backupDir/reports/ 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1
stat=$?
if [[ ! $stat -eq 0 ]]; then
echo "Failed to backup reports!"
failedBackupReports=1
else
echo "Reports backed up."
fi
}
backupImages() {
rsync -auv $imageLocation $backupDirImages 2>>$backupDir/logs/error.log 1>>$backupDir/logs/progress.log 2>&1
stat=$?
if [[ ! $stat -eq 0 ]]; then
echo "Failed to backup images!"
failedBackupImages=1
else
echo "Images backed up."
fi
}
checkForFailures() {
echo "Running checkForFailures()"
if [[ $failedBackupDB -eq 1 ]]; then
message="$message \nFailed to backup database."
fi
if [[ $failedBackupSnapins -eq 1 ]]; then
message="$message \nFailed to backup snapins."
fi
if [[ $failedBackupReports -eq 1 ]]; then
message="$message \nFailed to backup reports."
fi
if [[ $failedBackupImages -eq 1 ]]; then
message="$message \nFailed to backup images."
fi
}
sendNotif() {
echo "Running sendNotif()"
if [[ ! -z $message ]]; then
{
curl -d "FOG Backup Failed\n $message" ntfy.sh/XYZATEHFJICH
}
else
curl -d "FOG Backup Successful!" ntfy.sh/XYZATEHFJICH
fi
}
backupDB
backupSnapins
backupReports
backupImages
checkForFailures
sendNotif