Backing Up tar Archives Over ssh

OpenSSH ( provides tools to securely do remote login, remote execution, and remote file copy over network interfaces. By setting up two machines to share encryption keys, you can transfer files between those machines without entering passwords for each transmission. That fact lets you create scripts to back up your data from an SSH client to an SSH server, without any manual intervention.

From a central Linux system, you can gather backups from multiple client machines using OpenSSH commands. The following example runs the tar command on a remote site (to archive and compress the files), pipes the tar stream to standard output, and uses the ssh command to catch the backup locally (over ssh) with tar:

$ mkdir mybackup ; cd mybackup
$ ssh marvin@server1 ‘tar cf – myfile*’ | tar xvf –
marvin@server1’s password: ******

In the example just shown, all files beginning with myfile are copied from the home directory of marvin on server1 and placed in the current directory. Note that the left side of the pipe creates the archive and the right side expands the files from the archive to the current directory. (Keep in mind that ssh will overwrite local files if they exist, which is why we created an empty directory in the example.)

To reverse the process and copy files from the local system to the remote system, we run a local tar command first. This time, however, we add a cd command to put the files in the directory of our choice on the remote machine:

$ tar cf – myfile* | ssh marvin@server1 \
‘cd /home/marvin/myfolder; tar xvf -’
marvin@server1’s password: ******

In this next example, we’re not going to untar the files on the receiving end, but instead write the results to tgz files:

$ ssh marvin@server1 ‘tar czf – myfile*’ | cat > myfiles.tgz
$ tar cvzf – myfile* | ssh marvin@server1 ‘cat > myfiles.tgz’

The first example takes all files beginning with myfile from the marvin user’s home directory on server1, tars and compresses those files, and directs those compressed files to the myfiles.tgz file on the local system. The second example does the reverse by taking all files beginning with myfile in the local directory and sending them to a myfiles.tgz file on the remote system.

The examples just shown are good for copying files over the network. Besides providing compression they also enable you to use any tar features you choose, such as incremental backup features.


About msotela

This blog is for anyone who wants to access the power of a Linux system as a systems administrator or user. You may be a Linux enthusiast, a Linux professional, or possibly a computer professional who is increasingly finding the Windows systems in your data center supplanted by Linux boxes.

Posted on September 23, 2009, in Unix/Linux. Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: