https://gist.github.com/patrakov/8fca04e0bec139e9a9e169dc4b9783f4

mkdir /etc/package-list
nano /etc/package-list/persist.sh
#!/bin/sh

exec > /var/log/persist-packages.log 2>&1
set -x

PKGS=/etc/package-list/extra-packages.txt

# https://github.com/openwrt/packages/issues/6744, not fixed in 18.06.x
fix_route53() {
  if grep -q CURL_SSL /usr/lib/ddns/update_route53_v1.sh 2>/dev/null ; then
    sed -i /CURL_SSL/d /usr/lib/ddns/update_route53_v1.sh
  fi
}

list_extra_packages() {
  if [ ! -f $PKGS ] || [ /usr/lib/opkg/status -nt $PKGS ] ; then
    # Find the latest package in the ROM
    PROG='
      $1 == "Installed-Time:" && ($2 < NEWEST || NEWEST=="") {
        NEWEST=$2
      }
      END {
        print NEWEST
      }
    '
    FLASH_TIME="$( awk "$PROG" /rom/usr/lib/opkg/status )"

    # All user-installed packages minus those from ROM mistakenly marked as such
    # plus (those explicitly marked previously minus those not installed)
    MARKED=`awk '{RE=RE "|" $0} END { print RE }' $PKGS`
    MARKED='^('${MARKED#|}')$'
    PROG='
      $1 == "Package:" {
        PKG=$2
        USR=""
      }
      $1 == "Status:" && $3 ~ "user" {
        USR=1
      }
      $1 == "Installed-Time:" && $2 > FT && ( USR || ( PKG ~ MARKED ) ) {
        print PKG
      }
    '
    awk -v FT="$FLASH_TIME" -v MARKED="$MARKED" "$PROG" /usr/lib/opkg/status | sort > $PKGS.tmp
    
    mv $PKGS.tmp $PKGS

    # Maybe somebody updated it and OpenWRT team forgot to propagate the fix?
    fix_route53
  fi
}

install_extra_packages() {
  set -e
  opkg update
  UPGRADE=`opkg list-upgradable | cut -d ' ' -f 1`
  if [ -n "$UPGRADE" ] ; then
    opkg upgrade $UPGRADE
    opkg flag ok $UPGRADE
  fi
  INSTALL_CANDIDATES=`cat $PKGS`
  # Weed out packages that don't exist anymore
  INSTALL=""
  for CANDIDATE in $INSTALL_CANDIDATES ; do
    if [ -n "`opkg list $CANDIDATE`" ] ; then
      INSTALL="$INSTALL $CANDIDATE"
    fi
  done

  if [ -n "$INSTALL" ] ; then
    opkg install $INSTALL
  fi
  fix_route53
  find /etc -name \*-opkg -exec rm '{}' ';'
  touch /usr/.extra-pakages-installed
  sleep 70
  reboot
}

if [ -f /usr/.extra-pakages-installed ] ; then
  list_extra_packages
elif [ -f $PKGS ] ; then
  install_extra_packages
fi
chmod 755 /etc/package-list/persist.sh
echo "*/5 * * * * flock -n /var/lock/persist.lock /etc/package-list/persist.sh" > /etc/crontabs/root
echo "/etc/package-list/" >> /etc/sysupgrade.conf
echo "/etc/crontabs/root" >> /etc/sysupgrade.conf
echo "/root/.ssh/" >> /etc/sysupgrade.conf
echo "/etc/config/autossh" >> /etc/sysupgrade.conf
echo "/etc/init.d/autossh" >> /etc/sysupgrade.conf
touch /usr/.extra-pakages-installed
reboot