Site Tools


backup_script

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
backup_script [2025/04/24 18:57] – -Condense, grammar, bullet prerequisites hogwildbackup_script [2025/04/28 18:23] (current) – [Goals] hogwild
Line 1: Line 1:
 ====== Backup Script ====== ====== Backup Script ======
  
-Let's say you wanted to schedule a backup done via a cron job. Let's assume you want that script initiated from, and stored in a safe location on a server on the network, (not on the router).+==== Goals ====
  
-Further, you do not wish to install SFTP just for these backups.+ \\
  
-The following script will download the script without needing to have SFTP enabled on the router.+  - You want to schedule a backup 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.
  
  \\  \\
  
-One option would be to create the backup as a cron job on the router itself. However, let's assume you want to have it all done in just one run. For this reason, creation of the backup in an individual file with timestamp was included in the script.+The script at the bottom of this page will create and download the backup without needing to have SFTP enabled on the router.
  
-In this way, just one run of the script on the backup server would create the backup and download it to to a safe location.+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 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.
  
 Action is based on using a here doc to execute commands on the router. Action is based on using a here doc to execute commands on the router.
  
-The backup is created by using the "//nvram save//" command. This is how backups are done in the web interface. You may cross-check (what? at the command line?) by using "//nvram convert//" command.+ \\ \\ The backup is created using the "//nvram save//" command. This is how backups are done "under the hood" in the web interface.
  
-The script then archives the resulting data in a tar file and sends it through the netcat command.+You may cross-check that the backups are identical to the ones via the GUI using the following steps:
  
  \\  \\
  
-Prerequisites:+  - 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// <filename1/2>  > result_file1/2.txt" 
 +  - Perform a diff between the two resulting text files.
  
  \\  \\
  
-  - The netcat command must be available on the backup server +The script then archives the resulting data in a tar file and sends it through the netcat command, transferring it over the network. 
-  - The SSH key of of the executing user from backup server must be available to the router's root user.+ 
 +A configurable number of backups is kept. Older ones are deleted. 
 + 
 + \\ 
 + 
 + 
 +==== 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.
  
  \\  \\
Line 40: Line 59:
    EXT=.cfg    EXT=.cfg
    TRANSFER_FILENAME=config.tar    TRANSFER_FILENAME=config.tar
 +   
 +   NO_OF_DIFF_FILES_TO_BE_KEPT=10
        
    ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3`    ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3`
Line 49: Line 70:
    # VAR=`nvram get os_version`    # VAR=`nvram get os_version`
    # seem not to work in bash via here doc, so write results into script file and source it    # 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+   # Furtherthe individual filename is generally not known, so tar it into temp file
    #    #
    ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<<ENDSSH    ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<<ENDSSH
Line 64: Line 85:
            tar -cvf ${TRANSFER_FILENAME} *_20[234][0-9][01][0-9][0123][0-9]_[012][0-9][0-5][0-9]${EXT}            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}            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}            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    ENDSSH
Line 69: Line 91:
    tar -xvf ${TRANSFER_FILENAME}    tar -xvf ${TRANSFER_FILENAME}
    rm ${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    popd
  
backup_script.1745517476.txt.gz · Last modified: 2025/04/24 18:57 by hogwild