Synchronization Solution on Windows (RSYNC)

rsync-windows.jpg
rsync-windows.jpg

img from unsplash

These are the ba­sic steps to set up and use an rsync tool for syn­chro­niza­tion on Win­dows.

RSYNC Server

Setup RSYNC Server on Windows

i. Manual Setup

  1. Install RSYNC on the server machines.
    Install Rsync software like a linux through Chocolatey
choco install rsync -y
  1. On the server, create a folder to share
mkdir -p C:\rsync_share

2023-06-08_164244.png
2023-06-08_164244.png

e.g. C:\rsync_share. as rsync share folder.

  1. On the server,Create the C:\rsync\rsyncd.conf file with the following contents:
[rsync_share]
path = /cygdrive/c/rsync_share
use chroot = no
ignore errors
read only = no
list = yes

This con­fig­ures the share name as "rsync_share" point­ing to the C:\rsync_share folder.

Use cygdrive path prefix as /cygdrive/c/rsync_share

  1. On the server, open PowerShell and run the rsync daemon:
rsync --daemon --config=C:\rsync\rsyncd.conf

This will run the rsync dae­mon us­ing the con­fig file C:\rsync\rsyncd.conf.

  1. You may need to configure firewall rules on the server to allow incoming TCP connections to port 873

2023-06-08_151454.png
2023-06-08_151454.png

(The rsync dae­mon de­fault port).

  1. Rsync service is set to run automatically on startup on the server via "Task Scheduler".

2023-06-08_151641.png
2023-06-08_151641.png

2023-06-08_151656.png
2023-06-08_151656.png


ii. Automatic via Script

Here are Win­dows scripts to au­to­mat­i­cally in­stall rsync, con­fig­ure it, and open the fire­wall port:
  1. Update Policy to allow execute self PowerShell script.
Set-ExecutionPolicy RemoteSigned
Ex­e­cu­tion Pol­icy Change
The ex­e­cu­tion pol­icy helps pro­tect you from scripts that you do not trust. Chang­ing the ex­e­cu­tion pol­icy might ex­pose
you to the se­cu­rity risks de­scribed in the about_Ex­e­cu­tion_Poli­cies help topic at
https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to change the ex­e­cu­tion pol­icy?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Sus­pend [?] Help (de­fault is "N"): y
  1. Create Windows PowerShell Script
Save all be­low con­tent to a Pow­er­Shell script as names "rsync-con­fig.ps1"
# Create rsync config and share folders
New-Item -Path C:\rsync -ItemType Directory
New-Item -Path C:\rsync_share -ItemType Directory

# Create rsync.conf file
@"
[rsync_share]
path = /cygdrive/c/rsync_share
ignore errors
read only = no
list = yes
auth users = rsync
secrets file = /cygdrive/c/rsync/rsync.passwd
"@ | Add-Content -Path C:\rsync\rsyncd.conf

# Open port 873 for rsync
New-NetFirewallRule `
    -Name "Rsync Daemon (873)" `
    -DisplayName "Rsync Daemon (873)" `
    -Protocol TCP `
    -LocalPort 873

# Create scheduled task to run rsync daemon
$Trigger = New-ScheduledTaskTrigger -AtStartup
$Action = New-ScheduledTaskAction `
    -Execute "Powershell.exe" `
    -Argument "rsync --daemon --config=C:\rsync\rsyncd.conf"
Register-ScheduledTask `
    -TaskName "Rsync Daemon" `
    -Trigger $Trigger `
    -Action $Action `
    -RunLevel Highest `
    -User System
ParameterDescriptionPath
rsyncConfiguration folder for rsyncC:rsync\
rsync_shareShared folder for rsyncC:rsync_share\
/cygdrive/c/rsync_shareCygwin path prefix up to the shared folder locationC:rsync_share\
auth users = rsyncSpecifies that only the "rsync" user can connect
secrets file = /cygdrive/c/rsync/rsync.passwdSpecifies the location of the password file that will be used for authenticating rsync connections.C:rsyncrsync.passwd

The for­mat of the rsync password file(C:rsyn­cr­sync.passwd) is as fol­lows:

username:password
  • username: This is the username for the rsync account.
  • password: This is the password for the rsync account.

Each line in the file rep­re­sents a sin­gle rsync ac­count. Cre­ate mul­ti­ple ac­counts by adding ad­di­tional lines to the file.

  1. Execute the PowerShell Script

2023-06-08_151758.png
2023-06-08_151758.png

run on Win­dows server to au­to­mat­i­cally setup rsync dae­mon, cre­ate share folder names "rsync_share" and con­fig­ure the fire­wall. more­over, the rsync ser­vice startup on re­boot.

RSYNC Client

Setup RSYNC Client on Windows

In­stall RSYNC on the client ma­chines.
In­stall Rsync soft­ware like a linux through Chocolatey

choco install rsync -y

Operating

i. Manual

On the client PC, cre­ate a folder to store rsync data

mkdir -p C:\rsync_client
Directory: C:\

Mode Last­Write­Time Length Name

---- ------------- ------ ----

d----- 7/​11/​2023 12:46 PM rsync_­client

For ex­am­ple:

To sync files from client to server (PUSH):

rsync -avz /cygdrive/c/rsync_client  rsync://server_ip_address/rsync_share

To sync files from server to client (PULL):

rsync -avz rsync://server_ip_address/rsync_share /cygdrive/c/rsync_client

To sync files from server to client with --progress flag on Linux

rsync -avz --progress --update --chmod=ugo=rwX --delete rsync://server_ip_address/rsync_share ./rsync_client

2023-06-08_163246.png
2023-06-08_163246.png

OptionDescription
-aEnables archive mode
-vEnables verbose output
-zEnables compression
--progressDisplays progress during the transfer
--updateSkips files that are newer on the destination
--chmod=ugo=rwXSets permissions of synced files
--deleteRemoves files on the destination that do not exist on the source
rsync://server_ip_address/rsync_shareSpecifies the remote directory as rsync_share
./ClientFolderSpecifies the current directory on the local machine

i. Automatic via Script

Up­date Pol­icy to al­low ex­e­cute self Pow­er­Shell script.

Set-ExecutionPolicy RemoteSigned
  1. Create Windows PowerShell Script
Save all be­low con­tent to a Pow­er­Shell script as names "rsync-client.ps1"
# Set variables
$RsyncPullFolder = "C:\rsync_pull"
$RsyncConfigFolder = "C:\rsync"
$Source = "/cygdrive/c/rsync_pull"
$Destination = "rsync_server_ip::share"
$Username = "rsync"
$Password = "P@ssw0rd123!"
$TaskName = "Rsync Pull"
$TriggerTime = "08:00"


# Check if rsync_pull and rsync folders exist, and create them if they don't
if (!(Test-Path $RsyncPullFolder)) {
    New-Item -ItemType Directory -Path $RsyncPullFolder
}
if (!(Test-Path $RsyncConfigFolder)) {
    New-Item -ItemType Directory -Path $RsyncConfigFolder
}
New-Item -Path "$RsyncConfigFolder" -Name "rsync.passwd" -ItemType "File"
Add-Content -Path "$RsyncConfigFolder\rsync.passwd" -Value "$Password"


# Build rsync command
$RsyncCommand = "rsync -avz --progress --update --chmod=ugo=rwX --delete --password-file=/cygdrive/c/rsync/rsync.passwd $Username@$Destination $Source"

# Create scheduled task to run rsync command
$Trigger = New-ScheduledTaskTrigger -Daily -At $TriggerTime
$Action = New-ScheduledTaskAction -Execute "Powershell.exe" -Argument "$RsyncCommand"
Register-ScheduledTask -TaskName $TaskName -Trigger $Trigger -Action $Action -RunLevel Highest -User "SYSTEM"
VariableDescription
$RsyncPullFolderSpecifies the download directory on the local machine
$RsyncConfigFolderSpecifies the config file storage directory on the local machine
$SourceSpecifies the source directory on the local machine
$DestinationSpecifies the destination directory on the remote server
$UsernameSpecifies the username of the rsync user on the remote server
$PasswordSpecifies the password for the rsync user on the remote server
$TaskNameSpecifies the name of the scheduled task
$TriggerTimeSpecifies the time when the scheduled task will run

This script runs on a Win­dows client and au­to­mat­i­cally sets up an rsync sched­ule, cre­ates an rsync folder named 'r­sync_pull', and con­fig­ures the user­name and pass­word for au­tho­riza­tion.


Conclusion

rsync can be a re­li­able and ef­fi­cient so­lu­tion for file syn­chro­niza­tion on Win­dows, with its abil­ity to pre­serve file at­trib­utes, com­press data dur­ing trans­fer, and show ver­bose out­put.

To use rsync on Win­dows, you can in­stall a ver­sion of rsync com­pat­i­ble with Win­dows, con­fig­ure your sys­tem's fire­wall and net­work set­tings, and cre­ate a con­fig­u­ra­tion file to spec­ify the files and di­rec­to­ries to be synced.


Reference


Related