Simple test to see if backup is needed

I've written a script that I can run daily to backup my openwrt configs. This was not hard (except getting webdav to upload it to my ownCloud instance).

I'd like to refine it by testing whether a new backup is needed: only run the backup process when there has been a change in the configs. I don't want to use rsync... would rather leverage sysupgrade.

Currently, as a starter, I'm just using find /etc/config/ -type f -mtime -1 and testing its return code, which works fine but is limited. Its return code is 0 if there are any changes in the last 24 hours.

Is there a command option in sysupgrade, owut or some other standard openwrt utility that would provide a simple test to see if there are changes that warrant a new -backup- of my config?

My thoughts are to try to use sysupgrade -l and find, but I'm not sure if I'm overlooking a simpler/cleaner solution.

Any ideas?

Are you familiar with owut check?

$ owut check
ASU-Server     http://10.1.1.207:8000
Upstream       https://downloads.openwrt.org
Target         x86/64
Profile        generic
Package-arch   x86_64
Version-from   24.10.5 r29087-d9c5716d1d (kernel 6.6.119)
Version-to     24.10.5 r29087-d9c5716d1d (kernel 6.6.119)
All packages are up-to-date
There are no changes, upgrade not necessary

Ooops, my-bad. I meant to say “backup” rather than “upgrade”.

Sorry for the confusion…

Do you keep the backup locally also? You can use find to see if anything is newer than the backup file’s modified timestamp.

find /etc/ -type f -newer /tmp/mylastbackupfile.tar.gz
1 Like

To clarify, I want to check the modification dates of files listed from sysupgrade -l and if there is any file w/ a modification date within the last 24 hours, I want to trigger sysupgrade -b.

Creating the list of files to check is easy:

sysupgrade -l > /tmp/backup-list.txt

Just not sure how to process it w/ find or which other command… ie. to test the modification date of each file listed on /tmp/backup-list.txt.

root@router:~# ll /etc/config/wireless 
-rw-r--r--    1 root     root          1374 Dec 27 06:27 /etc/config/wireless
root@router:~# date -r /etc/config/wireless +%s
1766834820

Mod time in epoch seconds.

2 Likes

Are you changing your configs on a daily basis or generally frequently? Nothing changes on openwrt without user interaction to change the configs, so typically a daily backup is entirely unnecessary.

Personally, I run a backup after I make a change so that I have the latest config saved, but it is easy since I’m already connected to the device.

2 Likes

There's one weird sidenote to this. If you have luci-app-statistics installed, and you have backup turned on, every time you run sysupgrade -l, it will create a new backup of the stats:

$ uci show luci_statistics.collectd_rrdtool.backup
luci_statistics.collectd_rrdtool.backup='1'

$ sysupgrade -l
...
/etc/luci_statistics/rrdbackup.sysupgrade.tgz
/etc/luci_statistics/sysupgrade.trustme.txt

$ ll /etc/luci_statistics/
drwxr-xr-x    1 root     root          1024 Dec 31 09:39 ./
drwxr-xr-x    1 root     root          1024 Dec 29 05:24 ../
-rw-r--r--    1 root     root           178 Dec 17 13:08 README.backups
-rw-r--r--    1 root     root        476141 Dec 31 09:39 rrdbackup.sysupgrade.tgz
-rw-r--r--    1 root     root        476141 Dec 31 09:39 rrdbackup.tgz
-rw-r--r--    1 root     root            26 Dec 31 09:39 sysupgrade.dont.trustme.txt
-rw-r--r--    1 root     root            26 Dec 31 09:39 sysupgrade.trustme.txt

Wait a minute and do it again, the date stamps will change...

If this has any relevance to you, see https://github.com/efahl/luci/tree/master/applications/luci-app-statistics for the write up on this behavior.

1 Like

Rephrasing… I will eventually change the title… Create a new backup ONLY if a file or setting has been changed. Do this in a script, and run it every 24 hours.

I’m getting closer to having this work now. The epoch seconds, date +%s and date +%s -r /etc/config/dhcp seems like it is what I need to use, for instance.

========

I’m forgetful… :person_shrugging: , but you are right.

so much stuff hidden under the hood…!

kachinnngggg! paydirt.

Changed since when? What do you have so far in your script?

My script already can be run daily to create a backup (sysupgrade -b) and upload it to my ownCloud instance. The only refinement is to do the backup only if there is a change in any of the settings (settings proper) or installation (additional package(s) added).

