WikiDevi.Wi-Cat.RU:Tomato/Backup Settings, Logs, & More to USB Drive Script

From WikiDevi.Wi-Cat.RU
Jump to navigation Jump to search
Copyright (c) 2010, AustinSaintAubin@gmail.com

ISC License:

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

This is a script that will back up all of your routers settings, logs, NVRAM data, bandwidth, web usage, sysinfo and more. It is very useful if you often make changes to your router and want the reassurance that your settings are being backed up. Their is nothing worse than doing a 30/30/30 reset and then thinking "…did I write down my settings…". Use this script if you would like to have your router back itself up to a usb storage device on a regular basis by using the scheduler. Also, this script makes use of router lights to let you know when the script is running and if it was successful. Lets get started!

Here are the links to the latest versions.

The Script - [The script show might not be the most up to date version, please use download link above]

Short Description

Backup your router and its contents on a schedule or at anytime.

This is a script that will back up all of your routers settings, logs, NVRAM data, bandwidth, web usage, sysinfo and more. It is very useful if you often make changes to your router and want the reassurance that your settings are being backed up. Their is nothing worse than doing a 30/30/30 reset and then thinking "...did I write down my settings...". Use this script if you would like to have your router back itself up to a usb storage device on a regular basis by using the scheduler. Also, this script makes use of router lights to let you know when the script is running and if it was successful.

Lets get started!

Setup

1. Download the Tomato Full Backup Script
2. Copy the Tomato Full Backup Script that you downloaded to your USB Hard-drive or Flash-drive.
3. Log into your router with the web interface. Usually http://192.168.1.1
4. Then go to "USB and NAS -> USB Support" and in the "Run after mounting" box put the following if statement.
if [ -d /mnt/USB_Disk ]; then
    nvram set usb_disk_main=/tmp/mnt/USB_DISK
    (sh /tmp/scripts/flashLED.sh aoss 5000 6 off) & echo Flashing LED
fi

# Set Bandwith Save History Location
nvram set rstats_path=$(nvram get usb_disk_main)/Tomato/BandwidthUsage/
5. Click "[SAVE]"
6. Unmount you usb drive by clicking [ Unmount ], then remount that same drive by clicking [ Unmount ].
Note: You can also unplug the drive and plug it back in
7. Then go to “Administration -> Scheduler” and add the "Run Backup" command as seen bellow, place it the "Command" field of Custom 1,2, or 3 just use one that's available.
# Run Backups
sh $(nvram get usb_disk_main)/Tomato/TomatoFullBackup.sh
8. Click "[SAVE]"
9. Goto "Tools -> System". The tile of the page should say "Execute System Commands"
10. Copy and past:
# Run Backups
sh $(nvram get usb_disk_main)/Tomato/TomatoFullBackup.sh
11. Click "[Execute] to test if the script is working"
12. You should now have your first set of backups. Congrats, your done..

You can not test your script with the command: sh $(nvram get usb_disk_main)/Tomato/backupToUSB.sh

Or: sh "/tmp/mnt/USB_DRIVE/Tomato/My Scripts of Blue"

General Usage

In the ternal of the router.

# Run Backups
sh $(nvram get usb_disk_main)/Tomato/TomatoFullBackup.sh

Also, the output of the script is now sent to the syslog, so you can now view everything in "Status -> Logs" from the web interface.

To restore Config File you can use the following, or use the web interface.

nvram backup <filename>
nvram restore <filename>

Known Bugs

If you find any bugs please email AustinSaintAubin@gmail.com.

Tomato Backup Script 2011/02/23 - v7.8

If you get this error: "wget: server returned error: HTTP/1.0 401 Unauthorized" then change "admin" to "root" in "wget "http://admin:$(nvram get http_passwd)@$(nvram get lan_ipaddr)/"".

Script

