Auto copy folders from ubuntu server to windows and vice versa

1. I need a script(like a scheduled cron job) which should perform Automatic copy (backup) of a directory from ubuntu server to windows and vice versa....

i tried every thing like samba mounting,ftp,etc..
with samba its possible to map the network drive of an ubuntu directory in windows..
but if the ubuntu server is not available then i cant access the network share..

Exactly this situation is happening for me while backing up tmp directory of ubuntu server ...

so in this case i need a permanent backup of tmp directory of ubuntu server in windows rather than a network share.

2. Also any ideas or scripts regarding automatic ftp (while doing a backup) from ubuntu server to windows... which can easily solve the above issue...

OK... <takes deep breath>... I've just tried this and made a few notes! It's not perfect though!

1. Install cygwin on your windows PC, following through the setup wizard, which you can get from here:
- when prompted by the installer, select Install from internet, accept the default settings
- when prompted for a local package directory, enter something like c:\packages
- choose a suitable mirror geographically near you
- At the select packages screen, select the following to install
Net -> openssh
Net -> rsync

Then click Next and follow through the rest of the setup

2. Configure cygwin's openssh server as follows:
- Start a bash shell from the windows machine. This is found under Start Menu -> Programs -> Cygwin -> Cygwin Bash Shell.
Under Vista/W7 You MUST right click on this and select Run as Administrator from the menu
- run

Code:

ssh-host-config

and answer questions as follows:

Should privilege separation be used? no (yes may be more secure, but might give problems if you want free reign to back up stuff)
Do you want to install sshd as a service? yes
Enter the value of CYGWIN for the daemon: ntsec
Do you want to use a different name? no
Create new privileged user account 'cyg_server'? yes
Please enter the password: <your choice>
Reenter: <same choice>

- run:

Code:

net start sshd

3. You will probably also need configure the windows firewall to allow SSH connections.
In Vista, you can start the Firewall configuration by searching for Firewall in the start menu. You want "Windows Firewall Settings".

On the Exceptions tab, click on "Add Port", enter "SSHD" as the name, and 22 as the port number. The protocol is TCP. Then click OK

Personally, I'm more a Linux man than a Windows man, so I'm a little hazy on Windows permissions, so you might have to fiddle a little with windows user accounts to get things to work properly for your backup.

4. Set up public key authentication between the 2 boxes.
The idea here is that both machines can log in to each other without a password. If you need help with this aspect, just let me know. But basically, it's like this:

a. On the Linux box, as the user from which you will be performing the rsync (ie the user which pulls data from the windows box):

Code:

ssh-keygen -t rsa

(accept the defaults, blank passphrase)
b. Get the public key, and copy it to the clipboard:

Code:

cat ~/.ssh/id_rsa.pub

(then copy the resulting text to the clipboard)
c. On the Windows box, logged in as the user you will be pulling data from, start a Cygwin bash session
d. Create the directory .ssh and change to it:

Code:

mkdir .ssh
cd .ssh

e. Create a file "authorized_keys", by using echo, as no other editor is probably installed:

Code:

echo "[I]<paste public key here>[/I]" >> authorized_keys

or if that's complicated, just do

Code:

notepad authorized_keys

and paste in there
f. From the Linux machine, test you can log in without a password. This is essential for unattended backups.

The first time you will get a message warning you that the authenticity of the new host can't be established.
This is perfectly normal, you'll only see this once. Say "yes".

5. Perform an rsync
Now you are ready to try an rsync!

For this step, bear in mind that the local PC c: drive is accessible from within the Cygwin bash shell as /cygdrive/c/

So the syntax goes something like this, this example is run from the Linux machine (replce with your own details):

rsync -az neil@10.43.0.50:/cygdrive/c/Users/neil/ backups/

On my setup, this will back up the whole of my User directory to the backups/ directory on my linux box

When you have this command working as you want, you can then automate it via cron.

This also works the other way round of course, from the Linux box to the windows box.
You would just have to sort out public key authentication the other way round, and then reverse the last 2 parameters of rsync

I hope that helps you, sorry it's not perfect, but hopefully it will get you started.