Using rsync and cygwin to Sync Files from a Linux Server to a Windows Notebook PC
You will be installing some open-source software on the notebook PC called cygwin.
Within cygwin is a program / package called: rsync. rsync downloads files from the Linux server to the notebook PC. rsync is secure because it uses encryption; it can be used without fear that sensitive data will be intercepted or compromised.
Using rsync, the server's files can be downloaded easily no matter whether the notebook is local (on the same LAN as the server), or in a remote location. The first time the rsync script is run it will copy all of the server's files that you specify. After that, rsync will only download any newer or changed files from the server.
If you have physical access to the client's notebook PC, skip to the section below titled: Installation of cygwin and rsync. If you don't have the notebook PC in front of you, you will first need to gain temporary control of the notebook to add the cygwin and rsync software.
Getting Control of the Notebook PC from a Remote Location
For notebook PCs with Windows XP Pro, you can use Windows' Remote Desktop to control the notebook PC from a remote location. You will probably also need to use a VPN to connect your controlling PC to the notebook PC's LAN.
As an alternative to Remote Desktop, you might also take a look at TightVNC. With TightVNC both parties can see the screen simultaneously. Just follow the instructions to setup a TightVNC server on the remote (host) PC and a TightVNC client on the PC you will be doing the Cygwin installation from.
If the client's notebook PC has never used Remote Desktop it must be enabled. This article from Microsoft explains how to enable Remote Desktop. In the Windows Control panel, System Option, Remote tab, check the box under Remote Desktop that says: Allow users to connect remotely to this computer.
If the client's notebook PC does not have Windows XP Pro, you can not use Windows Remote Desktop to remotely control that PC. Instead you must use another resource such as Fog Creek Software's Copilot.
Installation of cygwin and rsync
Once you have control of the notebook PC, go to the cygwin website and click on the link that says: Install or update now!. This will download cygwin's setup.exe program. Run the program and select the following choices. Press the Next button after each choice.
- Install from Internet.
- Accept the Root Directory of C:\cygwin and Install For All Users. Default Text File Type is Unix / binary.
- Accept the default Local Package Directory, which is the Windows desktop of the notebook PC. This will enable you to easily put a cygwin icon on the Windows desktop.
- Select Direct Connection if directly connected to the Internet or connected through a transparent router.
- Pick one of the Available Download Sites. The sites change frequently but I most recently used http://cygwin.lilengine.com. This will download the list of current packages / programs, from which you will choose what you want installed.
- You can Maximize the Cygwin Setup window to see more. Click on Net. Scroll down to openssh and select it to install it. Also select rsync. Next click on Shells, and select bash if it is not already selected.
- Press the Next button at the lower right corner of the screen. The setup and installation of Cygwin will begin.
- Setup is complete. Check Create icon on Desktop, then press finish. The cygwin icon will appear on your desktop.
Cygwin, rsync, and the bash shell are now installed.
Generate an ssh keypair
Next you will create a pair of computer cryptographic "keys" that permit the notebook PC to login and download the server's files without entering a password. Start cygwin by doubleclicking on the cygwin icon on the notebook PC's desktop. A command window similar to the DOS command window will open.
You will now generate your ssh keypair using this command:
ssh-keygen -t dsa -b 2048
The -t dsa option specifies that the key type generated will be dsa for SSH-2 protocol.
The -b 2048 option specifies a key length of 2048 bits, which should be highly secure. When generating the keys you will be prompted for a passphrase. Press Enter without typing in a pass phrase so that you won't be prompted during the file downloading procedure.
Next, you will need to enable the user's account on the Linux server to permit bash logins. From the Linux server, type this command:
smbldap-usermod -s /bin/bash [username]
Once the keys are generated you will need to add your public key to the Linux server's list of approved keys. Usually this involves concatenating your new public key to the server's authorized_keys2 file.
Once your public key has been successfully installed on the server, you can test the connection by logging into the server using ssh. Type this command in the cygwin window:
ssh [your user name]@[server name]
After a few seconds your prompt in the command window should change to reflect that you are now logged into the remote Linux server. Type exit to close the connection to the remote server.
Once you can login to the Linux server without being prompted for your password, you are ready to setup the script that downloads the files from the Linux server to the notebook PC.
Create a Script on the Notebook PC
From the cygwin window, create a script like the one below in this directory on the notebook PC:
Note that in cygwin directories are separated by forward slashes: /
Name the script: server-download.sh and put some comments at the start of the script before the actual rsync command:
# server-download.sh # # Install on Notebook PCs # # rsync tool to download server data # from [server name] # to [user name's notebook PC] # # uses ssh key pairs to login automatically # # last edited: [last edit date] by [author] # # download only those files on [server name] in [server target directory] # that are newer than what is already on the notebook PC rsync -avzu [user name]@[server name]:[server directory] [notebook PC directory]
- You should add the -n option to rsync the first time this is run to do a "dry run" which does not do any actual downloading. By doing this you will be able to see if the script is going to correctly do what you want. Thus, the rsync command becomes:
rsync -avzun [remainder as above]
- You can create additional rsync lines in the script file to download data from more than one directory on the server.
Next you need to create an icon on the desktop that will permit the script to be easily run by the user so that they won't have to enter the script name in a command window to do the file download.
Add an Icon to the Notebook PC's Desktop
- Right click on the notebook PC's desktop. Select New, Shortcut from the menu that appears.
- Push Browse and go to the folder called: C:\cygwin\bin
- Select the program bash.exe and press OK.
- Create a useful name for the desktop shortcut such as: server-download.
- Press Finish.
- The new icon will appear on the desktop. Right click on the new icon to bring up the Properties box.
- From the Shortcut tab, append this information to the Target:
--login -i '/cygdrive/c/cygwin/server-download.sh'
- The full Target should now read:
C:\cygwin\bin\bash.exe --login -i '/cygdrive/c/cygwin/server-download.sh'
- In the Start in box enter: C:\cygwin\bin
- Push the Change Icon button, browse to the C:\cygwin directory, and select cygwin.ico as the icon to be displayed.
- Press the Apply button at lower right.
- Press the OK button.
Now the icon for the script will appear on the desktop.
If you used the -n option with rsync, you can double click the icon and do a dry run of the download. If everything goes well, you can edit the script to remove the -n option and the script will now do the full download. The first run might take awhile but after the first run only the changed files will be downloaded. When the script is finished the window will automatically close.
Earlier this year, we also began using an open-source file synchronization program called Unison, written by Professor Benjamin Pierce of the University of Pennsylvania. Unison supports bi-directional file synchronization, unlike rsync, which transfers files in only one direction at a time.
- If you need bi-directional file synchronization and you can manage the potential issues related to people accidentally trying to change the same file at the same time, take a look at Unison.
- If you only need to download files from a central source, and you are concerned that multiple users might accidentally make changes to the same original file at the same time, stick with rsync.
If you enjoyed this article, we invite you to learn more about how you can become a high achieving computer professional.