#!/bin/bash
#
# Simple backup script for Cisco switches
# 
#set -x

VERBOSE=''
DO_ECHO=''

BACKUP_SCRIPT='/root/bin/cisco_backup'
DISTCONFIG='/etc/sysconfig/cisco_backup'
GEL='/usr/bin/gel'
UPDATE_SWITCHBACKUP_SVN='/root/bin/update_switchbackup_svn'

# source our config
if [ -f "$DISTCONFIG" ]; then
    . "$DISTCONFIG"
else
    echo "$DISTCONFIG not found - please check if the file exists and is readable" >&2
    exit 1
fi

if [ ! -r "$SWITCH_DATA" ]; then
    echo "ERROR: $SWITCH_DATA not found or not readable" >&2
    exit 1
fi

for executable in $BACKUP_SCRIPT $UPDATE_SWITCHBACKUP_SVN $GEL ; do
	if [ ! -x "$executable" ]; then
		echo "ERROR: $executable not found or not executable" >&2
		exit 1
	fi
done


function usage {
    echo
    echo "Usage: $(basename $0) [-t][-v]"
    echo
    echo "       -f : force backup, even if disabled in config $DISTCONFIG"
    echo "       -t : just test, do nothing"
    echo "       -v : be verbose"
    echo
    echo "       Will read configuration from: $DISTCONFIG file"
    echo
    exit $1
}

function DEBUG {
	echo "INFO:    $1"
}

function LOG {
	local logfile="$2" 
	# use the same date/time format as the expect script
	DATE=$(date "+%a %b %d %H:%M:%S %Y")
	echo "$DATE $1" >> "$logfile"
}

while getopts 'htv' OPTION; do
	case $OPTION in
		h) usage 0
		;;
		v) VERBOSE='1'
		;;
		t) DO_ECHO='echo '
		;;
		f) FORCE_BACKUP='yes'
		;;
	esac
done

