Automated file sync across local Windows directory and remote Linux directory, with Cygwin and rsync

In Software Engineering, Snippet

See References for other relevant guides. These are my current set of notes.

Objectives

  • Sync a directory on a local Windows machine with a directory on a remote Linux machine
  • Schedule the sync on a time trigger

Setup

  • “Local” Windows 7 system with Cygwin 6.1 with rsync installed
  • “Remote” Linux system (with an rsync daemon running)

Create ssh client key on the local Win7 machine

C:\Users\victoryee\Lab>bash

bash-4.1$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/victoryee/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
23:74:59:58:69:ee:4a:3c:09:73:dc:63:58:c8:77:97 victoryee@PC01
The key's randomart image is:
+--[ RSA 2048]----+
|       . +o.   . |
|        +o= . E  |
|      ..oB . .   |
|     .o.+ =      |
|      .=S+ .     |
|       .=..      |
|       . o       |
|        .        |
|                 |
+-----------------+

bash-4.1$ scp .ssh/id_rsa.pub vyee@ssh.server.com:/home/vyee/.ssh/authorized_keys
Password for vyee:
id_rsa.pub                                         100%  396     0.4KB/s   00:00

Create shell script on the local Win7 machine

#! /bin/bash
datetimef=$(date +"%Y%d%m_%H%M")
logfile="/cygdrive/C/Users/victoryee/Logs/auto_rsync/auto_rsync_${datetimef}.log"
localdir="/cygdrive/C/Users/victoryee/Projects"
rsync.exe  -avz --log-file="$logfile" --delete "$localdir" vyee@ssh.server.com:/home/vyee/projects
unix2dos "$logfile"
C:\Users\victoryee>cd automate
C:\Users\victoryee\automate>dos2unix auto_rsync.sh
dos2unix: converting file auto_rsync.sh to Unix format ...

Schedule the shell script in the Task Scheduler on the local Win7 machine

A shortcut to the Task Scheduler is in Control Panel > Administrative Tools.
Alternatively, use Run > taskschd.msc

  1. Actions Panel > Create Task…
  2. Tab: General > Fill out the Name field
  3. (optional) Tab: General > Section: Security options > Select radio button “Run whether the user is logged in or not”
    (optional) Select checkbox “Do not store password.”
  4. Tab: Triggers > Click “New…”
  5. Dialog box: New Trigger > Section: Advanced settings > Select checkbox “Repeat task every:” > Select dropdown “1 hour”
  6. (optional) Also select the checkbox “Stop task if it runs longer than”
  7. Tab: Actions > Click “New…”
  8. Dialog box: New Action > Section: Settings > In the “Program/script” field, enter “C:\cygwin\bin\bash.exe” (or wherever the cygwin bash binary is).
  9. Then, while still in the “New Action” dialog box, enter in the “Add arguments (optional)” field: “C:\Users\victoryee\automate\auto_rsync.sh”
  10. Click OK on everything to save and exit.

Notes

  1. Dropbox or Google Drive (or any other cloud-based file sync solution) is typically my default action when I want to sync directories. But sometimes there are security or storage space concerns.
  2. rsync performs incremental sync, which saves bandwidth. But I have not found a (simple) way to have some real-time rsync client daemon monitor for file changes (as Dropbox seems to do seamlessly).
  3. Though the sync is being initialized from the local Windows machine, (I believe) this is a two-way sync. (I’m not sure since I actually do much of my dev work on a Windows machine but need the files mirrored on a linux filesystem that a compute cluster can access).
  4. Scheduling a sync from a local Linux machine to a remote Windows file share is a bit more complicated, and I haven’t found a reason to do this yet. Future post perhaps.
  5. I prefer to keep logs for debugging and tracking purposes, at least initially. But since a log file will be generated every hour (or whatever the scheduled interval is), a lot of log files can accumulate. It is possible to append logs to one another (if you don’t want to see the timestamp in the filename), or add some kind of log file flushing capability.
  6. To prevent rsync from making a new directory in the target location, add a forward slash (“/”) to the end of the source location.
  7. I may want to write an AutoHotKey script to create a keyboard shortcut to manually call the script when needed.

References

  1. O’Reilly Linux Server Hack #66 – Quick Logins with ssh Client Keys
  2. How to setup an automated backup of your data using cygwin rsync (freeware)
  3. LifeHacker: Geek to Live: Mirror files across systems with rsync

Leave a Reply