ScriptName="Tomato Backup Script - 2011/10/01 - v1.0.2 - Written By: Austin Saint Aubin"
# Link to this Script: https://docs.google.com/document/d/1gxLsOR-WawC7tYzWt_zRexa5N3EzwlWy5QONzJO8YTI/edit?hl=en
# Link to Tutorial: http://tomatousb.org/tut:backup-settings-logs-more-to-usb-drive-script
# Link to Fourm Post: http://tomatousb.org/forum/t-271380/tomato-backup-settings-log-to-usb-drive-script
# -------------------------------------------------------------------------------------------------
# Use "nvram set usb_disk_main=/tmp/mnt/USB_DISK" to set a global main usb disk location for all scripts.
# Use "nvram set usb_tomato=/tmp/mnt/USB_DISK/Tomato" to set a global tomato files/saves location for all scripts.
# -------------------------------------------------------------------------------------------------
# Gerneral Usage: sh $(nvram get usb_disk_main)/Tomato/Scripts/TomatoFullBackup.sh
# -------------------------------------------------------------------------------------------------
# WithNotes: sh $(nvram get usb_disk_main)/Tomato/Scripts/TomatoFullBackup.sh Here are some notes
# FullBackup: sh $(nvram get usb_disk_main)/Tomato/Scripts/TomatoFullBackup.sh full
# =================================================================================================
# To make backing up from ssh or telnet easy, so from the shell you can type "backup.sh This is a lable for the backup"
# Put "sh $(nvram get usb_disk_main)/Tomato/Scripts/TomatoFullBackup.sh $@" - No Quotes
# Inside "/opt/bin/backup.sh"
# =================================================================================================
# Notes:
#    Set "Color_Output" to stop the prompt from poping up.
#    Set "LED" to have the script turn on an led when starting, and flash that led when finished.
#    I have left some thing comented out, for you ease of use and tweeking pleasure.


# [ General Variables ] ---------------------------------------------------------------------------
Color_Output=yes #(yes/no) Turn color output for concle on with "yes". Looks good if using ssh or telnet.
LED=aoss # Set this to the led you would like to flash when the script is running, otherwise leave it blank and nothing will be run.
#   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
OS_VERSION=$(nvram get os_version)
OS_VERSION_UNDERSCORED=$(nvram get os_version | sed -e 's/ /_/g')
#   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -   -
# USB_Drive=$(nvram get usb_disk_tomato)
USB_Drive=$(nvram get usb_disk_main)
RootDIR=$USB_Drive/Tomato
# BackupsLocation="$RootDIR/Tomato/Backups/$OS_VERSION_UNDERSCORED" # Underscored OS Version instead of spaces.
BackupsLocation="$RootDIR/Backups/$OS_VERSION"
NumOfBackupsToKeep=128

# [ Note Variables & Setup ] DO NOT EDIT ----------------------------------------------------------
BackupNotes="$(echo $@ | sed -e 's/ /_/g')" # Do Not Change This
if [ -n "$BackupNotes" ] && [ "$BackupNotes" != "full" ]; then
    BackupNote="_($BackupNotes)"
else
    BackupNote=""
fi

# [ Log Variables ] -------------------------------------------------------------------------------
LogDirectoy="$BackupsLocation"
LogFileName="TomatoFullBackup_Log"
LogLocation="$LogDirectoy/$LogFileName"_"$(date +%Y-%m-%d_%H%M%S)$BackupNote.txt"
# LogLocation="$LogDirectoy/$LogFileName.txt"
# echo -n "" > "$LogLocation"

