7

我在 Apache Directory Studio 中创建了一个服务器。我还创建了一个分区并将一些条目从 Java 中插入该服务器。现在我想以编程方式在 LDIF 文件中备份和恢复这些数据。我是 LDAP 新手。因此,请向我展示一个详细的方法,以使用我的服务器中的 java 以编程方式将条目导出和导入到 LDIF。

当前解决方案:

现在我正在使用这种方法进行备份:

  EntryCursor cursor = connection.search(new Dn("o=partition"), "(ObjectClass=*)", SearchScope.SUBTREE, "*", "+"); 
  Charset charset = Charset.forName("UTF-8");
  Path filePath = Paths.get("src/main/resources", "backup.ldif");
  BufferedWriter writer = Files.newBufferedWriter(filePath, charset);
  String st = ""; 
  
  while (cursor.next()) { 
    Entry entry = cursor.get();
    String ss = LdifUtils.convertToLdif(entry);
    st += ss + "\n";
  }
  writer.write(st);
  writer.close();

为了恢复我正在使用这个:

  InputStream is = new FileInputStream(filepath);
  LdifReader entries = new LdifReader(is);
  
  for (LdifEntry ldifEntry : entries) {
    Entry entry = ldifEntry.getEntry();
    
    AddRequest addRequest = new AddRequestImpl();
    addRequest.setEntry(entry);
    addRequest.addControl(new ManageDsaITImpl());

    AddResponse res = connection.add(addRequest);
  }

但我不确定这是否是正确的方法。

此解决方案的问题:

当我备份我的数据库时,它会以随机方式将条目写入 LDIF,因此在我手动修复条目顺序之前,还原不起作用。我有什么更好的方法吗?请有人帮助我。

4

2 回答 2

9

经过长时间的搜索,我实际上明白了恢复条目的解决方案是一个简单的递归。在备份过程中不以随机方式打印条目,它保持树顺序。所以一个简单的递归可以很好地对条目进行排序。这是我使用的示例代码-

void findEntry(LdapConnection connection, Entry entry, StringBuilder sb)
    throws LdapException, CursorException {
  sb.append(LdifUtils.convertToLdif(entry));
  sb.append("\n");
  EntryCursor cursor = connection.search(entry.getDn(), "(ObjectClass=*)", SearchScope.ONELEVEL, "*", "+");
  while (cursor.next()) {
    findEntry(connection, cursor.get(), sb);
  }
}
于 2015-03-25T10:45:06.580 回答
1

好吧,您标记为 Java 并查看UnboundID LDAP SDK,或者您正在使用 APacheDS,为什么不查看Apache LDAP API

它们中的任何一个都会起作用。我目前使用具有 [LDIF 特定 API] 的 [UnboundID LDAP SDK]。3 . 我假设 [Apache LDAP API] 也可以,但我没有使用它们。

于 2014-12-25T10:40:25.433 回答