Site Tools


backup_script

This is an old revision of the document!


Do not know whether this is the right place to publish this, just wanted to share…..

I want to schedule backup by cron job, initiated by and stored at a save place on any server in the network, not the just on the router.

Further, I do not want to install SFTP just for the backups.

Did not find a solution in the wiki/support….

So I did a little script that downloads the backup for me w/o SFTP on the router enabled.

Option to create the backup were a cron job on the router itself, but I wanted to have it all in just one run.

Thus, creation of the backup in individual file with timestamp is included in the script.

So it is just one run of the script on the backup server to create the backup and download to save place.

Action is based on using a here doc to execute commands on the router.

The backup by “mvram save”. This is how the backups are created inthe GUI, you may cross check by “nvram convert”.

Then it tars the file and sends through netcat.

Prerequisites: netcat has to be installed on backup server, ssh key from backup server must installed on router.

Script starts here:

 #!/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
 
 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 general 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}
         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}
 popd
backup_script.1745504630.txt.gz · Last modified: 2025/04/24 15:23 by thilo