# =================================================================================================
whatToBackUp()
{
    # This is where all the magic happens, change the backThisUp lines as you like.
    # backThisUp "(1)title of backup for note in logs", "(2) WEB - type of backup", "(3)save location", "(4)filename prefex", "(5)file extention/type", "(6)num of backups to keep", "(7)WEB URL Selector / SH Command / ARK Command", "(8)ARK Folder to Archive"
    #backThisUp "Router Configuration" web "cfg/tomato_v128_mb60ee6.cfg" "$BackupsLocation/Configurations" "tomato_($OS_VERSION_UNDERSCORED)" cfg $NumOfBackupsToKeep
    backThisUp "Router Configuration" nvram backup "$BackupsLocation/Configurations" "tomato_($OS_VERSION_UNDERSCORED)" cfg $NumOfBackupsToKeep
    ## backThisUp "System Log" sh "cat /tmp/var/log/messages" "$BackupsLocation/SysLogs" "syslog" txt $NumOfBackupsToKeep
    backThisUp "System Log" cp file "/tmp/var/log/messages" "$BackupsLocation/SysLogs" "syslog" txt $NumOfBackupsToKeep
    backThisUp SysInfo sh "sysinfo" "$BackupsLocation/SysInfo" "sysInfo" txt $NumOfBackupsToKeep
    backThisUp "NVRam Full List (Show-Raw)" nvram show "$BackupsLocation/NVRam/Show-Raw" "NVRam_($OS_VERSION_UNDERSCORED)" txt $NumOfBackupsToKeep
    backThisUp "NVRam Full List (Quote)" nvram quote "$BackupsLocation/NVRam/Quote" "NVRam_($OS_VERSION_UNDERSCORED)" txt $NumOfBackupsToKeep
    backThisUp "NVRam Full List (Quote Filtered)" nvram quote "$BackupsLocation/NVRam/Quote Filtered - Cross Importable" "NVRam_($OS_VERSION_UNDERSCORED)" txt $NumOfBackupsToKeep "NC|clkfreq|ddnsx|dhcpd|ftp|http|lan_hostname|lan_ipaddr|lan_netmask|lan_proto|log|ntp|qos|rstats|sch|script|sesx|smbd|snmp|sshd|telnetd|tm|usb|vpn_server_|vpn_server1|wan_dns|wan_hostname|web|wl0_" "Remove-NOTHING"
    backThisUp "NVRam Full List (Set)" nvram set "$BackupsLocation/NVRam/Set" "NVRam_($OS_VERSION_UNDERSCORED)" txt $NumOfBackupsToKeep
    backThisUp "Web Domains" cp file "/proc/webmon_recent_searches" "$BackupsLocation/WebUsage/Domains" WebDomains txt $NumOfBackupsToKeep
    backThisUp "Web Searches" cp file "/proc/webmon_recent_searches" "$BackupsLocation/WebUsage/Searches" WebSearches txt $NumOfBackupsToKeep
    backThisUp "Scripts Backup" cp folder "/tmp/*.sh" "$BackupsLocation/Scripts-Backup" "Tomato-Scripts" $NumOfBackupsToKeep
    
    # Backup RStats -------------------
    # Commenting this out beacause you can set bandwidth mon to create its own backups in "Bandwidth > Bandwidth Monitoring > [Create Backups], you could create a backup setting to bakup your Save History Location.
    # BRK 3
    # wget -O - "http://admin:$(nvram get http_passwd)@$(nvram get lan_ipaddr)/bwm-daily.asp" >> /dev/null
    # rstats
    # sleep 3
    # backThisUp "Bandwidth Usage" web "bwm/tomato_rstats_c03f0eb60ee6.gz" "$BackupsLocation/BandwidthUsage-Archive" tomato_rstats_c03f0eb60ee6 gz $NumOfBackupsToKeep
    
    # Backup Optware every Sunday.
    BRK 3
    if [ "Sun" == "$(date +%a)" ] && [ "1" == "$(date +%H)" ] || [ "$BackupNotes" == "full" ]; then
        BLK "Today is: $(date +%a) at 1:xx, or Backup Note was set to the overide command."
        backThisUp "Archive Optware" ark "czf" "/opt" "$BackupsLocation/Optware-Archive" "Optware" "tar.gz" 8
        ### To Extract the Optware Archive:        http://www.dd-wrt.com/wiki/index.php/Optware#.2Fopt_backup
        # cd /; tar xvzf "$(nvram get usb_disk_main)/Tomato/Optware-Archive/Optware_2011-05-01_181510.tar.gz"
    else
        say "Today is ($(date +%a) at $(date +%H):xx) NOT: (Sun at 1:xx), waiting untell then to backing up Optware."
        say "However, you can use (TomatoFullBackup.sh \"full\"), have a look at Usage at the top of the script."
    fi
}

# ==========================================================================================================================================
# There is no need to edit anythign under this line.
# ==========================================================================================================================================

