lib_bin/save.sh
2021-09-01 20:44:18 +01:00

65 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
USAGE="save.sh [-v (verbose)] list_files_to_save root_path_to_save"
[ "$1" == "-h" ] && echo "$USAGE" && exit 0
[ "$1" == "-v" ] && VERBOSE="v" && shift
LIST_TO_SAVE=${LIST_TO_SAVE:-list_to_save}
[ -z "$TARGET_PATH" ] && echo "TARGET_PATH not defined in enviroment" && exit 1
[ -n "$1" ] && [ -r "$1" ] && LIST_TO_SAVE=$1
[ -n "$2" ] && [ -r "$2" ] && TARGET_PATH=$2
[ ! -d "$TARGET_PATH" ] && echo "$TARGET_PATH for root save not found" && exit 1
# A manual method scanning path to get files
# Alternative method in one line:
# $(find "." -type f | grep -v "target/" | grep -v .git)
get_list() {
local path
for it in "$1"/??*
do
[[ "$it" =~ /node_modules ]] && continue
[[ "$it" =~ /target ]] && continue
[[ "$it" =~ /.git ]] && continue
if [ -d "$it" ] ; then
get_list "$it"
else
# Avoid use of sed (shellcheck SC2001)
path="${it//"$ROOT_PATH"//}"
# If no files in this path so copy path itself
[ "$(basename "$path")" == "??*" ] && path=$(dirname "$path")
echo "${path//\.\//}"
fi
done
# scan hidden files
for it in "$1"/.??*
do
[[ "$it" =~ /.git ]] && continue
[ "$(basename "$it")" == ".??*" ] && continue
echo "${it//\.\//}"
done
}
WK_FILE="/tmp/save.$$"
if [ ! -r "$LIST_TO_SAVE" ] ; then
echo "$LIST_TO_SAVE not defined"
echo "Using default list no 'target' no 'node_modules' no '.git'"
get_list "." > "$WK_FILE"
LIST="$WK_FILE"
else
LIST="$LIST_TO_SAVE"
fi
DEBUG=${DEBUG:-0}
now_is=$(date +%Y_%m_%d_%H%M%S)
SRC_PATH=$(basename "$PWD")
TARGET=$TARGET_PATH/$SRC_PATH"_$now_is.tar.gz"
echo "---------------------------"
line_info="$(wc -l "$LIST" | cut -f1 -d"/" | tr -d " " ) files"
[ -z "$VERBOSE" ] && echo "Saving $line_info ..."
if tar cz"$VERBOSE"f "$TARGET" --files-from "$LIST" ; then
[ -n "$VERBOSE" ] && echo -e "\n$line_info saved !"
echo -e "\nDone save $SRC_PATH to $TARGET"
fi
echo "---------------------------"
rm -f "$WK_FILE"