Tuesday, October 4, 2011

Using Rsync to synchronize files/folders from Windows to Linux with SSH tunnel


Rsync Server on Linux

1. Rsync can be found in Linux repository and is easy to install using yum or apt-get commands.
                >> yum install rsync
                >> apt-get install rsync

2. Create a file rsyncd.conf at /etc and add following lines
                >> nano /etc/rsyncd.conf
               [your_user_sync]
                path = /home/your_user/backup
                comment = backup
                uid = your_user
                gid = your_user
                read only = false
                auth users = your_user
                secrets file = /etc/rsyncd.secrets
Here your_user is the username in your Linux machine.

3. Set permission to that file
                >>chmod 644 /etc/rsyncd.conf

4. Create secrets file and add following lines
                >> nano /etc/rsyncd.secrets
                Your_user:your_password

5. Give permission
                >>chmod 600 /etc/rsyncd.secrets

6. Set RSYNC_ENABLE=true in the file /etc/default/rsync

7. Restart rsync
                >> /etc/init.d/rsync restart

Rsync Client on Windows (without SSH)

1.For rsync client on windows, install Cygwin from http://www.cygwin.com/
Don’t forget to select rsync and nano  while installing cygwin.
Note: If you want to use SSH also for this task then better install ssh  also while installing Cygwin.

2. Add C:/cygwin_install_dir/bin to Windows path.

3. Create secret file in Windows. Open cygwin console/shell and make a secret file in root. The content is same as you have entered in secret file in Linux.
                >> nano / secret
                Your_user:your_password

4. Give permission and owner
                >>chmod 600 /secret
                >> chown Administrator:SYSTEM  /secret

5. Now create a Windows batch file to run rsync. The example contents in this batch file might be

C:\cygwin\bin\rsync.exe-qrtz-password-file=C:\cygwin\secret”/cygwin/d/your_folder”
Your_user@ipaddress::your_user_sync

Here your_folder is the folder you are backing up. As suggested from rsyncd.conf file, this folder will be backed up at /home/your_user/backup.


6. For scheduling the syncing process, above batch file (let us name it rsync.bat) can be put in Windows Scheduled Tasks and can be configured to run it automatically at your desired time instance.

Syncing using SSH with Rsync

Sometime rysnc alone cannot sync files/folders from Windows to Linux. In Windows 2003 Server often you will get problem when syncing using rsync alone. In this case we can use SSH.

1. SSH needs username and password to login where human intervention is needed. But scheduling or automatic syncing, computer has to do the task without the need of entering username and password. For this you have to generate RSA public/private key. Go to Cygwin shell in Windows and type following command to generate the key
>>ssh-keygen -t rsa
 Just press enter key for all the questions asked.

2.  Get the file where key is saved(id_rsa.pub)
>>cd ~/.ssh
    Here you will find the file id_rsa.pub

3. Just to test. Login to Linux machine by ssh
>>ssh -l remote_user remote_ipaddress
    Just press yes and enter password. As you can see there you need password to connect remote machine.
>>exit

4. Copy id_rsa.pub to Linux machine. You can use scp to do that in cygwin shell
                                           >>scp ~/.ssh/id_rsa.pub remote_user@ipaddress:/root/
Enter password and your file will be copied in /root folder

5. Connect to remote machine like in 3. and add content of this id_rsa.pub to authorized keys.
                   >>cd ~/.ssh/
                  >>cat /root/id_rsa.pub>>authorized_keys
You can copy and paste content of id_rsa.pub in the file authorized_keys file manually also.
                  >>exit

6. Now test if you need password again to connect to Linux using SSH
                 >>ssh -l remote _user remote_ipaddress
You should be logged in without password

7.Now make a batch file with following contents.
C:\cygwin\bin\rsync.exe-avrtze ssh”/cygwin/d/your_folder” remote_user@ipaddress:
/home/remote_user/backup

8. You can run this batch file using Windows Scheduled Task to automate it.

No comments:

Post a Comment