Example code to rsync

| | 2 min read

rsync command is used to synchronize files and folder from source to destination. It may be same or different server. If the synchronization is in between different servers we have to use ssh credentials for connecting the two.

Example given below:


rsync -ravpzHogt --delete -e ssh {source-path} {sshuser_id}@{ip/site-address}:{destination-path}

Some of the options of rsync listed below.

r - rsync to copy directories recurssively.

a - archive - presave almost anything but does not presave the hardlinks. For that we use -H.

v - verbose, using this option we can see which files is transferring at that time.

p - permission, this option helps to update the permission of remote file is same as source file permission.

z - compress, this option helps to compress any data in files and sends it to destination.

H - Hardlink, recreate hard links on the remote system to be the same as the local system. If we doesnot use this option,
the hardlinks treated as normal files.

o -Using the option, to set the owner of the files to be same as source file.

g - group, rsync to set the group of destination file same as the source file.
This helps when the receiving program is not run by super user only groups have option to sent and recieving the data.

t - time, rsync the modification time along with files and folder and is updated on remote server.

--delete - This option helps to delete files from destination folder which is not present in source directory.

-e - command, This option allows to choose alternative remote shell program for rsync the files from source to destination server.

You can run this code as terminal command. Hope this article is useful.

Please get in touch with us to know more.