This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
backup_script [2025/04/24 18:35] – -Clarity, grammar, italicize nvram commands hogwild | backup_script [2025/04/28 18:23] (current) – [Goals] hogwild | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Backup Script ====== | ====== Backup Script ====== | ||
- | Let's say you'd like to schedule a backup done via a cron job. Assume you want that script initiated from, and stored on 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 | + | - You want to schedule a backup done via a cron job. |
+ | - You want the script | ||
+ | - 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 | + | The script at the bottom of this page will create and download the backup without needing |
+ | |||
+ | We could, of course, | ||
- | Thus, 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. | Action is based on using a here doc to execute commands on the router. | ||
- | The backup is created | + | \\ \\ The backup is created using the "// |
- | The script then archives | + | You may cross-check that the backups are identical to the ones via the GUI using the following steps: |
\\ | \\ | ||
- | Prerequisites: | + | - Download the backup |
+ | - Create a backup | ||
+ | - Copy both files to router | ||
+ | - Convert both files via the command "// | ||
+ | - Perform a diff between the two resulting text files. | ||
- | Script starts here: | + | \\ |
+ | |||
+ | The script then archives the resulting data in a tar file and sends it through the netcat command, transferring it over the network. | ||
+ | |||
+ | A configurable number of backups is kept. Older ones are deleted. | ||
+ | |||
+ | \\ | ||
+ | |||
+ | |||
+ | ==== Prerequisites ==== | ||
+ | |||
+ | \\ | ||
+ | |||
+ | - The netcat command must be available on the backup server. | ||
+ | - The router' | ||
+ | |||
+ | \\ | ||
# | # | ||
Line 35: | Line 59: | ||
| | ||
| | ||
+ | |||
+ | | ||
| | ||
Line 44: | 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 | + | # Further, the individual filename is generally |
# | # | ||
ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<< | ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE}<< | ||
Line 59: | 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} | ||
| | ||
Line 64: | 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, | ||
popd | popd | ||
+ | |||
+ | \\ | ||
+ | |||
+ | \\ | ||