Skip to content

Instantly share code, notes, and snippets.

@rampageX
Created March 27, 2020 09:04
Show Gist options
  • Select an option

  • Save rampageX/76f3e914f08f60db888bba849d52c5f1 to your computer and use it in GitHub Desktop.

Select an option

Save rampageX/76f3e914f08f60db888bba849d52c5f1 to your computer and use it in GitHub Desktop.
Tomato firmware nvram backup
#!/bin/sh
#USE AT YOUR OWN RISK.
#THIS SCRIPT DOES NOT COME WITH ANY WARRANTY WHATSOEVER.
#
#Backs up selected nvram variables in "nvram export --set" format.
#
#Correctly handles multi-line entries.
#
#Thanks to ryzhov_al for basic approach.
#
#Should work equally well with both MIPS and ARM builds.
#
#Looks for a list of items to export in $etc/scriptname.ini
#OR enter items to grep for below.
#
#The items list is a list of regular expressions to match against the
#nvram variable names.
#
#Script assumes all entries are at beginning of line(prefixed with ^).
#
#Leave items list blank to backup up all of nvram. Resulting in essentially
#the same output as MIPS "nvram export --set"
#
#The items list below is only intended as example and is not complete or
#comprehensive. Customize for your own use.
#
#Edit list below if not using .ini file, it is ignored if .ini file is found
items='
DSCP_
atm_overhead
cifs[1-2]
ctf_
ct_
dhcp_
dhcpd_
ddnsx[0-2]
dnsmasq_
dns_
https_
http_enable
http_lanport
http_wanport
http_passwd
lan_hostname
lan_ipaddr
lan_proto
modem_ipaddr
ne_
new_qoslimit_
nf_
ntp_
portforward
ppp_
pppoe_
qos_
qosl_
router_name
rrule[0-9]
cstats_
rstats_
script_
smbd_
sch_
sshd_eas
sshd_forwarding
sshd_motd
sshd_pass
sshd_port
sshd_remote
sshd_rport
tm_
usb_
upnp_
wan_dns
wan_proto
wan_hostname
wan_domain
wan_hwaddr
wan_mtu
web_css
web_mx
wl[0-9]_security_mode
wl[0-9]_ssid
wl[0-9]_wpa_psk
'
backup_dir="/tmp"
#file to output - default to stdout
filename="$1"
#curr_date=$(date +"%Y-%m-%d")
curr_date="newest"
if [ $# -eq 0 -o "$filename" = "" ]; then
backupfile="${backup_dir}/nvram_backup-${curr_date}.sh"
else
backupfile="${backup_dir}/${filename}-${curr_date}.sh"
fi
grepstr=$( { [ -r $config ] && cat $config || echo "$items" ; } | sed -e 's/[\t ]//g;/^$/d' | sed ':a;N;$!ba;s/\n/\\\|\^/g')
{
echo "#!/bin/sh"
echo "#Exporting $grepstr"
for item in $(nvram show 2>/dev/null | grep "^.*=" | grep "$grepstr" | grep -v "hwaddr" | awk -F= "{print \$1}" | 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
}>"${backupfile}"
chmod +x ${backupfile}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment