Posted: Sun Dec 21, 2008 11:35 Post subject: Backup settings and restore them to even different hardware.
When DD-WRT does a backup of the settings this is done in a binary way. Even if no variables have changed and the hardware has not changed, it is not totally safe to restore this binary backup on another version.
I'm saving these variables to a script. This was not my idea, but the other implementation on this forum was saving more than it should.
This script also creates some variables that don't really exist. I previously wrote a script that was able to avoid this, but it had a terrible speed-impact. The variables it creates (makes up) comes from data containing a "=". This really is not a problem.
I'm deliberately skipping some variables that contain hardware-specific data. This way it is safe to restore these settings on different hardware.
After an upgrade you can run this restore-script and be up and running in no time.
Code:
#!/bin/sh
#
# This shell script creates a shell file with lines of the form
# nvram set x="y"
# for every nvram variable found from
# nvram show
#
DATE=`date +%m%d%Y`
MAC=`nvram get lan_hwaddr | tr -d ":"`
FILE=${MAC}.${DATE}
CUR_DIR=`dirname $0`
FOLDER=/opt/var/backups
TO_ALL=${FOLDER}/${MAC}.${DATE}.all.sh
TO_INCLUDE=${FOLDER}/${MAC}.${DATE}.essential.sh
TO_EXCLUDE=${FOLDER}/${MAC}.${DATE}.dangerous.sh
FTPS=ftp://192.168.10.210/backups
USERPASS=user:pass
#
echo -e "#!/bin/sh\n#\necho \"Write variables\"\n" | tee -i ${TO_EXCLUDE} | tee -i ${TO_ALL} > ${TO_INCLUDE}
cat /tmp/all_vars | while read var
do
if echo ${var} | grep -q -f "${CUR_DIR}/vars_to_skip" ; then
bfile=$TO_EXCLUDE
else
bfile=$TO_INCLUDE
fi
# get the data out of the variable
data=`nvram get ${var}`
if [ "${data}" == "" ] ; then
echo -e "nvram set ${var}=" | tee -ia ${TO_ALL} >> ${bfile}
else
# 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 's/\\/\\\\/g' | sed 's/`/\\`/g' | sed 's/\$/\\\$/g' | sed 's/\"/\\"/g' | tee -ia ${TO_ALL} >> ${bfile}
echo -e "\"" | tee -ia ${TO_ALL} >> ${bfile}
fi
done
rm /tmp/all_vars
echo -e "\n# Commit variables\necho \"Save variables to nvram\"\nnvram commit" | tee -ia ${TO_ALL} | tee -ia ${TO_EXCLUDE} >> ${TO_INCLUDE}
chmod +x ${TO_ALL}
chmod +x ${TO_INCLUDE}
chmod +x ${TO_EXCLUDE}
_________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge
It will now parse that file and make 3 shell scripts. One will contain every variable. If a variable is inside the file "vars_to_skip" it will not go into the "essential" file.
Code:
cat /tmp/all_vars | while read var
do
...
done
After parsing it will use curl to upload it to an FTP-server _________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge
This one-liner was a while loop in which it also checked if the variable found wasn't by chance part of the data of the previously found variable containing an "=".
It was taking 2~3 minutes. _________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge
Thank you for sharing the script with us, but I think many people are daunted by it since there are no installation or usage instructions, and because the curl and FTP part is so specific to your setup.
For those of us who don't know bash scripting (me), it'd be really helpful to have an idiots guide to using it in the various dd-wrt storage scenarios.
In particular, (I've worked out most of it), but I'd have liked to know:
1) Where do you suggest we put the backup_nvram.sh script and vars file (I'm using /opt/var/backups). The script looks for the vars file in CURRENT_DIR, so if one puts them in the same folder, one has to navigate to that folder before running the script.
2) What else does the script assume. FI, that we have /opt mounted and have created a /opt/var/backups folder. (Or have an FTP server at 192.168.1.*** etc)
3) If we mount /opt to an SD/MMC/USB HDD we can/should delete the FTP part. (Perhaps a START FTP CUT HERE and END FTP CUT HERE comment?)
4) To backup - we navigate to the folder and run the backup_nvram.sh script (which creates three dated files in in /opt/var/backups).
5) To restore - we navigate to /opt/var/backups and run the latest essentials script.
6) The other files are for...? Completeness?
Instructions along these lines would have saved bash scripting noob me, in particular, a lot have time and made using the script a synch.
Hi frater, thanks for the script but as eXisor has said I think it would be great if you will post kind of newie guide, without FTP uploading part so everyone can run it and copy the right file from the /var/tmp folder
_________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge
but i thought saving/restoring backups from different firmwares (let alone different hardware) would possibly cause a bricked router?
is this safe?
I'm not restoring a backup-file.
I'm setting all the parameters one by one, but skip those that are hardware dependent.... _________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge
one by one.... in 20 seconds _________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge
It's a script which should be stored on your router. Maybe jffs or opt.
Giving your questions I don't think you have the basic Linux skills to pull it off.
It's a script which writes 3 scripts that can recover your settings. These 3 scripts are FTP'd to an FTP-server for which you have the credentials.
This needs the package "curl" to be installed on your router.
I don't think you know how to install curl (optware), but that's not really a problem. It will just not FTP those 3 scripts. These scripts will then be on your router and you can get them with winSCP. _________________ Asus RT16N + OTRW
Kingston 4GB USB-disk 128 MB swap + 1.4GB ext3 on /opt + 2 GB ext3 on /mnt
Copperjet 1616 modem in ZipB-config
Asterisk, pixelserv & Pound running on router
Another Asus RT16N as WDS-bridge