if [ "$START_SWITCHBACKUP" == "yes" ] || [ "$FORCE_BACKUP" == "yes" ]; then
  while read line; do
    if [[ "$line" == \#* ]] || [[ "$line" == "" ]]; then
        continue
    fi

    SSH_ONLY=''
    
    switch=$(              echo $line | cut -d '|' -f 1 | tr -d '[:blank:]')    
    tftp_address=$(        echo $line | cut -d '|' -f 2 | tr -d '[:blank:]')
    ssh_user=$(            echo $line | cut -d '|' -f 3 | tr -d '[:blank:]')
    ssh_key_or_pass=$(     echo $line | cut -d '|' -f 4 | tr -d '[:blank:]')
    ssh_only=$(            echo $line | cut -d '|' -f 5 | tr -d '[:blank:]') || ''
    dump_opts=$(           echo $line | cut -d '|' -f 6) || ''

	[ "$VERBOSE" ] && DEBUG "Line from $SWITCH_DATA = $line"

	# validate config
	if [ "$switch"x == ""x ]; then	
		echo "Did not find switch name in line ($line) - skipping" 2>&1
		continue
	fi

	case "$switch" in
		'defaultswitch')
			IS_DEFAULT='1'
			DEFAULT_TFTP_ADDRESS="$tftp_address"
			DEFAULT_SSH_USER="$ssh_user"
			if [ -r "$ssh_key_or_pass" ]; then
				DEFAULT_SSH_KEY="$ssh_key_or_pass"
			else
				DEFAULT_SSH_PASS="$ssh_key_or_pass"
			fi
			continue
		;;
	esac

	if [ -n "$tftp_address" ]; then
		[ "$VERBOSE" ] && DEBUG "No TFTP address given - using default: $DEFAULT_TFTP_ADDRESS"
		tftp_address="$DEFAULT_TFTP_ADDRESS"
	fi
	if [ -n "$ssh_user" ]; then
		[ "$VERBOSE" ] && DEBUG "No SSH user name given - using default: $DEFAULT_SSH_USER"
		ssh_user="$DEFAULT_SSH_USER"
	fi
	if [ -n "$ssh_key_or_pass" ]; then
		if [ -r "$ssh_key_or_pass" ]; then
			[ "$VERBOSE" ] && DEBUG "Found SSH key at $ssh_key_or_pass"
			ssh_key="$ssh_key_or_pass"
		else
			[ "$VERBOSE" ] && DEBUG "Using given SSH password"
			ssh_pass="$ssh_key_or_pass"
		fi
	else
		if [ ! -r "$DEFAULT_SSH_KEY" ]; then
			[ "$VERBOSE" ] && DEBUG "No SSH key given - using default: $DEFAULT_SSH_KEY"
			ssh_key="$DEFAULT_SSH_KEY"
		else
			[ "$VERBOSE" ] && DEBUG "No SSH key given and no default key at $DEFAULT_SSH_KEY found - using default SSH password"
			ssh_pass="$DEFAULT_SSH_PASS"
		fi
	fi
	if [ -n "$ssh_only" ]; then
		[ "$VERBOSE" ] && DEBUG "SSH only option is set to $ssh_only - will not use the expect script"
		SSH_ONLY='yes'
	fi
	if [ -n "$dump_opts" ]; then
		[ "$VERBOSE" ] && DEBUG "Will use additional dump options: $dump_opts"
	fi

	# create log directory if it does not exist
	[ "$VERBOSE" ] && DEBUG "Using: ${DEFAULT_SWITCHBACKUP_LOG_PATH}/${switch}.dump.log as log file"
	test -d "$DEFAULT_SWITCHBACKUP_LOG_PATH" || $DO_ECHO mkdir -p "$DEFAULT_SWITCHBACKUP_LOG_PATH"
	logfile="${DEFAULT_SWITCHBACKUP_LOG_PATH}/${switch}.dump.log"
	
	if [ "$SSH_ONLY" != 'yes' ]; then
		LOG "Starting backup for $switch via expect script" "$logfile"
		$DO_ECHO $BACKUP_SCRIPT 'backup' "$switch" "$tftp_address" "$ssh_user" "$ssh_key" "$logfile" "$SWITCHBACKUP_SUBDIR" >> "$logfile"
	else
		LOG "Starting backup for $switch via ssh" "$logfile"
		case $dump_opts in 
			*use_yes*)
				[ "$VERBOSE" ] && DEBUG "use_yes is set: calling SSH with yes pre-command"
				precommand='yes "" | '
			;;
			*) precommand=''
			;;
		esac
		if [ -n "$ssh_key" ]; then
			$DO_ECHO $precommand /usr/bin/ssh -n -l "$ssh_user" -i "$ssh_key" "$switch" "copy running-config tftp://$tftp_address/$SWITCHBACKUP_SUBDIR/$switch.conf $dump_opts " >> "$logfile" 2>>"$logfile"
		else
			LOG "ERROR: using SSH log in with password on command line is not implemented yet" "$logfile"
		fi 
	fi
	LOG "Backup of $switch finished" "$logfile"
  done < "$SWITCH_DATA"

  LOG "Calling $UPDATE_SWITCHBACKUP_SVN" "$logfile"
  $DO_ECHO $UPDATE_SWITCHBACKUP_SVN

  switchbackup_dir="$SWITCHBACKUP_BASE_DIR/$SWITCHBACKUP_SUBDIR"
  switchbackup_xml_dir="$SWITCHBACKUP_BASE_DIR/$SWITCHBACKUP_XML_SUBDIR"

  for i in $(grep -l R800_NIK_1_4_194_194 $switchbackup_dir/*.conf) ; do
	if [ ! -f $i ]; then continue; fi	
        F=$(basename $i)
        $DO_ECHO rm -f $switchbackup_xml_dir/$F.error
        $DO_ECHO $GEL -s /usr/share/doc/packages/python-Gelatin/syntax-ios.gel $i > $switchbackup_xml_dir/$F.xml 2> $switchbackup_xml_dir/$F.error
        test -s $switchbackup_xml_dir/$F.error || $DO_ECHO rm $switchbackup_xml_dir/$F.error
  done
fi

#set +x


