Skip to content

Instantly share code, notes, and snippets.

@rluzuriaga
Created July 5, 2023 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rluzuriaga/a2cd00cbff9a5cc70d0fb93afdd15566 to your computer and use it in GitHub Desktop.
Save rluzuriaga/a2cd00cbff9a5cc70d0fb93afdd15566 to your computer and use it in GitHub Desktop.
Script to backup FOG database, images, snapins, and reports
#!/bin/bash
fogServerAddress="10.xx.xx.xx" # Change to actual FOG server address
fogServerSSHConnection="USERNAME@$fogServerAddress" # Change username
sendToEmailAddress="USERNAME_1@DOMAIN.com;USERNAME_2@DOMAIN.com"
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() {
scp -r $fogServerSSHConnection:$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() {
scp -r $fogServerSSHConnection:$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 -e "ssh" $fogServerSSHConnection:$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
}
sendEmail() {
echo "Running sendEmail()"
if [[ ! -z $message ]]; then
{
printf "To: $sendToEmailAddress\n"
printf "Subject: FOG Backup Failed\n"
printf "$message\n"
} | msmtp $sendToEmailAddress
fi
}
backupDB
backupSnapins
backupReports
backupImages
checkForFailures
sendEmail
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment