Backing up a Remote Linux Machine With Windows (Using Rsync and Cygwin)

KB ID 0000892 

Problem

The goal here was for me to get a backup of my VPS server (Running CentOS 6). My background is primarily Windows based, so I wanted a solution where I can just run the backup from a Windows machine, (i.e. my my laptop) and let it connect, login and perform the backup.

What is Rsync? If you are familiar with Robocopy it’s similar, it can perform a backup/sync of data and encrypt that data while it’s passing over the network. In addition, once the initial sync has been done, the next time you run it, it only replicates the changes. This makes it ideal for backups.

What is Cygwin? Basically it’s a Linux ‘Shell’ that will run on a Windows machine.

Solution

 

Step 1: Install Cygwin on My Windows Client

1. Download Cygwin (URL is on the image below), and install on the machine that will be performing the backups. When prompted accept all the defaults to download form the internet > Accept the install directory C:cgywin64 > Install for All users > Set the local Package Directory to the Desktop > Internet = Direct Connection > Choose a Download site > Next.

Install Cygwin

2. When asked to select packages > Expand Net.

Cygwin Packages

3. Select openssh and rsync to be installed.

Cygwin openssh and rsync

4. Expand shells > Select bash to be installed. > Complete the installation.

Cygwin install bash

Step 2: Generate SSH Keys in Cygwin

5. Launch Cygwin and generate some SSH Keys.

ssh-keygen -t rsa -b 2048

KEEP PRESSING ENTER TO ACCEPT THE DEFAULTS, AND HAVE A BLANK PASSPHRASE

Cygwin Create SSH Keys

Step:3 Create a User (On the Remote Linux Host) to Perform Backups

6. Connect to the server via SSH, (or open a terminal session). Logon as, (or su to) root.

useradd {username} -s /bin/bash
passwd {username}
ENTER AND CONFIRM THE PASSWORD 

Linux Create Backup User

7. To ensure your user has the correct folders in their home folder the easiest way is generate a pair of keys on the remote Linux machine (the same as you did before).

ssh-keygen -t rsa -b 2048

Create .ssh directory

Step 4: Copy the Public SSH Key from the Windows Machine to the Linux Machine

8. Above, Cygwin told us the keys are in /home/{username}/.ssh > Go to that directory and make sure they are there > Make a copy of the id_rsa.pub key > Call the copy authorized_keys > Copy that key to the correct folder on the remote Linux machine (via SCP).

cd /home/{username}/.ssh <<Note This is the username on the Cygwin machine)
ls
CHECK id_rsa.pub IS LISTED
cp id_rsa.pub authorized_keys
ls
CHECK authorized_keys IS LISTED
scp
authorized_keys {username}@{Linux Machine's name/IP}:/home/{username}/.ssh

Note The username (above) is the username on the Linux Machine

Copy from Cygwin to Linux via SCP

9. Now check we can login to the remote Linux machine, from the Windows machine (without having to provide a password for the user we created). Note: Sometimes you need to do this twice before it will work.

ssh {username}@{Linux Machine's name/IP}

If successful, your prompt should change to that of the remote Linux machine.

Test SSH from Cygwin

10. To return to Cygwin, simply type exit.

Disconnect SSH Session

Step 5: On the Windows Machine Create a Backup Job

11. On the Windows machine create a folder that will hold the backup files (create it in the C:cygwin64 folder).

Backup VPS to Windows

12. Lets test our backup to that folder. (Note: This does not back any data up it just performs a ‘dry run’).

rsync -avzun {username}@{Linux Machine's name/IP}:/ /VPS-Backup

Note: Above I’ve chosen the root ‘/’ directory, you may just want to select specific folders to backup e.g.

  • /var/www/ The Default location for Apache’s Website Files.
  • /var/lib/mysql The Default location for MySQL Databases.

Warning: Folder locations may differ depending on the server and how it was setup.

Rsync to backup VPS

13. Tailor the following, and save it on the Windows machine, in the C:cyqwinbin directory as Remote-Server-Backup.sh

# Remote-Server-Backup.sh
#
#
# rsync tool to download server data
# from [Remote Linux Server name] to [Windows Backup Machine]
#
#
# download only those files on [Remote Linux Server name] in
#
[server directory]
# Only files that are newer than what is already on the
#
[Windows Backup Machine Directory]
#
# Syntax
#
# rsync -avzu [user name]@[Remote Linux Server name]:
#[server directory] [Windows Backup Machine Directory]
# Windows Shortcut Target Should be
#C:cygwin64binbash.exe --login -i '/bin/Remote-Server-Backup.sh'

rsync -avzu {username}@{Linux Machine's name/IP}:/ /VPS-Backup

Remote Backup Server Script

14. On the Windows machine create a new shortcut.

Windows Create Shortcut

15. Browse to, and select c:cygwinbinbash.exe

Windows Run Bash shell

16. Give it a sensible name > Finish

Shortcut Name

17. Open the properties of the shortcut and change the Target: to;

C:cygwin64binbash.exe --login -i '/bin/Remote-Server-Backup.sh'

Note: You may also want to change the icon to the Cygwin one at C:cygwin64Cygwin.ico

Cygwin Launch Shell Script

18. Run the shortcut to perform the backup.

Run Rsync Backup from Cygwin

You could (if you wanted), use the Window scheduler to schedule this for you, but I prefer to do it myself.

Related Articles, References, Credits, or External Links

NA

Author: Migrated

Share This Post On