This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
backup_script [2025/06/10 17:38] – [Goals] -Remove "Action is based on using a here doc" hogwild | backup_script [2025/09/10 15:45] (current) – Key issue fixec 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. | ||
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. | ||
Line 41: | Line 41: | ||
- The netcat command must be available on the backup server. | - The netcat command must be available on the backup server. | ||
- | - The router' | + | - The user executing the script on backup server |
\\ | \\ | ||
Line 49: | Line 49: | ||
\\ | \\ | ||
- | - Command line arguments: backup directory, id-file and router (as IP address). \\ If provided, these will be used. Otherwise, the defaults will apply \\ (such Router IP from "ip r"). | + | - Command line arguments: backup directory, id-file and router (as IP address). \\ If provided, these will be used. Otherwise, the defaults will apply \\ (search |
- Sometimes tar may fail. You can solve this by just cleaning. The next \\ cron run may do that. | - 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. | - All earlier backups from the same day are abandoned. | ||
Line 101: | Line 101: | ||
| | ||
- | # It may be, that Tomato router has no - or other, wrong - timeother or no timee | + | # It may happen, that Tomato router has no - or other, wrong - time |
- | # take date from localhost (i.e. backupserver) into backup filename | + | # take date from localhost (i.e. backup server) into backup filename |
| | ||
+ | |||
pushd ${DIR2BACKUP} > /dev/null | pushd ${DIR2BACKUP} > /dev/null | ||
Line 111: | Line 111: | ||
# 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 | + | # Further the individual filename is generally |
# | # | ||
# Kill netcat zombies | # Kill netcat zombies | ||
kill -9 `ps -ef | grep -v grep | grep netcat | sed -e "s/ [ ]*/ /g" | cut -d " " -f 2` 2> /dev/null | kill -9 `ps -ef | grep -v grep | grep netcat | sed -e "s/ [ ]*/ /g" | cut -d " " -f 2` 2> /dev/null | ||
| | ||
+ | # Create the backup file on the router by executing the following commands (indented lines) there | ||
ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE} << | ssh ${USER}@${ROUTER} -i ${LOCAL_ID_FILE} << | ||
rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} | rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} | ||
Line 129: | Line 130: | ||
tar -cvf ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} > /dev/null | tar -cvf ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} > /dev/null | ||
cat ${TRANSFER_FILENAME} | nc ${BACKUPHOST} ${PORT} | cat ${TRANSFER_FILENAME} | nc ${BACKUPHOST} ${PORT} | ||
- | sleep 5 # just wait a little | + | sleep 5 # just wait a little |
rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} | rm -f ${SCRIPT_FILE} ${TRANSFER_FILENAME} ${PREFIX}_*_${DATE_REGEX}_${TIME_REGEX}.${EXT} | ||
| | ||
Line 148: | Line 149: | ||
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) | ||
- | OLDER_FILES=`ls | + | OLDER_FILES=`ls ${ALL_BACKUP_FILES_PREFIX}_${DATE_REGEX}_${TIME_REGEX}.${EXT} |
if [ " | if [ " | ||
echo "keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} in total, delete:" | echo "keep only ${NO_OF_DIFF_FILES_TO_BE_KEPT} in total, delete:" |