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 993

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 > 192.168.0.181

Server2: IP Address >  192.168.0.182

Task :

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

At Server 1: 192.168.0.181

[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: 192.168.0.182

[root@server2 html]# hostname

server2.kvit.in

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

Where:

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@192.168.0.182 “cat > /backup/wesite-backup.`date +%m-%d-%y`.tar.gz”

tar: Removing leading `/’ from member names

root@192.168.0.182’s password:

[root@server1 ~]#

At Server2:

[root@server2 ~]# cd /backup

[root@server2 backup]# ls

wesite-backup.06-27-16.tar.gz

[root@server2 backup]#

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

you can restore tar backup over ssh session:

# cd /
# ssh root@192.168.0.182 “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 – (192.168.0.181) –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 – 192.168.0.182  : at server 2

Run this command at server1

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

root@192.168.0.182’s password:

[root@server1 ~]#

Step 3: Upload Generated Public Keys to – 192.168.0.182

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

root@192.168.0.182’s password:

[root@server1 ~]#

Step 4: Set Permissions on – 192.168.0.182

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

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

[root@server1 ~]#

Step 5: Login from 192.168.0.182 to 192.168.0.182  Server without Password

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

[root@server1 ~]# ssh root@192.168.0.182

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

[root@server2 ~]#

Note:

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

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

SIMILAR ARTICLES

0 1236

0 897

NO COMMENTS

Leave a Reply

Required Captcha *