File transfers using rsyncd

Posted by : on

Category : powershell   scripts   network


Steps to install and configure rsyncd, script to use it on windows and easily copy directory structures

πŸ“¦ Rsync Server Setup Guide (rsyncd)

This guide explains how to install and configure rsyncd (rsync daemon mode) on a Linux server. It includes setting up authentication, defining modules, logging, and ensuring the daemon starts on boot.


βœ… 1. Install rsync

sudo apt update
sudo apt install rsync

βœ… 2. Create the rsync daemon config file

Create or edit /etc/rsyncd.conf:

uid = nobody
gid = nogroup
use chroot = no
max connections = 4
log file = /var/log/rsyncd.log
timeout = 300

[new_movies]
    path = /mnt/external/Nouveautes/NEW_MOVIES
    comment = New Movies
    read only = false
    list = yes
    auth users = backupuser
    secrets file = /etc/rsyncd.secrets

[new_movies_datassd]
    path = /mnt/datassd/Nouveautes/NEW_MOVIES
    comment = New Movies
    read only = false
    list = yes
    auth users = backupuser
    secrets file = /etc/rsyncd.secrets

[new_tv_programs]
    path = /mnt/external/Nouveautes/NEW_TV_PROGRAMS
    comment = New TV Programs
    read only = false
    list = yes
    auth users = backupuser
    secrets file = /etc/rsyncd.secrets

[datapackages]
    path = /mnt/external/DataPackages/rsync
    comment = Fast shared rsync directory
    read only = false
    list = yes
    auth users = backupuser
    secrets file = /etc/rsyncd.secrets

βœ… 3. Create the secrets file

Create /etc/rsyncd.secrets and set proper permissions:

echo "backupuser:YourStrongPasswordHere" | sudo tee /etc/rsyncd.secrets
sudo chmod 600 /etc/rsyncd.secrets

βœ… 4. Create systemd service for rsyncd

Create a new systemd service file:

sudo nano /etc/systemd/system/rsyncd.service

Paste the following:

[Unit]
Description=Fast remote file copy program daemon
After=network.target

[Service]
ExecStart=/usr/bin/rsync --daemon --no-detach
Restart=always

[Install]
WantedBy=multi-user.target

Save and enable it:

sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable rsyncd.service
sudo systemctl start rsyncd.service

βœ… 5. Open Firewall Port (optional)

sudo ufw allow 873/tcp

βœ… 6. Verify It’s Running

sudo systemctl status rsyncd.service

You should see something like:

Active: active (running)

βœ… 7. Monitor Logs

tail -f /var/log/rsyncd.log

πŸ§ͺ Example Client Command

From another machine:

rsync -avz backupuser@your.server.ip::new_movies /destination/path

You’ll be prompted for the password you specified in rsyncd.secrets.


πŸ” Security Notes

  • Ensure your rsyncd.secrets file has strict permissions (chmod 600).
  • Consider wrapping rsync in SSH for extra security if you do not fully trust the network.

🧰 Useful Commands

  • Restart daemon:

    sudo systemctl restart rsyncd
    
  • View active connections:

    sudo lsof -i :873
    
  • View real-time logs:

    tail -f /var/log/rsyncd.log
    

πŸͺŸ Rsync Client Setup on Windows (with Chocolatey)

This guide explains how to install and use the rsync client on Windows using Chocolatey, and how to sync files with a Linux rsyncd server.


βœ… 1. Install Chocolatey (if not already installed)

Run this in PowerShell (Admin):

Set-ExecutionPolicy Bypass -Scope Process -Force; `
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12; `
iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

βœ… 2. Install rsync via Chocolatey

Install cwRsync (a native Windows rsync wrapper):

choco install cwrsync -y

This installs it to:

C:\Program Files\cwRsync\bin\

Chocolatey will automatically add it to your PATH.

Test it:

rsync --version

You should see output like:

rsync  version 3.1.x  protocol version 31

βœ… 3. Sync Files with the Linux rsync Server

To push a directory to the server:

rsync -av --progress "D:\Movies\" rsync://backupuser@10.0.0.111/new_movies

You’ll be prompted for the rsyncd password.

