Transfer Files by Rsync

rsync is a free software computer program for Unix and Linux like systems which synchronizes files and directories from one location to another while minimizing data transfer using delta encoding when appropriate. An important feature of rsync not found in most similar programs/protocols is that the mirroring takes place with only one transmission in each direction. This tool is very appropriate to keep backup or setup mirror service. In this article you will get some clear idea.

Transfer_Files_by_Rsync.png

It can perform differential uploads and downloads (synchronization) of files across the network, transferring only data that has changed. The rsync remote-update protocol allows rsync to transfer just the differences between two sets of files across the network connection.

Install:

If you are using Debian or Ubuntu Linux, type the following command:

apt-get install rsync

OR

sudo apt-get install rsync

If you are using Red Hat Enterprise Linux (RHEL) / CentOS 4.x or older version, type the following command:

up2date rsync

RHEL / CentOS 5.x or newer (or Fedora Linux) user type the following command:

yum install rsync

SSH-Keygen Generates Keys:

It is safe to use rsync over ssh. To safely use rsync and to be secured you can generate keys to use rsync without giving password. This key will help you to keep backup. For better knowledge you can visit my another post by clicking here.

ssh-keygen

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

 

Use remote machine ip to access without password.

ssh-copy-id -i ~/.ssh/id_rsa.pub "root@111.222.333.444 -p 22"

Tasks:

Here i will show you some tasks or examples.

#1. Copy file from a local computer to a remote server.

Copy file from /var/www/backup.tar.gz to a remote servers /var/www/ folder, called server1.domain.com

rsync -v -e ssh /var/www/backup.tar.gz root@server1.domain.com:~/var/www/

Please note that symbol ~ indicate the users home directory (/home/user).

#2. Copy file from a remote server to a local computer.

Copy file /var/www/backup.tar.gz from a remote server server1.domain.com to a local computer’s /var/www/ directory:

rsync -v -e ssh root@server1.domain.com:~/var/www/backup.tar.gz /var/www/

#3. Synchronize a local directory with a remote directory. (From local to server)

rsync -r -a -v -e "ssh -l root -p 22" --delete /var/www/ server1.domain.com:/var/www/

#4. Synchronize a remote directory with a local directory. (From server to local)

rsync -r -a -v -e "ssh -l root -p 22" --delete server1.domain.com:/var/www/ /var/www/

#5. Synchronize a local directory with a remote rsync server or vise-versa.

rsync -r -a -v --delete rsync://server1.domain.com/backup /var/www/backup

OR

rsync -r -a -v --delete /var/www/backup rsync://server1.domain.com/backup

#6. Mirror a directory between my “old” and “new” web server/ftp

You can mirror a directory between my “old” (server1.domain.com) and “new” web server with the command (assuming that ssh keys are set for password less authentication)

rsync -zavrR --delete --links --rsh="ssh -l root -p 22" server1.domain.com:/var/www /var/www

 

Backup Database:

Below command will be very helpful for backing up a database.

mysqldump --opt -Q -u root --password='password' dbname | gzip > /var/www/dbname.gz

Above command will backup the database by compressing that to gzip.