====== Backup Script ====== ==== Goals ==== \\ - You want to schedule a backup to be done via a cron job. - You want the script initiated from, and stored in a safe location \\ on a server on the network, (not on the router). - You do not wish to install SFTP just for these backups. \\ The script at the bottom of this page will create and download the backup without needing to have SFTP enabled on the router. You 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 of the script. To achieve this, the script covers creation of the backup in an individual file with timestamp and download. 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. \\ \\ 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 the GUI using the following steps (last tested at 2025-04-22): \\ - Download the backup via the graphical interface - Create a backup via a script - Copy both files to router - Convert both files via the command: \\ ''"nvram convert > result_file1/2.txt"'' - Perform a diff between the two resulting text files. \\ The script then archives the resulting data in a tar file and sends it through the netcat command, transferring it over the network. Restore a backup file uses the same procedure as restoring an archive created in the web interface. This was last tested on 2025-04-29. \\ ==== Prerequisites ==== \\ - The netcat command must be available on the backup server. - The router's root user must have access to the public SSH key \\ of the user executing the script on backup server. \\ ==== Remarks ==== \\ - Command line arguments: backup directory, id-file and router (as IP address). \\ If provided, these will be used. Otherwise, the defaults will apply \\ (such Router IP from "ip r"). - Sometimes tar may fail. You can solve this by just cleaning. The next \\ cron run may do that. - All earlier backups from the same day are abandoned. - A total number of backups is kept. This number is configurable. \\ Older backups are deleted. \\ #!/bin/bash DATE_REGEX=20[0-9][0-9][01][0-9][0123][0-9] TIME_REGEX=[012][0-9][0-5][0-9] NO_OF_DIFF_FILES_TO_BE_KEPT=10 DIR2BACKUP=/home/${LOGNAME}/Router_Backups LOCAL_ID_FILE=/home/${LOGNAME}/.ssh/id_tomato_ecdsa ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3` while [[ $# -gt 0 ]]; do case $1 in -d|--dir2backup) DIR2BACKUP=$2 ;; -i|--idfile) LOCAL_ID_FILE=$2 ;; -r|--router) ROUTER=$2 ;; -*|--*) echo "Unknown option $1" ;; -h|--help) echo "usage $0