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

NEW VERSION

 opkg install <pkg>     apk add <pkg>       Install a package
 opkg remove <pkg>      apk del <pkg>       Remove a package
 opkg upgrade           apk upgrade         Upgrade all packages
 opkg files <pkg>       apk info -L <pkg>   List package contents
 opkg list-installed    apk info            List installed packages
 opkg update            apk update          Update package lists
 opkg search <pkg>      apk search <pkg>    Search for packages
#!/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_opkg() {
  # If file with packages not exists or packages wer installed after this file was updated (status file newr than $PKGS)
  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
}

list_extra_packages_apk() {
  # If file with packages not exists or some packages were installed after this file was updated (status file newr than $PKGS)
  if [ ! -f $PKGS ] || [ /etc/apk/world -nt $PKGS ] ; then
    cat /etc/apk/world /rom/etc/apk/world | sort | uniq -u > $PKGS.tmp
    mv $PKGS.tmp $PKGS
    # Maybe somebody updated it and OpenWRT team forgot to propagate the fix?
    fix_route53
  fi
}

install_extra_packages_opkg() {
  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
}

install_extra_packages_apk() {
  set -e
  apk update
  INSTALL=""
  for PKG in `cat /etc/package-list/extra-packages.txt`; do 
    grep $PKG /etc/apk/world 1>/dev/null || INSTALL="$INSTALL $PKG"
  done

  if [ -n "$INSTALL" ] ; then
    apk add $INSTALL
  fi

  fix_route53

  touch /usr/.extra-pakages-installed
  sleep 70
  reboot
}

if [ -f /usr/.extra-pakages-installed ] ; then
  opkg list-installed && list_extra_packages_opkg || list_extra_packages_apk
elif [ -f $PKGS ] ; then
  opkg list-installed && install_extra_packages_opkg || install_extra_packages_apk
fi