backThisUp()
{
    BRK 2
    WHT "[Backuping up ($1]"
    
    DATE=$(date +%Y-%m-%d_%H%M%S)
    
    if [ -n "$BackupNotes" ] && [ "$BackupNotes" != "full" ]; then
        WHT "With Backup Note: \"$BackupNotes\""
    fi
    
    # cd "$3/"
    if [ $2 == sh ]; then
        # Shell Command
        output_file="$5_$DATE$BackupNote.$6"
        YEL "Using Shell Command: ($3) and logging to ($output_file)"
        checkFolder "$4"
        cleanupFiles "$4" "$5*.$6" "$7"
        # --------------------------------
        BLK "Output File: $output_file"
        BLK "Running Shell Command"
        BLK "$3"
        $3 > "$4/$output_file"
    elif [ $2 == web ]; then
        # Web Download
        YEL "Downloading with (wget)"
        output_file="$5_$DATE$BackupNote.$6"
        checkFolder "$4"
        cleanupFiles "$4" "$5*.$6" "$7"
        # --------------------------------
        BLK "Output File: $output_file"
        wget "http://root:$(nvram get http_passwd)@$(nvram get lan_ipaddr)/$3?_http_id=$(nvram get http_id)" -O "$4/$output_file"
    elif [ $2 == ark ]; then
        # File/Folder Archiving
        YEL "Archiving ($4) with ($3) to ($5)"
        output_file="$6_$DATE$BackupNote.$7"
        checkFolder "$5"
        cleanupFiles "$5" "$6*.$7" "$8"
        # --------------------------------
        BLK "Output File: $output_file"
        tar "$3" "$5/$output_file" "$4"
    elif [ $2 == cp ]; then
        # File/Folder Copy
        if [ $3 == file ]; then
            output_file="$6_$DATE$BackupNote.$7"
            file="$5/$output_file"
            YEL "Copying File: ($4) to ($file)"
            checkFolder "$5"
            cleanupFiles "$5" "$6*.$7" "$8"
            # --------------------------------
            BLK "Output File: $output_file"
            cp "$4" "$file"
            chmod 777 "$file"
        elif [ $3 == folder ]; then
            folder="$6_$DATE$BackupNote"
            YEL "Copying Folder/Dir: ($4) to ($folder)"
            cleanupFolder "$5" "$6_*" "$7"
            # --------------------------------
            BLK "Output Folder: $folder"
            checkFolder "$5/$folder"
            echo cp "$4" "$5/$folder"
            cp $4 "$5/$folder"
            chmod 777 "$5/$folder"
        else
            WRN "Copy Error ($3)"
        fi
    elif [ $2 == nvram ]; then
        checkFolder "$4"
        cleanupFiles "$4" "$5*.$6" "$7"
        # --------------------------------
        if [ $3 == backup ]; then
            output_file="$5"_"$DATE$BackupNote.$6"
            
            nvram backup /tmp/nvram_config.cfg
            mv "/tmp/nvram_config.cfg" "$4/$output_file"
            #rm -f "/tmp/nvram_config.cfg"
            
            BLK "Output File: $output_file"
            YEL "Router Configuration / NVRAM Backup"
        # --------------------------------
        elif [ $3 == quote ]; then
            if [ -n "$8" ] || [ -n "$9" ]; then
                output_file="$5"_"$DATE"_"(Quote-F)$BackupNote.$6"
                # Filtered NVRAM Backup
                YEL "Filtering In:  ($8)"
                YEL "Filtering Out: ($9)"
                nvram export --quote | egrep -i "($8).?+=" | egrep -i -v "($9).?+=" | sort | awk '{print "\#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-\nnvram set " $0}' | sed 's/\\n/\n/g' | sed 's/\$/\\$/g' | sed 's/`/\\`/g' | sed 's/\\t/$(echo -e "\\t")/g' > "$4/$output_file"
                echo -e "# ===========================================================================\nnvram commit # Commit these settings\n# ===========================================================================\n# Filtered In:  ($8)\n# Filtered Out: ($9)" >> "$4/$output_file"
            else
                output_file="$5"_"$DATE"_"(Quote)$BackupNote.$6"
                # Regular NVRAM Backup
                nvram export --quote | sort | awk '{print "\#-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-\nnvram set " $0}' | sed 's/\\n/\n/g' | sed 's/\$/\\$/g' | sed 's/`/\\`/g' | sed 's/\\t/$(echo -e "\\t")/g' > "$4/$output_file"
            fi
            BLK "Output File: $output_file"
            YEL "NVRAM Quote"
        elif [ $3 == set ]; then
            if [ -n "$8" ] || [ -n "$9" ]; then
                output_file="$5"_"$DATE"_"(Set-F)$BackupNote.$6"
                # Filtered NVRAM Backup
                YEL "Filtering In:  ($8)"
                YEL "Filtering Out: ($9)"
                nvram export --set | egrep -i "($8).?+=" | egrep -i -v "($9).?+=" > "$4/$output_file"
                echo -e "# ===========================================================================\nnvram commit # Commit these settings\n# ===========================================================================\n# Filtered In:  ($8)\n# Filtered Out: ($9)" >> "$4/$output_file"
            else
                output_file="$5"_"$DATE"_"(Set)$BackupNote.$6"
                # Regular NVRAM Backup
                nvram export --set > "$4/$output_file"
            fi
            BLK "Output File: $output_file"
            YEL "NVRAM Set"
        elif [ $3 == show ]; then
            if [ -n "$8" ] || [ -n "$9" ]; then
                output_file="$5"_"$DATE"_"(Show-Raw-F)$BackupNote.$6"
                # Filtered NVRAM Backup
                YEL "Filtering In:  ($8)"
                YEL "Filtering Out: ($9)"
                nvram show | egrep -i "($8).?+=" | egrep -i -v "($9).?+=" > "$4/$output_file"
                echo -e "# ===========================================================================\nnvram commit # Commit these settings\n# ===========================================================================\n# Filtered In:  ($8)\n# Filtered Out: ($9)" >> "$4/$output_file"
            else
                output_file="$5"_"$DATE"_"(Show-Raw)$BackupNote.$6"
                # Regular NVRAM Backup
                nvram show > "$4/$output_file"
            fi
            BLK "Output File: $output_file"
            YEL "NVRAM Show-Raw"
        else
            WRN "NVRAM Get Error ($3)"
        fi
    else
        WRN "backThisUp Error"
    fi
    
    BRK 4
    GRN "Backup of $1, Done"
}

