Friday, August 3, 2018

Automated Data Transfer - Linux To Windows + Vice Versa

Recently one of the developers where I work had an interesting task to accomplish: Transfer data from a Windows server to a Linux server and vice versa automatically every 5 minutes for a production environment. The data originated at the Windows server, was processed in the Linux server, copied to a different path and the result needed to be copied back to the Windows server, to a different folder. The accounting users were saving the original data to a Windows shared folder. The process needed to originate at the Windows server, since my team mates were not proficient in Linux, in case the files needed to be modified later on

Thanks to WinSCP, the solution was simple:

1) Create a batch file (WinSCP2Linux.bat) with permissions for service account to read and execute. The content should be something similar to this:

winscp.com /ini=nul /synchronize /script=C:\Users\LocalAdmin\ScriptCopyToOracle.txt

2) Create a text file (C:\Users\LocalAdmin\ScriptCopyToOracle.txt), with permissions for service account to read and execute. The content should be something similar to this:

open sftp://LinuxServerUsername:LinuxServerUserPassword@LinuxServerName/ -hostkey="ssh-rsa 2048 xx:yy:zz:aa:bb:cc..."
put C:\Path\To\Windows\Original\Files\* /path/to/Linux/server/firstfolder/
get /path/to/Linux/server/secondfolder/* C:\Path\To\Windows\Processed\Files\
exit

Note: LinuxServerUsername requires read and write permissions to both Linux folders for this procedure to work. Both Windows folders have read and write permissions

3) Create a task via "Task Scheduler" which runs the batch file every 5 minutes and runs under the service account specified in step 1)

The task will only synchronize files that are not in the folders every 5 minutes. I would recommend testing if the task works by opening an elevated command prompt ("Start", cmd, right click, "Run as administrator"), pasting winscp.com /ini=nul /synchronize /script=C:\Users\LocalAdmin\ScriptCopyToOracle.txt into it and hitting the "Enter" key. If there are no errors, you should see the processed files in the firstfolder in Linux and in the processed files folder in Windows

The Linux server was running Red Hat Linux and the Windows server was on Windows Server 2012

Regards,

F. Bobbio C.

No comments:

Post a Comment