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 1391

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:

[[email protected] ~]# cd /var/www/html

[[email protected] html]#

[[email protected] html]# ls

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

[[email protected] html]#

At Server2:

[[email protected] html]# hostname


[[email protected] html]#  ssh   [email protected] “tar czpf – /var/www/html” | tar xzpf – -C /


Now result will be >>

[[email protected] html]# cd /var/www/html

[[email protected] 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:

[[email protected] ~]#

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

tar: Removing leading `/’ from member names

[email protected]’s password:

[[email protected] ~]#

At Server2:

[[email protected] ~]# cd /backup

[[email protected] backup]# ls


[[email protected] backup]#

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

you can restore tar backup over ssh session:

# cd /
# ssh [email protected] “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

[[email protected] ~]# 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 [email protected]

The key’s randomart image is:

+–[ RSA 2048]—-+

|          .      |

|         . .     |

|        . +      |

|         = .     |

|        S . .    |

|             . ..|

|            . ..*|

|             *oB=|

|            E+OB+|


[[email protected] ~]#

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

Run this command at server1

[[email protected] ~]# ssh [email protected] mkdir -p .ssh

[email protected]’s password:

[[email protected] ~]#

Step 3: Upload Generated Public Keys to –

[[email protected] ~]# cat .ssh/id_rsa.pub | ssh [email protected] ‘cat >> .ssh/authorized_keys’

[email protected]’s password:

[[email protected] ~]#

Step 4: Set Permissions on –

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

[[email protected] ~]# ssh [email protected] “chmod 700 .ssh; chmod 640 .ssh/authorized_keys”

[[email protected] ~]#

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.

[[email protected] ~]# ssh [email protected]

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

[[email protected] ~]#


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

Download PDF

CEO, KV IT-Solutions Pvt. Ltd. | [email protected] | 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 “


0 1700

0 1354

0 1218


Leave a Reply