Posted: Wed Oct 03, 2012 14:18 Post subject: Useful Scripts for nvram and traffdata
This script makes a backup of traffdata once a year to /opt/ and deletes traffdata from nvram (can be used to backup to jffs)
run it as /opt/nvramcheck.sh &
the & is there to make the script run as a background process
The "$YEAR" -gt "2011" part is there to prevent the router to backup nvram on reboot. Some routers default to 1/1/1970 date before getting the current date thru NTP . And if you set it to backup on 1/1 it will backup after reboot, not the 1/1 of the next year
delete or comment (#) "for i in `nvram show | grep traff- | cut -f1 -d=""`; do nvram unset $i; done" if you don't want the traffdata to be deleted from nvram
STOP=0
if [[ "$MONTH" = "$TARGETMONTH" && "$DAY" = "$TARGETDAY" && "$YEAR" -gt "2011" ]]; then
for i in ` ls /opt/ |cut -f1 | grep traffdata`
do
if [ "$i" = "$FILE" ]; then
STOP=1
echo "Do nothing"
fi
done
else
echo "Not the target date"
STOP=1
fi
if [ "$STOP" = "0" ]; then
COUNT=1
echo TRAFF-DATA >> $URL
for i in `nvram show | grep traff- | cut -f1 -d=""`;
do
NEW=` nvram show | grep traff- | awk 'NR == a {print}' a=$COUNT`
COUNT=`expr $COUNT + 1`
echo $NEW >> $URL
done
for i in `nvram show | grep traff- | cut -f1 -d=""`; do nvram unset $i; done
fi
done;
This script checks if there is at least 1024 bytes of free nvram and if it isn't it will backup traffdata, delete traffdata from nvram and reboot
run it as /opt/backuptraff.sh &
change NVRAMSIZE to your nvramsize and INTERVAL to your liking (be careful not to create a boot loop!)
it is also useful to set upnp to clear values on reboot to be sure more nvram will be freed at reboot
(you can do this from the web gui)
COUNT=1
echo TRAFF-DATA >> $URL
for i in `nvram show | grep traff- | cut -f1 -d=""`;
do
NEW=` nvram show | grep traff- | awk 'NR == a {print}' a=$COUNT`
COUNT=`expr $COUNT + 1`
echo $NEW >> $URL
done
for i in `nvram show | grep traff- | cut -f1 -d=""`; do nvram unset $i; done
reboot
fi
done
In both cases the resulting traffdata.bak file is compatible with the restore function in Data Administration on the STATUS > WAN page and has a date stamp in the file name (ex: traffdata.3-Oct-2012.bak)
If you have suggestions or you found bugs in the scripts please let me know
Please modify the script to your on configuration (maybe you use jffs instead of opt, or have smaller nvram) before submiting bugs