rsync - 10 examples in 11 days (Day 11)
Tags: linux, bash, tutorial, rsync
Published: 2020-07-26 19:36:32

# Day 11

> 10 examples in 11 days

## Log Files

Well it seems we have come to the end of the series. Have in mind that **rsync** has a lot more to offer so please read the man page to get and use the best of **rsync**.

I can't end this series without talking about a great option. It's about the **--log-file=FILE** option.

Sometimes you may want or need a log file, let's say you want to check out what was done, or like what happened to me, our manager always made us send every morning an email with the log of all sync done the day before (we made a script for that hehe). But let's face it, it's a good practice to create a log file almost for everything (I do it all the time).

So, having **rsync** create its own log file after every sync huh?, yup, it's as easy as it sounds, let me show you what the syntax can look like:

```
rsync [--options] [Source] [Destination] --log-file=[Where the log file will be stored]
```

If you read [Day 10](https://dev.to/jmr_code_social/rsync-10-examples-in-11-days-day-10-4c0i) great and if you haven't, please do because today's option will use a lot of the arguments shown there. I will still use the folders I've been using so far **Users** and **Sync**.

Now, to get the picture, I will do the following:

1. I will empty out all files inside Sync
2. I will run rsync with the log file option
3. I will show what the log file looks like

```
iamgroot@laptop:~$ rm -r Sync/ && rsync -a Users/ Sync --log-file=Sync/logfile.txt && ls -la Sync/ && cat Sync/logfile.txt
total 16
drwxrwxr-x  2 iamgroot iamgroot 4096 jul 26 13:48 .
drwxrwxr-x 38 iamgroot iamgroot 4096 jul 26 02:15 ..
-rw-rw-r--  1 iamgroot iamgroot   30 jul 26 13:48 file1.txt
-rw-r--r--  1 iamgroot iamgroot  217 jul 26 14:16 logfile.txt
2020/07/26 14:16:35 [13004] building file list
2020/07/26 14:16:35 [13004] .d..t...... ./
2020/07/26 14:16:35 [13004] >f+++++++++ file1.txt
2020/07/26 14:16:35 [13004] sent 163 bytes  received 43 bytes  total size 30
```

You can see what the _logfile.txt_ looks like, let's go through it

`2020/07/26 14:16:35 [13004] building file list`: Rsync always shows this when it starts

`2020/07/26 14:16:35 [13004] .d..t...... ./` The directory of the source using the **_-i_** option

`2020/07/26 14:16:35 [13004] >f+++++++++ file1.txt` The file sync displayed using the **_-i_** option

`2020/07/26 14:16:35 [13004] sent 163 bytes received 43 bytes total size 30` End of the sync

As I said, rsync log file uses by default the **_-i_** option that I explained in [Day 10](https://dev.to/jmr_code_social/rsync-10-examples-in-11-days-day-10-4c0i).

### But what if we want to add some different arguments to the log file?

Well this is what I love about **rsync**, it always gives us something else. In this case we have the **--log-file-format** option. Is like the **--out-format** but explicitly for the log file.

Let's say I only want the length of the file in bytes, then we can use the **%l** argument like this:

```
iamgroot@laptop:~$ rsync -a Users/ Sync --log-file=Sync/logfile.txt --log-file-format='%l' && cat Sync/logfile.txt
2020/07/26 14:23:52 [14572] building file list
2020/07/26 14:23:52 [14572] 4096
2020/07/26 14:23:52 [14572] 30
2020/07/26 14:23:52 [14572] sent 163 bytes  received 43 bytes  total size 30
```

Let's compare what we had before and what we've got now:

Before: `2020/07/26 14:16:35 [13004] .d..t...... ./`

Now: `2020/07/26 14:23:52 [14572] 4096`

Before: `2020/07/26 14:16:35 [13004] >f+++++++++ file1.txt`

Now: `2020/07/26 14:23:52 [14572] 30`

The default options are **%o %h [%a] %m (%u) %f %l", and a "%t [%p]"** and all the options available are:

| option | description                                                                                                                                                                                                                                                                                                                                                                                                                          |
| ------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| %a     | the remote IP address (only available for a daemon)                                                                                                                                                                                                                                                                                                                                                                                  |
| %b     | the number of bytes actually transferred                                                                                                                                                                                                                                                                                                                                                                                             |
| %B     | the permission bits of the file (e.g. rwxrwxrwt)                                                                                                                                                                                                                                                                                                                                                                                     |
| %c     | the total size of the block checksums received for the basis file (only when sending)                                                                                                                                                                                                                                                                                                                                                |
| %C     | the full-file checksum if it is known for the file. For older rsync protocols/versions, the checksum was salted, and is thus not a useful value (and is not displayed when that is the case). For the checksum to output for a file, either the --checksum option must be in-effect or the file must have been transferred without a salted checksum being used. See the --checksum-choice option for a way to choose the algorithm. |
| %f     | the filename (long form on sender; no trailing "/")                                                                                                                                                                                                                                                                                                                                                                                  |
| %G     | the gid of the file (decimal) or "DEFAULT"                                                                                                                                                                                                                                                                                                                                                                                           |
| %h     | the remote host name (only available for a daemon)                                                                                                                                                                                                                                                                                                                                                                                   |
| %i     | an itemized list of what is being updated                                                                                                                                                                                                                                                                                                                                                                                            |
| %l     | the length of the file in bytes                                                                                                                                                                                                                                                                                                                                                                                                      |
| %L     | the string "-> SYMLINK", "=> HARDLINK", or "" (where SYMLINK or HARDLINK is a filename)                                                                                                                                                                                                                                                                                                                                              |
| %m     | the module name                                                                                                                                                                                                                                                                                                                                                                                                                      |
| %M     | the last-modified time of the file                                                                                                                                                                                                                                                                                                                                                                                                   |
| %n     | the filename (short form; trailing "/" on dir)                                                                                                                                                                                                                                                                                                                                                                                       |
| %o     | the operation, which is "send", "recv", or "del." (the latter includes the trailing period)                                                                                                                                                                                                                                                                                                                                          |
| %p     | the process ID of this rsync session                                                                                                                                                                                                                                                                                                                                                                                                 |
| %P     | the module path                                                                                                                                                                                                                                                                                                                                                                                                                      |
| %t     | the current date time                                                                                                                                                                                                                                                                                                                                                                                                                |
| %u     | the authenticated username or an empty string                                                                                                                                                                                                                                                                                                                                                                                        |
| %U     | the uid of the file (decimal)                                                                                                                                                                                                                                                                                                                                                                                                        |

Now I will give you all a nice tip, like I did in [Day 06](https://dev.to/jmr_code_social/rsync-10-examples-in-11-days-day-06-454o), and I really suggest you do this if you work in a team. Add the logged user _$USER_ who is performing the sync and also the file that was sync. Let me show you how:

```
iamgroot@laptop:~$ rsync -a Users/ Sync --log-file=Sync/logfile.txt --log-file-format='%i --File: %n -- User: '$USER && cat Sync/logfile.txt
2020/07/26 14:33:03 [15332] building file list
2020/07/26 14:33:03 [15332] .d..t...... --File: ./ -- User: iamgroot
2020/07/26 14:33:03 [15332] >f+++++++++ --File: file1.txt -- User: iamgroot
2020/07/26 14:33:03 [15332] sent 163 bytes  received 43 bytes  total size 30
```
Isn't this awesome?

OK, that'll be it for today and this is the end of the **rsync** series. I really hope you all enjoyed it. If you have any doubt don't hesitate asking me, will be more than happy to help.

---

Thanks for reading, keep it safe!!

Don't forget to Follow, :heart: or :unicorn:

`iamgroot@laptop:~$ exit`

:+1: