From 9aa896f5010f49f4154d063cbb028a069cb505f2 Mon Sep 17 00:00:00 2001 From: redxef Date: Thu, 8 Jun 2023 17:49:26 +0200 Subject: [PATCH] Add postgres plugin, do some cleanup. --- tarback.sh | 18 +++++++++++++----- tarback/postgres.sh | 32 ++++++++++++++++++++++++++++++++ tarback/ssh.sh | 6 +----- tests/test-001-ssh.sh | 12 ++++++------ tests/test-002-docker.sh | 8 ++++---- tests/test-003-ssh-docker.sh | 8 ++++---- 6 files changed, 60 insertions(+), 24 deletions(-) create mode 100644 tarback/postgres.sh diff --git a/tarback.sh b/tarback.sh index e529a72..a47b2fb 100755 --- a/tarback.sh +++ b/tarback.sh @@ -53,14 +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_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() { @@ -117,10 +125,10 @@ 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 diff --git a/tarback/postgres.sh b/tarback/postgres.sh new file mode 100644 index 0000000..70ff81f --- /dev/null +++ b/tarback/postgres.sh @@ -0,0 +1,32 @@ +#!/bin/sh + +_tarback_plugin_postgres_host= +_tarback_plugin_postgres_port= +_tarback_plugin_postgres_user= + +short_options='h:p:U:' +while getopts "$short_options" arg; do + case "$arg" in + h) + _tarback_plugin_postgres_host="$OPTARG" + ;; + p) + _tarback_plugin_postgres_port="$OPTARG" + ;; + U) + _tarback_plugin_postgres_user="$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="pg_dump $_tarback_plugin_postgres_command "'"$1"' +_tarback_plugin_postgres_extract_command="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 diff --git a/tarback/ssh.sh b/tarback/ssh.sh index f70a5fd..7d6163a 100644 --- a/tarback/ssh.sh +++ b/tarback/ssh.sh @@ -18,16 +18,12 @@ _tarback_plugin_ssh_transform_ssh_argument() { fi } -short_options='p:s:' +short_options='s:' while getopts "$short_options" arg; do case "$arg" in s) TARBACK_REMOTE="$OPTARG" ;; - *) - echo "unknown option $1" >&2 - #exit 1 - ;; esac done diff --git a/tests/test-001-ssh.sh b/tests/test-001-ssh.sh index bece3b5..94d5605 100755 --- a/tests/test-001-ssh.sh +++ b/tests/test-001-ssh.sh @@ -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" diff --git a/tests/test-002-docker.sh b/tests/test-002-docker.sh index 9f7b5cb..a40ab26 100755 --- a/tests/test-002-docker.sh +++ b/tests/test-002-docker.sh @@ -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 diff --git a/tests/test-003-ssh-docker.sh b/tests/test-003-ssh-docker.sh index f735438..517a7ee 100755 --- a/tests/test-003-ssh-docker.sh +++ b/tests/test-003-ssh-docker.sh @@ -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