rsync

Was ist rsync?

“rsync” is a program which enables you to synchronize 2 folders. Basically it’s a better version of “cp”. rsync is built on the SSH protocol to fo example handle authorization and authentication.

Can’t I just use FTP or SFTP?

FTP => NO, SFTP => OK but not as good as rsync

A detailed description of the most common file transfer methods can be found HERE.

Why is rsync better than SFTP if both are based on SSH?

Requirement: Rsync is installed on both sides, client and server.

The main difference here is, that rsync only transfers the changed files from one system to another. Rsync uses a special “delta codec algorithm” and therefore saves a lot of time and traffic.

How do i use rsync?

First you have to check if rsync is installed on both client and server. Via the command “rsync --version” you can check which version you are currently running. Currently it should be (September 2019) at Version 3.

Let’s image the following example:

Your current computer (PC1) has a local folder, which should be synced to another external computer (PC2).

Command

rsync -aP <source> <destination>

So we are logged in to PC1 and have a folder called “wordpress” in our home directory which we want to sync to the external computer PC2 into the directory /var/www/html. For our external computer PC2 we use as an example the address devguide.at and as a user “admin“.

rsync -aP wordpress admin@devguide.at:/var/ww/html

What happens now? Depending on the SSH-Daemon settings of the external PC2 their could a password prompt or nothing happens.

But since we don’t want to enter the users password everytime we want to transfer something we can use the “Public-Key-Auth” so we don’t need to enter anything. See HERE for details.

So now we have configured our “Public-Key-Auth” and can login to the external PC without a password.

ssh admin@devguide.at

Now the following command should run through without any problems.

rsync -aP wordpress admin@devguide.at:/var/ww/html

What happens now?

The folder “wordpress” will be synced into the folder “/var/ww/html”. So now we have the folder structure “/var/ww/html/wordpress” on PC2.

But what if we just want to sync the content from the folder “wordpress” and not the whole folder?

rsync -aP wordpress/ admin@devguide.at:/var/ww/html

The difference here is the appended / at the end of the <source> part.
It wouldn’t make a difference on the <destination> part if you append a / or not.

But I just want to download something from PC2 to my local PC1!

Then just swap <source> and <destination>!

rsync -aP admin@devguide.at:/var/ww/html/ wordpress

I just want the files from <source> in <destination> and nothing else!

Per default rsync never deletes anything from <destination> even if they are not present in <source>.

But there are situations when you want to delete any unwanted files from the <destination> and therefore just want to have the state of <source> being present after the transfer is complete.

Thats what the option “–delete” does

Example

Files in folder “html” on PC1

  • index.html
  • logo.svg
  • style.css

Files in folder “/var/www/html” on PC1

  • index.html
  • logo.svg
  • style.css
  • nfo.php

Therefore the following command

rsync -aP --delete html/ admin@devguide.at:/var/ww/html

all files from PC1 will be transferred to PC2 AND the nfo.php will be deleted since it is not present in PC1.

If you remove the “--delete” option the nfo.php won’t be deleted.

More options for rsync

  • -a is the same as -rlptgoD. Basically recursive + permission + owner + group + times
    • -r: Copies folders recursively
    • -l: If Symlinks are present don’t remove them
    • -p: Show the process of the sync why syncing
    • -t: Also copy creation- and modification-date of the files and folders.
    • -g: If possible transfer the group of each filer and folder from <source> to <destination>
    • -o: If possible transfer the owner of each filer and folder from <source> to <destination>
    • -D: Also transfer “special files” and “devices” from <source> to <destination>
  • -P is the same as --partial --progress
    • If a connection gets interrupted , keep partially transferred files in <destination> and show the progress
  • –delete
    • remove files which are present in <destination> but are not present in <source>
  • –exclude
    • exclude specific files and/or folders

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

The reCAPTCHA verification period has expired. Please reload the page.

Durch die weitere Nutzung der Seite stimmst du der Verwendung von Cookies zu. Weitere Informationen

Die Cookie-Einstellungen auf dieser Website sind auf "Cookies zulassen" eingestellt, um das beste Surferlebnis zu ermöglichen. Wenn du diese Website ohne Änderung der Cookie-Einstellungen verwendest oder auf "Akzeptieren" klickst, erklärst du sich damit einverstanden.

Schließen