以下命令可直接使用:
rsync -rtuc --delete-after --exclude '.git*' --filter 'protect .git/**/*' ~/some/source/ ~/some/destination/
但是当通过java:
运行时private Boolean syncFiles() {
// Success flag default to true
Boolean success = true;
// Attempt sync repo
try {
ProcessBuilder runtimeProcessBuilder = new ProcessBuilder().inheritIO().command(new String[]{
"rsync", "-rtuc","--delete-after", "--exclude", "'.git*'", "--filter", "'protect .git/**/*'", "~/some/source/", "~/some/destination/"
});
// Wait until process terminates
int output = runtimeProcessBuilder.start().waitFor();
// Determine if successful
if (output == 0) {
System.out.println("Backup of " + getSource() + " to " + getDestination()
+ " was successful");
} else {
System.out.println("Error: rsync returned error code: " + output);
success = false;
}
} catch (Exception ex) {
success = false;
System.out.println("Error:");
System.out.println(ex.getMessage());
Logger.getLogger(Rsync.class.getName()).log(Level.SEVERE, null, ex);
}
return success;
}
我收到错误:
未知过滤规则:`'protect .git / ** / *''错误:rsync返回错误 代码:1 rsync错误:exclude.c上的语法或用法错误(代码1)(902) [客户= 3.1.2]
答案 0 :(得分:0)
解决方案的答案如下:
ProcessBuilder 对象需要按如下方式初始化:
ProcessBuilder runtimeProcessBuilder = new ProcessBuilder().inheritIO().command(new String[]{
"rsync", "-rtuc","--delete-after", "--filter", "protect .git", "--exclude", "'.git*'", "~/some/source/", "~/some/destination/"
});
答案 1 :(得分:0)
shell在将参数传递给命令之前处理引用。
您可以使用命令行的这一部分:
'protect .git/**/*'
shell将其解释为单个参数:
protect .git/**/*
如果首先没有单引号,则shell将具有:
解决方案是通过:
"protect .git/**/"
作为您的Java参数之一,而不是"'protect .git/**/*'"
。
您可能会遇到与~
类似的问题,shell将扩展到您的主目录。