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/30 15:11] – [Remarks] -Clarity hogwildbackup_script [2025/12/01 12:45] (current) – [Remarks] thilo
Line 5: Line 5:
  \\  \\
  
-  - You want to schedule a backup done via a cron job.  +  - 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 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.   - You do not wish to install SFTP just for these backups.
  
Line 13: Line 13:
 The script at the bottom of this page will create and download the backup without needing to have SFTP enabled on the router. The script at the bottom of this 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 of the script. To achieve this, the script covers creation of the backup in an individual file with timestamp and download.+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. 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.  \\ \\ 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 22.04.2025):+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):
  
  \\  \\
Line 28: Line 26:
   - Create a backup via a script   - Create a backup via a script
   - Copy both files to router   - Copy both files to router
-  - Convert both files via the command "//nvram convert// <filename1/2>  > result_file1/2.txt"+  - Convert both files via the command: \\  ''"nvram convert <filename1/2>  > result_file1/2.txt"''
   - Perform a diff between the two resulting text files.   - 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. +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.
-Restore a backup file is the same procedure as resorting an archive created in the WEB-GUI (last tested 29.04.2025).+
  
  \\  \\
Line 44: Line 41:
  
   - The netcat command must be available on the backup server.   - 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.+  - The user executing the script on backup server must have access to the router's root user via SSH key.
  
  \\  \\
Line 52: Line 49:
  \\  \\
  
-  - Date covers from 1970 in case Tomato Router has no time+  - Command line arguments: backup directory, id-file and router (as IP address). \\ If provided, these will be used. Otherwise, the defaults will apply \\ (search Router IP from "ip r")
-  - Sometimes tar fails - cover this error by just cleaningnext cron run may do it +  - Sometimes tar may failYou can solve this by just cleaning. The next \\ cron run may do that. 
-  - All eaRlier backups from the same day are abandoned. +  - All earlier backups from the same day are abandoned. 
-  - A total number of backups is kept. This number is configurable. \\ Older ones are deleted.+  - A total number of backups is kept. This number is configurable. \\ Older backups are deleted.
  
  \\  \\
Line 61: Line 58:
    #!/bin/bash    #!/bin/bash
        
-   USER=root+   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    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 <option argument> 
 +                        echo "options are:"
 +                        echo "  -d \| --dir2backup      directory to save backup,               default: ${DIR2BACKUP} "
 +                        echo "  -r \| --router          router ostname or IP to be saved,       default: ${ROUTER}"
 +                        echo "  -i \| --idfile          directory to save backuip,              default: ${ROUTER}
 +                        exit ;;
 +        esac
 +        shift; shift
 +   done
 +   
 +   BACKUPHOST=`ip r | grep "${ROUTER%1}0/24" | head -1 | cut -d " " -f 9`
 +   echo
 +   echo "Backup dir:  "${DIR2BACKUP}
 +   echo "id file:     "${LOCAL_ID_FILE}
 +   echo "Backing up:  "${ROUTER}
 +   echo "Backup host: "${BACKUPHOST}
 +   echo
 +   USER=root
    PORT=5555    PORT=5555
-   BACKUP_DIR=/home/${LOGNAME}/Router_Backups 
    SCRIPT_FILE=nvram_save_cfg.sh    SCRIPT_FILE=nvram_save_cfg.sh
    PREFIX=FreshTomato    PREFIX=FreshTomato
-   EXT=.cfg+   EXT=cfg
    TRANSFER_FILENAME=config.tar    TRANSFER_FILENAME=config.tar
        
-   # DATE_REGEX covers back to 1970 and furtherin case Tomato Router has no date or date 1970-01-101 +   # It may happenthat Tomato router has no or other, wrong time 
-   # Total coverage: 1900-01-01 until 2999-12-31 :-+   # take date from localhost (i.e. backup serverinto backup filename 
-   DATE_REGEX=[12][09][0-9][0-9][01][0-9][0123][0-9]_[012][0-9][0-5][0-9]+   DATE=`date +%Y%m%d_%H%M`
        
