How to backup a directory over network via tar and ssh?

If you wish to make a backup of a file system between two Unix machines, you may use scp command.

scp {filename} {username@hostname}:{remote filename}

The scp command will copy file (or directory) one at a time uncompressed, so it will take a longer time to transfer files between two machines. On the other hand, if you use tar command, it will compress the file (or directory) before transferring so it will take less time to transfer. The time it will take to compress will be much less than the time it will take to transmit larger content.

tar cfz - {directory} | ssh {username@hostname} "cat > {remote file}"
example> tar cfz - /home |ssh root@hostname "cat > /home/home.tgz"
username@host's password:

You may also use dd in place of cat command.

tar cfz - {directory} | ssh {username@hostname} "dd of={remote file}"
example> tar cfz - /home |ssh root@hostname "dd of=/home/home.tgz"
username@host's password:

Finally, if you wish to explore the archive at the remote end, you may wish to combine the command.

tar cfz - {directory} | ssh {username@hostname} "(cd {directory}; tar xfz -)"
example> tar cfz - /home |ssh root@hostname "(cd /home; tar xfz -)"
username@host's password:

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment