Skip to content

Instantly share code, notes, and snippets.

@crabdancing
Created October 5, 2021 10:33
Show Gist options
  • Save crabdancing/4c505d8f1ad6fe1dcd6fd16612af2b25 to your computer and use it in GitHub Desktop.
Save crabdancing/4c505d8f1ad6fe1dcd6fd16612af2b25 to your computer and use it in GitHub Desktop.
Automatically backup your OpenWRT router configuration to a local tarball under ~/Backups
#!/usr/bin/env bash
# script based on instructions from:
# https://openwrt.org/docs/guide-user/troubleshooting/backup_restore
router_hostname=mainrouter.lan
# Generate/update backup
ssh "$router_hostname" 'umask go=; sysupgrade -b /tmp/backup-${HOSTNAME}-$(date +%F).tar.gz'
# Download backup
mkdir -p ~/Backups/"$router_hostname"
scp root@"$router_hostname":/tmp/backup-*.tar.gz ~/Backups/"$router_hostname"
@jstrot
Copy link

jstrot commented Feb 20, 2024

Thanks for sharing your script.

I ended up avoiding the scp altogether:

backup_file="/somewhere/backup-$hostname-$(date +%F).tar.gz"
echo "Creating $backup_file"
ssh -q root@$hostname "sysupgrade -b -" > "$backup_file"

The "-" tells sysupgrade to send to stdout and then I just pipe the output to the final file location.
From sysupgrade --help:

        -b | --create-backup <file>
                     create .tar.gz of files specified in sysupgrade.conf
                     then exit. Does not flash an image. If file is '-',
                     i.e. stdout, verbosity is set to 0 (i.e. quiet).

Cheers

@crabdancing
Copy link
Author

@jstrot

Yes, that's definitely much better. Good catch!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment