From 8c61bd4395811888d4687014ef3cdbaa3d9508b4 Mon Sep 17 00:00:00 2001 From: lucianposton Date: Thu, 2 Apr 2020 21:10:09 -0500 Subject: [PATCH] Add -i option to include files prior to exclusion (#30) --- mkstage4.sh | 18 ++++++++++++++---- tests/0008.bats | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 tests/0008.bats diff --git a/mkstage4.sh b/mkstage4.sh index 8547805..5e6b67c 100755 --- a/mkstage4.sh +++ b/mkstage4.sh @@ -12,6 +12,7 @@ EXCLUDE_CONFIDENTIAL=0 EXCLUDE_LOST=0 QUIET=0 USER_EXCL=() +USER_INCL=() S_KERNEL=0 PARALLEL=0 HAS_PORTAGEQ=0 @@ -22,20 +23,21 @@ then fi USAGE="usage:\n\ - $(basename "$0") [-q -c -b -l -k -p] [-s || -t ] [-e ] [custom-tar-options]\n\ + $(basename "$0") [-q -c -b -l -k -p] [-s || -t ] [-e ] [-i ] [custom-tar-options]\n\ -q: activates quiet mode (no confirmation).\n\ -c: excludes some confidential files (currently only .bash_history and connman network lists).\n\ -b: excludes boot directory.\n\ -l: excludes lost+found directory.\n\ -p: compresses parallelly using pbzip2.\n\ -e: an additional excludes directory (one dir one -e, donot use it with *).\n\ + -i: an additional target to include. This has higher precedence than -e, -t, and -s.\n\ -s: makes tarball of current system.\n\ -k: separately save current kernel modules and src (smaller & save decompression time).\n\ -t: makes tarball of system located at the .\n\ -h: displays help message." # reads options: -while getopts ":t:e:skqcblph" flag +while getopts ":t:e:i:skqcblph" flag do case "$flag" in t) @@ -62,6 +64,9 @@ do e) USER_EXCL+=("--exclude=${OPTARG}") ;; + i) + USER_INCL+=("${OPTARG}") + ;; p) PARALLEL=1 ;; @@ -156,6 +161,11 @@ EXCLUDES_DEFAULT_PORTAGE=( EXCLUDES+=("${USER_EXCL[@]}") +INCLUDES=( +) + +INCLUDES+=("${USER_INCL[@]}") + if [ "$TARGET" == '/' ] then EXCLUDES+=("--exclude=$(realpath "$STAGE4_FILENAME")") @@ -215,7 +225,7 @@ then echo "example: \$ $(basename "$0") -s /my-backup --exclude=/etc/ssh/ssh_host*" echo echo "COMMAND LINE PREVIEW:" - echo 'tar' "${TAR_OPTIONS[@]}" "${EXCLUDES[@]}" "${OPTIONS[@]}" -f "$STAGE4_FILENAME" "${TARGET}" + echo 'tar' "${TAR_OPTIONS[@]}" "${INCLUDES[@]}" "${EXCLUDES[@]}" "${OPTIONS[@]}" -f "$STAGE4_FILENAME" "${TARGET}" if ((S_KERNEL)) then echo @@ -230,7 +240,7 @@ fi # start stage4 creation: if [ "$AGREE" == 'yes' ] then - tar "${TAR_OPTIONS[@]}" "${EXCLUDES[@]}" "${OPTIONS[@]}" -f "$STAGE4_FILENAME" "${TARGET}" + tar "${TAR_OPTIONS[@]}" "${INCLUDES[@]}" "${EXCLUDES[@]}" "${OPTIONS[@]}" -f "$STAGE4_FILENAME" "${TARGET}" if ((S_KERNEL)) then tar "${TAR_OPTIONS[@]}" -f "$STAGE4_FILENAME.ksrc" "${TARGET}usr/src/linux-$(uname -r)" diff --git a/tests/0008.bats b/tests/0008.bats new file mode 100644 index 0000000..3908310 --- /dev/null +++ b/tests/0008.bats @@ -0,0 +1,47 @@ +#!/usr/bin/env bats + +load test_helper + +setup() { + f test/home/user/.dotfile + f test/home/user/.keep + f test/etc/ssh/key + f test/etc/ssh/config + f test/boot/boot + f test/boot/kernel + f test/mnt/5/media + mkstage4.sh \ + -i 'test/home/user/.keep' \ + -e 'user/.*' \ + -i 'test/etc/ssh/config' \ + -e 'ssh' \ + -b \ + -i 'test/boot/boot' \ + -i 'test/mnt/5' \ + -q -t test test +} + +teardown() { + rm -rf test test.tar.bz2 +} + +@test "-i 'test/home/user/.keep and -e 'user/.*'" { + assert_tar_excludes test/home/user/.dotfile + assert_tar_includes test/home/user/.keep +} + +@test "-i 'test/etc/ssh/config and -e 'ssh'" { + assert_tar_excludes test/etc/ssh/key + assert_tar_includes test/etc/ssh/config +} + +@test "-i 'test/boot/boot' and -b" { + assert_tar_excludes test/boot/kernel + assert_tar_includes test/boot/boot +} + +@test "-i 'test/mnt/5'" { + assert_tar_includes test/mnt/5/media +} + +# vim: ft=bash