This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
backup_script [2025/04/24 23:38] – thilo | backup_script [2025/05/06 08:08] (current) – little typo thilo | ||
---|---|---|---|
Line 11: | Line 11: | ||
\\ | \\ | ||
- | The script | + | The script |
- | \\ One option would be to create the backup as a cron job on the router itself. Then, in a second step, use the mechaism | + | We could, of course, |
- | In this way, just one run of the script on the backup server | + | In this way, just one run of the script on the backup server |
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 using the "// | + | \\ \\ The backup is created using the "// |
- | You may cross-check that the backups are identical to the ones via web interface by a) download backup via GUI b) create backup by script, c) copy both files to router d) convert both files by "nvram convert < | + | 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): |
- | The script then archives | + | \\ |
+ | |||
+ | - Download | ||
+ | - Create | ||
+ | - Copy both files to router | ||
+ | - Convert both files via the command | ||
+ | - Perform a diff between | ||
\\ | \\ | ||
- | Prerequisites: | + | 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 is the same procedure as resorting an archive created in the WEB-GUI (last tested 29.04.2025). | ||
+ | |||
+ | \\ | ||
+ | |||
+ | |||
+ | ==== Prerequisites | ||
\\ | \\ | ||
Line 33: | Line 45: | ||
- The netcat command must be available on the backup server. | - The netcat command must be available on the backup server. | ||
- The router' | - The router' | ||
+ | |||
+ | \\ | ||
+ | |||
+ | ==== Remarks ==== | ||
+ | |||
+ | \\ | ||
+ | |||
+ | - 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. | ||
+ | - Sometimes tar fails. | ||
+ | - All earlier backups from the same day are abandoned. | ||
+ | - A total number of backups is kept. This number is configurable. Older ones are deleted. | ||
\\ | \\ | ||
Line 38: | Line 62: | ||
# | # | ||
- | USER=root | + | DIR2BACKUP=/ |
| | ||
+ | | ||
+ | |||
+ | while [[ $# -gt 0 ]]; do | ||
+ | case $1 in | ||
+ | -d|--dir2backup) | ||
+ | DIR2BACKUP=$2 ;; | ||
+ | -i|--idfile) | ||
+ | LOCAL_ID_FILE=$2 ;; | ||
+ | -r|--router) | ||
+ | ROUTER=$2 ;; | ||
+ | esac | ||
+ | shift; shift | ||
+ | done | ||
+ | |||
+ | | ||
+ | echo | ||
+ | echo " | ||
+ | echo "id file: " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo | ||
+ | | ||
| | ||
- | | ||
| | ||
| | ||
- | EXT=.cfg | + | |
| | ||
- | ROUTER=`ip r | grep default | head -1 | cut -d " " | + | # DATE_REGEX covers back to 1970 and further, in case Tomato Router has no date or date 1970-01-01 |
- | pushd ${BACKUP_DIR} | + | # Total coverage: 1900-01-01 until 2999-12-31 :-) |
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | pushd ${DIR2BACKUP} > /dev/null | ||
- | (netcat | + | rm -f ${TRANSFER_FILENAME} |
# | # | ||
# Thinks like | # Thinks 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 | ||
# Further the individual filename is general not known, so tar it into temp file | # Further the individual filename is general not known, so tar it into temp file | ||
# | # | ||
- | ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<< | + | # Kill netcat zombies |
- | | + | kill -9 `ps -ef | grep -v grep | grep netcat | sed -e "s/ [ ]*/ /g" | cut -d " " -f 2` 2> /dev/null |
- | | + | |
- | | + | ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE} << |
- | | + | rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} |
- | | + | echo "nvram save ${PREFIX}" |
- | | + | nvram get os_version | sed -e "s/ .*$//" >> ${SCRIPT_FILE} |
- | | + | echo " |
- | | + | nvram get t_model_name | tr " " " |
- | cat ${SCRIPT_FILE} | + | nvram get router_name >> ${SCRIPT_FILE} |
- | source ${SCRIPT_FILE} | + | date +%Y%m%d_%H%M >> ${SCRIPT_FILE} |
- | | + | sed -e " |
- | | + | source ${SCRIPT_FILE} |
- | | + | tar -cvf ${TRANSFER_FILENAME} |
- | | + | cat ${TRANSFER_FILENAME} | nc ${BACKUPHOST} |
+ | sleep 5 # just wait a little bit before | ||
+ | rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} | ||
| | ||
- | tar -xvf ${TRANSFER_FILENAME} | + | THIS_BACKUP_FILE=`tar -xvf ${TRANSFER_FILENAME} |
- | rm ${TRANSFER_FILENAME} | + | if [ " |
- | | + | echo "Saved on this computer in `pwd`:" |
+ | echo ${THIS_BACKUP_FILE} | ||
+ | echo | ||
+ | TODAYS_BACKUP_FILES_PREFIX=${THIS_BACKUP_FILE%_*} | ||
+ | ALL_BACKUP_FILES_PREFIX=${TODAYS_BACKUP_FILES_PREFIX%_*} | ||
+ | DATE=`date +%Y%m%d_%H%M` | ||
+ | if ! [[ `find -cmin 5 | grep ${THIS_BACKUP_FILE}` ]] ; then | ||
+ | # 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} | ||
+ | fi | ||
+ | # 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}.${EXT} 2> /dev/null | sed -e 1,1d` | ||
+ | if [ " | ||
+ | echo " | ||
+ | rm -fv ${LIST_OLD_BACKUPS_OF_TODAY} | ||
+ | echo | ||
+ | fi | ||
+ | # In total, keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} files - delete older files (of any day) | ||
+ | OLDER_FILES=`ls -t ${ALL_BACKUP_FILES_PREFIX}_${DATE_REGEX}_${TIME_REGEX}.${EXT} | sed -e 1, | ||
+ | if [ " | ||
+ | echo "keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} in total, delete:" | ||
+ | rm -fv ${OLDER_FILES} | ||
+ | echo | ||
+ | fi | ||
+ | # delete transfer file only when tar was successfull, | ||
+ | | ||
+ | | ||
+ | echo " | ||
+ | fi | ||
+ | | ||
+ | |||
\\ | \\ | ||