unix – How to synchronize files between two servers?


Two servers have directories in which some files are located. Currently, using the rsync utility, I can synchronize these directories with each other.

But there is a problem: garbage. If you delete one or more files on one of the servers, after synchronization, it will remain on the other. Option --delete It doesn't help either, since it deletes the directory before synchronization.

That is, I want to achieve the following behavior in the scenarios:

  • Delete files on server 2 that are not on server 1.
  • After that, delete the files on Server 1 that are not on Server 2.

the rsync The utility only runs on Server 1.

Tell me, please, how can this result be achieved?