TechInfoDepot:DD-WRT/Backup settings and restore them

From TechInfoDepot
Jump to navigationJump to search

Broadcom

Credit to hardwarewizard and frater

Reference Thread

If you have OTRW installed, this script is included in

/opt/usr/sbin/backupessential

and the resulting files will be saved to

/opt/var/backups


#!/bin/sh
#########################################################
#   Script Requirements
#
#   Files:
#      vars_to_skip
#      vars_preferred
#
#   Programs:
#      curl
#########################################################

#########################################################
# setup variables
#
# DATE         - Date
# MAC         - Mac address
# FILE         - File Name Prefix
# CUR_DIR      - Current Directory
# transfer      - FTP Transfer ON/OFF (Default is OFF)
# FOLDER      - Location where backup scripts are stored
# VARFILE      - Location & Name of Temp File
# TO_ALL      - Location & Name of script File with all nvram variables
# TO_INCLUDE   - Location & Name of script File with essential nvram variables
# TO_EXCLUDE   - Location & Name of script File with dangerous nvram variables
# TO_PREFERRED   - Location & Name of script File with preferred nvram variables
#
#########################################################

DATE=`date +%m%d%Y`
MAC=`nvram get lan_hwaddr | tr -d ":"`
FILE=${MAC}.${DATE}
CUR_DIR=`dirname $0`
transfer=0
FOLDER=/opt/var/backups
VARFILE=/opt/tmp/all_vars
TO_ALL=${FOLDER}/${MAC}.${DATE}.all.sh
TO_INCLUDE=${FOLDER}/${MAC}.${DATE}.essential.sh
TO_EXCLUDE=${FOLDER}/${MAC}.${DATE}.dangerous.sh
TO_PREFERRED=${FOLDER}/${MAC}.${DATE}.preferred.sh

#########################################################
#FTP Login information change to your info
#########################################################
 
FTPS=ftp://192.168.1.100/backups
USERPASS=user:pass

#########################################################
# read command line switches
#
#   example command lines
#
#   ./backupvars.sh -t
#
#   The above command with use the user and password and
#   server information embedded in this script.
#   (See FTP Login information above)
#
#
#   ./backupvars.sh -t -u user:pass -f ftp://192.168.1.100/backups
#
#   The above command with use the user and password and
#   server information from the command line
#
#########################################################

while getopts tu:f: name
do
  case $name in
  t)   transfer=1;;
  u)   USERPASS="$OPTARG";;
  f)   FTPS="$OPTARG";;
  ?)   printf "Usage: %s: [-t] [-u username:password] [-f ftpserver]\n" $0
       exit 2;;
  esac
done
shift $(($OPTIND - 1))

#########################################################
#create NVRAM variale list and write to /opt/tmp/all_vars
#########################################################

nvram show 2>/dev/null | egrep '^[A-Za-z][A-Za-z0-9_\.\-]*=' | awk -F = '{print $1}' | sort -r -u >${VARFILE}

#########################################################
# Write header to restore scripts
#########################################################

echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" | tee -i ${TO_EXCLUDE} | tee -i ${TO_PREFERRED} | tee -i  ${TO_ALL} > ${TO_INCLUDE}

#########################################################
# scan NVRAM variable list and send variable to proper
# restore script
#########################################################

cat ${VARFILE} | while read var
do
  pref=0
 ### replaced with next line by Andon Mancev :  if echo "${var}" | grep -q -f "${CUR_DIR}/vars_to_skip" ; then
if cat "${CUR_DIR}/vars_to_skip" | grep -q "${var}"  ; then 
    bfile=$TO_EXCLUDE
  else
    bfile=$TO_INCLUDE
    pref=`echo "${var}" | grep -cf "${CUR_DIR}/vars_preferred"`
  fi

  # get the data out of the variable
  data=`nvram get ${var}`
  # write the var to the file and use \ for special chars: (\$`")
  echo -en "nvram set ${var}=\"" | tee -ia ${TO_ALL} >> ${bfile}
  echo -n "${data}" |  sed -e 's/[$`"\]/\\&/g' | tee -ia  ${TO_ALL} >> ${bfile}
  echo -e "\"" | tee -ia  ${TO_ALL} >> ${bfile}
  if [ ! ${pref} == 0 ]; then
    echo -en "nvram set ${var}=\"" >> ${TO_PREFERRED}
    echo -n "${data}" |  sed -e 's/[$`"\]/\\&/g' >> ${TO_PREFERRED}
    echo -e "\"" >> ${TO_PREFERRED}
  fi
