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 1320

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

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

[[email protected] html]# hostname

server2.kvit.in

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

Where:

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

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

[[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 – (192.168.0.181) –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 – 192.168.0.182  : 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 – 192.168.0.182

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

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

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

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

[[email protected] ~]#

Note:

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

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 “

SIMILAR ARTICLES

0 1592

0 1237

0 1131

NO COMMENTS

Leave a Reply