We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

Learn how to – How to use rsync command on Linux/Unix with examples

Learn how to – How to use rsync command on Linux/Unix with examples.

Introduction

From the man page of rsync, Rsync is a fast and extraordinarily versatile file copying tool. It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. It offers a large number of options that control every aspect of its behavior and permit very flexible specification of the set of files to be copied. It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.

Rsync is widely used for backups and mirroring and as an improved copy command for everyday use. It finds files that need to be transferred using a “quick check” algorithm (by default) that looks for files that have changed insize orin last-modified time. Any changes in the other preserved attributes (as requested by options) are made on the destination file directly when the quick check indicates that the file’s data does not need to be updated.

More features of Rsync include:

  • Support for copying links, devices, owners, groups, and permissions
  • Exclude and exclude-from options similar to GNU tar
  • A CVS exclude mode for ignoring the same files that CVS would ignore
  • Can use any transparent remote shell, including ssh or rsh
  • Does not require super-user privileges
  • Pipelining of file transfers to minimize latency costs
  • Support for anonymous or authenticated rsync daemons (ideal for mirroring)

At a high level, below is the syntax when using rsync

Using it on the Local machine/computer:

rsync [OPTION…] … 

Using it via remote shell:

Pulling files from remote host:

rsync [OPTION…] [USER@]REMOTE-HOST:… 

Pushing files from local to remote host:

rsync [OPTION…] … [USER@]REMOTE-HOST:

Most common options for rsync

  • -v, –verbose increase verbosity
  • -q, –quiet suppress non-error messages
  • -r, –recursive recurse into directories
  • -o, –owner preserve owner (super-user only)
  • –progress show progress during transfer
  • -z, –compress compress file data during the transfer
  • -H, –hard-links preserve hard links
  • -h, –human-readable output numbers in a human-readable format
  • -W, –whole-file copy files whole. rsync copies incrementally by default. This option forces it to copy the whole file.
  • –delete Delete files in the destination but missing in source
  • -e, specify the remote shell to use e.g ssh
  • –max-size=SIZE don’t transfer any file larger than SIZE
  • –min-size=SIZE don’t transfer any file smaller than SIZE

There is a tonne of other options to use with rsync. Kindly view its man pages for more.

Installation of rsync

Installing rsync on Debian/Ubuntu

sudo apt update
sudo apt -y install rsync

Installing Rsync on RHEL / CentOS / Fedora

sudo yum -y install rsync
sudo dnf -y install rsync

Installing rsync on Arch Linux / Manjaro

sudo pacman -S rsync

Installing rsync on FreeBSD

sudo pkg install rsync

Examples of using rsync

Copy file on the local computer to a directory

rsync -azvh --progress forgeeks.tar /tmp/

Sync files in one directory to another on the local computer with progress

rsync -arvzh --progress /home/tech/django/ /home/pench/backup/

Copy/sync files in a local directory to a remote host

rsync -arvh --progress /home/tech/django/ user@192.168.1.4:/tmp/django-backup/

Copy/sync files from a remote host to your local machine

rsync -arvh --progress user@192.168.1.3:/home/pench/flask/ /home/tech/flask/

Copy/sync files using ssh. Ssh offers transfer of files over a secure tunnel and rsync can benefit from this especially if you are transferring/syncing your files over an insecure network. Here we will use the -e option.

Remote to local

rsync -arvhe ssh --progress user@192.168.1.3:/home/pench/flask/ /home/tech/flask/ 

Local to remote

rsync -arvhe ssh --progress /home/tech/django/ user@192.168.1.:4/tmp/django-backup/ 

Copy/sync files with exclude option

You can include and exclude files from being sync by using a regular expression (regex)

rsync -avzh --progress --include '.txt' --exclude '.pdf' user@192.168.1.3:/tmp/django-backup/ /home/tech/django/

The above will include all files ending with the txt extension and exclude all files ending with the pdf extension during sync

Copy/sync files that are not bigger than a given size

Local to remote

rsync -avzhe ssh --max-size='50k' /home/tech/django/ user@192.168.1.4:/tmp/django-backup/ 

Above, any file that is larger than 50k in the /home/tech/django/ directory will not be copied over to the remote site.

Copy/sync files that are not smaller than a given size

rsync -avzhe ssh --min-size='50k' /home/tech/django/ user@192.168.1.4:/tmp/django-backup/ 

Here, any file that is smaller than 50k in the /home/tech/django/ directory will not be copied over to the remote site.

Conclusion

rsync can be used as a backup solution because it syncs your files and makes sure whatever you would wish to remain intact remains intact. There are other options available for rsync. If the above is not sufficient, please check out its man pages for more. Thank you for reading through.

Check out the following interesting guides as well:

Cart

Your Cart is Empty

Back To Shop