This is an old revision of the document!
The script in the code window at the bottom of page will create and download the backup without needing to have SFTP enabled on the router.
We could, of course, create the backup as a cron job on the router itself, and then use the mechanism applied in the script below to download the backup file. However, let's assume you want everything done in just one run. For this reason, creation of the backup in an individual file with timestamp and download is covered by the script.
In this way, just one run of the script on the backup server will create the backup and download it to to a safe location.
Action is based on using a here doc to execute commands on the router.
The backup is created using the “nvram save” command. This is how backups are done “under the hood” in the web interface.
You may cross-check that the backups are identical to the ones via web interface by
a) downloading the backup via GUI
b) creating backup by script,
c) copy both files to router
d) convert both files by “nvram convert <filename1/2> > result_file1/2.txt e) diff the two resulting text files. (I believe this cross check hints are obsolete….?!?!?)
The script then archives the resulting data in a tar file and sends it through the netcat command, which transfers it over the network.
A configurable number of backups is kept, older ones are deleted.
#!/bin/bash USER=root LOCAL_ID_FILE=/home/${LOGNAME}/.ssh/id_tomato_ecdsa PORT=5555 BACKUP_DIR=/home/${LOGNAME}/Router_Backups SCRIPT_FILE=nvram_save_cfg.sh PREFIX=FreshTomato EXT=.cfg TRANSFER_FILENAME=config.tar NO_OF_DIFF_FILES_TO_BE_KEPT=10 ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3` pushd ${BACKUP_DIR} (netcat -l -p ${PORT} > ${TRANSFER_FILENAME}) & # # Thinks like # VAR=`nvram get os_version` # seem not to work in bash via here doc, so write results into script file and source it # Further, the individual filename is generally not known, so tar it into temp file # ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<<ENDSSH rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_20[234][0-9][01][0-9][0123][0-9]_[012][0-9][0-5][0-9]${EXT} echo "nvram save ${PREFIX}" >> ${SCRIPT_FILE} nvram get os_version | sed -e "s/ .*$//" >> ${SCRIPT_FILE} echo "on" >> ${SCRIPT_FILE} nvram get t_model_name | tr " " "_" >> ${SCRIPT_FILE} nvram get router_name >> ${SCRIPT_FILE} date +%Y%m%d_%H%M >> ${SCRIPT_FILE} sed -e "N;N;N;N;N;s/\n/_/g;s/$/${EXT}/" -i ${SCRIPT_FILE} cat ${SCRIPT_FILE} source ${SCRIPT_FILE} tar -cvf ${TRANSFER_FILENAME} *_20[234][0-9][01][0-9][0123][0-9]_[012][0-9][0-5][0-9]${EXT} cat ${TRANSFER_FILENAME} | nc caisfiles ${PORT} sleep 3 # just wait a little bit before deleting the files not needed here any more rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_20[234][0-9][01][0-9][0123][0-9]_[012][0-9][0-5][0-9]${EXT} ENDSSH tar -xvf ${TRANSFER_FILENAME} rm ${TRANSFER_FILENAME} echo deleting: rm -fv `ls -t ${PREFIX}_*_20[234][0-9][01][0-9][0123][0-9]_[012][0-9][0-5][0-9]${EXT} | sed -e 1,${NO_OF_DIFF_FILES_TO_BE_KEPT}d` popd