DEV Community

Cover image for rsync - 10 examples in 11 days (Day 00)
m4r4v
m4r4v

Posted on • Updated on

rsync - 10 examples in 11 days (Day 00)

Day 00

10 examples in 11 days

In GNU/Linux, I use a lot the rsync tool, to backup ,copy and keep synchronized files locally and remotely.

Why to use rsync?

Because it is a fantastic tool to copy files locally and remotely.

How does it work?

It works based on its delta-transfer algorithm which identifies parts of the source file and it computes the difference between the source and destination, this way it knows what exactly to sync.

But what exactly determines what to sync?

Rsync splits the source and destination files into a series of blocks (non-overlapping fixed-sized blocks), and the last block should be smaller than the total of bytes of the blocks. Each block is calculated based on two checksums. A 32-bit checksum and a strong 128-bit MD4 checksum.

The blocks checksums are sent from the source to the destination. The destination file calculates the length in bytes of all weak and strong checksums to find any difference between them. If the checksums are different, is then when it starts to sync between the two files.

The process goes like this:
(reference)

  • client >>> The client initiates the synchronisation.
  • server >>> The remote rsync process or system to which the client connects either within a local transfer, via a remote shell or via a network socket. This is a general term and should not be confused with the daemon.

Once the connection between the client and server is established the distinction between them is superseded by the sender and receiver roles.

  • daemon >>> An Rsync process that awaits connections from clients. On a certain platform this would be called a service.
  • remote shell >>> One or more processes that provide connectivity between an Rsync client and an Rsync server on a remote system.
  • sender >>> The Rsync process that has access to the source files being synchronised.
  • receiver >>> As a role the receiver is the destination system. As a process the receiver is the process that receives update data and writes it to disk.
  • generator >>> The generator process identifies changed files and manages the file level logic.

Some features of rsync

  • Copies links, owners, groups and permissions
  • Excludes given options
  • Can use ssh for remote connections
  • Non-root can use it

Now let's rsync syntax

Local

rsync [options] source destination
Enter fullscreen mode Exit fullscreen mode

Remote Push Process

rsync [options] source [user@host::destination]
Enter fullscreen mode Exit fullscreen mode

Remote Pull Process

rsync [options] [user@host::source] destination
Enter fullscreen mode Exit fullscreen mode

Now let's see some frequently used rsync options

The beauty of rsync is the amount of options that it has, check it out:

-v, --verbose increase verbosity
-q, --quiet suppress non-error messages
-r, --recursive recurse into directories
-b, --backup make backups (see --suffix & --backup-dir)
--backup-dir=DIR make backups into hierarchy based in DIR
-u, --update skip files that are newer on the receiver
-l, --links copy symlinks as symlinks
-H, --hard-links preserve hard links
-p, --perms preserve permissions
-E, --executability preserve executability
--chmod=CHMOD affect file and/or directory permissions
-o, --owner preserve owner (super-user only) permissions
-g, --group preserve group
--remove-source-files sender removes synchronized files (non-dir)
--delete-after receiver deletes after transfer, not during
--ignore-errors delete even if there are I/O errors
--force force deletion of dirs even if not empty
--max-size=SIZE don't transfer any file larger than SIZE
-z, --compress compress file data during the transfer
--progress show progress during transfer
-i, --itemize-changes output a change-summary for all updates
--version print version number
-h human-readable, output numbers in a human-readable format
(-h), --help show help
-i, --itemize-changes List of the changes for each file that will be sync/copied

Ok, this was it, check in again for the rest of this series

Top comments (0)