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#
| Tool | Purpose |
|---|---|
| cwRsync | Rsync for Windows |
| Win32-OpenSSH | SSH Server for Windows |
| Linux rsync/ssh | Pulling data securely |
| Cron | Scheduled sync automation |
| Nextcloud OCC | Scan/index synced files |
Windows Server Setup#
1. Install cwRsync#
- Download: https://itefix.net/cwrsync
- Install to:
C:\Program Files\cwRsync- Add to PATH:
System Properties → Environment Variables → Add:
C:\Program Files\cwRsync\bin- Test:
rsync --version2. Install Win32-OpenSSH#
- Download ZIP:
https://github.com/PowerShell/Win32-OpenSSH/releases
- Extract to:
C:\Program Files\OpenSSH- Open PowerShell as Administrator:
cd 'C:\Program Files\OpenSSH'
powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1- Enable SSH:
Start-Service sshd
Set-Service sshd -StartupType Automatic- Allow SSH in firewall:
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 223. Create .ssh Folder and Authorized Key File#
- On Windows:
mkdir "C:\Users\Administrator\.ssh"- On Linux:
ssh-keygen -t rsa -b 4096
cat ~/.ssh/id_rsa.pub- Paste public key into:
C:\Users\Administrator\.ssh\authorized_keys- Fix Permissions (PowerShell):
icacls "C:\Users\Administrator\.ssh" /inheritance:r /grant Administrator:F
icacls "C:\Users\Administrator\.ssh\authorized_keys" /inheritance:r /grant Administrator:F4. 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
.sshfolder:
`` # Match Group administrators # AuthorizedKeysFile __PROGRAMDATA__/ssh/administrators_authorized_keys ``
Restart SSH:
Restart-Service sshd5. Test SSH from Linux#
ssh Administrator@<windows-ip>✅ You should log in without password.
Folder Structure (Windows)#
Create folders for each user:
mkdir D:\sam\data
mkdir D:\harry\data
mkdir D:\kane\dataAdd test files (optional):
D:\sam\data\report.pdfLinux Side (Nextcloud Host)#
1. Folder Structure#
Ensure these exist:
/mnt/ncdata/sam/files/
/mnt/ncdata/harry/files/
/mnt/ncdata/kane/files/Per-User Rsync Script#
Example: /root/sam.sh#
#!/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.shharry.shkane.sh
Schedule with Cron#
Edit crontab:
crontab -eAdd per-user jobs:
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>&1Debugging Tips#
- SSH logs:
C:\ProgramData\ssh\logs\sshd.log or sshd-session.log
- Linux rsync test:
rsync -avz Administrator@<windows-ip>:/cygdrive/d/sam/ /mnt/ncdata/sam/files/- Test permissions:
chown -R www-data:www-data /mnt/ncdata/sam- Scan Nextcloud:
sudo -u www-data php /var/www/nextcloud/occ files:scan --path="/sam/files/"