TechInfoDepot:Tomato/Basic backup script

From TechInfoDepot
Jump to navigationJump to search

https://bitbucket.org/pedro311/workspace/snippets/gnGxry/script-for-exporting-backup-selected

#!/bin/sh

# USE AT YOUR OWN RISK.
# THIS SCRIPT DOES NOT COME WITH ANY WARRANTY WHATSOEVER.
#
# Backs up selected nvram variables; import them just by running the resulting script.
# Should work equally well with both MIPS and ARM builds.
#
# Thanks to ryzhov_al for basic approach.
#
# 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.
#
# 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='
 dhcpd_
 dns_
 script_
 wl[0-9]
 wan_
 lan_
 mwan_
 vlan_
'

etc=/tmp/etc
base=${0##*/}; base=${base%.*}
config=$etc/$base.ini

# file to output - default to stdout
[ "$1" != "" ] && {
	backupfile="$1"
} || {
	backupfile=/proc/$$/fd/1
}

grepstr=$( { [ -r $config ] && cat $config || echo "$items" ; } | sed -e 's/[\t ]//g;/^$/d' | sed ':a;N;$!ba;s/\n/\\\|\^/g')

{
echo -e "#!/bin/sh\n"
for item in $(nvram show 2>/dev/null | grep "^.*=" | grep "$grepstr"  | 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
echo "nvram commit"
}>"$backupfile"