'Tar and Rsync in a single go using pipe

I want to create a zipped file with tar command and rsync the output file to remote machine. I have tried the following:

tar -cjvf etc.tar.bz /etc | rsync -r &1 user@remoteip:/home/user/

tar -cjvf etc.tar.bz /etc | rsync -r '{}' user@remoteip:/home/user/

tar -cjvf etc.tar.bz /etc | rsync -r '{}' 'user@remoteip:/home/user/'

None of them work. Please tell me how to do this.

Thanks.



Solution 1:[1]

You are piping the result of tar to rsync. It will not work. You can try:

tar -cjvf etc.tar.bz /etc ; rsync etc.tar.bz user@remoteip:/home/user/

Or If you send directly the file through the network:

tar cjvf - /etc | ssh user@remoteip "cat > /home/user/etc.tar.bz"

Solution 2:[2]

You can use this command to pipe directly to tar on the other side

tar cjvf - /etc | ssh user@hostname "tar xvjf - --keep-newer-files"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cyrus
Solution 2 Abhijit