#!/bin/bash

###
# The routine moves config files into the archive area and compresses the files.
# Old logs are also removed if over "n" days old.
# Script orginally developed by RSA PS.  This script has been updated by Sean Koniarz to be compliant with new RSA methods.
# SA Version: 10.6
# Date: 04/06/2016
# Version: 2.0
# Changes:
# Removed old comments to clean up script
# Added new error checking and logging
###

#----- Define Variables
HOST=`hostname`
day=`date +%d`
month=`date +%b`
year=`date +%Y`
BACKUP=/tmp/backup
DECCONBRK1=/etc/netwitness/ng
FTPSERVER1=/etc/vsftpd
SASERVER1=/home/rsasoc/rsa/soc/reporting-engine
SASERVER2=/var/lib/netwitness/uax
ESASERVER1=/opt/rsa/esa
LOGCOL=/var/netwitness/logcollector
DBBAK=LOCATIONOFDATABASEJARFILE CHANGEME
USER="CHANGEME"
SCPHOST="CHANGEME"


# Check for backup directory
if [ -d "${BACKUP}" ]; then
	#Directory /backup exists.
	echo .
else
	#Error: Directory /backup does not exists.
	mkdir ${BACKUP}
	# Log error
	logger -i "Backup Script: Backup directory did not exist, created ${BACKUP}"
fi;

########################################################
## Core Appliances
## Decoder, Concentrator, or Broker
########################################################
	if [ -d "${DECCONBRK1}" ]; then
		#Directory exists.
		tar -C / --exclude=Geo*.dat -cvJf ${BACKUP}/etc-netwitness-ng.$year.$day.$month.tar.xz ${DECCONBRK1}
		sha1sum ${BACKUP}/etc-netwitness-ng.$year.$day.$month.tar.xz > etc-netwitness-ng.$year.$day.$month.sha1
		scp ${BACKUP}/etc-netwitness-ng.$year.$day.$month.tar.xz $USER@$SCPHOST:$HOST.etc-netwitness-ng.$year.$day.$month.tar.xz
		# Error Handling
		if [ $? -ne 0 ]; then
			logger -i "Backup Script Failed: Error occured during backup - Core Service"
		fi;
		scp ${BACKUP}/etc-netwitness-ng.$year.$day.$month.sha1 $USER@$SCPHOST:$HOST.etc-netwitness-ng.$year.$day.$month.sha1
	else
	    #Error: Directory does not exists.	Log error
	    logger -i "Backup Script: Decoder, Concentrator or Broker does not exist on this system"
	fi;
########################################################
## VSFTPD Server
########################################################
    if [ -d "${FTPSERVER1}" ]; then
        #Directory exists.
        tar -C / -cvJf ${BACKUP}/etc-vsftpd.$year.$day.$month.tar.xz ${FTPSERVER1}
		sha1sum ${BACKUP}/etc-vsftpd.$year.$day.$month.tar.xz > ${BACKUP}/etc-vsftpd.$year.$day.$month.sha1
        scp ${BACKUP}/etc-vsftpd.$year.$day.$month.tar.xz $USER@$SCPHOST:$HOST.etc-vsftpd.$year.$day.$month.tar.xz
		# Error Handling
		if [ $? -ne 0 ]; then
			logger -i "Backup Script: Error occured during backup - VSFTPD"
		fi;
		scp ${BACKUP}/etc-vsftpd.$year.$day.$month.sha1 $USER@$SCPHOST:$HOST.etc-vsftpd.$year.$day.$month.sha1
    else
        #Error: Directory does not exists.
        logger -i "Backup Script: VSFTPD does not exist on this system"
    fi;
#####################################################
## Reporting Engine, SA Server
########################################################
	if [ -d "${SASERVER1}" ]; then
		#Directory exists.
		tar -C / --exclude='/home/rsasoc/rsa/soc/reporting-engine/resultstore' --exclude='/home/rsasoc/rsa/soc/reporting-engine/livecharts' --exclude='/home/rsasoc/rsa/soc/reporting-engine/statusdb' --exclude='/home/rsasoc/rsa/soc/reporting-engine/logs' --exclude='/home/rsasoc/rsa/soc/reporting-engine/temp' --exclude='/home/rsasoc/rsa/soc/reporting-engine/formattedReports' --exclude='/home/rsasoc/rsa/soc/reporting-engine/subreports' --exclude='/home/rsasoc/rsa/soc/reporting-engine/archives' -cvJf ${BACKUP}/home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.tar.xz ${SASERVER1}
		sha1sum ${BACKUP}/home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.tar.xz > ${BACKUP}/home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.sha1
		scp ${BACKUP}/home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.tar.xz $USER@$SCPHOST:$HOST.home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.tar.xz
		# Error handling
		if [ $? -ne 0 ]; then
			logger -i "Backup Script Failed: Error occured during backup - Reporting Engine"
		fi;
		scp ${BACKUP}/home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.sha1 $USER@$SCPHOST:$HOST.home-rsasoc-rsa-soc-reporting-engine.$year.$day.$month.sha1
	else
	    #Error: Directory does not exists.	 
	    logger -i "Backup Script: Reporting Engine does not exist on this system"
	fi;
	
	if [ -d "${SASERVER2}" ]; then
		#Directory exists.
		tar -C / --exclude='/var/lib/netwitness/uax/content'  --exclude='/var/lib/netwitness/uax/temp/*' --exclude='/var/lib/netwitness/uax/cache/' --exclude='/var/lib/netwitness/uax/scheduler/*/*.pcap' --exclude='/var/lib/netwitness/uax/scheduler/*/*.log' -cvJf ${BACKUP}/var-lib-netwitness-uax.$year.$day.$month.tar.xz ${SASERVER2}
		sha1sum ${BACKUP}/var-lib-netwitness-uax.$year.$day.$month.tar.xz > ${BACKUP}/var-lib-netwitness-uax.$year.$day.$month.sha1
		# # Stop Jetty
		# stop jettysrv
		# sleep 20
		# cd /var/lib/netwitness/uax/db
		# java -cp ${DBBAK} -file ${BACKUP}/var-lib-netwitness-uax-db.$year.$day.$month.db.bak
		# # Start Jetty
		# start jettysrv
		# sha1sum ${BACKUP}/var-lib-netwitness-uax-db.$year.$day.$month.db.bak > ${BACKUP}/var-lib-netwitness-uax-db.$year.$day.$month.db.sha1
		scp ${BACKUP}/var-lib-netwitness-uax.$year.$day.$month.tar.xz $USER@$SCPHOST:$HOST.var-lib-netwitness-uax.$year.$day.$month.tar.xz
		# Error Handling
		if [ $? -ne 0 ]; then
			logger -i "Backup Script Failed: Error occured during backup - Web Server"
		fi;
		# scp ${BACKUP}/var-lib-netwitness-uax-db.$year.$day.$month.db.bak $USER@$SCPHOST:$HOST.var-lib-netwitness-uax-db.$year.$day.$month.db.bak
		# # Error Handling
		# if [ $? -ne 0 ]; then
			# logger -i "Backup Script Failed: Error occured during backup - Web Server DB"
		# fi;
		scp ${BACKUP}/var-lib-netwitness-uax.$year.$day.$month.sha1 $USER@$SCPHOST:$HOST.var-lib-netwitness-uax.$year.$day.$month.sha1
		# scp ${BACKUP}/var-lib-netwitness-uax-db.$year.$day.$month.db.sha1 $USER@$SCPHOST:$HOST.var-lib-netwitness-uax-db.$year.$day.$month.db.sha1
	else
	    #Error: Directory does not exists.
	    logger -i "Backup Script: Web Server does not exist on this system"
	fi;

########################################################
## ESA Server
########################################################
	if [ -d "${ESASERVER1}" ]; then
		#Directory exists.
		tar -C / --exclude=/opt/rsa/esa/logs --exclude=/opt/rsa/esa/db --exclude=/opt/rsa/esa/bin --exclude=/opt/rsa/esa/lib -cvJf ${BACKUP}/opt-rsa-esa.$year.$day.$month.tar.xz ${ESASERVER1}
		sha1sum ${BACKUP}/opt-rsa-esa.$year.$day.$month.tar.xz > ${BACKUP}/opt-rsa-esa.$year.$day.$month.sha1
		scp ${BACKUP}/opt-rsa-esa.$year.$day.$month.tar.xz $USER@$SCPHOST:$HOST.opt-rsa-esa.$year.$day.$month.tar.xz
		# Error handling
		if [ $? -ne 0 ]; then
			logger -i "Backup Script Failed: Error occured during backup - ESA"
		fi;
		scp ${BACKUP}/opt-rsa-esa.$year.$day.$month.sha1 $USER@$SCPHOST:$HOST.opt-rsa-esa.$year.$day.$month.sha1
	else
	    #Error: Directory does not exists.	
	   logger -i "Backup Script: ESA does not exist on this system"
	fi;
	
########################################################
## Log Collector
########################################################
	if [ -d "${LOGCOL}" ]; then
		#Directory exists.
		tar -C / --exclude='/var/netwitness/logcollector/statdb/*' -cvJf ${BACKUP}/var-log-collector.$year.$day.$month.tar.xz ${LOGCOL}
		sha1sum ${BACKUP}/var-log-collector.$year.$day.$month.tar.xz > ${BACKUP}/var-log-collector.$year.$day.$month.sha1
		scp ${BACKUP}/var-log-collector.$year.$day.$month.tar.xz $USER@$SCPHOST:$HOST.var-log-collector.$year.$day.$month.tar.xz
		# Error Handling
		if [ $? -ne 0 ]; then
			logger -i "Backup Script Failed: Error occured during backup - Log Collector"
		fi;
		scp ${BACKUP}/var-log-collector.$year.$day.$month.sha1 $USER@$SCPHOST:$HOST.var-log-collector.$year.$day.$month.sha1
	else
	    #Error: Directory does not exists.	
	   logger -i "Backup Script: Log Collector does not exist on this system"
	fi;	

#----- Cleanup the Backup Area
find ${BACKUP} -mtime +15 -exec rm {} \;
