harr2969 DD-WRT Novice
Joined: 08 Jun 2011 Posts: 32
|
Posted: Fri Mar 24, 2017 22:45 Post subject: How-to: Include-style multi line backup script |
|
I spent a lot of time testing and trying to fix backup scripts which were supposed to handle multi-line NVRAM data (such as an ssh key, or multiple cron lines).
None of them worked for me on my WRT1900ACv2 nor my rt-ac3200. I'll paste my refactored script here, and I'll add some detail afterward.
Code: |
#!/bin/sh
#Discussion: http://www.dd-wrt.com/phpBB2/viewtopic.php?p=1072420
#set the config file
config=/opt/etc/backup_nvram.ini
#get the timestamp into a variable we can more easily use
now=$(date +"%Y%m%d-%I%M%S")
#set the backupfile
backupfile=/opt/backup/ddwrt-config.$now.txt
#show where we output to
echo -e "Output to $backupfile\n"
#to use stdout instead of the output file, uncomment the line below
#backupfile=/proc/$$/fd/1
#check if the config file exists, or throw an error
if [ -r $config ] ; then
#read each config line and get it into a format that is parsable by grep. This is slightly better than simply using grep -f in our case.
grepstr=^$(cat $config | sed -e 's/[\t ]//g;/^$/d' | sed ':a;N;$!ba;s/\n/\|\^/g')
else
echo -e "$config was empty or missing, create it with one desired output term per line: \n e.g. cron_ \n ddns_ \n dhcp_ \n"
exit
fi
#show what our grep string looks like. Note this output shows some regular expressions (regex) such as ^(newline), $ (EOL), | (OR operator)
echo -e "Exporting $grepstr \n"
#read the nvram data into a loop, locate only those items that have an equal sign, then exclude hardware addresses, items that have NULL values, and settings that equal ZERO
#note: If you specifically disable (aka set to ZERO) stuff that is enabled by default, remove "|\=0$" from the string below so it will be exported.
for item in $(nvram show 2>/dev/null | grep "^.*=" | grep -v -E hwaddr\|=$\|\=0$ | grep -E "$grepstr" | awk -F= "{print \$1}" | sort -u)
#loop through the exported values and get them into the format needed for re-import
do
item_value=$(nvram get $item | sed 's!\([\$\"\`]\)!\\\1!g')
echo "nvram set ${item}=\"$item_value\"" >>"$backupfile"
done
echo " Lines Words Characters"
cat $backupfile | wc
|
My backup_nvram.ini file looks like this - you may need to add or take away items you don't care about:
Code: | ath[0-2]_ssid
ath[0-2]_security_mode
ath[0-2]_akm
ath[0-2]_wpa_psk
ath[0-2]_net_mode
wl[0-2]_ssid
wl[0-2]_security_mode
wl[0-2]_akm
wl[0-2]_wpa_psk
wl[0-2]_crypto
cron_
ddns_
dhcp_
dns_redirect
dnsmasq_
filter_
forward_
lan_domain
local_dns
ntp_
rc_startup
rc_shutdown
recursive_dns
refresh_time
router_style
schedule_
security_mode
sshd_
static_leases
static_route
sv_localdns
time_zone
traff
usb_
wan_dns
wan_domain
wan_hostname
|
I have my USB drive auto mounted as /opt, as I think most people do.
Other related scripts: http://www.dd-wrt.com/phpBB2/viewtopic.php?p=252281
http://www.linksysinfo.org/index.php?threads/nvram-export-can-not-handle-wrap-text.68478/ https://gist.github.com/ilium007/c35ef98a3d92352d2ba525e2fe4d722e
There were two main problems I uncovered with other scripts that didn't work:
1. The sed line had an extra slash in the output - was: sed ':a;N;$!ba;s/\n/\\\|\^/g'
2. The grep line didn't interpret the pipe as OR until I added -E
Notes:
This version is an INCLUDE rather than EXCLUDE type.
I changed this to output to file by default, with a timestamp of YYMMDD-HHMMSS
I don't save variables which have a setting of "0", because I don't disable any built-in functions. This is noted in the script.
Put this in your cron file: Administration -> Management
* * * * 0 root /opt/settings_backup.sh |
|