Reinstalling user package(s) after firmware upgrade

#1

If you want to save and reinstall user installed package(s) after firmware upgrade, this is the script that I use :wink:

Script name: overlaypkgs.sh

#!/bin/sh
#set -x
#by Faruk Tezcan

do_backup() {
  echo
  echo "Checking. Please wait"
  [ -f $filename ] && rm $filename >/dev/null 2>&1
  echo > $filename >/dev/null 2>&1; ret="$?"
  [ $ret -ne 0 ] && { echo "Invalid file name: $filename"; exit 1; }
  rm $filename >/dev/null 2>&1
  [ $(ls -1 /overlay$UP/usr/lib/opkg/info/*.control | wc -l) = 0 ] && {
    echo "No user packages found"; exit 1; }
  echo
  echo "Saved package(s):"
  for EACH in /overlay$UP/usr/lib/opkg/info/*.control
  do
    NAME="$(egrep "^Package: " $EACH | cut -f 2 -d ' ')"
    opkg whatdepends $NAME >/tmp/opkg.whatdepends
    found=0;
    while read line
    do
      if [ $found -eq 0 ]; then {
        [ "$line" == "What depends on root set" ] && found=1 
      } else found=$((found+1))
      fi
    done </tmp/opkg.whatdepends
    [ $found -eq 1 ] && { echo "--> $NAME"; echo $NAME >>$filename; }
  done
  rm /tmp/opkg.whatdepends >/dev/null 2>&1
  echo         
  echo "$filename created"                                        
  echo     
  echo "If you keep your settings during the firmware upgrade process,"
  echo "you will have this file after the upgrade."
  echo
  echo "Otherwise, you should save this file now and finish your"
  echo "firmware upgrade and restore $filename."
  echo
  echo "Enter '$(basename "$0") restore' command to reinstall user-installed packages."                                                                              
} # do_backup

do_restore() {
  [ -f $filename ] || { echo "$filename could not found"; exit 1; }
  opkg update
  echo "Reinstalling user packages"
  echo "Installed packages:" >$filename.log

  for EACH in $(cat $filename); do
    MSG=""
    [ -f /usr/lib/opkg/info/$EACH.control ] && MSG="already installed" || { 
      [ $(opkg info $EACH | wc -l) = 0 ] && MSG="could not found" || opkg install $EACH >>$filename.log; }
    echo "--> $EACH $MSG"
  done

  echo "All the package(s) in $filename processed"
  echo
  echo "Please check '$filename.log'"
} # do_restore

usage() {
cat <<EOF

Usage: $(basename "$0") [OPTION(S)]...[backup|restore]
where: -f <xxx> file name to save package names
                default='/etc/config/overlaypkgs.lst'
       -d       debug option
       -h       show this help text

Backup user installed packages before firmware upgrade and restore after that


EOF

exit 1
} # usage

[ $# -eq 0 ] && usage

filename="/etc/config/overlaypkgs.lst"

while getopts ':df:h' flag
do
  case "${flag}" in
    d    ) set -x ;;
    f    ) filename=$OPTARG ;;
    \?   ) echo "Invalid option: -$OPTARG"; usage ;;
    :    ) echo "Invalid option: -$OPTARG requires an argument"; usage ;;
    h\?* ) usage ;;
  esac
done

shift "$((OPTIND-1))"   # Discard the options and sentinel --
[ $# -ne 1 ] && usage

UP=""; [ -d /overlay/upper ] && UP="/upper"
cmd=$1

case $cmd in
  backup  ) do_backup ;;
  restore ) do_restore ;;
  *       ) echo "Invalid option: -$cmd"; usage; exit 1 ;;
esac

exit 0

Usage :
overlaypkgs.sh backup
This command simply creates a list from /overlay drive for user installed package(s) in /etc/config. If you keep your settings during the firmware upgrade process you will have this file after the upgrade or you can save /etc/config/overlaypkgs.lst file somewhere else.

overlaypkgs.sh restore
Make sure that your created user package(s) list file is ready. Enter this command after the firmware upgrade and enjoy your new system without missing anyhting.

1 Like
#2

With luck, capturing the user-installed packages will be part of v19 -- it is already part of master with slightly different logic

4 Likes