Reputation: 97
We have an architecture where there hundreds of java components deployed on multiple servers(40 odd). Business process involves data being passed from one component to another. Each component spits out logs. Every time there is an issue, I have to go on to the various server log directories(all mapped on as different network drives) and look at the log files.
I was wondering as whats the fastest way to copy from all these folders on 40 odd servers and copy to my local machine. I want to create a program/script that could do this. I can run this script right before I start researching an issue.
I wrote my own java program and used FileUtils.copyDirectory. Called in loop with 40 odd source directories and my local destination directory. Unfortunately, FileUtils.copyDirectory throws exception if the source file being written into.
This would be a huge help if I can implement a solution so that my trouble shooting process that spans across these file.
This is on Windows.
I am familiar with java. Could try other languages. Please do no recommend buying any products. This is just a developer hack that I am thinking of developing to solve my problems.
Upvotes: 0
Views: 5587
Reputation: 115
public static int extraction()
{
String hostname = "*server host name*";
String username = "*enter username*";
String password = "*enter password*";
String copyFrom = "*server path for example /home/file/abc.png *";
String copyTo = "*local path for example C:\\Documents *";
JSch jsch = new JSch();
Session session = null;
System.out.println("Trying to connect.....");
try
{
session = jsch.getSession(username, hostname, 22);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftpChannel = (ChannelSftp) channel;
sftpChannel.get(copyFrom, copyTo);
sftpChannel.exit();
session.disconnect();
}
catch (JSchException e)
{
e.printStackTrace();
}
catch (SftpException e)
{
e.printStackTrace();
}
System.out.println("Done !!");
return 0;
}
This was a method I used to obtain server log files from a single source but it can be reworked to obtain as many as required. DO excuse the formatting as this is formatting that I use and find comfortable.
Reference Link used: https://vinaydvd.wordpress.com/2013/12/08/copying-files-from-a-remote-linux-server-to-local-windows-server-using-java/
Upvotes: 4