We help IT Professionals succeed at work.

Remotely creating a tar file via ssh

David Aldridge
David Aldridge asked
I'm not even sure I can do this, but I want to have a script that ssh's to another server and creates a tar file on the remote server.  Is that even possible?  Here's what I have been working with, which isn't very much because I can't figure out how to make it work.

Thanks in advance for any help!



ssh $server 'tar cvf $server.logs.tar.gz /opt/bmc/Patrol3/log/Patrol*'
Watch Question

Distinguished Expert 2017
Where do you want the file to end up?
To be compressed you are missing the z flag.
Not sure why you are including the v option...

The script/command you have needs to have a key/pair between the user under whose rights this script rubs and remote system

You might better off setting up the process as a cron job if the file is to be created locally.

If you want the file created on the remote system you need to modify the line as

ssh $server '/usr/bin/tar cf - /opt/bmc/Patrol3/log/Patrol' | /usr/bin/gzip -c > "/path/to/where/you/want/the/file/$server.logs.tgz"

If your tar include the compress, you could use the z (cfz) with the output directed into the file (cut out the |gzip -c

The same issue, the user and credential gave to use use keys, or it will become an interactive requiring a password.

IMHO, using ssh_PASSWD env variable is not ...since that variable has to be defined......


Modified your answer slightly by putting it in the background on the remote server, but you were spot on.