Search This Blog

Wednesday, August 5, 2015

SETUP AUTO BACKUP BATCH SCRIPT in WINDOWS using CYGWIN RSYNC SSH

 

Add Cygwin Path to Windows Environment Variable

  Follow Step 1 to 5

 

 

Rsync on Windows

Rsync is available for Windows through cygwin or as stand-alone packaged in cwrsync http://sourceforge.net/projects/sereds. This is very convenient for automated backups. Install one of them (not both) and add the path to the Windows system variables: # Control Panel -> System -> tab Advanced, button Environment Variables. Edit the "Path" system variable and add the full path to the installed rsync, e.g. C:\Program Files\cwRsync\bin or C:\cygwin\bin. This way the commands rsync and ssh are available in a Windows command shell.

Public key authentication

Rsync is automatically tunneled over SSH and thus uses the SSH authentication on the server. Automatic backups have to avoid a user interaction, for this the SSH public key authentication can be used and the rsync command will run without a password.
All the following commands are executed within a Windows console. In a console (Start -> Run -> cmd) create and upload the key as described in SSH, change "user" and "server" as appropriate. If the file authorized_keys2 does not exist yet, simply copy id_dsa.pub to authorized_keys2 and upload it.
# ssh-keygen -t dsa -N ''                   # Creates a public and a private key
# rsync user@server:.ssh/authorized_keys2 . # Copy the file locally from the server
# cat id_dsa.pub >> authorized_keys2        # Or use an editor to add the key
# rsync authorized_keys2 user@server:.ssh/  # Copy the file back to the server
# del authorized_keys2                      # Remove the local copy
Now test it with (in one line):
rsync -rv "/cygdrive/c/Documents and Settings/%USERNAME%/My Documents/" \
'user@server:My\ Documents/'

Automatic backup

Use a batch file to automate the backup and add the file in the scheduled tasks (Programs - Accessories - System Tools - Scheduled Tasks). For example create the file backup.bat and replace user@server
 Source: remote Linux server user@server:/home/abc/imp-files
 Destination: local XP machine C:\cygwin\home\xyz\backup\
 
@ECHO OFF

REM rsync the directory My Documents

SETLOCAL
SET CWRSYNCHOME=C:\cygwin\home\xyz\backup\
SET CYGWIN=nontsec

SET CWOLDPATH=%PATH%

REM uncomment the next line when using cygwin

SET PATH=%CWRSYNCHOME%\BIN;%PATH%

rsync -avzv --exclude "log" -e 'ssh ' "user@server:/home/abc/imp-files/" '/cygdrive/c/cygwin/home/xyz/backup/'

No comments: