base-files: add sysupgrade -o to save all overlay files
authorLuiz Angelo Daros de Luca <luizluca@gmail.com>
Fri, 17 Aug 2018 23:49:52 +0000 (20:49 -0300)
committerKevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
Sun, 9 Dec 2018 18:04:11 +0000 (18:04 +0000)
Add sysupgrade '-o' option in order to include all overlay files in
backup, except for those that are from packages but including files
listed in conffiles, sysupgrade.conf or /lib/upgrade/keep.d.

With '-u' option, it will skip files equals to /rom and conffiles that
were not changed.

Signed-off-by: Luiz Angelo Daros de Luca <luizluca@gmail.com>
package/base-files/files/sbin/sysupgrade

index d4bb7a1..55c323d 100755 (executable)
@@ -9,6 +9,7 @@ export INTERACTIVE=0
 export VERBOSE=1
 export SAVE_CONFIG=1
 export SAVE_OVERLAY=0
+export SAVE_OVERLAY_PATH=
 export SAVE_PARTITIONS=1
 export SKIP_UNCHANGED=0
 export CONF_IMAGE=
@@ -27,7 +28,8 @@ while [ -n "$1" ]; do
                -v) export VERBOSE="$(($VERBOSE + 1))";;
                -q) export VERBOSE="$(($VERBOSE - 1))";;
                -n) export SAVE_CONFIG=0;;
-               -c) export SAVE_OVERLAY=1;;
+               -c) export SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/etc;;
+               -o) export SAVE_OVERLAY=1 SAVE_OVERLAY_PATH=/;;
                -p) export SAVE_PARTITIONS=0;;
                -u) export SKIP_UNCHANGED=1;;
                -b|--create-backup) export CONF_BACKUP="$2" NEED_IMAGE=1; shift;;
@@ -54,12 +56,14 @@ IMAGE="$1"
 [ -z "$IMAGE" -a -z "$NEED_IMAGE" -a $CONF_BACKUP_LIST -eq 0 -o $HELP -gt 0 ] && {
        cat <<EOF
 Usage: $0 [<upgrade-option>...] <image file or URL>
-       $0 [-q] [-i] [-c] [-u] <backup-command> <file>
+       $0 [-q] [-i] [-c] [-u] [-o] <backup-command> <file>
 
 upgrade-option:
        -f <config>  restore configuration from .tar.gz (file or url)
        -i           interactive mode
        -c           attempt to preserve all changed files in /etc/
+       -o           attempt to preserve all changed files in /, except those
+                    from packages but including changed confs.
        -u           skip from backup files that are equal to those in /rom
        -n           do not save configuration over reflash
        -p           do not attempt to restore the partition table after flash.
@@ -129,12 +133,52 @@ add_conffiles() {
 
 add_overlayfiles() {
        local file="$1"
-       ( cd /overlay/upper/; find ./etc \( -type f -o -type l \) $find_filter | sed \
+
+       local packagesfiles=$1.packagesfiles
+       touch "$packagesfiles"
+
+       if [ "$SAVE_OVERLAY_PATH" = / ]; then
+               local conffiles=$1.conffiles
+               local keepfiles=$1.keepfiles
+
+               list_conffiles | cut -f2 -d ' ' | sort -u > "$conffiles"
+
+               # backup files from /etc/sysupgrade.conf and /lib/upgrade/keep.d, but
+               # ignore those aready controlled by opkg conffiles
+               find $(sed -ne '/^[[:space:]]*$/d; /^#/d; p' \
+                       /etc/sysupgrade.conf /lib/upgrade/keep.d/* 2>/dev/null) \
+                       \( -type f -o -type l \) 2>/dev/null | sort -u |
+                       grep -h -v -x -F -f $conffiles > "$keepfiles"
+
+               # backup conffiles, but only those changed if '-u'
+               [ $SKIP_UNCHANGED = 1 ] &&
+                       list_changed_conffiles | sort -u > "$conffiles"
+
+               # do not backup files from packages, except those listed
+               # in conffiles and keep.d
+               {
+                       find /usr/lib/opkg/info -type f -name "*.list" -exec cat {} \;
+                       find /usr/lib/opkg/info -type f -name "*.control" -exec sed \
+                               -ne '/^Alternatives/{s/^Alternatives: //;s/, /\n/g;p}' {} \; |
+                               cut -f2 -d:
+               } |  grep -v -x -F -f $conffiles |
+                    grep -v -x -F -f $keepfiles | sort -u > "$packagesfiles"
+               rm -f "$keepfiles" "$conffiles"
+       fi
+
+       # busybox grep bug when file is empty
+       [ -s "$packagesfiles" ] || echo > $packagesfiles
+
+       ( cd /overlay/upper/; find .$SAVE_OVERLAY_PATH \( -type f -o -type l \) $find_filter | sed \
                -e 's,^\.,,' \
                -e '\,^/etc/board.json$,d' \
                -e '\,/[^/]*-opkg$,d' \
                -e '\,^/etc/urandom.seed$,d' \
-       )> "$file"
+               -e '\,^/usr/lib/opkg/.*,d' \
+       ) | grep -v -x -F -f $packagesfiles > "$file"
+
+       rm -f "$packagesfiles"
+
        return 0
 }