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.

Revisions

  1. SteveMcGrath revised this gist Aug 4, 2020. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -11,6 +11,27 @@
    #
    # The latest version can be found at:
    # https://gist.github.com/SteveMcGrath/6296691
    #
    # Copyright (c) 2020 Steven McGrath
    #
    # Permission is hereby granted, free of charge, to any person obtaining a copy
    # of this software and associated documentation files (the "Software"), to deal
    # in the Software without restriction, including without limitation the rights
    # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    # copies of the Software, and to permit persons to whom the Software is
    # furnished to do so, subject to the following conditions:
    #
    # The above copyright notice and this permission notice shall be included in all
    # copies or substantial portions of the Software.
    #
    # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    # SOFTWARE.
    #

    #### CONFIGURATION

  2. SteveMcGrath revised this gist Oct 3, 2016. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -63,15 +63,21 @@ function backup_securitycenter()
    local bdate=$(date +%Y-%m-%d)
    local tarball="${BACKUP_PATH}/sc-backup-${bdate}.${sc_version}.tar.gz"
    local -a bfiles
    local -a excludes
    bfiles=(
    ~tns/admin
    ~tns/data
    ~tns/orgs
    ~tns/repositories
    ~tns/*db
    )
    # Uncomment the lines below if you want to exclude the data from the backup
    excludes=(
    # --exclude="VDB/*"
    # --exclude="repositories/*"
    )

    tar -zcf "$tarball" "${bfiles[@]}"
    tar -zcf "$tarball" ${excludes[@]} "${bfiles[@]}"
    rc=$?
    if [ $rc -ne 0 ]; then
    mv $tarball "${tarball/sc-backup-/sc-backup-errors-}"
  3. SteveMcGrath revised this gist Jul 28, 2015. 1 changed file with 36 additions and 19 deletions.
    55 changes: 36 additions & 19 deletions sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@

    # This is the base path for backups. This could be a NFS share, local storage,
    # a backup LUN, etc.
    BACKUP_PATH=/backup/sc4
    BACKUP_PATH=/backup/sc

    # Whats the maximum amount of time that we want to wait before timing out the
    # backup?
    @@ -29,17 +29,20 @@ TIMEOUT=1800
    # 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"
    function shutdown_securitycenter()
    {
    local is_running=1 # True
    local start_time=$(date +%s)
    local tns_process_count=1
    service SecurityCenter stop
    while [ $is_running == "True" ];do
    if [[ $(ps -U tns --no-headers | wc -l) -eq 0 ]];then
    is_running="Stopped"
    while [ $is_running -eq 1 ]; do
    tns_process_count=$(set -o pipefail; ps -U tns --no-headers | wc -l)
    if [ ${tns_process_count:-1} -eq 0 ]; then
    is_running=0 # False
    else
    sleep 1
    if [[ $(( $(date +%s) - $start_date )) -gt $TIMEOUT ]];then
    is_running="Timeout"
    if [ $(( $(date +%s) - $start_time )) -gt $TIMEOUT ]; then
    is_running=2 # Timeout
    fi
    fi
    done
    @@ -53,30 +56,44 @@ function shutdown_securitycenter {
    # 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)
    function backup_securitycenter()
    {
    local rc
    local sc_version=$(rpm -q --qf '%{v}' SecurityCenter)
    local bdate=$(date +%Y-%m-%d)
    local tarball=${BACKUP_PATH}/sc-backup-${bdate}.${sc_version}.tar.gz
    local bfiles='
    local tarball="${BACKUP_PATH}/sc-backup-${bdate}.${sc_version}.tar.gz"
    local -a bfiles
    bfiles=(
    ~tns/admin
    ~tns/data
    ~tns/orgs
    ~tns/repositories
    ~tns/*db
    '
    tar czf ${tarball} ${bfiles}
    )

    tar -zcf "$tarball" "${bfiles[@]}"
    rc=$?
    if [ $rc -ne 0 ]; then
    mv $tarball "${tarball/sc-backup-/sc-backup-errors-}"
    fi

    return $rc
    }

    ## 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

    if shutdown_securitycenter; then
    if ! backup_securitycenter; then
    echo 'CRITICAL: Backup had errors.'
    fi
    else
    echo "CRITICAL: Could not Shutdown SecurityCenter within specified timeout."
    echo -e "CRITICAL: Processes Still Running:\n$(ps -fU tns --no-headers)"
    echo 'CRITICAL: Could not Shutdown SecurityCenter within specified timeout.'
    echo 'CRITICAL: Processes Still Running:'
    ps fU tns
    fi

    service SecurityCenter start
  4. SteveMcGrath revised this gist Jul 28, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -58,11 +58,11 @@ function backup_securitycenter {
    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
    ~tns/admin
    ~tns/data
    ~tns/orgs
    ~tns/repositories
    ~tns/*db
    '
    tar czf ${tarball} ${bfiles}
    }
  5. SteveMcGrath revised this gist Jul 27, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -73,7 +73,7 @@ function backup_securitycenter {
    # everything down, then print out the processes that are still running. Lastly,
    # start everything back up.
    shutdown=$(shutdown_securitycenter)
    if [ $shutdown == "Stopped"];then
    if [ $shutdown == "Stopped" ];then
    backup_securitycenter
    else
    echo "CRITICAL: Could not Shutdown SecurityCenter within specified timeout."
  6. SteveMcGrath revised this gist Aug 21, 2013. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,8 @@
    #
    # 1 45 * * * root /opt/scripts/backups/sc-backup.sh
    #
    # Please visit for the most up-to-date version: https://gist.github.com/SteveMcGrath/6296691
    # The latest version can be found at:
    # https://gist.github.com/SteveMcGrath/6296691

    #### CONFIGURATION

  7. SteveMcGrath revised this gist Aug 21, 2013. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,8 @@
    # this as a cronjob is below:
    #
    # 1 45 * * * root /opt/scripts/backups/sc-backup.sh
    #
    # Please visit for the most up-to-date version: https://gist.github.com/SteveMcGrath/6296691

    #### CONFIGURATION

  8. SteveMcGrath created this gist Aug 21, 2013.
    79 changes: 79 additions & 0 deletions sc-backup.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    #!/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

    #### 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