Trying to connect your Linux-based servers to Windows workstations to pull data via rsync? Here’s how.
Install OpenSSH service
On the Windows side, install the optional OpenSSH Service:
# Powershell
Add-WindowsCapability -Online -Name OpenSSH.Server*
Start-Service sshd
Set-Service -Name sshd -StartupType 'Automatic'
New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd)' `
-Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
There are plenty of tutorials to do it through the GUI with Optional Features; given how much Microsoft is changing their menu options, you will need to look it up for your particular version of Windows.
From your Linux machine, make sure you can connect via SSH to your machine:
# If using a domain account
ssh USERNAME@fqdn@WindowsHost
# If using a local username (or after your initial connection)
ssh USERNAME@WindowsHost
You should see a Command Prompt appear in your terminal. You can optionally change the subsystem to use Powershell, but that’s for another post.
Add your public key to your Windows target
Add your public key to either %USERPROFILE%\.ssh\authorized_keys. If you need help finding how to generate or use keys, check out my post about SSH Agents. A caveat, if you are planning on logging in as the Built-In Administrator account, you must add the public key to C:\ProgramData\administrators_authorized_keys. If you are using Notepad, you’ll need to make sure to remove the .txt extension being added by default.
Test this out by running the above ssh commands; optionally, add -A to enable Agent Forwarding (if you have an agent running before you SSH’d to your Linux box) or -i /path/to/private/key. Using an agent is easiest.
Install cygwin for rsync
Download Cygwin from their official website (under the heading “Cygwin version”).
During the installation:
- (o) Install from Internet
- Install For: (o) All Users
- Choose the defaults until you get to “Choose a Download Site”. Choose one that is close to you, then click Next.
- The Package Selection screen comes up. Search (at the top) for
rsync, and change View to Full. In thersyncrow that appears, change theNewoption to the second-latest version. In my case, it’s3.3.0-1. Click Next at the bottom. - Confirm, then click Next to start installing. This may take a few minutes if you have a slow connection.
- On the last page, click Finish.
Congratulations, you’ve installed rsync to C:\cygwin\bin\rsync.exe! Now, let’s make it useful.
Test your rsync command
From your Linux side, create a test folder to try copying files into (for example, ~/test), then try to rsync into it:
# Make a directory called `test` under your home folder
mkdir -p ~/test
# Copy all attributes, be verbose with filenames, allow Partial (resumeable) transfers, and compress the transfer
rsync -avPz [email protected]:/cygdrive/c/ProgramData/ ~/test/
Create a snap-shot style script
Assuming you’re here cause you want to make a backup system, here’s how you can snapshot it.
Create a new bash script, and save it somewhere you can access on your Linux box:
#!/bin/bash
# Backs up the Windows target. Adapted from https://wiki.archlinux.org/title/Rsync#Snapshot_backup
TARGET_HOST="10.2.10.8"
TARGET_USER="administrator"
TARGET_SRC="/cygdrive/c/ProgramData/"
BACKUP_TARGET="/var/backups/winbox/"
### DO NOT EDIT BELOW
#####################
OPT="-aPhz"
LINK="--link-dest=${BACKUP_TARGET}/"
LAST="${BACKUP_TARGET}/last"
date="$(date "+%Y-%b-%d:_%T")"
# Sanity checks
if [ -z "${TARGET_HOST}" ] || [ -z "${TARGET_USER}" ] || [ -z "${TARGET_SRC}" ] ; then
echo "Error! Variables not defined for the TARGET"
exit 1
fi
if [ -z "${BACKUP_TARGET}" ]; then
echo "Error! Backup folder not set! Aborting..."
exit 1
fi
if [ ! -d "${BACKUP_TARGET}" ]; then
echo "Backup folder does not exist. Attempting to create..."
mkdir -p "${BACKUP_TARGET}"
if [ ! -d "${BACKUP_TARGET}" ]; then
echo "Failed to create backup folder. Aborting..."
exit 2
fi
fi
rsync "${OPT}" "${LINK}" "${TARGET_USER}@${TARGET_HOST}:${TARGET_SRC}" "${BACKUP_TARGET}/${date}"
rm -f "${LAST}"
ln -s "${SNAP}/${date}" "${LAST}"
This will produce time-stamped and hard-linked copies of existing files (read: unmodified files do not take up extra room), with the most recent being linked to a folder called ${BACKUP_TARGET}/last.