openWRT - Linksys WRT1900AC v2 (cobra) ( System Upgrade )

openWRT - Linksys WRT1900AC v2 (cobra) ( System Upgrade )

The upgrade of an openWRT device not always, but most of the times are straight forward. It will consist of a backup of the system configuration and optionally the system packages and then the upload of the new firmware.

Let's start backing up our configuration :

LuCI - System -> Backup/Flash Firmware -> Backup - Generate Archive

We can now download the new version and perform the upgrade.

LuCI - System -> Backup/Flash Firmware -> Flash new firmware image

This part is important, if you uncheck the option to keep the settings the router is going to be flashed with the default settings on the other hand if you check the option to keep the settings your configuration is going to be restored, but not the packages you have installed.

I have found a very good script on the openWRT forum to help with packages and configuration files backup. Big thanks for the user faruktezcan that took the time to write the script. Unfortunately, he does not have a github page and I will paste the script below for backup purposes.

# overlaypkgs.sh
#!/bin/sh
#set -x

do_opkg_update() {
  local dir=$(cat /etc/opkg.conf | grep lists_dir | awk '{print $3}')
  if [ -d $dir ]; then
    [ -z "$(ls -1 ${dir}/* 2>/dev/null)" ] && `opkg update`
  else
    echo "opkg '${dir}' not found"
    exit 1
  fi
}

do_backup() {
  echo -e "\nChecking. Please wait\n"
  rm -f $FILENAME 1>/dev/null 2>&1
  touch $FILENAME 1>/dev/null 2>&1 || {
    echo "Invalid file name: '${FILENAME}'"
    rm -f ${FILENAME} 1>/dev/null 2>&1
    exit 1
  }

  do_opkg_update

  COUNT=0; CONTROL="$(ls -1 /overlay$UP/usr/lib/opkg/info/*.control 2>/dev/null)"
  [ -n "$CONTROL" ] && for EACH in $CONTROL; do
    NAME=$(egrep "^Package: " $EACH | cut -f 2 -d ' ')
    if [ ! -f /rom/usr/lib/opkg/info/$NAME.control ]; then
      opkg whatdepends $NAME 1>/tmp/opkg.whatdepends 2>/tmp/opkg.error
      [ $? -ne 0 ] && { echo "Error! Check '/tmp/opkg.error'"; exit 1; }
      if [ -z "$(egrep "depends on $NAME" /tmp/opkg.whatdepends)" ]; then
        [ $COUNT -eq 0 ] && echo -e "\nSaved package(s):"
        COUNT=$((COUNT+1)); printf "\r%3d " $COUNT
        echo "$NAME"; echo $NAME >> $FILENAME
      fi
    fi
  done

  COUNT=0; CONTROL="$(ls -1 /etc/rc.d | grep "S" 2>&1)"
  [ -n "$CONTROL" ] && for EACH in $CONTROL; do
    [ $COUNT -eq 0 ] && echo -e "\nSaved service(s):"
    COUNT=$((COUNT+1)); printf "\r%3d " $COUNT
    echo "+${EACH:3}"; echo "+${EACH:3}" >> $FILENAME
  done

  CONTROL="$(ls -1 /etc/init.d 2>&1)"
  [ -n "$CONTROL" ] && for EACH in $CONTROL; do
    [ -e /etc/rc.d/S??$EACH ] || {
      COUNT=$((COUNT+1)); printf "\r%3d " $COUNT
      echo "-$EACH"; echo "-$EACH" >> $FILENAME
    }
  done

  rm -f /tmp/opkg.wahtdepends 1>/dev/null 2>&1

  if [ $COUNT -eq 0 ]; then
    echo -e "\nNo user installed package(s) and/or service(s) found"
    exit 1
  fi

  echo -e "\n'$FILENAME' created"
  echo -e "\nIf you keep your settings during the firmware upgrade process,"
  echo -e "you will have $FILENAME after the upgrade."
  echo -e "\nOtherwise, you should save this file now, then finish your"
  echo -e "firmware upgrade and restore $FILENAME."
  echo -e "\nEnter '$(basename "$0") restore' command to reinstall user-installed package(s) and/or service(s)."
} # do_backup

do_restore() {
  [ -f $FILENAME ] || { echo -e  "\n'${FILENAME}' not found"; exit 1; }

  do_opkg_update

  echo -e "\nReinstalling user packages\n"
  echo "Reinstalling user packages" > $FILENAME.log

  CONTROL=$(cat $FILENAME)
  [ -z "$CONTROL" ] && { echo -e "\nNo user installed package(s) and/or service(s) found"; exit 1; }

  for EACH in $CONTROL; do
    CONT=${EACH:0:1}; [ $CONT != "+" -a $CONT != "-" ] && {
      if [ ! -f /usr/lib/opkg/info/$EACH.control ]; then
        [ -n "$(opkg info $EACH)" ] && { opkg install $EACH >> $FILENAME.log; MSG="OK"; } ||  MSG="not found"
      else
        MSG="bypassing - already installed"
      fi
      echo "$EACH --> $MSG"; echo "$EACH --> $MSG" >> $FILENAME.log
    }
  done

  echo >> $FILENAME.log; echo "Enabling service(s)" >> $FILENAME.log
  echo -e "\nEnabling service(s)\n"

  for EACH in $CONTROL; do
    CONT=${EACH:1}
    if [ ${EACH:0:1} == "+" ]; then
      if [ ! -e /etc/rc.d/S??${CONT} ]; then
        $(/etc/init.d/${CONT} enable)
        echo "${CONT} --> enabled"; echo "${CONT} --> enabled" >> $FILENAME.log
      else
        echo "${CONT} --> bypassing - already enabled"; echo "${CONT} --> bypassing - already enabled" >> $FILENAME.log
      fi
    fi
  done

  echo >> $FILENAME.log
  echo "Disabling service(s)" >> $FILENAME.log
  echo -e "\nDisabling service(s)\n"

  for EACH in $CONTROL; do
    CONT=${EACH:1}
    if [ ${EACH:0:1} == "-" ]; then
      if [ -e /etc/rc.d/S??${CONT} ]; then
        $(/etc/init.d/${CONT} disable)
        echo "${CONT} --> disabled"; echo "${CONT} --> disabled" >> $FILENAME.log
      else
        echo "${CONT} --> bypassing - already disabled"; echo "${CONT} --> bypassing - already disabled" >> $FILENAME.log
      fi
    fi
  done

  echo -e "\nAll the package(s) and/or service(s) in $FILENAME processed"
  echo -e "\nPlease check '$FILENAME.log' and restart the router"

} # do_restore

usage() {
cat <<EOF

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

Backup user installed package(s) and/or service(s) before firmware upgrade and restore them later

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 ;;
esac

exit 0

Run the script with the flag backup before the upgrade and save the file /etc/config/overlaypkgs.lst if not keeping the current config, however, if keeping the settings just run the script with the restore flag.

In a future project we will create scripts to automatically back up our routers' configuration to a backup server on our Tucana Cloud.