done

#########################################################
# cleanup remove /opt/tmp/all_vars
# uncomment to remove file
#########################################################

rm ${VARFILE}

#########################################################
# Write footer to restore script
#########################################################

echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit"  | tee -ia  ${TO_ALL} | tee -ia  ${TO_PREFERRED} | tee -ia  ${TO_EXCLUDE} >> ${TO_INCLUDE}

#########################################################
# Change permissions on restore scripts to make them
# executable
#########################################################

chmod +x ${TO_INCLUDE}
chmod +x ${TO_PREFERRED}
chmod +x ${TO_EXCLUDE}
chmod +x ${TO_ALL}

#########################################################
# Compress restore scripts and send them to ftp server
#########################################################

if [ ${transfer} -ne 0 ] ; then
  tar cpf - -C / "${TO_INCLUDE}" 2>/dev/null | gzip -c |  /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.essential.sh.tgz" -T -
  tar cpf - -C / "${TO_PREFERRED}" 2>/dev/null | gzip -c |  /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.preferred.sh.tgz" -T -
  tar cpf - -C / "${TO_EXCLUDE}" 2>/dev/null | gzip -c |  /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.dangerous.sh.tgz" -T -
  tar cpf - -C / "${TO_ALL}" 2>/dev/null | gzip -c |  /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.all.sh.tgz" -T -
fi

D-link DIR825

By popamor Source

Here is what I have done. Since there is no sort nor tee on DIR825 and looking at the script, the sort process is used to sort the variables. The next step looks up each variable from the vars_to_skip and write then out to the dangerous file. I think it is safe to remove the sort, so I did. Next the "tee" issue. It made the code really pretty "one-liner" to write out to the various files. I had to split these lines into separate ones and just used ">" and ">>". Frater, please chime in if what I did is dangerous. It worked on my DIR825 to restore my settings after I upgraded to the latest BS release and 30-30-30.

I did not bother with the ftp part as I am doing this on a USB memory stick. I do not know if that works or not. Did not have the time to play with it.

Here is the updated script.

 
#!/bin/sh
#########################################################
# Script Requirements
#
# Files:
# vars_to_skip
# vars_preferred
#
# Programs:
# curl
#########################################################

#########################################################
# setup variables
#
# DATE - Date
# MAC - Mac address
# FILE - File Name Prefix
# CUR_DIR - Current Directory
# transfer - FTP Transfer ON/OFF (Default is OFF)
# FOLDER - Location where backup scripts are stored
# VARFILE - Location & Name of Temp File
# TO_ALL - Location & Name of script File with all nvram variables
# TO_INCLUDE - Location & Name of script File with essential nvram variables
# TO_EXCLUDE - Location & Name of script File with dangerous nvram variables
# TO_PREFERRED - Location & Name of script File with preferred nvram variables
#
#########################################################

DATE=`date +%m%d%Y`
MAC=`nvram get lan_hwaddr | tr -d ":"`
FILE=${MAC}.${DATE}
CUR_DIR=`dirname $0`
transfer=0
FOLDER=/opt/root/backups
VARFILE=/opt/root/all_vars
TO_ALL=${FOLDER}/${MAC}.${DATE}.all.sh
TO_INCLUDE=${FOLDER}/${MAC}.${DATE}.essential.sh
TO_EXCLUDE=${FOLDER}/${MAC}.${DATE}.dangerous.sh
TO_PREFERRED=${FOLDER}/${MAC}.${DATE}.preferred.sh

#########################################################
#FTP Login information change to your info
#########################################################

FTPS=ftp://192.168.1.100/backups
USERPASS=user:pass

#########################################################
# read command line switches
#
# example command lines
#
# ./backupvars.sh -t
#
# The above command with use the user and password and
# server information embedded in this script.
# (See FTP Login information above)
#
#
# ./backupvars.sh -t -u user:pass -f ftp://192.168.1.100/backups
#
# The above command with use the user and password and
# server information from the command line
#
#########################################################

while getopts tu:f: name
do
case $name in
t) transfer=1;;
u) USERPASS="$OPTARG";;
f) FTPS="$OPTARG";;
?) printf "Usage: %s: [-t] [-u username:password] [-f ftpserver]\n" $0
exit 2;;
esac
done
shift $(($OPTIND - 1))

#########################################################
# Ensure backup directory exists, if not create it
#########################################################

if [ ! -d $FOLDER ]; then
mkdir -p $FOLDER
fi

#########################################################
#create NVRAM variale list and write to /opt/opt/all_vars
#########################################################

nvram show 2 | egrep '^[A-Za-z][A-Za-z0-9_\.\-]*=' | awk -F = '{print $1}' > ${VARFILE}

#########################################################
# Write header to restore scripts
#########################################################

echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" > ${TO_EXCLUDE}
echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" > ${TO_PREFERRED}
echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" > ${TO_ALL}
echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" > ${TO_INCLUDE}

#########################################################
# scan NVRAM variable list and send variable to proper
# restore script
#########################################################

cat ${VARFILE} | while read var
do
pref=0
### replaced with next line by Andon Manc(ev : if echo "${var}" | grep -q -f "${CUR_DIR}/vars_to_skip" ; then
if cat "${CUR_DIR}/vars_to_skip" | grep -q "${var}" ; then
bfile=$TO_EXCLUDE
else
bfile=$TO_INCLUDE
pref=`echo "${var}" | grep -cf "${CUR_DIR}/vars_preferred"`
fi

# get the data out of the variable
data=`nvram get ${var}`
# write the var to the file and use \ for special chars: (\$`")
echo -en "nvram set ${var}=\"" >> ${TO_ALL}
echo -en "nvram set ${var}=\"" >> ${bfile}
echo -n "${data}" | sed -e 's/[$`"\]/\\&/g' >> ${TO_ALL}
echo -n "${data}" | sed -e 's/[$`"\]/\\&/g' >> ${bfile}
echo -e "\"" >> ${TO_ALL}
echo -e "\"" >> ${bfile}
if [ ! ${pref} == 0 ]; then
echo -en "nvram set ${var}=\"" >> ${TO_PREFERRED}
echo -n "${data}" | sed -e 's/[$`"\]/\\&/g' >> ${TO_PREFERRED}
echo -e "\"" >> ${TO_PREFERRED}
fi
done

#########################################################
# cleanup remove /opt/all_vars
# uncomment to remove file
#########################################################

# rm ${VARFILE}

#########################################################
# Write footer to restore script
#########################################################

echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit" >> ${TO_ALL}
echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit" >> ${TO_PREFERRED}
echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit" >> ${TO_EXCLUDE}
echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit" >> ${TO_INCLUDE}

#########################################################
# Change permissions on restore scripts to make them
# executable
#########################################################

chmod +x ${TO_INCLUDE}
chmod +x ${TO_PREFERRED}
chmod +x ${TO_EXCLUDE}
chmod +x ${TO_ALL}

#########################################################
# Compress restore scripts and send them to ftp server
#########################################################

if [ ${transfer} -ne 0 ] ; then
tar cpf - -C / "${TO_INCLUDE}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.essential.sh.tgz" -T -
tar cpf - -C / "${TO_PREFERRED}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.preferred.sh.tgz" -T -
tar cpf - -C / "${TO_EXCLUDE}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.dangerous.sh.tgz" -T -
tar cpf - -C / "${TO_ALL}" 2>/dev/null | gzip -c | /opt/bin/curl -s -u ${USERPASS} "${FTPS}/${FILE}.all.sh.tgz" -T -
fi

Files

The vars_to_skip file

DD_BOARD
^board
browser_method
^cfe
ct_modules
custom_shutdown_command
^def_
^default_
dist_type
dl_ram_addr
early_startup_command
^et0
^et1
^ezc
generate_key
gozila_action
gpio
^hardware
^is_
^kernel_
lan_default
^lan_hw
^lan_ifname
landevs
manual_boot_nv
misc_io_mode
need_commit
^os_
overclocking
pa0maxpwr
phyid_num
pmon_ver
pppd_pppifname
pppoe_ifname
pppoe_wan_ifname
primary_ifname
probe_blacklist
regulation_domain
rescue
reset_
scratch
sdram
^sh_
^skip
sshd_dss_host_key
sshd_rsa_host_key
startup_command
^wan_default
^wan_hw
^wan_if
^wan_vport
^wandevs
web_hook_libraries
^wifi_
wl0.1_hwaddr
wl0.2_hwaddr
wl0.3_hwaddr
wl0_hwaddr
wl0_ifname
wl0_radioids
^wl_
^wlan_

The vars_preferred file. This is the file you add your vars to that you want to backup.

daylight_time
time_zone