Use tar and ssh to copy files and directories across systems

Snippet

To save on disk space, and be able to retain file permissions and directory structures, use tar and ssh to copy files and directories across systems.

This works better than scp because the files are compressed (gzipped) before sending across the wire.

Use if you have lots of small files (think, "git repositories").

# general case
$ ssh user@machine-where-precious-data-is "tar czpf - /some/important/data" | tar xzpf - -C /new/root/directory
 
# specific example
$  ssh mgs "sudo tar czpf - /home/sites" | tar xzpf - -C sites
 
# this will create sites/home/sites/...
 
# If you need root and (correctly) have root ssh login turned off, use this:
$ ssh remotehost "echo 'PASSWORD' | sudo -S tar czpf - /remote-path-to-copy" | tar xvpf - -C local-path
 
# back the other way
$ tar cpf - /path/to/data | ssh user@remote "tar xpf - -C /path/do/final/location/"