KB
Backup & Recovery

Windows-to-Linux Rsync Sync for Nextcloud (via SSH)

2 min read493 words22 code blocks

At a glance#

  • Purpose: Replicate files from a Windows server to a Linux Nextcloud instance over SSH using rsync.
  • Applies to: Windows Server with cwRsync or WSL, to Linux Nextcloud.
  • Risk: Medium - misconfigured paths can overwrite data on the target.
  • Time: About 1 hour.
Environment: - Windows Server 2012 - Ubuntu Linux (Nextcloud host) - Users: Real usernames (e.g., sam, harry, etc.)

Objective#

Sync specific user folders from a Windows Server to a Linux-based Nextcloud instance using rsync over SSH with key-based authentication.


Tools Used#

ToolPurpose
cwRsyncRsync for Windows
Win32-OpenSSHSSH Server for Windows
Linux rsync/sshPulling data securely
CronScheduled sync automation
Nextcloud OCCScan/index synced files

Windows Server Setup#

1. Install cwRsync#

  1. Download: https://itefix.net/cwrsync
  2. Install to:
text
C:\Program Files\cwRsync
  1. Add to PATH:

System Properties → Environment Variables → Add:

text
C:\Program Files\cwRsync\bin
  1. Test:
text
rsync --version

2. Install Win32-OpenSSH#

  1. Download ZIP:

https://github.com/PowerShell/Win32-OpenSSH/releases

  1. Extract to:
text
C:\Program Files\OpenSSH
  1. Open PowerShell as Administrator:
powershell
cd 'C:\Program Files\OpenSSH'
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1
  1. Enable SSH:
powershell
Start-Service sshd
Set-Service sshd -StartupType Automatic
  1. Allow SSH in firewall:
powershell
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

3. Create .ssh Folder and Authorized Key File#

  1. On Windows:
text
mkdir "C:\Users\Administrator\.ssh"
  1. On Linux:
bash
ssh-keygen -t rsa -b 4096
cat ~/.ssh/id_rsa.pub
  1. Paste public key into:
text
C:\Users\Administrator\.ssh\authorized_keys
  1. Fix Permissions (PowerShell):
powershell
icacls "C:\Users\Administrator\.ssh" /inheritance:r /grant Administrator:F
icacls "C:\Users\Administrator\.ssh\authorized_keys" /inheritance:r /grant Administrator:F

4. Fix sshd_config#

File: C:\ProgramData\ssh\sshd_config

Do this:

  • Uncomment or add:

`` PubkeyAuthentication yes AuthorizedKeysFile __PROGRAMDATA__/ssh/authorized_keys %h/.ssh/authorized_keys ``

  • 💡 Optional: Comment out this block at last line to use .ssh folder:

`` # Match Group administrators # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys ``

Restart SSH:

powershell
Restart-Service sshd

5. Test SSH from Linux#

bash
ssh Administrator@<windows-ip>

✅ You should log in without password.


Folder Structure (Windows)#

Create folders for each user:

text
mkdir D:\sam\data
mkdir D:\harry\data
mkdir D:\kane\data

Add test files (optional):

text
D:\sam\data\report.pdf

Linux Side (Nextcloud Host)#

1. Folder Structure#

Ensure these exist:

bash
/mnt/ncdata/sam/files/
/mnt/ncdata/harry/files/
/mnt/ncdata/kane/files/

Per-User Rsync Script#

Example: /root/sam.sh#

bash
#!/bin/bash

REMOTE_USER="Administrator"
REMOTE_IP="198.51.100.251"
NEXTCLOUD_OCC="/var/www/nextcloud/occ"

USER="sam"
REMOTE_PATH="/cygdrive/d/${USER}/"
LOCAL_PATH="/mnt/ncdata/${USER}/files/"

echo "Syncing $USER ..."

rsync -avz --blocking-io -e ssh "${REMOTE_USER}@${REMOTE_IP}:${REMOTE_PATH}" "${LOCAL_PATH}"
chown -R www-data:www-data "/mnt/ncdata/${USER}"
sudo -u www-data php "${NEXTCLOUD_OCC}" files:scan --path="/${USER}/files/"

echo "Sync complete for $USER"

Repeat for each user:

  • sam.sh
  • harry.sh
  • kane.sh

Schedule with Cron#

Edit crontab:

bash
crontab -e

Add per-user jobs:

text
0 2 * * * /root/sam.sh >> /var/log/sync_sam.log 2>&1
5 2 * * * /root/harry.sh >> /var/log/sync_harry.log 2>&1
10 2 * * * /root/kane.sh >> /var/log/sync_kane.log 2>&1

Debugging Tips#

  • SSH logs:

C:\ProgramData\ssh\logs\sshd.log or sshd-session.log

  • Linux rsync test:
bash
rsync -avz Administrator@<windows-ip>:/cygdrive/d/sam/ /mnt/ncdata/sam/files/
  • Test permissions:
bash
chown -R www-data:www-data /mnt/ncdata/sam
  • Scan Nextcloud:
bash
sudo -u www-data php /var/www/nextcloud/occ files:scan --path="/sam/files/"