Skip to content

Instantly share code, notes, and snippets.

@hectorm
Last active August 14, 2019 23:01
Show Gist options
  • Save hectorm/3c9b5bb660e6f2a484730f2cc188a98f to your computer and use it in GitHub Desktop.
Save hectorm/3c9b5bb660e6f2a484730f2cc188a98f to your computer and use it in GitHub Desktop.
#!/bin/sh
# Author: Héctor Molinero Fernández <[email protected]>
# License: MIT, https://opensource.org/licenses/MIT
# Usage:
# ssh [email protected] 'sh -s' < tomato_backup.sh > tomato_restore.sh
# ssh [email protected] 'sh -s' < tomato_restore.sh
set -eu
export LC_ALL=C
template=$(cat <<-'EOF'
# Reset
nvram erase
# Basic Settings
## Network
### MultiWAN
nvram set mwan_num='{{mwan_num}}'
nvram set mwan_cktime='{{mwan_cktime}}'
nvram set mwan_ckdst='{{mwan_ckdst}}'
### WAN Settings
nvram set wan_proto='{{wan_proto}}'
nvram set wan_ipaddr='{{wan_ipaddr}}'
nvram set wan_netmask='{{wan_netmask}}'
nvram set wan_gateway='{{wan_gateway}}'
nvram set wan_get_dns='{{wan_get_dns}}'
nvram set wan_sta='{{wan_sta}}'
nvram set wan_dns_auto='{{wan_dns_auto}}'
nvram set wan_dns='{{wan_dns}}'
nvram set wan_mtu_enable='{{wan_mtu_enable}}'
nvram set wan_run_mtu='{{wan_run_mtu}}'
nvram set wan_mtu='{{wan_mtu}}'
nvram set wan_modem_ipaddr='{{wan_modem_ipaddr}}'
### LAN
nvram set dnssec_enable='{{dnssec_enable}}'
nvram set wan_wins='{{wan_wins}}'
#### LAN 1
nvram set lan_ipaddr='{{lan_ipaddr}}'
nvram set lan_ifname='{{lan_ifname}}'
nvram set lan_stp='{{lan_stp}}'
nvram set lan_netmask='{{lan_netmask}}'
nvram set lan_proto='{{lan_proto}}'
nvram set dhcp_start='{{dhcp_start}}'
nvram set dhcp_lease='{{dhcp_lease}}'
nvram set dhcpd_endip='{{dhcpd_endip}}'
nvram set dhcpd_startip='{{dhcpd_startip}}'
#### LAN 2
# nvram set lan1_ipaddr='{{lan1_ipaddr}}'
# nvram set lan1_ifname='{{lan1_ifname}}'
# nvram set lan1_stp='{{lan1_stp}}'
# nvram set lan1_netmask='{{lan1_netmask}}'
# nvram set lan1_proto='{{lan1_proto}}'
# nvram set dhcp1_start='{{dhcp1_start}}'
# nvram set dhcp1_lease='{{dhcp1_lease}}'
# nvram set dhcpd1_endip='{{dhcpd1_endip}}'
# nvram set dhcpd1_startip='{{dhcpd1_startip}}'
### Ethernet Ports State
nvram set lan_state='{{lan_state}}'
nvram set lan_desc='{{lan_desc}}'
nvram set lan_invert='{{lan_invert}}'
### Wireless (2.4 GHz / eth1)
nvram set wl0_radio='{{wl0_radio}}'
nvram set wl0_mode='{{wl0_mode}}'
nvram set wl0_net_mode='{{wl0_net_mode}}'
nvram set wl0_ssid='{{wl0_ssid}}'
nvram set wl0_closed='{{wl0_closed}}'
nvram set wl0_channel='{{wl0_channel}}'
nvram set wl0_chanspec='{{wl0_chanspec}}'
nvram set wl0_bw_cap='{{wl0_bw_cap}}'
nvram set wl0_nbw='{{wl0_nbw}}'
nvram set wl0_nbw_cap='{{wl0_nbw_cap}}'
nvram set wl0_gmode='{{wl0_gmode}}'
nvram set wl0_nmcsidx='{{wl0_nmcsidx}}'
nvram set wl0_nmode='{{wl0_nmode}}'
nvram set wl0_nctrlsb='{{wl0_nctrlsb}}'
nvram set wl0_security_mode='{{wl0_security_mode}}'
nvram set wl0_akm='{{wl0_akm}}'
nvram set wl0_crypto='{{wl0_crypto}}'
nvram set wl0_wpa_psk='{{wl0_wpa_psk}}'
nvram set wl0_wpa_gtk_rekey='{{wl0_wpa_gtk_rekey}}'
nvram set wl0_country_code='{{wl0_country_code}}'
### Wireless (5 GHz / eth2)
nvram set wl1_radio='{{wl1_radio}}'
nvram set wl1_mode='{{wl1_mode}}'
nvram set wl1_net_mode='{{wl1_net_mode}}'
nvram set wl1_ssid='{{wl1_ssid}}'
nvram set wl1_closed='{{wl1_closed}}'
nvram set wl1_channel='{{wl1_channel}}'
nvram set wl1_chanspec='{{wl1_chanspec}}'
nvram set wl1_bw_cap='{{wl1_bw_cap}}'
nvram set wl1_nbw='{{wl1_nbw}}'
nvram set wl1_nbw_cap='{{wl1_nbw_cap}}'
nvram set wl1_gmode='{{wl1_gmode}}'
nvram set wl1_nmcsidx='{{wl1_nmcsidx}}'
nvram set wl1_nmode='{{wl1_nmode}}'
nvram set wl1_nctrlsb='{{wl1_nctrlsb}}'
nvram set wl1_security_mode='{{wl1_security_mode}}'
nvram set wl1_akm='{{wl1_akm}}'
nvram set wl1_crypto='{{wl1_crypto}}'
nvram set wl1_wpa_psk='{{wl1_wpa_psk}}'
nvram set wl1_wpa_gtk_rekey='{{wl1_wpa_gtk_rekey}}'
nvram set wl1_country_code='{{wl1_country_code}}'
## IPv6
nvram set ipv6_service='{{ipv6_service}}'
## Identification
nvram set router_name='{{router_name}}'
nvram set wan_hostname='{{wan_hostname}}'
nvram set wan_domain='{{wan_domain}}'
## Time
nvram set tm_sel='{{tm_sel}}'
nvram set tm_tz='{{tm_tz}}'
nvram set tm_dst='{{tm_dst}}'
nvram set ntp_updates='{{ntp_updates}}'
nvram set ntp_tdod='{{ntp_tdod}}'
nvram set ntp_server='{{ntp_server}}'
## DHCP/ARP/BW
nvram set dhcpd_static='{{dhcpd_static}}'
nvram set dhcpd_static_only='{{dhcpd_static_only}}'
## Wireless Filter
nvram set wl_macmode='{{wl_macmode}}'
nvram set wl_maclist='{{wl_maclist}}'
nvram set macnames='{{macnames}}'
# Advanced Settings
## AdBlock
nvram set adblock_enable='{{adblock_enable}}'
nvram set adblock_blacklist='{{adblock_blacklist}}'
nvram set adblock_blacklist_custom='{{adblock_blacklist_custom}}'
nvram set adblock_whitelist='{{adblock_whitelist}}'
nvram set dnsmasq_debug='{{dnsmasq_debug}}'
## Miscellaneous
nvram set ctf_disable='{{ctf_disable}}'
nvram set jumbo_frame_enable='{{jumbo_frame_enable}}'
nvram set jumbo_frame_size='{{jumbo_frame_size}}'
# Port Forwarding
## Basic
nvram set portforward='{{portforward}}'
## Basic (IPv6)
nvram set ipv6_portforward='{{ipv6_portforward}}'
# USB & NAS
## USB Support
nvram set usb_enable='{{usb_enable}}'
nvram set usb_usb3='{{usb_usb3}}'
nvram set usb_usb2='{{usb_usb2}}'
nvram set usb_storage='{{usb_storage}}'
nvram set usb_fs_ext4='{{usb_fs_ext4}}'
nvram set usb_fs_ntfs='{{usb_fs_ntfs}}'
nvram set usb_fs_fat='{{usb_fs_fat}}'
nvram set usb_fs_exfat='{{usb_fs_exfat}}'
nvram set usb_ntfs_driver='{{usb_ntfs_driver}}'
nvram set usb_automount='{{usb_automount}}'
nvram set script_usbmount='{{script_usbmount}}'
nvram set script_usbumount='{{script_usbumount}}'
nvram set idle_enable='{{idle_enable}}'
nvram set usb_apcupsd='{{usb_apcupsd}}'
nvram set script_usbhotplug='{{script_usbhotplug}}'
# Administration
## Admin Access
### Admin Access Settings
nvram set http_enable='{{http_enable}}'
nvram set http_lanport='{{http_lanport}}'
nvram set https_enable='{{https_enable}}'
nvram set https_lanport='{{https_lanport}}'
nvram set remote_management='{{remote_management}}'
nvram set remote_mgt_https='{{remote_mgt_https}}'
nvram set http_wanport='{{http_wanport}}'
nvram set web_wl_filter='{{web_wl_filter}}'
### SSL Certificate
nvram set https_crt_cn='{{https_crt_cn}}'
nvram set https_crt_file='{{https_crt_file}}'
nvram set https_crt_gen='{{https_crt_gen}}'
nvram set https_crt_save='{{https_crt_save}}'
### Authorization Settings
nvram set http_username='{{http_username}}'
nvram set http_root='{{http_root}}'
nvram set http_passwd='{{http_passwd}}'
### SSH Daemon
nvram set sshd_eas='{{sshd_eas}}'
nvram set sshd_motd='{{sshd_motd}}'
nvram set sshd_port='{{sshd_port}}'
nvram set sshd_pass='{{sshd_pass}}'
nvram set sshd_remote='{{sshd_remote}}'
nvram set sshd_rport='{{sshd_rport}}'
nvram set sshd_forwarding='{{sshd_forwarding}}'
nvram set sshd_authkeys='{{sshd_authkeys}}'
### Telnet Daemon
nvram set telnetd_eas='{{telnetd_eas}}'
nvram set telnetd_port='{{telnetd_port}}'
### Admin Restrictions
nvram set rmgt_sip='{{rmgt_sip}}'
nvram set ne_shlimit='{{ne_shlimit}}'
## TomatoAnon
nvram set tomatoanon_answer='{{tomatoanon_answer}}'
nvram set tomatoanon_enable='{{tomatoanon_enable}}'
## Bandwidth Monitoring
nvram set rstats_enable='{{rstats_enable}}'
nvram set rstats_path='{{rstats_path}}'
nvram set rstats_stime='{{rstats_stime}}'
nvram set rstats_sshut='{{rstats_sshut}}'
nvram set rstats_bak='{{rstats_bak}}'
nvram set rstats_offset='{{rstats_offset}}'
nvram set rstats_exclude='{{rstats_exclude}}'
## IP Traffic Monitoring
nvram set cstats_enable='{{cstats_enable}}'
nvram set cstats_path='{{cstats_path}}'
nvram set cstats_stime='{{cstats_stime}}'
nvram set cstats_sshut='{{cstats_sshut}}'
nvram set cstats_bak='{{cstats_bak}}'
nvram set cstats_offset='{{cstats_offset}}'
nvram set cstats_exclude='{{cstats_exclude}}'
nvram set cstats_include='{{cstats_include}}'
nvram set cstats_all='{{cstats_all}}'
nvram set cstats_labels='{{cstats_labels}}'
## SES/WPS/AOSS Button
nvram set sesx_b0='{{sesx_b0}}'
nvram set sesx_b1='{{sesx_b1}}'
nvram set sesx_b2='{{sesx_b2}}'
nvram set sesx_b3='{{sesx_b3}}'
nvram set sesx_script='{{sesx_script}}'
## Logging
### Router Logging
nvram set log_file='{{log_file}}'
nvram set log_file_size='{{log_file_size}}'
nvram set log_file_keep='{{log_file_keep}}'
nvram set log_file_custom='{{log_file_custom}}'
nvram set log_file_path='{{log_file_path}}'
nvram set log_remote='{{log_remote}}'
nvram set log_remoteip='{{log_remoteip}}'
nvram set log_remoteport='{{log_remoteport}}'
nvram set log_mark='{{log_mark}}'
nvram set log_events='{{log_events}}'
nvram set log_in='{{log_in}}'
nvram set log_out='{{log_out}}'
nvram set log_limit='{{log_limit}}'
### Web Monitor
nvram set log_wm='{{log_wm}}'
nvram set log_wmtype='{{log_wmtype}}'
nvram set log_wmip='{{log_wmip}}'
nvram set log_wmdmax='{{log_wmdmax}}'
nvram set log_wmsmax='{{log_wmsmax}}'
nvram set webmon_bkp='{{webmon_bkp}}'
nvram set webmon_shrink='{{webmon_shrink}}'
nvram set webmon_dir='{{webmon_dir}}'
## Scheduler
nvram set sch_rboot='{{sch_rboot}}'
nvram set sch_rcon='{{sch_rcon}}'
nvram set sch_c1='{{sch_c1}}'
nvram set sch_c1_cmd='{{sch_c1_cmd}}'
nvram set sch_c2='{{sch_c2}}'
nvram set sch_c2_cmd='{{sch_c2_cmd}}'
nvram set sch_c3='{{sch_c3}}'
nvram set sch_c3_cmd='{{sch_c3_cmd}}'
nvram set sch_c4='{{sch_c4}}'
nvram set sch_c4_cmd='{{sch_c4_cmd}}'
nvram set sch_c5='{{sch_c5}}'
nvram set sch_c5_cmd='{{sch_c5_cmd}}'
## Scripts
nvram set script_init='{{script_init}}'
nvram set script_shut='{{script_shut}}'
nvram set script_fire='{{script_fire}}'
nvram set script_wanup='{{script_wanup}}'
# Commit
nvram commit
reboot
EOF
)
# Escape strings in sed: https://stackoverflow.com/a/29613573
quoteRe() { printf -- '%s' "$1" | sed -e 's/[^^]/[&]/g; s/\^/\\^/g; $!a'\\''"$(printf '\n')"'\\n' | tr -d '\n'; }
quoteSubst() { printf -- '%s' "$1" | sed -e ':a' -e '$!{N;ba' -e '}' -e 's/[&/\]/\\&/g; s/\n/\\&/g'; }
# Substitute params
params=$(printf -- '%s\n' "$template" | sed -n 's/.*{{\([[:alnum:]_-]\{1,\}\)}}.*/\1/p' | sort -u)
for param in $params; do
k=$(quoteRe "$param")
v=$(quoteSubst "$(nvram get "$param" 2>/dev/null)" | sed "s/'/'\\\''/g")
template=$(printf -- '%s\n' "$template" | sed "s/{{${k}}}/${v}/g")
done
# Print script
printf -- '%s\n\n%s\n' '#!/bin/sh' "$template"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment