Compare commits

...

7 commits

10 changed files with 222 additions and 35 deletions

View file

@ -2,8 +2,10 @@
set -eu
XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/local/share:/usr/share}"
_="${XDG_DATA_HOME:=$HOME/.local/share}"
_="${XDG_DATA_DIRS:=/usr/local/share:/usr/share}"
_="${XDG_CONFIG_HOME:=$HOME/.config}"
_="${XDG_CONFIG_DIRS:=/etc}"
TARBACK_REMOTE_DEFAULT=
TARBACK_TRANSFORM_REMOTE_COMMAND_DEFAULT=
@ -22,7 +24,19 @@ TARBACK_COMPRESSION_DEFAULT='xz'
TARBACK_SPLIT_DEFAULT="split -b $((128*1024*1024)) -"
TARBACK_MERGE_DEFAULT='cat'
TARBACK_PLUGIN_SEARCH_PATH_DEFAULT="$(dirname "$0"):$XDG_DATA_HOME:$XDG_DATA_DIRS"
TARBACK_CONFIG_SEARCH_PATH_DEFAULT="$(dirname "$0"):$XDG_CONFIG_HOME:$XDG_CONFIG_DIRS"
_="${TARBACK_PLUGIN_SEARCH_PATH:=$TARBACK_PLUGIN_SEARCH_PATH_DEFAULT}"
_="${TARBACK_CONFIG_SEARCH_PATH:=$TARBACK_CONFIG_SEARCH_PATH_DEFAULT}"
TARBACK_PLUGIN_SEARCH_PATH="$(echo "$TARBACK_PLUGIN_SEARCH_PATH" | tr ':' '\n')"
TARBACK_CONFIG_SEARCH_PATH="$(echo "$TARBACK_CONFIG_SEARCH_PATH" | tr ':' '\n' | tac)"
# load configs
for config_dir in $TARBACK_CONFIG_SEARCH_PATH; do
config_file="$config_dir/tarback/config.sh"
[ ! -f "$config_file" ] && continue
. "$config_file"
done
# example: TARBACK_REMOTE="ssh myuser@myserver" ./tarback ...
TARBACK_REMOTE="${TARBACK_REMOTE:-$TARBACK_REMOTE_DEFAULT}"
@ -39,16 +53,22 @@ _="${TARBACK_TAR_EXTRACT_USE_ALT_COMMAND:=$TARBACK_TAR_EXTRACT_USE_ALT_COMMAND_D
_="${TARBACK_COMPRESSION:=$TARBACK_COMPRESSION_DEFAULT}"
_="${TARBACK_SPLIT:=$TARBACK_SPLIT_DEFAULT}"
_="${TARBACK_MERGE:=$TARBACK_MERGE_DEFAULT}"
_="${TARBACK_PLUGIN_SEARCH_PATH:=$TARBACK_PLUGIN_SEARCH_PATH_DEFAULT}"
TARBACK_PLUGIN_SEARCH_PATH="$(echo "$TARBACK_PLUGIN_SEARCH_PATH" | tr ':' '\n')"
_="${TARBACK_EXTENSION:=tar.$TARBACK_COMPRESSION}"
_transform_split_name() {
case "$1" in
*"$TARBACK_EXTENSION")
n="$1"
;;
*)
n="$1.$TARBACK_EXTENSION"
;;
esac
command_name="$(echo "$TARBACK_SPLIT" | awk '{print $1}')"
if echo "$command_name" | grep -q 'split'; then
echo "$1.part"
else
echo "$1"
n="$n.part"
fi
echo "$n"
}
_transform_remote() {
@ -105,17 +125,25 @@ restore() {
_restore "$@"
}
short_options='p:'
short_options='P:'
while getopts "$short_options" arg; do
case "$arg" in
p)
P)
for p in $(echo "$OPTARG" | tr ',' ' '); do
# source plugin file(s)
sourced=false
for loc in $TARBACK_PLUGIN_SEARCH_PATH; do
if [ -e "$loc/tarback/$p.sh" ]; then
# shellcheck disable=SC1090 # disable cannot follow source
old_short_options="$short_options"
old_arg="$arg"
old_OPTIND="$OPTIND"
old_OPTARG="$OPTARG"
. "$loc/tarback/$p.sh"
short_options="$old_short_options"
arg="$old_arg"
OPTIND="$old_OPTIND"
OPTARG="$old_OPTARG"
sourced=true
break
fi
@ -125,7 +153,6 @@ while getopts "$short_options" arg; do
exit 1
fi
done
shift 2
;;
*)
echo "unknown option $1" >&2
@ -134,6 +161,8 @@ while getopts "$short_options" arg; do
esac
done
shift $((OPTIND-1))
case "$1" in
create)
shift

View file

@ -1,7 +1,25 @@
#!/bin/sh
_tarback_plugin_docker_cmd='docker run -i --rm --workdir=/mnt --volume="$1":/mnt/"$1" alpine '
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"'
_tarback_plugin_docker_use_container=
short_options='c:'
OPTIND=1
while [ "${TARBACK_DOCKER_ARGS:-}" != '' ] && getopts "$short_options" arg ${TARBACK_DOCKER_ARGS:-}; do
case "$arg" in
c)
_tarback_plugin_docker_use_container="$OPTARG"
;;
esac
done
if [ -n "$_tarback_plugin_docker_use_container" ]; then
_tarback_plugin_docker_cmd="docker exec -i '$_tarback_plugin_docker_use_container' "
TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_docker_cmd $TARBACK_TAR_CREATE_COMMAND"
TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_docker_cmd $TARBACK_TAR_EXTRACT_COMMAND"
else
_tarback_plugin_docker_cmd='docker run -i --rm --workdir=/mnt --volume="$1":/mnt/"$1" alpine '
TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_docker_cmd $(echo "$TARBACK_TAR_CREATE_COMMAND" | sed 's_$1_/mnt/$1_g')"
TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_docker_cmd $(echo "$TARBACK_TAR_EXTRACT_COMMAND" | sed 's_$1_/mnt/$1_g')"
fi
TARBACK_TAR_CREATE_USE_ALT_COMMAND=false
TARBACK_TAR_EXTRACT_USE_ALT_COMMAND=false

38
tarback/postgres.sh Normal file
View file

@ -0,0 +1,38 @@
#!/bin/sh
_tarback_plugin_postgres_host=
_tarback_plugin_postgres_port=
_tarback_plugin_postgres_user=
_tarback_plugin_postgres_password=
short_options='h:p:U:w:'
OPTIND=1
while [ "${TARBACK_POSTGRES_ARGS:-}" != '' ] && getopts "$short_options" arg ${TARBACK_POSTGRES_ARGS:-}; do
case "$arg" in
h)
_tarback_plugin_postgres_host="$OPTARG"
;;
p)
_tarback_plugin_postgres_port="$OPTARG"
;;
U)
_tarback_plugin_postgres_user="$OPTARG"
;;
w)
_tarback_plugin_postgres_password="$OPTARG"
;;
esac
done
_tarback_plugin_postgres_command=
[ -n "$_tarback_plugin_postgres_host" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -h '$_tarback_plugin_postgres_host'"
[ -n "$_tarback_plugin_postgres_port" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -p '$_tarback_plugin_postgres_port'"
[ -n "$_tarback_plugin_postgres_user" ] && _tarback_plugin_postgres_command="$_tarback_plugin_postgres_command -U '$_tarback_plugin_postgres_user'"
_tarback_plugin_postgres_create_command="PGPASSWORD='$_tarback_plugin_postgres_password' pg_dump --clean $_tarback_plugin_postgres_command "'"$1"'
_tarback_plugin_postgres_extract_command="PGPASSWORD='$_tarback_plugin_postgres_password' psql $_tarback_plugin_postgres_command"
TARBACK_TAR_CREATE_COMMAND="$_tarback_plugin_postgres_create_command"
TARBACK_TAR_EXTRACT_COMMAND="$_tarback_plugin_postgres_extract_command"
TARBACK_EXTENSION='xz'
TARBACK_TAR_CREATE_USE_ALT_COMMAND=false
TARBACK_TAR_EXTRACT_USE_ALT_COMMAND=false

View file

@ -18,16 +18,12 @@ _tarback_plugin_ssh_transform_ssh_argument() {
fi
}
short_options='p:s:'
while getopts "$short_options" arg; do
short_options='s:'
OPTIND=1
while [ "${TARBACK_SSH_ARGS:-}" != '' ] && getopts "$short_options" arg ${TARBACK_SSH_ARGS:-}; do
case "$arg" in
s)
TARBACK_REMOTE="$OPTARG"
shift 2
;;
*)
echo "unknown option $1" >&2
#exit 1
;;
esac
done

View file

@ -6,7 +6,9 @@ early_return="${TARBACK_TEST_EARLY_RETURN:-false}"
failed=0
for f in ./tests/test-*.sh; do
printf '%s' "Running test $f ... "
if "$f"; then
if "$f" 1>"$f.stdout.log" 2>"$f.stderr.log"; then
rm "$f.stdout.log"
rm "$f.stderr.log"
echo "succeeded"
else
failed=$((failed+1))
@ -15,6 +17,7 @@ for f in ./tests/test-*.sh; do
break
fi
fi
find ./tests/ -type f -size 0 -delete
done
if [ $failed -ne 0 ]; then

View file

@ -8,24 +8,24 @@ export TARBACK_REMOTE='ssh localhost'
# archive whole directory
workdir="$(create_directories)"
./tarback.sh -p ssh create "$workdir/source" "$workdir/dest/source.tar.xz"
./tarback.sh -p ssh restore "$workdir/dest/source.tar.xz" "$workdir/restore"
./tarback.sh -P ssh create "$workdir/source" "$workdir/dest/source.tar.xz"
./tarback.sh -P ssh 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 ssh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
./tarback.sh -p ssh restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
./tarback.sh -P ssh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
./tarback.sh -P ssh 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 ssh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -p ssh restore "$workdir/dest/source.tar.xz" "$workdir/restore/$filename"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -P ssh create "$workdir/source/$filename" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT="split -b 10 -" ./tarback.sh -P ssh 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"

View file

@ -8,10 +8,10 @@ set -eu
workdir="$(create_directories 127)"
filename="$(basename "$(find "$workdir/source" -type f)")"
docker_volume="$(create_docker_volume "$workdir" "$filename")"
./tarback.sh -p docker create "$docker_volume" "$workdir/dest/source.tar.xz"
./tarback.sh -P docker create "$docker_volume" "$workdir/dest/source.tar.xz"
docker volume rm "$docker_volume" 2>/dev/null 1>&2
docker_volume="$(create_docker_volume)"
./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
./tarback.sh -P docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
extract_docker_volume "$docker_volume" "$workdir" "$filename"
diff "$workdir/source" "$workdir/restore"
docker volume rm "$docker_volume" 2>/dev/null 1>&2
@ -20,10 +20,10 @@ rm -rf "$workdir"
workdir="$(create_directories 127)"
filename="$(basename "$(find "$workdir/source" -type f)")"
docker_volume="$(create_docker_volume "$workdir" "$filename")"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p docker create "$docker_volume" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -P docker create "$docker_volume" "$workdir/dest/source.tar.xz"
docker volume rm "$docker_volume" 2>/dev/null 1>&2
docker_volume="$(create_docker_volume)"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -P docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
extract_docker_volume "$docker_volume" "$workdir" "$filename"
diff "$workdir/source" "$workdir/restore"
docker volume rm "$docker_volume" 2>/dev/null 1>&2

View file

@ -10,10 +10,10 @@ export TARBACK_REMOTE='ssh localhost'
workdir="$(create_directories 127)"
filename="$(basename "$(find "$workdir/source" -type f)")"
docker_volume="$(create_docker_volume "$workdir" "$filename")"
./tarback.sh -p ssh,docker create "$docker_volume" "$workdir/dest/source.tar.xz"
./tarback.sh -P ssh,docker create "$docker_volume" "$workdir/dest/source.tar.xz"
docker volume rm "$docker_volume" 2>/dev/null 1>&2
docker_volume="$(create_docker_volume)"
./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
./tarback.sh -P ssh,docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
extract_docker_volume "$docker_volume" "$workdir" "$filename"
diff "$workdir/source" "$workdir/restore"
docker volume rm "$docker_volume" 2>/dev/null 1>&2
@ -22,10 +22,10 @@ rm -rf "$workdir"
workdir="$(create_directories 127)"
filename="$(basename "$(find "$workdir/source" -type f)")"
docker_volume="$(create_docker_volume "$workdir" "$filename")"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p ssh,docker create "$docker_volume" "$workdir/dest/source.tar.xz"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -P ssh,docker create "$docker_volume" "$workdir/dest/source.tar.xz"
docker volume rm "$docker_volume" 2>/dev/null 1>&2
docker_volume="$(create_docker_volume)"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -p ssh,docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
TARBACK_SPLIT='split -b 10 -' ./tarback.sh -P ssh,docker restore "$workdir/dest/source.tar.xz" "$docker_volume"
extract_docker_volume "$docker_volume" "$workdir" "$filename"
diff "$workdir/source" "$workdir/restore"
docker volume rm "$docker_volume" 2>/dev/null 1>&2

51
tests/test-004-postgres.sh Executable file
View file

@ -0,0 +1,51 @@
#!/bin/sh
set -eu
. ./tests/common.sh
postgres_container_name="postgres-test-$(uuidgen)"
docker run --name "$postgres_container_name" -p 5432:5432 -e POSTGRES_PASSWORD=password123 -d postgres
trap "docker rm -f '$postgres_container_name'; exit" EXIT
for _ in $(seq 1 10); do
if echo '' | PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres; then
break
fi
sleep 1
done
PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
create table a(
i int
);
insert into a(i) values (1), (2), (3), (4);
EOF
d="$(mktemp -d)"
TARBACK_POSTGRES_ARGS='-h 10.1.0.100 -U postgres -p 5432 -w password123' ./tarback.sh -P postgres create postgres "$d/dump"
PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
insert into a(i) values (6), (7)
EOF
TARBACK_POSTGRES_ARGS='-h 10.1.0.100 -U postgres -p 5432 -w password123' ./tarback.sh -P postgres restore "$d/dump" postgres
out="$(PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
select i from a;
EOF
)"
out="$(echo "$out" | tail -n+3)"
expected_out="$(cat <<EOF
1
2
3
4
(4 rows)
EOF
)"
if [ "$out" != "$expected_out" ]; then
echo "$out" | xxd
echo "$expected_out" | xxd
exit 1
fi
rm -r "$d"

52
tests/test-005-ssh-postgres.sh Executable file
View file

@ -0,0 +1,52 @@
#!/bin/sh
set -eu
. ./tests/common.sh
export TARBACK_REMOTE='ssh localhost'
postgres_container_name="postgres-test-$(uuidgen)"
docker run --name "$postgres_container_name" -p 5432:5432 -e POSTGRES_PASSWORD=password123 -d postgres
trap "docker rm -f '$postgres_container_name'; exit" EXIT
for _ in $(seq 1 10); do
if echo '' | PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres; then
break
fi
sleep 1
done
PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
create table a(
i int
);
insert into a(i) values (1), (2), (3), (4);
EOF
d="$(mktemp -d)"
TARBACK_POSTGRES_ARGS='-h 10.1.0.100 -U postgres -p 5432 -w password123' ./tarback.sh -P ssh,postgres create postgres "$d/dump"
PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
insert into a(i) values (6), (7)
EOF
TARBACK_POSTGRES_ARGS='-h 10.1.0.100 -U postgres -p 5432 -w password123' ./tarback.sh -P ssh,postgres restore "$d/dump" postgres
out="$(PGPASSWORD=password123 psql -h 10.1.0.100 -U postgres -p 5432 postgres <<EOF
select i from a;
EOF
)"
out="$(echo "$out" | tail -n+3)"
expected_out="$(cat <<EOF
1
2
3
4
(4 rows)
EOF
)"
if [ "$out" != "$expected_out" ]; then
echo "$out" | xxd
echo "$expected_out" | xxd
exit 1
fi
rm -r "$d"