# [ Support Functions ] ---------------------------------------------------------------------------
checkFolder() { [ -d "$@" ] && BLU "Folder Exists: $@" || (MAG "Making Folder: $@"; mkdir -p "$@"); } #CheckFolder - v4 - 2011/07/29
say() { echo "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }

# Color_Output Prompt  ----------------------------------------------------------------------------
if [ -z $Color_Output ]; then
    read -t 10 -n 1 -p "Would you like color output (y/n)? "
    echo ""
    
    if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
        echo "Choice: Yes"
        Color_Output=yes
    else
        echo "Choice: No, or prompt timed out"
        Color_Output=no
    fi
fi

# COLOR( "Your Text" ) Only one color per line when logging ---------------------------------------
if [ $Color_Output == yes ]; then
    BLK() { echo -e "\033[30;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    RED() { echo -e "\033[31;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    GRN() { echo -e "\033[32;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    YEL() { echo -e "\033[33;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    BLU() { echo -e "\033[34;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    MAG() { echo -e "\033[35;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    CYN() { echo -e "\033[36;40;1m$@\033[0m"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    WHT() { echo -e "\033[37;40;1m$@\033[0m \7"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; } # "\7" = BEL = Bell Sound
    WRN() { echo -e "\033[31;40;1m/\033[33;40;1m!\033[31;40;1m\\ \033[33;40;1mWARNING:\033[37;40;1m $@ \033[31;40;1m/\033[33;40;1m!\033[31;40;1m\ \\033[0m"; echo "/!\\ WARNING: $@ /!\\" >> "$LogLocation"; logger -t BackupToUSB "/!\\ WARNING: $@ /!\\"; }
    PASS() { echo -e "\033[30;40;1m$@ \t \033[37;40;1m[\033[32;40;1mPass\033[37;40;1m]\033[0m"; echo -e "$@ \t [Fail]" >> "$LogLocation"; logger -t BackupToUSB "$@ [Pass]"; }
    FAIL() { echo -e "\033[37;40;1m$@ \t \033[37;40;1m[\033[31;40;1mFail\033[37;40;1m]\033[0m \7"; echo -e "$@ \t [Pass]" >> "$LogLocation"; logger -t BackupToUSB "$@ [Fail]"; }
elif [ $Color_Output == no ]; then
    BLK() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    RED() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    GRN() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    YEL() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    BLU() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    MAG() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    CYN() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; }
    WHT() { echo -e "$@"; echo "$@" >> "$LogLocation"; logger -t BackupToUSB "$@"; } # "\7" = BEL = Bell Sound
    WRN() { echo -e "/!\\ WARNING: $@ /!\\ \7"; echo "/!\\ WARNING: $@ /!\\" >> "$LogLocation"; logger -t BackupToUSB "/!\\ WARNING: $@ /!\\"; }
    PASS() { echo -e "$@ \t [Pass]"; echo -e "$@ \t [Pass]" >> "$LogLocation"; logger -t BackupToUSB "$@ [Pass]"; }
    FAIL() { echo -e "$@ \t [Fail] \7"; echo -e "$@ \t [Fail]" >> "$LogLocation"; logger -t BackupToUSB "$@ [Fail]"; }
else
    WRN() { echo -e "/!\\ WARNING: $@ /!\\ \7"; echo "/!\\ WARNING: $@ /!\\" >> "$LogLocation"; logger -t BackupToUSB "/!\\ WARNING: $@ /!\\"; } # "\7" = BEL = Bell Sound
    WRN "Color_Output=(yes/no), please set Color_Output variable to \"yes\" or \"no\""
    exit 0
fi

# BRK( Number ) Line Formating Brake --------------------------------------------------------------
BRK()
{
    if [ $1 == 1 ]; then
        YEL "***===============*****===============***"
    elif [ $1 == 2 ]; then
        BLU "========================================="
    elif [ $1 == 3 ]; then
        BLK "~---------------------------------------~"
    elif [ $1 == 4 ]; then
        BLK "~ - - - - - - - - - - - - - - - - - - - ~"
    elif [ $1 == 5 ]; then
        BLK "........................................."
    else
        WRN "Break Formating ERROR [ $2 ]"
    fi
}

# checkDependencies [AppToCheck]
checkDependencies() # v2 - 2011-30-2011, Checks to make sure that the script has everything it needs to run.
{
    # DependenciesList is the list of application this script needs to have avalible in order to run.
    if [ -z "$1" ]; then
        DependenciesList="echo usleep sleep wget grep sed tar cp chmod rm rstats sh"
        YEL "Checking Scripts Dependencies"
        BLK "$DependenciesList"
    else
        DependenciesList="$1"
    fi
    
    BRK 5
    for DPCY in $DependenciesList; do
        DPCY_Location="$(which $DPCY)"
        if [ -z "$DPCY_Location" ]; then
            FAIL "$DPCY"
            # WRN "Exiting script because of Dependency Failure"
            WRN "Dependency Failure, installation of [$DPCY] needed"
            
            if [ -n "$(which ipkg)" ]; then
                read -t 30 -n 1 -p "Would you like to install [$DPCY] by using \"ipkg\" (y/n)? "
                echo ""
                if [ "$REPLY" == "y" ] || [ "$REPLY" == "Y" ]; then
                    # echo "Choice: Yes"
                    BLK "Updating IPKG Package List"
                    ipkg update
                    MAG "Running IPKG Install of: $DPCY"
                    ipkg install "$DPCY"
                    # Call checkDependencies Function for DPCY and test to see if install was a success.
                    checkDependencies "$DPCY"
                else
                    # echo "Choice: No"
                    WRN "Exiting script because of Dependency Failure, install [$DPCY] manualy, or install optware and use it to install [$DPCY]"
                    exit 0
                fi
            else
                WRN "Exiting script because of Dependency Failure, install [$DPCY] manualy, or install optware and use it to install [$DPCY]"
                exit 0
            fi
            
        else
            PASS "$DPCY\t |\t $DPCY_Location"
        fi
    done
    
    BRK 5
    if [ -z "$1" ]; then
        GRN "All Dependencies are Pressent. =^-^="
    fi
}

# cleanupFiles [DIR] [FileFilter] [NumOFilesKeep]
cleanupFiles()
{
    BRK 4
    CYN "Running Cleanup for $1/$2"
    YEL "Num of Backups to keep: $3"
    
    cd "$1/"
    
    BRK 5
    if [ "$(ls -A $2)" ]; then
        
        NumOFiles=$(find $2 | grep -v "/" | wc -l);
        i=$NumOFiles
        ls -1 $2  | grep -v "/" | while read file
        do
            # say "Checking($i): $file"
            BLK "Checking($(printf "%02d" $i)): $file"
            if [ $NumOFiles -gt $3 ]; then
                RED "[$NumOFiles > $3 ] Deleting #$i: $file"
                 rm -f "$file"
                NumOFiles=$(find $2 | grep -v "/" | wc -l);
            fi
            let i--
        done
    else
        RED "Folder: \"$1\" is empty of \"$2\""
    fi
    BRK 5
}

# cleanupFolder [DIR] [Filter] [NumOFilesKeep]
cleanupFolder()
{
    BRK 4
    CYN "Running Cleanup for $1/$2"
    YEL "Num of Backups to keep: $3"
    
    BRK 5
    if [ "$(ls -A "$1")" ]; then
        cd "$1/"
        
        NumOFolders=$(find $2 | grep -v "/" | wc -l);
        i=$NumOFolders
        ls -1 -d */ | while read folder
        do
            # say "Checking($i): $folder"
            BLK "Checking($(printf "%02d" $i)): $folder"
            if [ $NumOFolders -gt $3 ]; then
                RED "[$NumOFolders > $3 ] Deleting #$i: $folder"
                rm -f -R "$folder"
                NumOFolders=$(find $2 | grep -v "/" | wc -l);
            fi
            let i--
        done
    else
        RED "Folder: \"$1\" is empty of \"$2\""
    fi
    BRK 5
}

# Flash LED Script v1.5 2011/08/10
# Written By: Austin Saint Aubin
# flashLED( [LEDNAME] [SleepTime] [NumberOfTimes] [EndState] )
# flashLED( $LED 12500 5 off )
flashLED()
{
    if [ $1 ]; then
        if [ $2 == 0 ]; then
            BLK "Turn LCD ($1): $4"
            led $1 $4
        else
            BLK "Flash LED ($1) for ($2), $3 times"
            i=1
            while [ $i -le $3 ]; do
            logger -s -t FlashLED "Flash LED ($1) for ($2), $3 times. (Iterration: $i)"
            usleep $2
            led $1 on
            usleep $2
            led $1 off
            let i++
            done
            
            led $1 $4
        fi
    else
        BLK Not flashing any LED, LCD varible not set.
    fi
}


###################################################################################################
echo "Starting Backup Script: $ScriptName"

# [ Main ] ========================================================================================
if [ -d "$USB_Drive" ]; then
    checkFolder "$RootDIR"
    checkFolder "$BackupsLocation"
    
    BRK 1
    WHT "***[Running Backup]*** $(date +%Y-%m-%d_%H%M%S)"
    RED "$ScriptName"
    CYN "$OS_VERSION"
    BRK 1
    
    if [ -n "$BackupNotes" ] && [ "$BackupNotes" != "full" ]; then
        WHT "With Backup Notes: \"$BackupNotes\""
        BRK 2
    fi
    
    
    BLK "Color_Output set to: $Color_Output"
    BLK "You can change this by setting \"Color_Output\" to \"yes\" or \"no\""
    BRK 2
    
    checkDependencies
    BRK 2
    
    flashLED "$LED" 0 0 on
    
    say "USB Drive: $USB_Drive"
    say "Checking backup location: $(checkFolder "$BackupsLocation")"
    BRK 2
    
    BLK "Cleaning Old Logfiles in: $LogDirectoy"
    cleanupFiles "$LogDirectoy" "$LogFileName_*.txt" "$NumOfBackupsToKeep"
    BLK "Loging to: $LogLocation"
    # ====================================================================================
    whatToBackUp
    # ====================================================================================
    BRK 3
    
    #cp /tmp/scripts/TomatoFullBackup.sh "$BackupsLocation"
    flashLED "$LED" 500000 3 off
    GRN "***[Backup Completed]*** $(date +%Y-%m-%d_%H%M%S)"
    exit 1
else
    LogLocation="/tmp/$LogFileName"_"$(date +%Y-%m-%d_%H%M%S)$BackupNote.txt"
    BLK "Log Location: $LogLocation"
    BRK 4
    
    WRN "USB_Drive not set! (or) FlashDrive not valid"
    RED "USB_Drive is set to: \"$USB_Drive\""
    
    BRK 4
    YEL "Your USB_Drive Options are:"
    WHT "$(ls /mnt/)"
    BRK 4
    
    YEL "One way to fix this problem if the varable is not set is to do the following."
    WHT "Go to \"USB and NAS -> USB Support\" and in the \"Run after mounting\" box put the following if statement."
    WHT "\"nvram set usb_disk_main=/mnt/YOUR_USB_DISK\" (NO QUOTES)"
    WHT "\"nvram set rstats_path=$(nvram get usb_disk_main)/\""
    WHT "Click \"[SAVE]\""
    BRK 4
    
    WRN "Exiting Script."
    flashLED "$LED" 100000 10 on
fi