This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
backup_script [2025/04/25 15:50] – typos and minor iomprovements thilo | backup_script [2025/06/20 19:06] (current) – [Goals] thilo | ||
---|---|---|---|
Line 5: | Line 5: | ||
\\ | \\ | ||
- | - You want to schedule a backup done via a cron job. | + | - You want to schedule a backup |
- | - You want the script initiated from, and stored in a safe location | + | - You want the script initiated from, and stored in a safe location |
- You do not wish to install SFTP just for these backups. | - You do not wish to install SFTP just for these backups. | ||
\\ | \\ | ||
- | 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 | + | You 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 | + | \\ \\ The backup |
- | The backup is created using the "// | + | 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): |
- | 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 < | + | \\ |
- | The script then archives | + | - Download |
+ | - Create | ||
+ | - Copy both files to router | ||
+ | - Convert both files via the command: \\ ''" | ||
+ | - Perform a diff between | ||
+ | |||
+ | \\ | ||
- | A configurable number of backups is kept, older ones are deleted. | + | 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: | + | |
+ | ==== Prerequisites | ||
\\ | \\ | ||
- 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 (as IP address). \\ If provided, these will be used. Otherwise, the defaults will apply \\ (search 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. | ||
\\ | \\ | ||
Line 40: | Line 58: | ||
# | # | ||
- | USER=root | + | DATE_REGEX=20[0-9][0-9][01][0-9][0123][0-9] |
+ | | ||
+ | | ||
+ | |||
+ | | ||
| | ||
+ | | ||
+ | |||
+ | while [[ $# -gt 0 ]]; do | ||
+ | case $1 in | ||
+ | -d|--dir2backup) | ||
+ | DIR2BACKUP=$2 ;; | ||
+ | -i|--idfile) | ||
+ | LOCAL_ID_FILE=$2 ;; | ||
+ | -r|--router) | ||
+ | ROUTER=$2 ;; | ||
+ | -*|--*) | ||
+ | echo " | ||
+ | -h|--help) | ||
+ | echo "usage $0 <option argument> | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | exit ;; | ||
+ | esac | ||
+ | shift; shift | ||
+ | done | ||
+ | |||
+ | | ||
+ | echo | ||
+ | echo " | ||
+ | echo "id file: " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo | ||
+ | | ||
| | ||
- | | ||
| | ||
| | ||
- | EXT=.cfg | + | |
| | ||
- | NO_OF_DIFF_FILES_TO_BE_KEPT=10 | + | # It may be, that Tomato router has no - or other, wrong - timeother or no timee |
+ | # take date from localhost (i.e. backupserver) into backup filename | ||
+ | DATE=`date +%Y%m%d_%H%M` | ||
- | ROUTER=`ip r | grep default | head -1 | cut -d " " -f 3` | + | pushd ${DIR2BACKUP} > /dev/null |
- | pushd ${BACKUP_DIR} | + | |
- | (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 generally | + | # Further the individual filename is general |
# | # | ||
- | 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} |
- | date +%Y%m%d_%H%M | + | echo " |
- | | + | nvram get t_model_name | tr " " " |
- | cat ${SCRIPT_FILE} | + | nvram get router_name >> ${SCRIPT_FILE} |
- | source ${SCRIPT_FILE} | + | echo ${DATE} |
- | | + | sed -e " |
- | | + | source ${SCRIPT_FILE} |
- | | + | tar -cvf ${TRANSFER_FILENAME} |
- | | + | cat ${TRANSFER_FILENAME} | nc ${BACKUPHOST} |
+ | sleep 5 # just wait a little bit before deleting the files not needed here any more | ||
+ | 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 [ "${THIS_BACKUP_FILE}" ]; then |
- | | + | echo "Saved on this computer in `pwd`:" |
- | 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, | + | echo ${THIS_BACKUP_FILE} |
- | popd | + | echo |
+ | TODAYS_BACKUP_FILES_PREFIX=${THIS_BACKUP_FILE%_*} | ||
+ | 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 [ " | ||
+ | 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 | ||
+ | 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 successful, i.e. only here | ||
+ | rm ${TRANSFER_FILENAME} | ||
+ | | ||
+ | echo " | ||
+ | fi | ||
+ | | ||
\\ | \\ |