This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
backup_script [2025/05/06 08:08] – little typo 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. | ||
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. | ||
- | |||
- | 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 the GUI using the following steps (last tested at 22.04.2025): | + | 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): |
\\ | \\ | ||
Line 28: | Line 26: | ||
- Create a backup via a script | - Create a backup via a script | ||
- Copy both files to router | - Copy both files to router | ||
- | - Convert both files via the command "//nvram convert// < | + | - Convert both files via the command: \\ '' |
- Perform a diff between the two resulting text files. | - Perform a diff between the two resulting text files. | ||
\\ | \\ | ||
- | The script then archives the resulting data in a tar file and sends it through the netcat command, transferring it over the network. | + | 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 |
- | Restore a backup file is the same procedure as resorting | + | |
\\ | \\ | ||
Line 44: | 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 router' |
\\ | \\ | ||
Line 52: | Line 49: | ||
\\ | \\ | ||
- | - Command line arguments: backup directory, id-file and router | + | - Command line arguments: backup directory, id-file and router |
- | - Date regex covers from 1970 - in case Tomato Router has no time. | + | - Sometimes tar may fail. You can solve this by just cleaning. The next \\ cron run may do that. |
- | - Sometimes tar fails. - cover this error by just cleaning, next cron run may do it | + | |
- All earlier backups from the same day are abandoned. | - All earlier backups from the same day are abandoned. | ||
- | - A total number of backups is kept. This number is configurable. Older ones are deleted. | + | - A total number of backups is kept. This number is configurable. |
\\ | \\ | ||
# | # | ||
+ | |||
+ | | ||
+ | | ||
+ | | ||
| | ||
Line 74: | Line 74: | ||
-r|--router) | -r|--router) | ||
ROUTER=$2 ;; | ROUTER=$2 ;; | ||
+ | -*|--*) | ||
+ | echo " | ||
+ | -h|--help) | ||
+ | echo "usage $0 <option argument> | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | echo " | ||
+ | exit ;; | ||
esac | esac | ||
shift; shift | shift; shift | ||
Line 92: | Line 101: | ||
| | ||
- | # | + | # |
- | # | + | # |
- | DATE_REGEX=[12][09][0-9][0-9][01][0-9][0123][0-9] | + | DATE=`date +%Y%m%d_%H%M` |
- | TIME_REGEX=[012][0-9][0-5][0-9] | + | |
- | | + | |
- | | + | |
pushd ${DIR2BACKUP} > /dev/null | pushd ${DIR2BACKUP} > /dev/null | ||
Line 117: | Line 124: | ||
nvram get t_model_name | tr " " " | nvram get t_model_name | tr " " " | ||
nvram get router_name >> ${SCRIPT_FILE} | nvram get router_name >> ${SCRIPT_FILE} | ||
- | | + | |
sed -e " | sed -e " | ||
source ${SCRIPT_FILE} | source ${SCRIPT_FILE} | ||
Line 133: | Line 140: | ||
TODAYS_BACKUP_FILES_PREFIX=${THIS_BACKUP_FILE%_*} | TODAYS_BACKUP_FILES_PREFIX=${THIS_BACKUP_FILE%_*} | ||
ALL_BACKUP_FILES_PREFIX=${TODAYS_BACKUP_FILES_PREFIX%_*} | ALL_BACKUP_FILES_PREFIX=${TODAYS_BACKUP_FILES_PREFIX%_*} | ||
- | | + | # Keep only one file (the latest) per day - delete earlier file of same day |
- | if ! [[ `find -cmin 5 | grep ${THIS_BACKUP_FILE}` ]] ; then | + | LIST_OLD_BACKUPS_OF_TODAY=`ls ${TODAYS_BACKUP_FILES_PREFIX}_${TIME_REGEX}.${EXT} 2> /dev/null | grep -v ${THIS_BACKUP_FILE}` |
- | # Date of the new files is more that 5 min in the past => seems Tomato Router has no date (or date 1970-01-01) | + | |
- | touch ${THIS_BACKUP_FILE} | + | |
- | fi | + | |
- | | + | |
- | LIST_OLD_BACKUPS_OF_TODAY=`ls | + | |
if [ " | if [ " | ||
- | echo " | + | echo " |
rm -fv ${LIST_OLD_BACKUPS_OF_TODAY} | rm -fv ${LIST_OLD_BACKUPS_OF_TODAY} | ||
echo | echo | ||
Line 152: | Line 154: | ||
echo | echo | ||
fi | fi | ||
- | # delete transfer file only when tar was successfull, i.e. only here | + | # delete transfer file only when tar was successful, i.e. only here |
rm ${TRANSFER_FILENAME} | rm ${TRANSFER_FILENAME} | ||
else | else | ||
Line 158: | Line 160: | ||
fi | fi | ||
popd > /dev/null | popd > /dev/null | ||
- | | + | |
\\ | \\ | ||