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 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 explode 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:

Comments

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.