Monday, July 13, 2009

Backing Up tar Archives Over ssh

OpenSSH provides tools to securely do remote login, remote execution, and remote file copy over network . It also supports encryption keys so that you can use it on remote machisne wiht out entering passwords repeatedly.

tar can be used along with ssh so that you can create backups on/of remote machines quickly.
The following examples show how this can be achieved.

Create a back up directory and change to it.
$ mkdir backup ; cd backup

Use ssh and tar to copy files.

$ ssh fermi@ serverip ‘tar cf - myfile*’ | tar xvf -
fermi@serverip password: ******

The above command will copy all files beginning with myfile from the home
directory of fermi on serverip 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. (Be careful. ssh can overwrite files in the current directory with out asking you.)

On the otherhand if you want to copy files from the local system to the remote system, 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 fermi@serverip ‘cd /home/fermi/myfolder; tar xvf - ’
fermi@serverip password: ******

Now let us try to make tgz files at the recieveing end by compressing the tar archive.

$ ssh fermi@serverip ‘tar czf - myfile*’ | cat > myfiles.tgz
$ tar cvzf - myfile* | ssh fermi@serverip ‘cat > myfiles.tgz’

( Put the ip address of the server at all places where serverip is shown.)

No comments: