rsync - 10 examples in 11 days (Day 00)
Tags: linux, rsync, bash, tutorial
Published: 2020-07-15 19:39:59

#Day 00

> 10 examples in 11 days

In GNU/Linux, I use a lot the **[rsync](https://linux.die.net/man/1/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](https://rsync.samba.org/how-rsync-works.html))

* 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
```

**Remote Push Process**
```
rsync [options] source [user@host::destination]
```

**Remote Pull Process**
```
rsync [options] [user@host::source] destination
```

-----------------------------------------------------

####Now let's see some frequently used **rsync** options
The beauty of **rsync** is the amount of options that it has, check it out:

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

------------------------------------------------

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