Quickest way to transfer files and folders between servers using tar over...

Quickest way to transfer files and folders between servers using tar over ssh (scp)

by -
0 1071

TAR over SSH

This is very useful when we transfer data over the internet,  SCP is usually slow to transfer data.  Using tar over ssh is better method to transfer.

Using this method effectively sends the compressed tarball to the terminal. You then pipe that into an ssh session which is running the extract version of the previous tar function along with the change directory argument. This, essentially, sends the compressed tarball into a decompression process at the other end over a secure ssh “pipe”.

Our scenario:

Server1:   IP Addess >

Server2: IP Address >

Task :

Transfer data from Server1 (  the folder /var/www/html  to server2 ( into folder /var/www/html folder with preserved permission

At Server 1:

[root@server1 ~]# cd /var/www/html

[root@server1 html]#

[root@server1 html]# ls

abc         all-free-download.com.url  css                device.map  index.html  tours.html
about.html  contacts.html              destinations.html  images      js

[root@server1 html]#

At Server2:

[root@server2 html]# hostname


[root@server2 html]#  ssh   root@ “tar czpf – /var/www/html” | tar xzpf – -C /


Now result will be >>

[root@server2 html]# cd /var/www/html

[root@server2 html]# ls

abc                        contacts.html      device.map  js

about.html                 css                images      tours.html

all-free-download.com.url  destinations.html  index.html

Now Backup using same Command:

At server 1:

[root@server1 ~]#

[root@server1 ~]# tar zcf – /var/www/html/ | ssh root@ “cat > /backup/wesite-backup.`date +%m-%d-%y`.tar.gz”

tar: Removing leading `/’ from member names

root@’s password:

[root@server1 ~]#

At Server2:

[root@server2 ~]# cd /backup

[root@server2 backup]# ls


[root@server2 backup]#

You can also use dd command for clarity purpose:
# tar cvzf – /var/www/html/ | ssh root@ “dd of=/backup/backup.tar.gz”

you can restore tar backup over ssh session:

# cd /
# ssh root@ “cat /backup/backup.tar.gz” | tar zxvf –

If you wish to use above command in cron job or scripts  (Schedule ) , we should use password less ssh between this two servers

Create password less ssh  between this two server , follow this five steps

Password Less SSH

Step 1:

Create Authentication SSH-Kegen Keys on – ( –at server1

[root@server1 ~]# ssh-keygen -t rsa

Generating public/private rsa key pair.

Enter file in which to save the key (/root/.ssh/id_rsa):

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /root/.ssh/id_rsa.

Your public key has been saved in /root/.ssh/id_rsa.pub.

The key fingerprint is:

71:46:eb:bd:af:77:39:b9:a7:79:f4:3a:5e:86:ce:98 root@server1.kvit.in

The key’s randomart image is:

+–[ RSA 2048]—-+

|          .      |

|         . .     |

|        . +      |

|         = .     |

|        S . .    |

|             . ..|

|            . ..*|

|             *oB=|

|            E+OB+|


[root@server1 ~]#

Step 2: Create .ssh Directory on –  : at server 2

Run this command at server1

[root@server1 ~]# ssh root@ mkdir -p .ssh

root@’s password:

[root@server1 ~]#

Step 3: Upload Generated Public Keys to –

[root@server1 ~]# cat .ssh/id_rsa.pub | ssh root@ ‘cat >> .ssh/authorized_keys’

root@’s password:

[root@server1 ~]#

Step 4: Set Permissions on –

Due to different SSH versions on servers, we need to set permissions on .ssh directory and authorized_keys file.

[root@server1 ~]# ssh root@ “chmod 700 .ssh; chmod 640 .ssh/authorized_keys”

[root@server1 ~]#

Step 5: Login from to  Server without Password

From now onwards you can log into  as root user from server  as root user without password.

[root@server1 ~]# ssh root@

Last login: Mon Jun 27 23:34:15 2016 from

[root@server2 ~]#


Do same procedure if you want to password less ssh to from

CEO, KV IT-Solutions Pvt. Ltd. | vikas@kvit.in | 9810028374|
Linux Professional and an Industrial Trainer | 20 + years Experience in IT Industry

” We are born free, No Gate and Windows can snatch our freedom “

Download PDF


0 1353

0 995

0 894


Leave a Reply

Required Captcha *