The double slashes (rsync://...) indicate you’re connecting to an rsync daemon, not SSH.


βœ… 4. Automate the Sync with a Script

Create a sync.ps1 file:

$env:RSYNC_PASSWORD = "YourStrongPasswordHere"
rsync -av --progress "D:\Movies\" rsync://backupuser@10.0.0.111/new_movies

You can run it manually or schedule it via Task Scheduler.


πŸ” Security Tip

If you’re concerned about storing a password in plain text:

  • Use environment variables set via Task Scheduler.
  • Or consider switching to rsync over SSH for encrypted transport.

Your rsyncd server is now configured, secure, and starts automatically at boot. You can monitor transfers and connect using authenticated clients.

To use a password file with rsync on Windows, you can securely automate the connection to your Linux rsyncd server (which uses auth users and secrets file), without prompting for a password every time.


βœ… Steps to Use a Password File on Windows with rsyncd

1. Create the Password File

Create a file (e.g., rsync.pass) with just the password on a single line:

YourStrongPasswordHere

Place it somewhere secure (e.g., C:\Users\YourName\.rsync\rsync.pass).


2. Set File Permissions (IMPORTANT)

Run the following in PowerShell (Admin) to restrict file access:

icacls "C:\Users\YourName\.rsync\rsync.pass" /inheritance:r
icacls "C:\Users\YourName\.rsync\rsync.pass" /grant:r "$($env:USERNAME):R"

This ensures only your user can read the file.


3. Use --password-file with rsync

Example command:

rsync -av --progress --password-file="C:/Users/YourName/.rsync/rsync.pass" "D:/Movies/" rsync://backupuser@10.0.0.111/new_movies

Note: Use forward slashes (/) in the path even on Windows, or wrap in double quotes.


πŸ“Œ Full Example PowerShell Script

$rsync = "C:\Program Files\cwRsync\bin\rsync.exe"
$source = "D:/Movies/"
$dest = "rsync://backupuser@10.0.0.111/new_movies"
$passfile = "C:/Users/$env:USERNAME/.rsync/rsync.pass"

& $rsync -av --progress --password-file="$passfile" "$source" "$dest"

πŸ”’ Security Tip

  • Never store password files in public or shared directories.
  • Do not use --password-file over rsync over SSH β€” this only applies to rsyncd (daemon mode).

Get the list of rsync shares

$rsyncExe = "C:\ProgramData\chocolatey\bin\rsync.exe"
# or 
$rsyncExe = Find-Program -Name "rsync" -PathOnly -FirstMatch

 & "$rsyncExe" "rsync://10.0.0.111"

new_movies      New Movies
new_movies_datassd      New Movies
new_tv_programs New TV Programs
datapackages    Fast shared rsync directory

Separate with tab as split character:

 $rsyncExe = Find-Program -Name "rsync" -PathOnly -FirstMatch
 $remoteArg = "rsync://{0}" -f $Server

 & "$rsyncExe" "$remoteArg" | % { $_.Split("`t")[0].Trim() }

new_movies
new_movies_datassd
new_tv_programs
datapackages

Script:

function Get-RsyncShares {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Position = 0, Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]$Server = "mini"
    )

    $RsyncServerOpen = $False

    try {
        $cli = [system.Net.Sockets.TcpClient]::new("$Server", 873)
        $RsyncServerOpen = $cli.Connected
    } catch {
        $RsyncServerOpen = $False
    } finally {
        $cli.Dispose()
    }

    if (!$RsyncServerOpen) {
        return $Null
    }

    $rsyncExe = Find-Program -Name "rsync" -PathOnly -FirstMatch
    $remoteArg = "rsync://{0}" -f $Server

    & "$rsyncExe" "$remoteArg" | % { $_.Split("`t")[0].Trim() }
}

Copy Files - Script

The ArgumentCompleter script will use Get-RsyncShares to list valid shares when you press tab.



function Copy-RsyncToMini {
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        [Parameter(Position = 0, Mandatory = $true, HelpMessage = "Path to the file or directory to transfer")]
        [ValidateNotNullOrEmpty()]
        [ValidateScript({ Test-Path $_ })]
        [string]$Path,

        [Parameter(Position = 1, Mandatory = $true)]
        [ArgumentCompleter({
                param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters)

                $server = if ($fakeBoundParameters.ContainsKey('Server')) {
                    $fakeBoundParameters['Server']
                } else {
                    'mini'
                }

                try {
                    Get-RsyncShares -Server $server | Where-Object { $_ -like "$wordToComplete*" }
                } catch {
                    @()
                }
            })]
        [string]$Share,

        [Parameter(Position = 2, Mandatory = $false)]
        [string]$Server = "mini",

        [Parameter(Position = 3, Mandatory = $false)]
        [string]$User = "backupuser"
    )


    $NoTimesOpt = '--no-times'
    $NoPermOpt = '--no-perm'
    $OmitDirTimesOpt = '--omit-dir-times'

    $resolvedPath = Resolve-Path -Path $Path
    $driveLetter = $resolvedPath.Drive.Name.ToLower()
    $relativePath = $resolvedPath.Path.Substring(3) -replace '\\', '/'
    $cygdrivePath = "/cygdrive/{0}/{1}" -f $driveLetter, $relativePath

    $rsyncExe = Find-Program -Name "rsync" -PathOnly -FirstMatch
    $passFile = "$env:USERPROFILE\.rsync-mini.pass"
    $destination = "{0}@{1}::{2}" -f $User, $Server, $Share

    if (-not (Test-Path $rsyncExe)) {
        Write-Error "rsync.exe not found at '$rsyncExe'. Please install rsync (e.g., via Chocolatey)."
        return
    }

    if (-not (Test-Path $passFile)) {
        Write-Error "Password file not found at $passFile. Create it with your rsync password."
        return
    }

    $isDirectory = (Get-Item -LiteralPath $resolvedPath).PSIsContainer

    $displayType = if ($isDirectory) { "directory" } else { "file" }
    Write-Host "Transferring $displayType to $destination..." -ForegroundColor Cyan

    # If it's a directory, make sure to add a trailing slash to avoid nesting
    if ($isDirectory -and (-not $cygdrivePath.EndsWith('/'))) {
        $cygdrivePath += '/'
    }

    $rsyncOptions = @(
        "-a", "-v", "-P",
        "--omit-dir-times",
        "--no-perms", "--no-group", "--no-owner",
        "--password-file=$passFile"
    )

    [string[]]$Out = & $rsyncExe @rsyncOptions $cygdrivePath $destination
    $lines = $Out | Select -Last 2
    $lines

}



About Guillaume Plante
Guillaume Plante

A developper with a passion for technology, music, astronomy and art. Coding range: hardware/drivers, security, ai,. c/c++, powershell

Email : guillaumeplante.qc@gmail.com

Website : https://arsscriptum.ddns.net

Useful Links