OpenWrt Forum Archive

Topic: how to create UCI script mirroring current configuration?

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

How can I create an UCI shell script based on my current config?

I'm running a community trunk build of OpenWRT (wndr3700v4) and it's necessary to flash update and erase config each time. The advice is to run an UCI script to bring the settings back, but I've yet to successfully even configure one thing from command line UCI.

I have hopes that there is a way to build a UCI script based on my current config, so I can run it on future versions to reconfigure them.

Is there a way, please?

Absolutely, I've done uci show >  backup.txt then edited this file with the Sublime Text to insert the uci set for most entries.

Some things also require either uci add or uci add_list to be ran.

Here's a quip of what I have for my setup script:

uci set network.wan.peerdns=0
uci set network.wan.delegate=0
uci add_list network.wan.dns="8.8.8.8"
uci add_list network.wan.dns="8.8.4.4"
uci commit network

uci set system.@system[0].hostname="ArcherC7"
uci set system.@system[0].zonename="America/Vancouver"
uci set system.@system[0].timezone="PST8PDT,M3.2.0,M11.1.0"
uci set system.@system[0].cronloglevel=8
uci set system.@system[0].conloglevel=8
uci set system.ntp=timeserver
uci delete system.ntp.server
uci add_list system.ntp.server='0.openwrt.pool.ntp.org'
uci add_list system.ntp.server='1.openwrt.pool.ntp.org'
uci add_list system.ntp.server='2.openwrt.pool.ntp.org'
uci add_list system.ntp.server='3.openwrt.pool.ntp.org'
uci set system.ntp.enabled=1
uci set system.ntp.enable_server=1
uci commit system

Thank you. I will investigate modifying the output from uci show

I think the add_list things might be for my LAN names entries that I have in dnsmasq, but this will be a good start.

First, you need current opkg installed packages:
https://forum.openwrt.org/viewtopic.php?id=42739
Then there are clues:

opkg list-changed-conffiles

And the overlay partition are full of changed files. Remember the extroot?:

tar -C /overlay -cvf - . | tar -C /mnt/sda1 -xf -

If you backup overlay, there is all the changes, so what I do is backup over netcat:

Ubuntu wrote:

nc -l -p 10000 > overlay.tar

OpenWrt wrote:

tar -C /overlay -cvf - . | nc 192.168.1.XXX 10000

But, I'd better compare current uci with default uci:

telnet 192.168.1.1
r=$(opkg list-installed base-files | sed -e 's/^.*-r//')
m=$(grep machine /proc/cpuinfo | sed -e 's/^.* //')
opkg list-installed | awk '{print $1}' > /tmp/opkg.txt
uci -d ";" -c/rom/etc/config show | sort > /tmp/default.uci
uci -d ";" show | sort > /tmp/latest.uci
opkg list-changed-conffiles > /tmp/changed-conffiles.txt
tar -C / -T /tmp/changed-conffiles.txt -czf /tmp/$m-$r.tar.gz -C /tmp opkg.txt default.uci latest.uci
rm /tmp/default.uci /tmp/latest.uci /tmp/opkg.txt /tmp/changed-conffiles.txt
cd /www
ln -sf /tmp/$m-$r.tar.gz
echo "# http://$(uci get network.lan.ipaddr)/$m-$r.tar.gz"
# http://192.168.1.1/TL-MR3220-44154.tar.gz
exit

After download, I compare both uci files, to keep only the minimal changes.

cd /tmp
cp -p TL-MR3220-44154.tar.gz [SOME-BACKUP-PATH]/ar71xx/r44154/
tar -xzf [SOME-BACKUP-PATH]/ar71xx/r39729/TL-MR3220-39729.tar.gz default.uci latest.uci
diff -U0 default.uci latest.uci | sed -e '/^@@\|^---\|^+++/ d' | sort -t = -k 1.2,1 -k 1.1,1.1 > uci.diff
# Delete every - folowed by +
awk -v FS="=" '/^-/ {
if ( found ) print found
found=$0
key=substr($1, 2)
}
/^+/ {
if ( found && substr($1, 2) != key ) print found
found=""
print
}' uci.diff | sed -e '/ / s/^\(.\)\(.*\)/\1"\2"/' | sed -e 's/^+/set /' -e 's/^-/delete /' > uci.batch
# LOL
gedit uci.batch

As you can see, from last line of code, there are spurious changes that shouldn't be there:
- Delete the network config, is automatically added after first boot.
- DELETE the shadow password, you likely not defined a password when restore this backup.
- UCI list: Require manual edit.
- ADD commit after every block.
This is an incomplete WIP code to copy and paste, is not a .sh script file.

Immediately after flash the newer revision, is a good practice to backup uci again, to avoid the defaults without network config. I call this "fresh.uci" instead of "latest.uci" and the /rom one "default.uci" isn't required.
Add a new Murphy law: You'll never have the "fresh.uci" needed for an upgrade smile

To restore is as simple as:

telnet 192.168.1.1
uci batch <<'EOF'
set system.@system[0].hostname=TL-MR3220
set system.@system[0].timezone=ART3
set "system.@system[0].zonename=America/Argentina/Buenos Aires"
set wireless.radio0.country=AR
set wireless.radio0.disabled=0
set wireless.radio0.distance=60
set wireless.radio0.txpower=17
set wireless.@wifi-iface[0].encryption=psk2+tkip+ccmp
set "wireless.@wifi-iface[0].key=Some long key."
set wireless.@wifi-iface[0].ssid=Some
commit system
commit wireless
EOF
reboot & exit
harpingon wrote:

Thank you. I will investigate modifying the output from uci show

I think the add_list things might be for my LAN names entries that I have in dnsmasq, but this will be a good start.

Here's what I have for static leases and names in dnsmasq:
uci add dhcp host
uci set dhcp.@host[-1]=host
uci set dhcp.@host[-1].name=n54l
uci set dhcp.@host[-1].mac=xx:xx:xx:xx:xx:xx
uci set dhcp.@host[-1].ip=192.168.1.99
uci add dhcp domain
uci set dhcp.@domain[-1]=domain
uci set dhcp.@domain[-1].name=n54l
uci set dhcp.@domain[-1].ip=192.168.1.99

Been trying to figure this out for several hours' maybe someone is still listening over here.

I am a new OpenWRT user (was from NVRAM and dd-wrt which I understood but not this uci business).    Best I can find is a one line example to add static IPs:

uci batch <<-EOF
   #..... other non relevant code removed

    # Set a static leases
    add luci_ethers static_lease
    set luci_ethers.@static_lease[-1].macaddr='00:0c:29:f0:a5:5e'
    set luci_ethers.@static_lease[-1].ipaddr='192.168.1.222'
    commit luci_ethers

    # Set a different IP address for LAN
    set network.lan.ipaddr=192.168.0.1
    commit network
EOF

The questions I have are
a) is there an example anyone has of adding 2 or more static and not just one?   I have no idea what this subscript texting scheme [-1] is an indicator of?   Does the -1 mean it's a new entry or what value do I know to use here when I want to make 35 entries?   I think an example of a multiple line add would explain more.
b) is this added to a common xxx.sh script?
c) where can I save the script that won't be wiped out at next boot up time, or do I always have to WinSCP it over to the router and then run it wth PuTTY?    .... thanks....

Thanks!

The discussion might have continued from here.