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/05/03 13:21] – command line params thilobackup_script [2025/05/06 08:08] (current) – little typo thilo
Line 52: Line 52:
  \\  \\
  
-  - If an IP and an ID file are given, these will be used, else defaults apply (Router IP from `ip r`)+  - Command line arguments: backup directory, id-file and router /.eg. as IP). If provided, these will be used, else defaults apply (Router IP from `ip r`)
   - Date regex covers from 1970 - in case Tomato Router has no time.   - Date regex covers from 1970 - in case Tomato Router has no time.
   - Sometimes tar fails.  - cover this error by just cleaning, next cron run may do it   - Sometimes tar fails.  - cover this error by just cleaning, next cron run may do it
Line 62: Line 62:
    #!/bin/bash    #!/bin/bash
        
-   ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3` +   DIR2BACKUP=/home/${LOGNAME}/Router_Backups
-   USER=root+
    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    while [[ $# -gt 0 ]]; do
Line 78: Line 77:
         shift; shift         shift; shift
    done    done
- +    
-   echo +   BACKUPHOST=`ip r | grep "${ROUTER%1}0/24" | head -1 | cut -d " " -f 9`
-   echo "Backing up: "${ROUTER} +
-   echo "ID-file:    "${LOCAL_ID_FILE} +
-   echo +
-   BACKUPHOST=`ip r | grep ${ROUTER} | grep default | head -1 | cut -d " " -f 9`+
    echo    echo
    echo "Backup dir:  "${DIR2BACKUP}    echo "Backup dir:  "${DIR2BACKUP}
Line 92: Line 87:
    USER=root    USER=root
    PORT=5555    PORT=5555
-    
    SCRIPT_FILE=nvram_save_cfg.sh    SCRIPT_FILE=nvram_save_cfg.sh
    PREFIX=FreshTomato    PREFIX=FreshTomato
Line 104: Line 98:
    NO_OF_DIFF_FILES_TO_BE_KEPT=10    NO_OF_DIFF_FILES_TO_BE_KEPT=10
          
-   pushd ${BACKUP_DIR} > /dev/null+   pushd ${DIR2BACKUP} > /dev/null
        
    rm -f ${TRANSFER_FILENAME}     rm -f ${TRANSFER_FILENAME} 
-   +   #
    # Thinks like    # Thinks like
    # VAR=`nvram get os_version`     # VAR=`nvram get os_version` 
Line 131: Line 125:
         rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT}         rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT}
    ENDSSH    ENDSSH
-  +   
    THIS_BACKUP_FILE=`tar -xvf ${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
Line 141: Line 135:
         DATE=`date +%Y%m%d_%H%M`         DATE=`date +%Y%m%d_%H%M`
         if ! [[ `find -cmin 5 | grep ${THIS_BACKUP_FILE}` ]] ; then         if ! [[ `find -cmin 5 | grep ${THIS_BACKUP_FILE}` ]] ; then
-                # Date of the new files is more that 5 mind in the past  => seems Tomato Router has no date (or date 1970-01-01)+                # Date of the new files is more that 5 mins in the past  => seems Tomato Router has no date (or date 1970-01-01)
                 touch ${THIS_BACKUP_FILE}                 touch ${THIS_BACKUP_FILE}
         fi         fi
-        # Keep only one file (the latest) per day - delete earlier file of same day +        # Keep only one file (the latest) per day - delete earlier file of same day     
-         +        LIST_OLD_BACKUPS_OF_TODAY=`ls -t ${TODAYS_BACKUP_FILES_PREFIX}_${TIME_REGEX}.${EXT2> /dev/null sed -e 1,1d`
-        TODAYS_BACKUP_FILES_PREFIX=${THIS_BACKUP_FILE%_*} +
-        LIST_OLD_BACKUPS_OF_TODAY=`find -ctime 0 | grep ${ALL_BACKUP_FILES_PREFIX} | grep -v ${THIS_BACKUP_FILE}`+
         if [ "${LIST_OLD_BACKUPS_OF_TODAY}" ]; then         if [ "${LIST_OLD_BACKUPS_OF_TODAY}" ]; then
                 echo "deleting today's earlier backups (to keep just one per day - the most recent):"                 echo "deleting today's earlier backups (to keep just one per day - the most recent):"
Line 154: Line 146:
         fi         fi
         # In total, keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} files - delete older files (of any day)         # In total, keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} files - delete older files (of any day)
-        ALL_BACKUP_FILES_PREFIX=${TODAYS_BACKUP_FILES_PREFIX%_*} 
         OLDER_FILES=`ls -t ${ALL_BACKUP_FILES_PREFIX}_${DATE_REGEX}_${TIME_REGEX}.${EXT} | sed -e 1,${NO_OF_DIFF_FILES_TO_BE_KEPT}d`         OLDER_FILES=`ls -t ${ALL_BACKUP_FILES_PREFIX}_${DATE_REGEX}_${TIME_REGEX}.${EXT} | sed -e 1,${NO_OF_DIFF_FILES_TO_BE_KEPT}d`
         if [ "${OLDER_FILES}" ]; then         if [ "${OLDER_FILES}" ]; then
Line 167: Line 158:
    fi    fi
    popd > /dev/null    popd > /dev/null
 +  
  \\  \\
  
backup_script.1746274862.txt.gz · Last modified: 2025/05/03 13:21 by thilo