Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- ver="nvram_backup v0.95" #- rs232 - 02/24
- b="\033[0;40m"
- w="\033[1;37m"
- alias helpout='echo -e "┌───────────────────────────────────────────────────────────────────────────────┐
- │ ${w}FreshTomato $ver${b} │
- └───────────────────────────────────────────────────────────────────────────────┘
- ┌─────── optional ───┐ ┌─────── at least one is needed ──────┐
- ${w}nvram_backup${b} + │ ${w}usbrestore │ erase${b} │ + │ ${w}sec ipv4 ipv6 vlan mac time dhcp dns${b}│
- └────────────────────┘ │ ${w}wl wl_hw mcast port qos usb adblock${b} │
- │ ${w}tor vpn web tomatoanon restriction ${b} │
- │ ${w}portal misc admin --or--> all${b} │
- └─────────────────────────────────────┘
- ┌─ Usage ───────────────────────────────────────────────────────────────────────┐
- │• usbrestore creates the restore.autorun file on the root of your USB volume │
- │• restore.autorun runs atomatically at the next reboot (once only) │
- │• If usbrestore is not defined the script nvram_backup prints out to stout │
- │• The erase option adds a "nvram erase" at the top of the restore script │
- │• to redirect the config to a file just use nvram_backup \$par1 \$par2 ... >file │
- │• mac is needed only in certain cases like WDS, wl_hw same device restore only │
- └───────────────────────────────────────────────────────────────────────────────┘
- " && exit'
- rar() {
- r=''
- for i in $1; do
- [ "$i" != "$2" ] && r="$r $i"
- done
- echo "$r"
- }
- [ $# -eq 0 ] && helpout || {
- par=$@
- [ "$(echo "$par" | tr ' ' '\n' | grep -v 'usbrestore\|erase' | wc -l)" -eq 0 ] && helpout
- echo "$par" | grep -q "usbrestore" && { mount | grep -q /mnt && {
- loc="$(mount | grep /mnt | awk '{print $3}')"
- loc="${loc}/restore.autorun"
- [ -s "${loc}" ] && rm -f "${loc}" &>/dev/null
- touch "${loc}"
- chmod +x "${loc}"
- nvram set rstrd=0
- nvram commit
- exec >"${loc}"
- echo -e "#!/bin/sh"
- echo -e "nvram get rstrd 2>/dev/null | grep -q 1 && exit"
- } || echo -e "No USB device found. USB config autorestore is not possible"; }
- par=$(rar "$par" "usbrestore")
- echo "$par" | grep -q "erase" && echo -e "nvram erase"
- par=$(rar "$par" "erase")
- exclude="_hw"
- force="wan[0-3]?_hw"
- echo "$par" | grep -q "status-autorun" && {
- USB=$(mount | grep /mnt | awk '{print $3}')
- [ ! -z "$USB" ] && ls -1 "${USB}"/restore.autorun &>/dev/null && { echo -e "☢️ ${USB}/restore.autorun is present and will autorun at the next reboot<br>"
- grep -q 'nvram erase' "${USB}"/restore.autorun &>/dev/null && echo -e "☢️ restore.autorun contains nvram erase"
- echo -e "<br><input type=button value='🧹 delete ' onclick=removeAutorun();></input>" ; }
- exit
- }
- echo "$par" | grep -q "removeautorun" && { mount | grep -q /mnt && {
- loc=$(mount | grep /mnt | awk '{print $3}')
- loc="${loc}/restore.autorun"
- rm -f "${loc}" 2>/dev/null
- }
- exit
- }
- nvram show 1>/tmp/nvshow 2>/dev/null
- for ar in $par; do
- if [[ $ar == 'help' ]]; then helpout
- elif [[ $ar == "sec" ]]; then items="pass|crt|^ssh|https?"
- elif [[ $ar == "ipv4" ]]; then items="ipaddr|br|wan|domain|hostname|portforward|_pptp|ppp|^wan|^lan|^router"
- elif [[ $ar == "ipv6" ]]; then items="^ipv6"
- elif [[ $ar == "vlan" ]]; then items="vlan|^ctf|manual_boot_nv"
- elif [[ $ar == "mac" ]]; then items="^wan[0-9]?_mac|^[0-9]:macadd"
- elif [[ $ar == "time" ]]; then items="^ntp|^ntpd|^tm_"
- elif [[ $ar == "dhcp" ]]; then items="^dhcp|^dhcpd_"
- elif [[ $ar == "dns" ]]; then items="^dnsmasq_|^ddns|^dns_|^dnssec_|^mdns_"
- elif [[ $ar == "wl" ]]; then items="^wl|territory_code"
- elif [[ $ar == "wl_hw" ]]; then items="^[0-9]:"
- elif [[ $ar == "mcast" ]]; then items="^udpxy|^multicast|force_igmpv2|^emf_"
- elif [[ $ar == "port" ]]; then items="^trigforward|upnp|^dmz|^portforward"
- elif [[ $ar == "qos" ]]; then items="qos|limit|bwl_enable|DSCP_fix_enable|^ne_v"
- elif [[ $ar == "usb" ]]; then items="usb|smb|^bt|^ftp|_ftp|^ms_|gro_disable|txworkq|idle_enable"
- elif [[ $ar == "adblock" ]]; then items="^adblock"
- elif [[ $ar == "tor" ]]; then items="^tor_"
- elif [[ $ar == "vpn" ]]; then items="^vpn_|^pptp|tinc|^wg_"
- elif [[ $ar == "web" ]]; then items="^nginx_|^mysql_|^web_"
- elif [[ $ar == "tomatoanon" ]]; then items="^tomatoanon"
- elif [[ $ar == "restriction" ]]; then items="^rrule"
- elif [[ $ar == "portal" ]]; then items="^NC_|^web_"
- elif [[ $ar == "misc" ]]; then items="^webmon|^jumbo|boot_wait|^console_|^fw_|^ne_|^nf_|router_name|^stealth|^telnetd_|wait_time|watchdog"
- elif [[ $ar == "admin" ]]; then items="^script_|anport|ttb|^[r|^c]stats|^sesx|cifs|^jffs2|debug|^nfs_|^snmp_|^log|^sch_|^remote_"
- elif [[ $ar == "all" ]]; then items="pass|crt|^ssh|https?|ipaddr|br|wan|domain|hostname|portforward|_pptp|ppp|^wan|^lan|^ipv6|vlan|^ctf|manual_boot_nv|^wan[0-9]?_mac|^[0-9]:macadd|^ntp|^ntpd|^tm_|^dhcp|^dhcpd_|^dnsmasq_|^ddns|^dns_|^dnssec_|^mdns_|^wl|^[0-9]:|territory_code|^trigforward|upnp|^dmz|^portforward|^udpxy|^multicast|force_igmpv2|^emf_|qos|limit|bwl_enable|DSCP_fix_enable|^ne_v|usb|smb|^bt|^ftp|_ftp|^ms_|gro_disable|txworkq|idle_enable|^adblock|^tor_|^vpn_|^pptp|tinc|^wg_|^nginx_|^mysql_|^web_|^tomatoanon|^rrule|^webmon|^NC_|^web_|^jumbo|boot_wait|^console_|^fw_|^ne_|^nf_|router_name|^stealth|^telnetd_|wait_time|watchdog|^script_|anport|ttb|^[r|^c]stats|^sesx|cifs|^jffs2|debug|^nfs_|^snmp_|^log|^sch_|^remote_"
- else echo "One or more parameters have not been understood"; exit
- fi
- echo "###### $ar ######"
- grepstr=$( echo "$items" | sed -e 's/[\t ]//g;/^$/d' | sed ':a;N;$!ba;s/\n/\\\|/g' )
- echo "### grepstr=$grepstr"
- {
- for item in $(grep "^.*=." /tmp/nvshow | awk -F= "{print \$1}" | grep -E "${grepstr}" | grep -v "${exclude}" | sort -u); do
- item_value="$(nvram get "${item}"| sed 's!\([\$\"\`]\)!\\\1!g'; echo nvgetwasnull)"
- case $item_value in
- nvgetwasnull) ;;
- *) echo "nvram set ${item}=\"${item_value%
- nvgetwasnull}\"" ;;
- esac
- done
- for item in $(grep "^.*=." /tmp/nvshow | awk -F= "{print \$1}" | grep -E "${force}" | sort -u); do
- item_value="$(nvram get "${item}"| sed 's!\([\$\"\`]\)!\\\1!g'; echo nvgetwasnull)"
- case $item_value in
- nvgetwasnull) ;;
- *) echo "nvram set ${item}=\"${item_value%
- nvgetwasnull}\"" ;;
- esac
- done
- }
- done
- rm -f /tmp/nvshow &>/dev/null
- echo $@ | grep -q "usbrestore" && echo "mv \"${loc}\" \"${loc}.$(date +%Y-%h-%d_%H_%m_%S)\""
- echo "nvram commit
- sleep 1
- /sbin/reboot"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement