Using tar over ssh leads to gzip error in some cases

Viewed 26

I am trying to write a bash script to retrieve files over ssh using a tar archive.

If I do it like this it works fine:

function sshGetFiles()
{
    tar --create --gzip --file - "file1" "file2" "file3"
}
ssh user@server "sshGetFiles" | tar --extract --gzip --file - --directory .

But I want to add an error message and not tar anything in case the files or directories don't exist. So I did it like this:

function sshGetFiles()
{
    if ! [[ -f "file1" && -f "file2" && -f "file3" ]]; then
         echo "error, some files don't exist"
         exit 1
    fi
    tar --create --gzip --file - "file1" "file2" "file3"
}
result=$(ssh user@server "sshGetFiles")
if [[ $? -ne 0 ]]; then
    echo "${result}"
    exit 1
fi

And now for the cases where the files do exist, I tried to handle it in several ways, none of which worked:

echo "${result}" | tar --extract --gzip --file - --directory .
echo -n "${result}" | tar --extract --gzip --file - --directory .
tar --extract --gzip --file - --directory . <<< "${result}"

I have this kind of errors:

gzip: stdin has flags 0xc9 -- not supported
gzip: stdin is a a multi-part gzip file -- not supported

Why is it not working and how can I make it work ?

1 Answers

bash variables cannot hold binary data. In particular, they cannot contain null bytes. Your data is being corrupted by storing it in a variable.

You should save the output to a file.

Related