loading...
Cover image for rsync - 10 examples in 11 days (Day 02)

rsync - 10 examples in 11 days (Day 02)

jmr_code_social profile image Jorge Marquez Updated on ・3 min read

Day 02

10 examples in 11 days

Update Option

This article is about the update (-u --update) option. The -u --update option is very useful when either the source or destinations files are newer and we intent to keep them as they are. Very often we sync files between two locations that are active, by active I mean, that are both in use.

Let's say we have the following scenario. We have folder A which acts as my source and folder B which acts as my destination. We access folder B and change a file called user.config because we need to update some data inside of it.

The next day, we are going to perform a sync from A to B but we do not want user.config to changed at B, well this can be done by using the -u option. Let me show you and example and how to use it.

Let's see what we have inside our Users folder:

iamgroot@laptop:~$ stat -c "%s %n" ~/Users/* && stat -c "%s %n" ~/Sync/Users*
140 /home/iamgroot/Users/user01.json
57 /home/iamgroot/Users/user02.json
69 /home/iamgroot/Users/user03.json
43 /home/iamgroot/Users/user.config
140 /home/iamgroot/Sync/Users/user01.json
57 /home/iamgroot/Sync/Users/user02.json
69 /home/iamgroot/Sync/Users/user03.json
43 /home/iamgroot/Sync/Users/user.config

We have the exact same files, everything is sync, but let's modify our user.config at destination (~/Sync/Users/user.config) and see the difference between both files (source and destination)

iamgroot@laptop:~$ stat -c "%s %n" ~/Users/* && stat -c "%s %n" ~/Sync/Users*
140 /home/iamgroot/Users/user01.json
57 /home/iamgroot/Users/user02.json
69 /home/iamgroot/Users/user03.json
43 /home/iamgroot/Users/user.config
140 /home/iamgroot/Sync/Users/user01.json
57 /home/iamgroot/Sync/Users/user02.json
69 /home/iamgroot/Sync/Users/user03.json
133 /home/iamgroot/Sync/Users/user.config

As we can see, our user.config at ~/Sync/Users is now 133bytes instead of the 43bytes. Let's update it without having rsync to touch that file

iamgroot@laptop:~$ rsync -uvhr ~/Users ~/Sync/Users
sending incremental file list
Users/
Users/user.config
Users/user01.json
Users/user02.json
Users/user03.json

sent 635 bytes  received 96 bytes  1.46K bytes/sec
total size is 309  speedup is 0.42

Let's check again our file size in A and B

stat -c "%s %n" ~/Users/* && stat -c "%s %n" ~/Sync/Users/*
140 /home/jorge/Users/user01.json
57 /home/jorge/Users/user02.json
69 /home/jorge/Users/user03.json
43 /home/jorge/Users/user.config
140 /home/jorge/Sync/Users/user01.json
57 /home/jorge/Sync/Users/user02.json
69 /home/jorge/Sync/Users/user03.json
133 /home/jorge/Sync/Users/user.config
4096 /home/jorge/Sync/Users/Users

Rsync didn't perform any changes on user.config at B, this is because even though in B the file is 133bytes instead of the 43bytes in A, it knows that in B, the file is newer, so it is not needed to be updated.

In resume, using -u or --update will sync any other files rather than the new ones.

One last thing I want to point out. Whenever you want to update, use the --dry-run option. This option will tell rsync to show you what will be sync.


-u, --update

This forces rsync to skip any files which exist on the destination and have a modified time that is newer than the source file. (If an existing destination file has a modification time equal to
the source file’s, it will be updated if the sizes are different.)

Note that this does not affect the copying of symlinks or other special files. Also, a difference of file format between the sender and receiver is always considered to be important enough for an
update, no matter what date is on the objects. In other words, if the source has a directory where the destination has a file, the transfer would occur regardless of the timestamps.

This option is a transfer rule, not an exclude, so it doesn’t affect the data that goes into the file-lists, and thus it doesn’t affect deletions. It just limits the files that the receiver
requests to be transferred.


Ok, that'll be for today's example, thanks for reading!!!

Follow, ❤ or 🦄

Discussion

pic
Editor guide