Compare commits

..

2 commits

8 changed files with 128 additions and 9 deletions

View file

@ -64,6 +64,24 @@ _transform_remote() {
echo "$r" echo "$r"
} }
_transform_argument_ssh() {
command_name="$(echo "$TARBACK_REMOTE" | awk '{print $1}')"
if echo "$command_name" | grep -q 'ssh'; then
echo "'$(printf '%s' "$1" | sed "s/'/'\\\\''/g")'"
else
echo "$1"
fi
}
_transform_argument() {
a="$1"
for t in _transform_argument_ssh; do
[ -z "$t" ] && continue # skip empty transformers
a="$("$t" "$a")"
done
echo "$a"
}
_create() { _create() {
# $1 ... src # $1 ... src
# $2 ... dst # $2 ... dst
@ -75,7 +93,7 @@ _create() {
tarback_tar_create_command="$TARBACK_TAR_CREATE_COMMAND_FILE" tarback_tar_create_command="$TARBACK_TAR_CREATE_COMMAND_FILE"
fi fi
tarback_tar_create_command="$(_transform_remote "$tarback_tar_create_command")" tarback_tar_create_command="$(_transform_remote "$tarback_tar_create_command")"
$TARBACK_REMOTE sh -c "$tarback_tar_create_command" - "$src" \ $TARBACK_REMOTE sh -c "$tarback_tar_create_command" - "$(_transform_argument_ssh "$src")" \
| $TARBACK_COMPRESSION \ | $TARBACK_COMPRESSION \
| $TARBACK_SPLIT "$dst" | $TARBACK_SPLIT "$dst"
} }
@ -95,7 +113,7 @@ _restore() {
tarback_tar_extract_command="$(_transform_remote "$tarback_tar_extract_command")" tarback_tar_extract_command="$(_transform_remote "$tarback_tar_extract_command")"
$TARBACK_MERGE "$src"* \ $TARBACK_MERGE "$src"* \
| $TARBACK_COMPRESSION --decompress --stdout \ | $TARBACK_COMPRESSION --decompress --stdout \
| $TARBACK_REMOTE sh -c "$tarback_tar_extract_command" - "$dst" | $TARBACK_REMOTE sh -c "$tarback_tar_extract_command" - "$(_transform_argument_ssh "$dst")"
} }
restore() { restore() {

View file

@ -1,4 +1,5 @@
#!/bin/sh #!/bin/sh
TARBACK_TAR_CREATE_COMMAND='docker run -i --rm --workdir=/mnt --volume="$1":/mnt/"$1" alpine '"$TARBACK_TAR_CREATE_COMMAND" _tarback_plugin_docker_cmd='docker run -i --rm --workdir=/mnt --volume="$1":/mnt/"$1" alpine '
TARBACK_TAR_EXTRACT_COMMAND='docker run -i --rm --workdir=/mnt --volume="$1":/mnt/"$1" alpine '"$TARBACK_TAR_EXTRACT_COMMAND" TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_docker_cmd "'tar c -C "/mnt/$1" .'
TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_docker_cmd "'tar x -C "/mnt/$1"'

View file

@ -2,6 +2,7 @@
set -eu set -eu
early_return="${TARBACK_TEST_EARLY_RETURN:-false}"
failed=0 failed=0
for f in ./tests/test-*.sh; do for f in ./tests/test-*.sh; do
printf '%s' "Running test $f ... " printf '%s' "Running test $f ... "
@ -10,6 +11,9 @@ for f in ./tests/test-*.sh; do
else else
failed=$((failed+1)) failed=$((failed+1))
echo "failed" echo "failed"
if "$early_return"; then
break
fi
fi fi
done done

View file

@ -5,6 +5,9 @@ create_directories() {
mkdir -p "$workdir/source" mkdir -p "$workdir/source"
mkdir -p "$workdir/dest" mkdir -p "$workdir/dest"
mkdir -p "$workdir/restore" mkdir -p "$workdir/restore"
echo 'Hello World!' > "$workdir/source/hello_world.txt" # generate a filename with every possible byte in it to test
# escaping, the only character not included is `/` since that is
# illegal for filenames (obviously)
echo 'Hello World!' > "$workdir/source/$(for i in $(seq 0 255); do printf '%x' "$i" | xxd -r -p; done | tr -d '/')"
echo "$workdir" echo "$workdir"
} }

View file

@ -13,15 +13,17 @@ rm -rf "$workdir"
# archive single file # archive single file
workdir="$(create_directories)" workdir="$(create_directories)"
./tarback.sh create "$workdir/source/hello_world.txt" "$workdir/dest/source.tar.xz" filename="$(basename "$(find "$workdir/source" -type f)")"
./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/hello_world.txt" ./tarback.sh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore" diff "$workdir/source" "$workdir/restore"
rm -rf "$workdir" rm -rf "$workdir"
# split archive # split archive
workdir="$(create_directories)" workdir="$(create_directories)"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh create "$workdir/source/hello_world.txt" "$workdir/dest/source.tar.xz" filename="$(basename "$(find "$workdir/source" -type f)")"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/hello_world.txt" TARBACK_SPLIT="split -b 10 -" ./tarback.sh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore" diff "$workdir/source" "$workdir/restore"
[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ] [ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ]
rm -rf "$workdir" rm -rf "$workdir"

31
tests/test-001-ssh.sh Executable file
View file

@ -0,0 +1,31 @@
#!/bin/sh
set -eu
. ./tests/common.sh
export TARBACK_REMOTE='ssh localhost'
# 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)"
filename="$(basename "$(find "$workdir/source" -type f)")"
./tarback.sh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore"
rm -rf "$workdir"
# split archive
workdir="$(create_directories)"
filename="$(basename "$(find "$workdir/source" -type f)")"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore"
[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ]
rm -rf "$workdir"

29
tests/test-002-docker.sh Executable file
View file

@ -0,0 +1,29 @@
#!/bin/sh
set -eu
. ./tests/common.sh
# archive whole directory
workdir="$(create_directories)"
./tarback.sh -p docker create "$workdir/source" "$workdir/dest/source.tar.xz"
./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore"
diff "$workdir/source" "$workdir/restore"
rm -rf "$workdir"
# archive single file
workdir="$(create_directories)"
filename="$(basename "$(find "$workdir/source" -type f)")"
./tarback.sh -p docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore"
rm -rf "$workdir"
# split archive
workdir="$(create_directories)"
filename="$(basename "$(find "$workdir/source" -type f)")"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore"
[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ]
rm -rf "$workdir"

31
tests/test-003-ssh-docker.sh Executable file
View file

@ -0,0 +1,31 @@
#!/bin/sh
set -eu
. ./tests/common.sh
export TARBACK_REMOTE='ssh localhost'
# archive whole directory
workdir="$(create_directories)"
./tarback.sh -p docker create "$workdir/source" "$workdir/dest/source.tar.xz"
./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore"
diff "$workdir/source" "$workdir/restore"
rm -rf "$workdir"
# archive single file
workdir="$(create_directories)"
filename="$(basename "$(find "$workdir/source" -type f)")"
./tarback.sh -p docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore"
rm -rf "$workdir"
# split archive
workdir="$(create_directories)"
filename="$(basename "$(find "$workdir/source" -type f)")"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p docker create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
diff "$workdir/source" "$workdir/restore"
[ "$(ls -1 "$workdir/dest/"*.part* | wc -l)" -gt 1 ]
rm -rf "$workdir"