The arithmetic, in epoch seconds: If (( current_epoch_time - settings-file_epoch_time.) > 86400 ) then create a backup and upload it. Actually, if this condition is true for any settings-file, then stop comparing and do the backup/upload.

You probably don't need date stamps. You can use the shell's built-in -ot (or variant) operator:

touch x
sleep 10
touch y
[ "x" -ot "y" ] && echo "It's older!"

In your script, you could do touch /tmp/last-backup, but before that check if no last-backup, or candidate file is newer than /tmp/last-backup then save it...

1 Like

You could use a Procd init script using inotifywait. It runs as a service that will continuously monitor your configuration directory and create timestamped backups as needed.

2 Likes

Thanks to all who weighed in on the solution that works best for me. Everyone offered a good idea and I learned a lot.

My solution now does what I wanted originally: Trigger the script via crontab every day at say 1:20AM every day. Script checks to see of any alterations have been made to any files listed by sysupgrade -l within the last 24 hours, and if so, create a config tarball and upload it to my ownCloud instance. Return codes (0 for success, 1 for no backup was necessary, 2 for upload failure) are passed, in case I want to log or email something via the crontab entry.

#!/bin/ash

#Requires:
# openwrt
# nslookup
# curl
# sysupgrade

# Configuration
OWNCLOUD_SERVER="redacted"   # ip or FQDN only, no protocol like 'https://"
USERNAME="redacted"
PASSWORD="redacted"

# File and dir definitions
BACKUP_FILENAME="backup_$(uname -n)_$(date "+%Y%m%d-%H%M%S").tar.gz"
LOCAL_FILE="/tmp/$BACKUP_FILENAME"
REMOTE_FILE="/remote.php/webdav/path-to-the-file-in-the-owncloud-directory/$BACKUP_FILENAME"

# Check for executables



# Debugging
#echo $USERNAME
#echo $PASSWORD
#echo $BACKUP_FILENAME
#echo $LOCAL_FILE
#echo $REMOTE_FILE
#nslookup $OWNCLOUD_SERVER

# create a backuplist.txt [ list of all files that sysupgrade -l says would be backed up ]
# this is one file per line
sysupgrade -l > /tmp/backuplist.txt

# Check for modifications in last 24 hours; if the current time - file time
# is less than 1 day, then perform a backup.
now=$(date +%s)

while read settings_file # read one line of the backuplist.txt file
do
    filedate=$(date +%s -r "$settings_file")
    file_modification_age=$((now - filedate))

    # If there is a file in the config list that is younger than 24 hours, then run the backup.
    if [ $file_modification_age -lt 86400 ]; then
        logger "=== backup - found config file changed with last 24 hours"
        logger "=== backup - $settings_file was changed $file_modification_age seconds ago"
        logger "=== backup - others may have been changed, in addition"

        # Create backup archive
        sysupgrade -b "$LOCAL_FILE"

        # Upload, via webdav, the file using HTTPS and curl
        curl -k -u "$USERNAME:$PASSWORD" -T "$LOCAL_FILE" "https://$OWNCLOUD_SERVER/$REMOTE_FILE"  # -k for self-signed cert
        if [ $? -ne 0 ]; then
            logger "=== backup - upload to https://$OWNCLOUD_SERVER/$REMOTE_FILE failed"
            exit 2
        else
            logger "=== backup - upload to https://$OWNCLOUD_SERVER/$REMOTE_FILE succeeded"
        fi

        # Cleanup
        rm $LOCAL_FILE
        rm /tmp/backuplist.txt
        exit 0

        fi

done </tmp/backuplist.txt

exit 1



NB: I am still in the learning/adaptation stage wrt OpenWRT, and working on integrating it w/ Home Assistant, so I do frequent modifications/adjustments. What works for me may not work for others…

@psherman : thank you for your comments/question, which made me think it through a bit more.

While learning about a new system/piece of software, I tend to tinker and adjust, just to learn and see what is under the hood. Specifically, I’m still tweaking the wifi and usteer in order to get roaming optimized. With all this tinkering, I often forget to back up the configs.

I was surprised to see that on my machines, there was a counterexample to what you pointed out (which looked completely reasonable to me). The feeds to banip actually are updated periodically, and these showed up as a change in the configuration, as far as sysupgrade -l perceives things. I haven’t thought it through as to whether I have any other packages installed that would update their feeds periodically [ addblock and the like would probably do that ].