-   NO_OF_DIFF_FILES_TO_BE_KEPT=10 +   pushd ${DIR2BACKUP} > /dev/null
-       +
-   ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3` +
-   BACKUPHOST=`ip r | grep default | head -1 | cut -d " " -f 9`+
        
-   pushd ${BACKUP_DIR} +   rm -${TRANSFER_FILENAME} 
-   rm ${TRANSFER_FILENAME} +
-    +
-   (netcat -l -p ${PORT} > ${TRANSFER_FILENAME}) &+
    #    #
-   # Thinks like +   # Things like 
-   # 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. 
-   # Furtherthe individual filename is generally not known, so tar it into temp file+   # Further the individual filename is generally not known, so tar it into temp file.
    #    #
-   ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<<ENDSSH +   # Kill netcat zombies 
-           rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}${EXT} +   kill -9 `ps -ef | grep -v grep | grep netcat | sed -e "s/ [ ]*/ /g" | cut -d " " -f 2` 2> /dev/null 
-           echo "nvram save ${PREFIX}" >> ${SCRIPT_FILE} +   (netcat -l -p ${PORT} > ${TRANSFER_FILENAME}) & 
-           nvram get os_version | sed -e "s/ .*$//" >> ${SCRIPT_FILE} +   # Create the backup file on the router by executing the following commands (indented lines) there 
-           echo "on" >> ${SCRIPT_FILE} +   ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE} <<ENDSSH 
-           nvram get t_model_name | tr " " "_" >> ${SCRIPT_FILE} +        rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} 
-           nvram get router_name >> ${SCRIPT_FILE} +        echo "nvram save ${PREFIX}" >> ${SCRIPT_FILE} 
-           date +%Y%m%d_%H%M >> ${SCRIPT_FILE} +        nvram get os_version | sed -e "s/ .*$//" >> ${SCRIPT_FILE} 
-           sed -e "N;N;N;N;N;s/\n/_/g;s/$/${EXT}/" -i ${SCRIPT_FILE} +        echo "on" >> ${SCRIPT_FILE} 
-           cat ${SCRIPT_FILE} +        nvram get t_model_name | tr " " "_" >> ${SCRIPT_FILE} 
-           source ${SCRIPT_FILE} +        nvram get router_name >> ${SCRIPT_FILE} 
-           tar -cvf ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}${EXT} +        echo ${DATE} >> ${SCRIPT_FILE} 
-           cat ${TRANSFER_FILENAME} | nc ${BACKUPHOST} ${PORT} +        sed -e "N;N;N;N;N;s/\n/_/g;s/$/.${EXT}/" -i ${SCRIPT_FILE} 
-           sleep 5 # just wait a little bit before de +        source ${SCRIPT_FILE} 
-           leting the files not needed here any more +        tar -cvf ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} > /dev/null 
-           rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}${EXT}+        cat ${TRANSFER_FILENAME} | nc ${BACKUPHOST} ${PORT} 
 +        sleep 5 # just wait a little while before deleting the files not needed here any more 
 +        rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT}
    ENDSSH    ENDSSH
        
-   THIS_BACKUP_FILE=`tar -tvf ${TRANSFER_FILENAME} | sed -e "s/^.*${PREFIX}/${PREFIX}/"`+   THIS_BACKUP_FILE=`tar -xvf ${TRANSFER_FILENAME} | sed -e "s/^.*${PREFIX}/${PREFIX}/"`
    if [ "${THIS_BACKUP_FILE}" ]; then    if [ "${THIS_BACKUP_FILE}" ]; then
-        TODAYS_BACKUP_FILE=${THIS_BACKUP_FILE%_*} +        echo "Saved on this computer in `pwd`:" 
-        OLD_BACKUPS_OF_TODAY=`ls ${TTODAYS_BACKUP_FILEE}_[012][0-9][0-5][0-9]${EXT} 2> /dev/null` +        echo ${THIS_BACKUP_FILE} 
-        if [ "${OLD_BACKUPS_OF_TODAY}" ]; then +        echo 
-                echo "deleting today'earlier bakups (to keep just one per day):" +        TODAYS_BACKUP_FILES_PREFIX=${THIS_BACKUP_FILE%_*} 
-                rm -${OLD_BACKUPS_OF_TODAY}+        ALL_BACKUP_FILES_PREFIX=${TODAYS_BACKUP_FILES_PREFIX%_*} 
 +        # Keep only one file (the latest) per day - delete earlier file of same day 
 +        LIST_OLD_BACKUPS_OF_TODAY=`ls ${TODAYS_BACKUP_FILES_PREFIX}_${TIME_REGEX}.${EXT} 2> /dev/null | grep -v ${THIS_BACKUP_FILE}
 +        if [ "${LIST_OLD_BACKUPS_OF_TODAY}" ]; then 
 +                echo "deleting earlier backups from today (to keep just one per day - the most recent):" 
 +                rm -fv ${LIST_OLD_BACKUPS_OF_TODAY} 
 +                echo
         fi         fi
-        tar -xvf ${TRANSFER_FILENAME+        # In total, keep only ${NO_OF_DIFF_FILES_TO_BE_KEPTfiles - delete older files (of any day) 
-        DELETE_OLDER=`ls -t ${TODAYS_BACKUP_FILE%_*}_${DATE_REGEX}${EXT} | sed -e 1,${NO_OF_DIFF_FILES_TO_BE_KEPT}d` +        OLDER_FILES=`ls ${ALL_BACKUP_FILES_PREFIX}_${DATE_REGEX}_${TIME_REGEX}.${EXT} | sort -r | sed -e 1,${NO_OF_DIFF_FILES_TO_BE_KEPT}d` 
-        if [ "${DELETE_OLDER}" ]; then +        if [ "${OLDER_FILES}" ]; then 
-                echo "keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} in total delete:" +                echo "keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} in totaldelete:" 
-                rm -fv ${DELETE_OLDER}+                rm -fv ${OLDER_FILES} 
 +                echo
         fi         fi
 +        # delete transfer file only when tar was successful, i.e. only here
 +        rm ${TRANSFER_FILENAME}
    else    else
         echo "unknown error while untaring"         echo "unknown error while untaring"
    fi    fi
-   rm ${TRANSFER_FILENAME} +   popd > /dev/null
-   popd+
  
  \\  \\
backup_script.1746022307.txt.gz · Last modified: by hogwild