File(s) Synchronize from Remote System in RHEL/CentOS

Rsync (Remote Sync) is a most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux/Unix systems. With the help of rsync command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.

Some advantages and features of Rsync command

  • It efficiently copies and sync files to or from a remote system.
  • Supports copying links, devices, owners, groups and permissions.
  • It’s faster than scp (Secure Copy) because rsync uses remote-update protocol which allows to transfer just the differences between two sets of files. First time, it copies the whole content of a file or a directory from source to destination but from next time, it copies only the changed blocks and bytes to the destination.
  • Rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.

Basic syntax of rsync command

# rsync options source destination

Some common options used with rsync commands

  1. -v : verbose
  2. -r : copies data recursively (but don’t preserve timestamps and permission while transferring data
  3. -a : archive mode, archive mode allows copying files recursively and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
  4. -z : compress file data
  5. -h : human-readable, output numbers in a human-readable format

Installing rsync in RHEL/CentOS

# yum install rsync

Copy/Sync a File on a Local Computer
This following command will sync a single file on a local machine from one location to another location. Here in this example, a file name backup.tar needs to be copied or synced to /tmp/backups/ folder.

# rsync -z -v -h backup.tar /tmp/backups/

In above example, you can see that if the destination is not already exists rsync will create a directory automatically for destination.

Copy/Sync a Directory on Local Computer
The following command will transfer or sync all the files of from one directory to a different directory in the same machine. Here in this example, /root/rpms contains some rpm package files and you want that directory to be copied inside /tmp/backups/ folder.

# rsync -a -v -z -h /root/rpms /tmp/backups/

Copy a Directory from Local Server to a Remote Server
This command will sync a directory from a local machine to a remote machine. For example: There is a folder in your local computer “dir” which contains some RPM packages and you want that local directory’s content send to a remote server, you can use following command.

# rsync -a -v -z dir/ root@192.168.0.101:/home/
root@192.168.0.101's password:

Copy/Sync a Remote Directory to a Local Machine
This command will help you sync a remote directory to a local directory. Here in this example, a directory /home/user/dir which is on a remote server is being copied in your local computer in /tmp/dir.

#rsync -avzh root@192.168.0.100:/home/user/dir /tmp/dir
root@192.168.0.100's password: