OpenWrt Forum Archive

Topic: OpenWrt trunk automatic backup.

The content of this topic has been archived on 5 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

I was looking for a way to make OpenWrt do automatic backup. So far haven't found a good how-to or any information. Noticed backup option in LuCI, but this is a manual way.
I would like to ask whether any of you succeeded in doing this or not. I was thinking of making a script, that would make system to do a backup automatically and store it somewhere outside of the router, but knowledge does not allow me to do so, so is time.
Would appreciate any sorts of help in achieving that.

I use rsnapshot from an external server to do backups of my OpenWrt device. I don't see that it's packaged for OpenWrt, but it should be easy to run, and if you want backups entirely locally (to SD card, USB flash, etc.) you could use it on the device itself and setup cron execution.

It doesn't have anything to do with the LuCI backup option though (which is basically just config files), so you need to make sure you grab what's interesting. I make sure to backup more, including installed package lists and such.

I too wondered the same.  I have an Ubiquiti bullet with 4 M of flash.  While I can fit Luci on and have done so, that's more than 10% of my space, got other things I wanted on there.  Yet there seems no mechanism for saving configurations, I had to manually type them in every time I flashed (which was often, in the beginning wink ).  Otherwise, I'm fine with editing the configurations with ssh access, don't need no web GUI.
So I wrote this little bash script to do it for me and it works wonderfully.  I just flashed a new build today and used this script to put already saved config files in place, to my amazement it worked flawlessly.  I was afraid it was going to not get along with the overlay, etc., but everything worked perfectly.  It is so simple and trivial, yet I have searched and searched the forum & wiki and found nothing like it.
It uses SCP to d/l config files from your router to a directory chosen by you on your computer.  It works both ways, from router to computer and vice-versa.  When saving it puts the files in a folder named by you in the save process, like "standard_wireless_station".  And then in that folder puts the files in the same folder structure as you would find on the router.
Some things to note:
1.  It's highly suggested to use the dropbear public key authentication scheme described so well at: [url]https://wiki.openwrt.org/oldwiki/dropbearpublickeyauthenticationhowto?s[]=ssh[/url]
else you'll be inundated with requests to input your password with every file transferred to the router.  Besides that, it's really great, much easier access and much safer to boot.
2.  There are unique things that must defined for your particular setup, directories, etc. in the script.  They are marked with four (4) pound signs (#).
3.  It's totally your responsibility to decide which files you wish to save and you must configure that in the script.  My configuration is pretty basic so probably a good start.  It's pretty easy & straightforward though.  In addition, you must add directories to emulate the directory structure that they are being copied from, so they have a place to go.  One thing that may not be quite obvious about this is if you have something in say, /etc/config, you must add to the directory creation commands to make BOTH the /etc folder AND the etc/config folder.  Better coding could fix that...
4.  If down the line you decide to add things to be saved (and you will!), be aware that older saved configs will now produce "file not found" messages as it tries to restore non-existent files from the folders, not previously saved.  This won't hurt anything, you just need to be aware of it.  A little more fancy coding could fix that, but I didn't say it was fancy.
5.  Your saved configs must be named without a space, like "standard_wireless_station".  I'm sure again a better bash programmer could easily fix that, but I just struggle along...
6.  Not only are the files in the same folder structure as found on the router, but you can edit them on your computer.  BUT I'm not sure of the ramifications of editing and saving the files in something like Gedit, so beware!  Of course, you can always use vi on your computer too, not that I'm not a big vi fan...
Ok, that's all the caveats I can think of.  Use at your own risk, ymmv, etc.
As already alluded, I am not a very good bash programmer, so if anyone wishes to offer constructive criticism to improve some of this, feel free.  I know it's quite basic, I almost feel silly for posting it, in the light of all the fantastic knowledge flying around this forum.  But I would have appreciated it...
Enjoy!
Oh, and a big THANK YOU! to the great community of developers at OpenWrt big_smile  I love it.

#!/bin/bash

# OpenWrt Configuration Manager
# written by yuyu
# 4-6-11

# little prog to save and restore openwrt
# configurations on your host pc/router
# requires dropbear certificate access, no password setup
# http://wiki.openwrt.org/oldwiki/dropbearpublickeyauthenticationhowto?s[]=ssh

#### openwrt device ssh address
#### be sure to add the colon at the end -  example: OPENWRT="root@192.168.1.1:"
OPENWRT="root@192.168.1.1:"
#### the configuration storage folder on pc
#### be sure to start with forward slash, and have NO forward slash at the end
#### example "/home/yuyu/openwrt_configs"
CONFIGS="/home/yuyu/openwrt_configs"
#### location of your scp binary
SCP=/usr/bin/scp

function begin() {
echo "Do you wish to (r)estore or (s)ave an openwrt configuration?  (any other key to quit)"
read -n 1 MODE_STRING
echo
case $MODE_STRING in

    "s" | "S" ) # save configuration (read from device to pc)
    echo "Please provide a name (no spaces) for this configuration: "
    read DIR_STRING
    if [ -d "$CONFIGS/$DIR_STRING" ]; then
        echo $DIR_STRING "configuration already exists.  Overwrite it (y/n)?"
        read -n 1 yn
        echo
        case $yn in
            "n" | "N" )
            begin
        ;;
        esac
    fi
    echo "Saving" $DIR_STRING "configuration to" $CONFIGS/$DIR_STRING
    FROM_DIR=$OPENWRT
    TO_DIR=$CONFIGS/$DIR_STRING
    if [ -d $CONFIGS/$DIR_STRING ]; then
        rm -r -f $CONFIGS/$DIR_STRING
    fi
    mkdir $CONFIGS/$DIR_STRING
#### More directories may need to be added below, according to your needs
    mkdir $CONFIGS/$DIR_STRING/etc
    mkdir $CONFIGS/$DIR_STRING/etc/config
    mkdir $CONFIGS/$DIR_STRING/etc/init.d
    copy_config
    ;;

    "r" | "R" ) # restore configuration to device from pc
    echo "Please provide the name (no spaces) of the configuration: "
    read DIR_STRING
    if [ ! -d "$CONFIGS/$DIR_STRING" ]; then
        echo "that configuration doesn't exist.  Restarting..."
        sleep 4
        begin
    fi
    echo "Restoring" $DIR_STRING "configuration from" $CONFIGS/$DIR_STRING
    FROM_DIR=$CONFIGS/$DIR_STRING
    TO_DIR=$OPENWRT
    copy_config
    ;;
esac
exit
}
function copy_config() {
#### you may need to add or subtract files from this list, according to your needs
    $SCP $FROM_DIR/etc/config/* $TO_DIR/etc/config
    $SCP $FROM_DIR/etc/dnsmasq.conf $TO_DIR/etc
    $SCP $FROM_DIR/etc/ethers $TO_DIR/etc
    $SCP $FROM_DIR/etc/firewall.user $TO_DIR/etc
    $SCP $FROM_DIR/etc/hosts $TO_DIR/etc
    $SCP $FROM_DIR/etc/init.d/* $TO_DIR/etc/init.d
    $SCP $FROM_DIR/etc/profile $TO_DIR/etc
    $SCP $FROM_DIR/etc/rc.local $TO_DIR/etc
    $SCP $FROM_DIR/etc/resolv.conf $TO_DIR/etc
    $SCP $FROM_DIR/etc/sysctl.conf $TO_DIR/etc
    $SCP $FROM_DIR/etc/passwd $TO_DIR/etc
    $SCP $FROM_DIR/etc/passwd- $TO_DIR/etc
    exit
}
#### If you have a different file manager than nautilus, change the next line to it's name
#### or if it bugs you to have it open, just comment it out
nautilus $CONFIGS
begin

I prefer a simpler approach with crontab:

0 0 * * * tar -cvf /mnt/sda1/backups/`date -I`.tar /overlay

Thanks for the input arokh.  I am curious as to what others are doing, because surely there are other methods out there, such a universal need.
Yes, I have seen the tar examples.  I had the same problem with that discussed here: https://forum.openwrt.org/viewtopic.php?id=25513
But I'm not sure about the simpler part.  Of course, initially your way is simpler, but now that I have done the bash, it's essentially the same, push a button, done.  You still must keep a list of files that you wish to backup, so no change there.  As far as the certificate access, I wouldn't have it any other way anyhow, it is so convenient and safe.
And with the tar method you have an additional step to untar to see what's inside, etc., whereas in the other system they are there in a directory already, easy to find.  It's not like there is so much data that it fills your HD.  And you must scp it manually after you create it, to store on your computer.
If cron is important to you, most Linux distros have it running there also.
Certainly, both ways will work, to each his own.  Most seem to use the tar method, so maybe I'll change my mind after awhile.

Well, if you don't want to untar - just copy it smile We're talking megabytes after all.

The discussion might have continued from here.