HOW-TO: Script: List My OPKGs (to a file for backup)

Keywords: backup, opkg, plugins, list, howto, script, shell, luci, restore, ipk, tarball, conf


Description

List user’s (your) installed packages, writing them to a text file that can be used to reinstall the binaries of what a user manually installed. The resulting .list file will be use to opkg install to pull from the feeds. That is done with two commands in one execution.

The GL GUI doesn’t feature a backup function. Creating & restoring a backup achieve (.tar.gz, aka tarball) of all your configuration files (.conf) is described in the below section marked Note(s).

Result

root@GL-AXT1800:~# cat /root/backup/package-lists/2023-07-01-072023_-_GL-AXT1800_-_my-installed-packages.list
bash
drill
htop
libldns

Dependencies

  • root access via SSH to the GL device IP (default 192.168.8.1)
  • sed (should be already present)

Optional but Recommended

  • LuCI (Install via GL GUI → System → Advanced Settings)
  • openssh-sftp-server (to quickly upload/download files via your favorite SFTP client (eg: FileZilla))

Installation

  • Copy the below script to a text file, editing the variables to your preferences but make sure to save extension as .sh & not .txt.
    • Windows 10 Notepad should be fine but make sure UTF-8 is show in the lower right corner. Save it as something similar to list_my_installed_packages.sh .
    • You don’t have to change anything in it to use it; the defaults will work just fine.
  • SSH/SFTP into your GL device, copy or upload the script to an easy to remember location/dir. /root/bin/ is advised (mkdir /root/bin).
  • Make it executable: chmod +x /root/bin/list_my_installed_packages.sh

Usage

  • Run it: /root/bin/./list_my_installed_packages.sh
  • See what was recorded: ls -l /root/backup/package-lists/
    • then cat /root/backup/package-lists/[fileName] one of those listed files.
  • Download your list via SFTP or other method (eg: copying the cat output to a UTF-8 text file on your PC).

Script

#!/bin/ash

# (OpenWrt 21.02) List My OPKGs: list user (your) installed packages, writing
# them to a text file that  can be used to restore the binaries of what a user
# manually installed
# **Keywords:** *backup, opkg, plugins, list, howto, script, shell, luci, restore, ipk, tarball, conf*

# 2023-07-01-1205
# thread: https://forum.gl-inet.com/t/how-to-script-list-my-opkgs-to-a-file-for-backup/30963
# sources: https://forum.openwrt.org/t/solved-opkg-how-to-list-only-packages-that-i-have-installed/11983/5

# edit these variable to your preferences
# ensure double quote mark encapsulation
dateFormat="natural" # natural or iso-8601 formatted
saveDir="/root/backup/package-lists" # note: *no* ending slash
outputName="my-installed-packages.list" # a '.list' is just a text file

### [ do not edit below this line ] #####

# get the device's name fr the system hostname
deviceName=$(cat /etc/config/system | grep "hostname" | awk '{print $3}' | xargs)

# get the device's mac in case multiple of same device but same hostname
mac=$(cat /sys/class/net/eth0/address | tr '[:lower:]' '[:upper:]' | tr -d ':')

# set the date for the list according to the chosen preference
if [ "$dateFormat" = "iso-8601" ]; then
    timeStamp=$(date +"%Y%m%dT%H%M%S%z")
else
    timeStamp=$(date +"%Y-%m-%d-%H%M%S")
fi

# check for the existance of a save location/dir, creating it if needed
if [ ! -d "$saveDir" ]; then
    mkdir -p "$saveDir"
fi

# do it
ls /overlay/upper/usr/lib/opkg/info/*.list | sed -e 's/.*\///' | sed -e 's/\.list//' > "$saveDir"/"$timeStamp"_-_"$deviceName"_-_"$mac"_-_"$outputName"

# done
exit 0


Reinstalling

  • Login via SSH/SFTP
  • Upload your list
  • Update opkg & run opkg through the list.

Example

I’m restoring a list made 2023-07-01-085109. It is located on the GL device at /root/backup/package-lists/ as 2023-07-01-085109_-_GL-AXT1800_-_my-installed-packages.list. Edit to taste:

opkg update && for i in $(cat /root/backup/package-lists/2023-07-01-085109_-_GL-AXT1800_-_my-installed-packages.list); do opkg install $i; done

Note(s)

This does not restore any changes you’ve made to configuration files. It only installs the programs you’ve installed since the last time you upgraded or reset the device’s firmware.

The GL GUI doesn’t feature a backup function. Use LuCI → System → Backup / Flash firmware → Restore → Restore backup → Upload achieve … to restore the conf from a previous tarball (.tar.gz) after you’ve re-installed those binaries/programs from your .list.

You can make that tarball containing your conf(s) via LuCI → System → Backup / Flash firmware → Flash operations → Actions → Backup → Generate achieve.

To preview exactly what’s going into that tarball see LuCI → System → Backup / Flash firmware → Flash operations → Configuration → Configuration → Show current backup file list → Open list…

Tips

  • LuCI’s & SSH’s default password is the same as GL GUI. root is the default login name.
  • Add the script location/dirs to your future backups: LuCI → System → Backup / Flash firmware → Flash operations → Configuration → Configuration → add /root/ → Save
    • If you have custom firewall rules be sure to add /etc/firewall.user too.
    • If you have custom Encrypted DNS server settings of DNS over TLS, DNS over HTTPS or DNSCrypt add /etc/dnscrypt-proxy2/
  • Use GL GUI for firmware updates (GL GUI → System → Upgrade) but make backups via LuCI
  • The nano editor is very easy to edit text/conf files while logged into your GL device (opkg install nano)

Caveats

  • Assumes firmware 4.2.1-release4
  • This was scripted using the bash shell but OpenWrt defaults to ash. Please report any issues including error output.
  • No warranties are either expressed or implied; I’m not responsible if your devices become sentient & fight back from the abuse.
8 Likes

Changelog

  • 2023-07-01-1205: Added device’s MAC in case of multiple same model, same hostname
  • 2023-07-01-0720: Initial release

References & Resources

1 Like