2011-02-04

ssh - How to pipe output from local to remote server

In Bash, with pipe (the "|" symbol) and ssh makes a good combination of server administration tool, securely. Some examples :










  • file backup from local to remote server :
    tar zcf - /some/directory | ssh username@server "cat > backup.tar.gz"
    or same thing, using dump instead :
    tar zcf - /some/directory | ssh username@server "dd of=filename.bz2"
  • output from local to remote server :
    echo "some text" | ssh username@example.com "cat > somefile.txt"
    or
    cat ~/.ssh/id_rsa.pub | ssh username@example.com "cat >> ~/.ssh/authorized_keys"
  • backup local mysql database to remote server :
    mysqldump --opt database_name | gzip -c | ssh username@example.com "cat > /remote/dir/DB_backup.gz"
    or using tar to backup database :
    mysqldump | tar cf - | gzip -c | ssh username@example.com "cat > /remote/dir/DB_backup.tar.gz"
  • execute commands on remote server :
    ssh username@example.com "uname -a"
    or execute commands on remote server but save the output to local :
    ssh user@example.com "mysqldump -u DB_username -pDB_password DB_name | gzip -c" > /local/dir/DB_backup.gz
    *** the "-p" parameter of "mysqldump" needs the value immediately after the parameter, no space.

These are just a few ways to make use of bash & ssh. You have any?

Nemaste !!!

5 comments:

dave said...

I think you meant "tar zcf" for the first 2 examples. Also, the gzip in the pipeline is redundant.

PsyberMonkey said...

Hi dave, thank you for the correction! :)

Patrick said...

Hi

Someone know how to do this with ssh ...?

extundelete return files recovered creating a folder structure on the actual folder where you execute the command.
I want to recover files from a remote host and need that extundelete bring it to my client computer. How to do that?
Thank you

VJ Office said...

Was doing some bash scripting and had almost forgotten command for piping to remote server via ssh and your post helped me recollect it! Thanks for uploading for the posterity :-)

VJ Office said...

I was doing some bash scripting and had almost forgotten command to pipe output to remote server via ssh. Your post helped me to recollect!
Thanks for uploading it for the posterity :-)