This is my first post in this forum. Please be kind
I am doing automated backups of my openwrt config to a linux server and I wanted to do some documentation on how it is done, so why not document it here, for others to read, increase and use my solution?
I am only using standard linux tools on the linux side and openwrt on the router. Currently a TP-Link Archer C7 v2 is my main router, but this should be transferable to almost any device.
OpenWRT:
I want to backup my rrd database for statistics, unnecessary but nice. And I export a list of all the installed packages, in case of a reinstall you can install all of them at once in a shell.
System -> Scheduled Tasks:
# auto backup list of installed packages to a file
52 * * * * /usr/sbin/listuserpackages | sort > /etc/config/userpackages.list
System -> Backup -> Configuratioon -> Backup file list:
## This file contains files and directories that should
## be preserved during an upgrade.
# /etc/example.conf
# /etc/openvpn/
/usr/sbin/reconnect
/usr/sbin/listuserpackages
# backup rrd DB for statistics - may increase size A LOT!
/tmp/rrd
# add here whatever you want to keep
Linux server side
I wrote a script to use the OpenWRT backup tool to create a config file and transfer it to my linux machine. To automate this, I execute the script every hour via cron as a normal user, this creates a backup file containing creation day. This way I archive the last backup every day (one per day), the current backup overwrites the one from an hour ago. A full year of backups is <42MB for me so storage is no concern.
backup-router (must be executable)
#!/bin/sh
# creates a backup and fetches it to my backup server
# by Stich IX 2018
filename=backup-router-$(date "+%Y-%m-%d").tar.gz
/usr/bin/ssh -4 -i /home/stich/.ssh/owrt.key root@router /sbin/sysupgrade --create-backup /tmp/$filename
/usr/bin/scp -4 -i /home/stich/.ssh/owrt.key root@router:/tmp/$filename /mnt/backup/router/
/usr/bin/ssh -4 -i /home/stich/.ssh/owrt.key root@router rm /tmp/$filename
To make this work, you need an ssh keypair - here called owrt.key for the private key on linux and the public key in authorized_keys on OpenWRT.
crontab on linux:
55 * * * * stich /home/stich/bin/backup-router > /dev/null 2>&1
This is my solution for an automated backup of an OpenWRT installation to backup storage.
If you find something missing, typos, or improvements, please contact me!