Skip to content

How to Use rsync From Windows to Linux?

I have a folder that contains some files on windows and I want to move those files to my Linux server regularly.

 

Problem

How to use rsync to synchronize data from Windows to Linux?

 

Solution

As far as I know, there are several ways to do rsync in Windows such as using AOMEI Backupper software or also using WSL from Windows. But in this article, I am trying to do rsync using Cygwin application which is a POSIX-compatible programming and runtime environment that runs natively on Microsoft Windows. This application was first created by Steve Chamberlain in 1995 and at the time of this writing, this application is up to version 3.2.0.

In the Linux remote host, we must install rsync package and these are steps how to install and configure Cygwin on windows:

1. Download Cygwin

Download the Cygwin application for the 64-bit version and the following link for the 32-bit version. Because my Windows is the 64-bit version, so I install Cygwin application for the 64-bit version.

 

2. Install Cygwin

Double-click the Cygwin installer that we have downloaded, then there will be a display:

Cygwin Setup

 

Click the Next button, the below image will appear:

Choose Installation Type

 

I choose to Install from Internet, click the Next button and there will be a display like this:

Choose Installation Directory

 

I click the Next button, and there will be a display like this:

Local Package Directory

 

I click the Next button, and the image below will appear:

Connection type

 

I choose to Direct Connection, click the Next button, and there will be a display like this:

Choose a download site

 

I choose the site, click the Next button and there will be a display like this:

Cygwin Setup

 

The installer will parse the package, and the image below will appear like this:

Select the packages

 

Change the View to Full and in the search column, I write cron, bash, rsync, and openssh package (You have to write the packages one by one) and click the package that you want to install. After that, click the Next button and there will appear like this:

Review and confirm changes

 

Click the Next button, and the image below will appear like this:

The progress of the download or installation

 

The installer will download and install the packages, and we must wait until finish. After that, the below image will appear:

Create icons

 

Click the Finish button.

 

3. Integrate Cygwin and Windows

We want to integrate Cygwin and Windows. Right-Click This PC item and choose Properties like in the below picture:

Choose Properties

After that, choose the Advanced system settings item and there will be a display like this:

System Properties

 

Click the Environment Variables button, and there will be a display like this:

Choose Path

Click the Path column in System variables and click the Edit button, and there will be a display like this:

Create a new path environment

Click the New button and type your installation directory (I typed C:\cygwin64) and then click the OK button. After that, open the Command Prompt application and type Linux command like pwd command:

Command Prompt application

 

If the application gives a value like an image above, we have successfully integrated Cygwin with windows.

 

4. OpenSSH configuration

Next, we must configure the OpenSSH package in Cygwin. Open Cygwin64 Terminal application like in the image below:

Cygwin64 Terminal application

 

and type the command below:

ssh-keygen

 

Press the Enter if you asked to answer, like in the image below:

Execute ssh-keygen command in the Cygwin64 Terminal application

 

After that, open Command Prompt application and type the same command:

Execute ssh-keygen command in command prompt application

 

Next, we have to copy the id_rsa.pub key to the Linux remote host so we don’t need to type the password if we want to access to Linux remote host through windows. To copy the key, I copy the key manually to the Linux remote host in the .ssh/authorized_keys file. After that, try to access the remote host using a command prompt application using ssh and if we can access the remote host without typing the password like in the image below, our configuration is successful.

Test to the remote host

 

5. Execute rsync command

I want to copy the FormatFactory directory in the Documents in my Windows to the Linux remote host in 192.168.56.138. So, I go to the Documents folder and type the below command to synchronize data:

rsync -avzphi --chmod=ugo=rwX --checksum -e"C:\cygwin64\bin\ssh.exe -i C:\Users\SysAdmin/.ssh/id_rsa" "FormatFactory" [email protected]:/root

 

After typing the command above, the data will start synchronizing and we have to wait until the process is complete.

Sync process

 

From the picture above, we can see that the synchronization process was successful.

 

6. Cron on Cygwin

After successfully syncing, I want the folder to be synchronized regularly. Therefore, I need a cron application to do this and I have installed the cron package in Cygwin. Open Cygwin Terminal and run as administrator, type the below command:

cron-config

 

And answer the questions like the ones in the green box in the image below:

cron-config command

 

After that, check whether the cron service is running or not by typing the command:

ps -ef | grep cron

 

If the cron service is running, there will be a display like the image below:

Check cron service

 

You can see the cron service in the Services:

cron service in Services

 

In the Documents folder, I create a bash script to execute the rsync command like this and I named it backup.sh:

#!/bin/bash

rsync -avzphi --chmod=ugo=rwX --checksum -e"C:\cygwin64\bin\ssh.exe -i C:\Users\SysAdmin/.ssh/id_rsa" "FormatFactory" [email protected]:/root

 

After that, I type the below command to make the file executable:

chmod +x backup.sh

 

Next, we open crontab in the command prompt application. Type the below command:

crontab -e

 

In Cygwin, we have to define HOME and PATH first in crontab. I define HOME and PATH like this:

HOME=/cygdrive/c/Users/SysAdmin
PATH=/usr/local/bin:/usr/bin:/bin:$PATH

 

and I create a cron job for this case like this:

*/30 * * * *       cd Documents;sh backup.sh

 

The synchronization process will execute every 30 minutes. If you want to check whether your cron job running or not, type the below command:

cronevents

 

Note

In order to make it easier for us to type commands in Cygwin, then I suggest we use a username that only consists of one word or do not use spaces in writing users in windows because it will make it difficult for us to type commands in Cygwin.

 

References

wikipedia.org
cygwin.com
ubackup.com
stackoverflow.com
coolperspective.wordpress.com
linux.subogero.com

 

Print Friendly, PDF & Email

2 thoughts on “How to Use rsync From Windows to Linux?”

Leave a Reply

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