0

I have an application running on a Linux server. While running, this app creates certain files and after they are created they are copied to a different server (also Linux) using the rsync program. At first I was syncing the whole library using the next piece of code:

    private void sync(){
        try {
            String[] cmd = new String[]{"rsync", "--ignore-existing", "-Wre", "/usr/bin/sudo /usr/bin/ssh -i /home/user/.ssh/id_rsa", "/home/user/IntSrv/Sync/Observation",
            "user@some.server:/home/user/GenSrv/Sync/"};
            Process p = new ProcessBuilder().command(cmd).start();
        } catch (IOException ex) {ex.printStackTrace();}}

It was working fine and all the files were synced.

Currently I am trying to change the code in order to sync single file at a time. I have changed the code to:

private void sync(){
        String orgPath = "/home/user/IntSrv/Sync/" + F.getAbsolutePath().substring(F.getAbsolutePath().indexOf("Observation")).replace("\\", "/");
        String destPath = "user@some.server:/home/user/GenSrv/Sync/" + F.getAbsolutePath().substring(F.getAbsolutePath().indexOf("Observation"), F.getAbsolutePath().indexOf(F.getName())).replace("\\", "/");
        try {
            String[] cmd = new String[]{"rsync", "-avz", "/usr/bin/sudo /usr/bin/ssh -i /home/user/.ssh/id_rsa", orgPath, destPath};
            Process p = new ProcessBuilder().command(cmd).start();
        } catch (IOException ex) {ex.printStackTrace();}
    }

Unfortunately, the files does not sync. Here is an example for the orgPath and destPath:

orgPath = "/home/user/IntSrv/Sync/Observation/2017/204/17d/06/SROD204G30.17o"

destPath = "user@some.server:/home/user/GenSrv/Sync/Observation/2017/204/17d/06/"

I have printed out the rsync output on screen. While using the working code it prints out :

sending incremental file list Observation/2017/204/17d/08/BRIG204I18.17o 966.11K 100% 111.26MB/s 0:00:00 (xfer#1, to-check=0/6) sent 966.40K bytes received 36 bytes 1.93M bytes/sec

While running the second one it prints out:

Permission denied (publickey). rsync: connection unexpectedly closed (0 bytes received so far) [sender] rsync error: unexplained error (code 255) at io.c(600) [sender=3.0.6]

How is that possible? Why would i have permission to sync the whole directory but not a single file? What am i doing wrong? Can anyone solve this issue? Thank you for your help.

Yonatan
  • 2,543
  • 2
  • 19
  • 20

2 Answers2

0

You might get some clues from the output of rsync command. You can get this using: process.getInputStream(). (See How to redirect Process Builder's output to a string? for ways to consume). Also I'd check the obvious, do the files exist, and does the exact same command work on the command line? (I would have written the above as a comment rather than an answer but I'm a noob on here so dont have the required "reputation", anyway hope it helped.)

0

You are missing the -e flag, that will instruct rsync to use ssh with the ssh private key.

Change "-avz" to "-avze" and you are done.

Yonatan
  • 2,543
  • 2
  • 19
  • 20