Skip to content

Instantly share code, notes, and snippets.

@SteveMcGrath
Last active November 15, 2022 12:13
Show Gist options
  • Select an option

  • Save SteveMcGrath/6296691 to your computer and use it in GitHub Desktop.

Select an option

Save SteveMcGrath/6296691 to your computer and use it in GitHub Desktop.
SecurityCenter Backup Script
#!/bin/bash
## SecurityCenter Backup Script
#
# This script is intended to create backups of all of the SecurityCenter data
# on a daily/weekly/monthly/etc. basis. This is intended to be run as a cronjob
# and expect the SysAdmin to have configured the root@localhost mail alias to
# route through their email system in-case of errors. An example of how to run
# this as a cronjob is below:
#
# 1 45 * * * root /opt/scripts/backups/sc-backup.sh
#
# The latest version can be found at:
# https://gist.github.com/SteveMcGrath/6296691
#### CONFIGURATION
# This is the base path for backups. This could be a NFS share, local storage,
# a backup LUN, etc.
BACKUP_PATH=/backup/sc4
# Whats the maximum amount of time that we want to wait before timing out the
# backup?
TIMEOUT=1800
#### DO NOT EDIT BELOW THIS LINE
## Shutdown Function
#
# This function will shudown SecurityCenter and will not return back until all
# SecurityCenter related processes are completed. If we end up having to wait
# past the TIMEOUT value, then it will drop out as well.
function shutdown_securitycenter {
local is_running="True"
local start_time=$(date +%s)
service SecurityCenter stop
while [ $is_running == "True" ];do
if [[ $(ps -U tns --no-headers | wc -l) -eq 0 ]];then
is_running="Stopped"
else
sleep 1
if [[ $(( $(date +%s) - $start_date )) -gt $TIMEOUT ]];then
is_running="Timeout"
fi
fi
done
return $is_running
}
## Backup Generator
#
# Here is where we will actually perform the backup. The tarball that we
# generate will ONLY contain SecurityCenter data, not the binaries, scripts,
# or code that is installed along with SecurityCenter. This makes the data more
# portable in the end as its no longer dependent on architecture, simply just
# the version of SC that it was backed up from.
function backup_securitycenter {
local sc_version=$(rpm -qa | grep SecurityCenter | cut -d "-" -f 2)
local bdate=$(date +%Y-%m-%d)
local tarball=${BACKUP_PATH}/sc-backup-${bdate}.${sc_version}.tar.gz
local bfiles='
/opt/sc4/admin
/opt/sc4/data
/opt/sc4/orgs
/opt/sc4/repositories
/opt/sc4/*db
'
tar czf ${tarball} ${bfiles}
}
## Main Loop
#
# Now lets actually perform the backup. If there is an error with shutting
# everything down, then print out the processes that are still running. Lastly,
# start everything back up.
shutdown=$(shutdown_securitycenter)
if [ $shutdown == "Stopped" ];then
backup_securitycenter
else
echo "CRITICAL: Could not Shutdown SecurityCenter within specified timeout."
echo -e "CRITICAL: Processes Still Running:\n$(ps -fU tns --no-headers)"
fi
service SecurityCenter start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment