Add tests.
Fix single-file archiving.
This commit is contained in:
parent
91e9038bda
commit
482da4b833
5 changed files with 94 additions and 8 deletions
7
Makefile
7
Makefile
|
@ -3,8 +3,11 @@ SCRIPT := tarback.sh
|
|||
SCRIPT_NAME := $(SCRIPT:.sh=)
|
||||
PREFIX ?= /usr/local
|
||||
|
||||
install: $(PLUGINS)
|
||||
install: $(SCRIPT) $(PLUGINS)
|
||||
install -D -t $(PREFIX)/share/tarback -m 0644 -o root -g root $^
|
||||
install -D -m 0755 -o root -g root -T $(SCRIPT) $(PREFIX)/bin/$(SCRIPT_NAME)
|
||||
|
||||
.PHONY: install
|
||||
test: $(SCRIPT) $(PLUGINS)
|
||||
./test.sh
|
||||
|
||||
.PHONY: install test
|
||||
|
|
36
tarback.sh
36
tarback.sh
|
@ -8,9 +8,13 @@ XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
|
|||
TARBACK_REMOTE_DEFAULT=
|
||||
TARBACK_TRANSFORM_REMOTE_COMMAND_DEFAULT=
|
||||
# shellcheck disable=SC2016 # disable variable not expanding hint
|
||||
TARBACK_TAR_CREATE_COMMAND_DEFAULT='tar c -C "$(dirname "$1")" "$(basename "$1")"'
|
||||
TARBACK_TAR_CREATE_COMMAND_DEFAULT='tar c -C "$1" .'
|
||||
# shellcheck disable=SC2016 # disable variable not expanding hint
|
||||
TARBACK_TAR_EXTRACT_COMMAND_DEFAULT='tar x -C "$(dirname "$1")"'
|
||||
TARBACK_TAR_CREATE_COMMAND_FILE_DEFAULT='tar c -C "$(dirname "$1")" "$(basename "$1")"'
|
||||
# shellcheck disable=SC2016 # disable variable not expanding hint
|
||||
TARBACK_TAR_EXTRACT_COMMAND_DEFAULT='tar x -C "$1"'
|
||||
# shellcheck disable=SC2016 # disable variable not expanding hint
|
||||
TARBACK_TAR_EXTRACT_COMMAND_FILE_DEFAULT='tar x -C "$(dirname "$1")"'
|
||||
TARBACK_COMPRESSION_DEFAULT='xz'
|
||||
TARBACK_SPLIT_DEFAULT="split -b $((128*1024*1024)) -"
|
||||
TARBACK_MERGE_DEFAULT='cat'
|
||||
|
@ -23,7 +27,9 @@ TARBACK_REMOTE="${TARBACK_REMOTE:-$TARBACK_REMOTE_DEFAULT}"
|
|||
# remote command, see _transform_ssh_command for an example
|
||||
TARBACK_TRANSFORM_REMOTE_COMMAND="${TARBACK_TRANSFORM_REMOTE_COMMAND:-$TARBACK_TRANSFORM_REMOTE_COMMAND_DEFAULT}"
|
||||
TARBACK_TAR_CREATE_COMMAND="${TARBACK_TAR_CREATE_COMMAND:-$TARBACK_TAR_CREATE_COMMAND_DEFAULT}"
|
||||
TARBACK_TAR_CREATE_COMMAND_FILE="${TARBACK_TAR_CREATE_COMMAND_FILE:-$TARBACK_TAR_CREATE_COMMAND_FILE_DEFAULT}"
|
||||
TARBACK_TAR_EXTRACT_COMMAND="${TARBACK_TAR_EXTRACT_COMMAND:-$TARBACK_TAR_EXTRACT_COMMAND_DEFAULT}"
|
||||
TARBACK_TAR_EXTRACT_COMMAND_FILE="${TARBACK_TAR_EXTRACT_COMMAND_FILE:-$TARBACK_TAR_EXTRACT_COMMAND_FILE_DEFAULT}"
|
||||
TARBACK_COMPRESSION="${TARBACK_COMPRESSION:-$TARBACK_COMPRESSION_DEFAULT}"
|
||||
TARBACK_SPLIT="${TARBACK_SPLIT:-$TARBACK_SPLIT_DEFAULT}"
|
||||
TARBACK_MERGE="${TARBACK_MERGE:-$TARBACK_MERGE_DEFAULT}"
|
||||
|
@ -58,26 +64,44 @@ _transform_remote() {
|
|||
echo "$r"
|
||||
}
|
||||
|
||||
create() {
|
||||
_create() {
|
||||
# $1 ... src
|
||||
# $2 ... dst
|
||||
src="$1"
|
||||
dst="$(_transform_split_name "$2")"
|
||||
tarback_tar_create_command="$(_transform_remote "$TARBACK_TAR_CREATE_COMMAND")"
|
||||
if [ -d "$src" ]; then
|
||||
tarback_tar_create_command="$TARBACK_TAR_CREATE_COMMAND"
|
||||
else
|
||||
tarback_tar_create_command="$TARBACK_TAR_CREATE_COMMAND_FILE"
|
||||
fi
|
||||
tarback_tar_create_command="$(_transform_remote "$tarback_tar_create_command")"
|
||||
$TARBACK_REMOTE sh -c "$tarback_tar_create_command" - "$src" \
|
||||
| $TARBACK_COMPRESSION \
|
||||
| $TARBACK_SPLIT "$dst"
|
||||
}
|
||||
|
||||
restore() {
|
||||
create() {
|
||||
_create "$@"
|
||||
}
|
||||
|
||||
_restore() {
|
||||
src="$(_transform_split_name "$1")"
|
||||
dst="$2"
|
||||
tarback_tar_extract_command="$(_transform_remote "$TARBACK_TAR_EXTRACT_COMMAND")"
|
||||
if [ ! -e "$dst" ] && [ -d "$(dirname "$dst")" ]; then
|
||||
tarback_tar_extract_command="$TARBACK_TAR_EXTRACT_COMMAND_FILE"
|
||||
else
|
||||
tarback_tar_extract_command="$TARBACK_TAR_EXTRACT_COMMAND"
|
||||
fi
|
||||
tarback_tar_extract_command="$(_transform_remote "$tarback_tar_extract_command")"
|
||||
$TARBACK_MERGE "$src"* \
|
||||
| $TARBACK_COMPRESSION --decompress --stdout \
|
||||
| $TARBACK_REMOTE sh -c "$tarback_tar_extract_command" - "$dst"
|
||||
}
|
||||
|
||||
restore() {
|
||||
_restore "$@"
|
||||
}
|
||||
|
||||
short_options='p:'
|
||||
while getopts "$short_options" arg; do
|
||||
case "$arg" in
|
||||
|
|
22
test.sh
Executable file
22
test.sh
Executable file
|
@ -0,0 +1,22 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
failed=0
|
||||
for f in ./tests/test-*.sh; do
|
||||
printf '%s' "Running test $f ... "
|
||||
if "$f"; then
|
||||
echo "succeeded"
|
||||
else
|
||||
failed=$((failed+1))
|
||||
echo "failed"
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $failed -ne 0 ]; then
|
||||
echo "$failed tests failed"
|
||||
exit 1
|
||||
else
|
||||
echo "all succeeded"
|
||||
exit 0
|
||||
fi
|
10
tests/common.sh
Executable file
10
tests/common.sh
Executable file
|
@ -0,0 +1,10 @@
|
|||
#!/bin/sh
|
||||
|
||||
create_directories() {
|
||||
workdir="$(mktemp -d)"
|
||||
mkdir -p "$workdir/source"
|
||||
mkdir -p "$workdir/dest"
|
||||
mkdir -p "$workdir/restore"
|
||||
echo 'Hello World!' > "$workdir/source/hello_world.txt"
|
||||
echo "$workdir"
|
||||
}
|
27
tests/test-000-simple.sh
Executable file
27
tests/test-000-simple.sh
Executable file
|
@ -0,0 +1,27 @@
|
|||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
. ./tests/common.sh
|
||||
|
||||
# archive whole directory
|
||||
workdir="$(create_directories)"
|
||||
./tarback.sh create "$workdir/source" "$workdir/dest/source.tar.xz"
|
||||
./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore"
|
||||
diff "$workdir/source" "$workdir/restore"
|
||||
rm -rf "$workdir"
|
||||
|
||||
# archive single file
|
||||
workdir="$(create_directories)"
|
||||
./tarback.sh create "$workdir/source/hello_world.txt" "$workdir/dest/source.tar.xz"
|
||||
./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/hello_world.txt"
|
||||
diff "$workdir/source" "$workdir/restore"
|
||||
rm -rf "$workdir"
|
||||
|
||||
# split archive
|
||||
workdir="$(create_directories)"
|
||||
TARBACK_SPLIT="split -b 10 -" ./tarback.sh create "$workdir/source/hello_world.txt" "$workdir/dest/source.tar.xz"
|
||||
TARBACK_SPLIT="split -b 10 -" ./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/hello_world.txt"
|
||||
diff "$workdir/source" "$workdir/restore"
|
||||
[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ]
|
||||
rm -rf "$workdir"
|
Loading…
Reference in a new issue