#!/bin/bash # sab-dell2824-backup.sh, ABr, 20150122 # Backup Dell2824 configuration daily # host and login info - modify these for your environment l_sab_dell2824_backup_host=${sab_dell2824_backup_host:-192.168.1.254} l_sab_dell2824_backup_login_uid=${sab_dell2824_backup_login_uid:-admin} l_sab_dell2824_backup_login_pwd=${sab_dell2824_backup_login_pwd:-admin} # destination file support # default to current folder l_sab_dell2824_backup_dir=${sab_dell2824_backup_dir:-.} # parameterize the destination name with date thrown in l_sab_dell2824_backup_prefix='Dell2824-config-' l_sab_dell2824_backup_suffix='.cfg' l_sab_dell2824_backup_fname="$l_sab_dell2824_backup_prefix$(date '+%Y%m%d')$l_sab_dell2824_backup_suffix" l_sab_dell2824_backup_path="$l_sab_dell2824_backup_dir/$l_sab_dell2824_backup_fname" ## tftp file support (unused) #l_sab_dell2824_backup_tftphost=${sab_dell2824_backup_tftphost:-192.168.1.2} #l_sab_dell2824_backup_tftp_dir='/var/lib/tftpboot' #l_sab_dell2824_backup_tftp_fname="Dell2824-config-$(date '+%Y%m%d').cfg" #l_sab_dell2824_backup_tftp_path="$l_sab_dell2824_backup_tftp_dir/$l_sab_dell2824_backup_tftp_fname" # general config passed to curl - shouldn't need to modify these l_sab_dell2824_backup_useragent='Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Firefox/31.0' l_sab_dell2824_backup_cookie_jar='./sab-dell2824-backup.cookies' # login page info l_sab_dell2824_backup_login_page="http://$l_sab_dell2824_backup_host/config/log_off_page.htm" l_sab_dell2824_backup_login_data="restoreUrl=&errorCollector=&userName%24query=$l_sab_dell2824_backup_login_uid&password%24query=$l_sab_dell2824_backup_login_pwd" # "restore_saved" info (after login) l_sab_dell2824_backup_restoresaved_referer="http://$l_sab_dell2824_backup_host/home.htm" l_sab_dell2824_backup_restoresaved_page="http://$l_sab_dell2824_backup_host/config/restore_saved.htm" # "go_home" info (after restoring saved info) l_sab_dell2824_backup_gohome_referer="http://$l_sab_dell2824_backup_host/config/restore_saved.htm" l_sab_dell2824_backup_gohome_page="http://$l_sab_dell2824_backup_host/Home.htm" # history cleanup page l_sab_dell2824_backup_cleanuphistory_referer="http://$l_sab_dell2824_backup_host/config/software_ul.htm" l_sab_dell2824_backup_cleanuphistory_page="http://$l_sab_dell2824_backup_host/config/copy_history_delete.htm" # download instructions page info (so we can make the download) l_sab_dell2824_backup_dldata_referer="http://$l_sab_dell2824_backup_host/config/copy_history_delete.htm" l_sab_dell2824_backup_dldata_page="http://$l_sab_dell2824_backup_host/config/software_dl_data.htm" l_sab_dell2824_backup_dldata_index=0 # download page info (the whole point of this effort) l_sab_dell2824_backup_download_referer="http://$l_sab_dell2824_backup_host/config/software_dl_data.htm" l_sab_dell2824_backup_download_page="http://$l_sab_dell2824_backup_host/config/stupCfg.ber" l_sab_dell2824_backup_download_data_tmpl='rlCopyFreeHistoryIndex=INDEX&&rlCopyDestinationFileType=6' l_sab_dell2824_backup_download_filesize_bytes=0 # status page info (wait for download to complete) l_sab_dell2824_backup_wait_referer="http://$l_sab_dell2824_backup_host/config/software_ul.htm" l_sab_dell2824_backup_wait_page="http://$l_sab_dell2824_backup_host/config/copy_history_status.htm" l_sab_dell2824_backup_wait_data='[rlCopyHistoryTableVT]Query:rlCopyHistoryHistoryIndex=1' # logoff page info l_sab_dell2824_backup_logoff_referer="http://$l_sab_dell2824_backup_host/config/authentication_page.htm?logOff" l_sab_dell2824_backup_logoff_page="http://$l_sab_dell2824_backup_host/config/logOff_message.htm" # common work variables l_sab_dell2824_backup_rc=0 l_sab_dell2824_backup_ok=1 # generate a file name function sab-dell2824-backup-mkfile { echo "./sab-dell2824-backup-$1.$2" return 0 } # cleanup switch copy history (required to permit downloads) function sab-dell2824-backup-cleanuphistory { local l_sab_dell2824_backup_cleanuphistory_index=$1 sleep 1 echo -n "Checking switch copy history..." local l_sab_dell2824_backup_cleanuphistory_stdout="$(sab-dell2824-backup-mkfile $l_sab_dell2824_backup_cleanuphistory_index stdout)" local l_sab_dell2824_backup_cleanuphistory_output="$(sab-dell2824-backup-mkfile $l_sab_dell2824_backup_cleanuphistory_index output)" local l_sab_dell2824_backup_cleanuphistory_trace="$(sab-dell2824-backup-mkfile $l_sab_dell2824_backup_cleanuphistory_index trace)" curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_cleanuphistory_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --output "$l_sab_dell2824_backup_cleanuphistory_output" \ --trace "$l_sab_dell2824_backup_cleanuphistory_trace" \ "$l_sab_dell2824_backup_cleanuphistory_page" \ 2>&1 >"$l_sab_dell2824_backup_cleanuphistory_stdout" # now read the file and dump the last copy history entered local l_val='name=rlCopyHistoryHistoryIndex ' local l_index="$( \ grep -e "$l_val" "$l_sab_dell2824_backup_cleanuphistory_output" \ | tail -n 1 \ | sed -e 's#.*value=\"\([0-9]\+\)\".*#\1#' \ )" l_index=${l_index:-0} set +x if [ "$l_index" != "0" ]; then echo -n "delete index $l_index..." curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_cleanuphistory_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --data "restoreUrl=&rlCopyHistoryHistoryIndex=$l_index&rlCopyHistoryRowStatus=6" \ --output "$l_sab_dell2824_backup_cleanuphistory_output-b" \ --trace "$l_sab_dell2824_backup_cleanuphistory_trace-b" \ "$l_sab_dell2824_backup_cleanuphistory_page" \ 2>&1 >"$l_sab_dell2824_backup_cleanuphistory_stdout-b" fi echo "OK" return 0 } # logout of the switch function sab-dell2824-backup-logout { sleep 2 echo -n "Logging out of Dell2824..." local l_sab_dell2824_backup_logoff_stdout="$(sab-dell2824-backup-mkfile 99 stdout)" local l_sab_dell2824_backup_logoff_output="$(sab-dell2824-backup-mkfile 99 output)" local l_sab_dell2824_backup_logoff_trace="$(sab-dell2824-backup-mkfile 99 trace)" curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_logoff_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --output "$l_sab_dell2824_backup_logoff_output" \ --trace "$l_sab_dell2824_backup_logoff_trace" \ "$l_sab_dell2824_backup_logoff_page" \ 2>&1 >"$l_sab_dell2824_backup_logoff_stdout" echo "OK" return 0 } # cleanup previous run - but let's not and say we did :) #rm -f sab-dell2824-backup* ## verify TFTP (unused) #if [ ! -d "$l_sab_dell2824_backup_tftp_dir" ]; then # echo "You are missing required folder '$l_sab_dell2824_backup_tftp_dir'" # exit 1 #fi # login echo -n "Logging in to Dell2824..." l_sab_dell2824_backup_login_stdout="$(sab-dell2824-backup-mkfile 05 stdout)" l_sab_dell2824_backup_login_output="$(sab-dell2824-backup-mkfile 05 output)" l_sab_dell2824_backup_login_trace="$(sab-dell2824-backup-mkfile 05 trace)" curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_login_page" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --data "$l_sab_dell2824_backup_login_data" \ --output "$l_sab_dell2824_backup_login_output" \ --trace "$l_sab_dell2824_backup_login_trace" \ "$l_sab_dell2824_backup_login_page" \ 2>&1 >"$l_sab_dell2824_backup_login_stdout" l_sab_dell2824_backup_rc=$? if [ $l_sab_dell2824_backup_rc -eq 0 ]; then echo "OK" else echo "**fail**" l_sab_dell2824_backup_ok=0 fi # setup switch for user if [ $l_sab_dell2824_backup_ok -eq 1 ]; then sleep 2 echo -n "Configure switch for logged-in user..." l_sab_dell2824_backup_restoresaved_stdout="$(sab-dell2824-backup-mkfile 10 stdout)" l_sab_dell2824_backup_restoresaved_output="$(sab-dell2824-backup-mkfile 10 output)" l_sab_dell2824_backup_restoresaved_trace="$(sab-dell2824-backup-mkfile 10 trace)" curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_restoresaved_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --output "$l_sab_dell2824_backup_restoresaved_output" \ --trace "$l_sab_dell2824_backup_restoresaved_trace" \ "$l_sab_dell2824_backup_restoresaved_page" \ 2>&1 >"$l_sab_dell2824_backup_restoresaved_stdout" l_sab_dell2824_backup_rc=$? if [ $l_sab_dell2824_backup_rc -eq 0 ]; then echo "OK" else echo "*fail*" l_sab_dell2824_backup_ok=0 fi fi # add manual cookie based on restore_save.htm embedded dynamic redirect tail sab-*cookies -n 1 | sed -e 's#User.*#checked\ttrue#' >> sab-*cookies # one more connection to setup switch session if [ $l_sab_dell2824_backup_ok -eq 1 ]; then sleep 2 echo -n "Finishing session setup for logged-in user..." l_sab_dell2824_backup_gohome_stdout="$(sab-dell2824-backup-mkfile 15 stdout)" l_sab_dell2824_backup_gohome_output="$(sab-dell2824-backup-mkfile 15 output)" l_sab_dell2824_backup_gohome_trace="$(sab-dell2824-backup-mkfile 15 trace)" curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_gohome_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --output "$l_sab_dell2824_backup_gohome_output" \ --trace "$l_sab_dell2824_backup_gohome_trace" \ "$l_sab_dell2824_backup_gohome_page" \ 2>&1 >"$l_sab_dell2824_backup_gohome_stdout" l_sab_dell2824_backup_rc=$? if [ $l_sab_dell2824_backup_rc -eq 0 ]; then echo "OK" else echo "*fail*" l_sab_dell2824_backup_ok=0 fi fi if [ $l_sab_dell2824_backup_ok -eq 1 ]; then # cleanup history to emulate web client sab-dell2824-backup-cleanuphistory 20 fi if [ $l_sab_dell2824_backup_ok -eq 1 ]; then # get the download data to get the correct index sleep 2 echo -n "Querying switch for download instructions..." l_sab_dell2824_backup_dldata_stdout="$(sab-dell2824-backup-mkfile 25 stdout)" l_sab_dell2824_backup_dldata_output="$(sab-dell2824-backup-mkfile 25 output)" l_sab_dell2824_backup_dldata_trace="$(sab-dell2824-backup-mkfile 25 trace)" curl --location-trusted --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_dldata_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --output "$l_sab_dell2824_backup_dldata_output" \ --trace "$l_sab_dell2824_backup_dldata_trace" \ "$l_sab_dell2824_backup_dldata_page" \ 2>&1 >"$l_sab_dell2824_backup_dldata_stdout" l_sab_dell2824_backup_rc=$? # extract the info we need if [ $l_sab_dell2824_backup_rc -eq 0 ]; then l_val='Name=rlCopyFreeHistoryIndex$scalar' l_sab_dell2824_backup_dldata_index="$( \ grep -e "$l_val" "$l_sab_dell2824_backup_dldata_output" \ | sed -e 's#.*value=\"\([0-9]\+\)\".*#\1#' \ )" # account for not there l_sab_dell2824_backup_dldata_index=${l_sab_dell2824_backup_dldata_index:-0} # don't understand why, but webclient subtracts one from index. sigh... [ $l_sab_dell2824_backup_dldata_index -gt 0 ] \ && l_sab_dell2824_backup_dldata_index=$((l_sab_dell2824_backup_dldata_index-1)) fi if [ $l_sab_dell2824_backup_rc -eq 0 ]; then echo "OK (index: $l_sab_dell2824_backup_dldata_index)" else echo "*fail*" l_sab_dell2824_backup_ok=0 fi fi if [ $l_sab_dell2824_backup_ok -eq 1 ]; then ## setup destination TFTP path #touch "$l_sab_dell2824_backup_tftp_path" #chmod 666 "$l_sab_dell2824_backup_tftp_path" # and do the download sleep 2 echo -n "Downloading switch configuration via HTTP to '$l_sab_dell2824_backup_tftp_fname'..." l_sab_dell2824_backup_download_stdout="$(sab-dell2824-backup-mkfile 30 stdout)" l_sab_dell2824_backup_download_output="$(sab-dell2824-backup-mkfile 30 output)" l_sab_dell2824_backup_download_trace="$(sab-dell2824-backup-mkfile 30 trace)" l_sab_dell2824_backup_download_data=$( \ echo $l_sab_dell2824_backup_download_data_tmpl \ | sed -e "s#=INDEX#=$l_sab_dell2824_backup_dldata_index#" \ ) #set -x curl --location-trusted --silent --get \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_download_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --data "$l_sab_dell2824_backup_download_data" \ --output "$l_sab_dell2824_backup_download_output" \ --trace "$l_sab_dell2824_backup_download_trace" \ "$l_sab_dell2824_backup_download_page" \ 2>&1 >"$l_sab_dell2824_backup_download_stdout" # curl always returns error on the download...we deal with this if [ -s "$l_sab_dell2824_backup_download_output" ]; then # we got a file. assume it's ok. we'll check in a moment :) l_sab_dell2824_backup_download_filesize_bytes=`du -b "$l_sab_dell2824_backup_download_output" | cut -f1` echo "OK ($l_sab_dell2824_backup_download_filesize_bytes bytes)" else echo "*fail* (empty file)" l_sab_dell2824_backup_ok=0 fi fi if [ $l_sab_dell2824_backup_ok -eq 1 ]; then # let's get the last history data so we can compare file sizes. eck. echo -n "Checking to see if we got a good download..." l_sab_dell2824_backup_wait_ctr=0 l_sab_dell2824_backup_wait_xfer=0 l_sab_dell2824_backup_wait_last_file="" while [ $l_sab_dell2824_backup_wait_ctr -lt 3 ]; do l_sab_dell2824_backup_wait_ctr=$((l_sab_dell2824_backup_wait_ctr+1)) # make the call sleep 1 l_sab_dell2824_backup_wait_stdout="$(sab-dell2824-backup-mkfile 35-$l_sab_dell2824_backup_wait_ctr stdout)" l_sab_dell2824_backup_wait_output="$(sab-dell2824-backup-mkfile 35-$l_sab_dell2824_backup_wait_ctr output)" l_sab_dell2824_backup_wait_trace="$(sab-dell2824-backup-mkfile 35-$l_sab_dell2824_backup_wait_ctr trace)" l_sab_dell2824_backup_wait_last_file="$l_sab_dell2824_backup_wait_output" curl --location-trusted --get --silent \ --user-agent "$l_sab_dell2824_backup_useragent" \ --referer "$l_sab_dell2824_backup_wait_referer" \ --cookie "$l_sab_dell2824_backup_cookie_jar" \ --cookie-jar "$l_sab_dell2824_backup_cookie_jar" \ --data "$l_sab_dell2824_backup_wait_data" \ --output "$l_sab_dell2824_backup_wait_output" \ --trace "$l_sab_dell2824_backup_wait_trace" \ "$l_sab_dell2824_backup_wait_page" \ 2>&1 >"$l_sab_dell2824_backup_wait_stdout" l_sab_dell2824_backup_rc=$? # are we done? if [ $l_sab_dell2824_backup_rc -eq 0 ]; then # check for required entries l_val='NAME=rlCopyHistoryOperationState$repeat' l_sab_dell2824_backup_wait_opstate="$( \ grep -e "$l_val" "$l_sab_dell2824_backup_wait_output" \ | sed -e 's#.*value=\"\([0-9]\+\)\".*#\1#' \ )" set +x l_sab_dell2824_backup_wait_opstate=${l_sab_dell2824_backup_wait_opstate:-0} # if we get multiple chars back, take the last one l_sab_dell2824_backup_wait_opstate=$(echo $l_sab_dell2824_backup_wait_opstate | sed -e 's#.*\(.\)$#\1#') # echo "Found state: '$l_sab_dell2824_backup_wait_opstate'" l_val='NAME=rlCopyHistoryBytesTransferred$repeat' l_sab_dell2824_backup_wait_xfer="$( \ grep -e "$l_val" "$l_sab_dell2824_backup_wait_output" \ | sed -e 's#.*value=\"\([0-9]\+\)\".*#\1#' \ )" set +x l_sab_dell2824_backup_wait_xfer=${l_sab_dell2824_backup_wait_xfer:-0} #echo "Found xfer: '$l_sab_dell2824_backup_wait_xfer'" # allowed values case "$l_sab_dell2824_backup_wait_opstate" in 3) echo -n "(copy fail) " l_sab_dell2824_backup_ok=0 l_sab_dell2824_backup_wait_ctr=999 ;; 4) echo -n "(timeout) " l_sab_dell2824_backup_ok=0 l_sab_dell2824_backup_wait_ctr=999 ;; 5) l_sab_dell2824_backup_ok=1 l_sab_dell2824_backup_wait_ctr=999 ;; *) echo -n "." ;; esac set +x else l_sab_dell2824_backup_wait_ctr=999 fi done if [ $l_sab_dell2824_backup_rc -eq 0 ]; then if [ $l_sab_dell2824_backup_download_filesize_bytes -eq $l_sab_dell2824_backup_wait_xfer ]; then echo "OK ($l_sab_dell2824_backup_wait_xfer bytes)" # now copy the file to the destination yes | cp $l_sab_dell2824_backup_download_output $l_sab_dell2824_backup_path else echo "*fail* (expected $l_sab_dell2824_backup_wait_xfer bytes)" fi else echo "*fail*" l_sab_dell2824_backup_ok=0 fi # always cleanup history again...even in error sab-dell2824-backup-cleanuphistory 40 fi # cleanup old files find "$l_sab_dell2824_backup_dir" -name "$l_sab_dell2824_backup_prefix*" -mtime +14 -exec rm {} \; # logout sab-dell2824-backup-logout