Export and import apacheds to LDIF programmatically from java

I created a server in Apache Directory Studio. I also created a section and pasted some entries into this Java server form. Now I want to backup and restore this data in an LDIF file and programmatically. I am new to LDAP. So please show me a detailed way to export and import records programmatically using java from my server in LDIF.

Current solution:

Now I use this approach for backup:

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(); 

For recovery, I use this:

  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); } 

But I'm not sure if this is correct.

The problem with this solution is:

When I back up my database, it writes entries to LDIF randomly, so the restore does not work until I fix the order of the entries manually. Am I the best way there? Please help me.

+7
java ldap apacheds ldif
source share
2 answers

After a long search, I really understand that a record recovery solution is a simple recursion. The backup procedure does not randomly print records; it maintains the order of trees. In this way, simple recursion can organize records well. Here is an example of the code I use -

 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); } } 
+9
source share

Well, you marked it as Java and therefore see the UnboundID LDAP SDK or, as you use APacheDS, why not look at the Apache LDAP API

Any of them will work. I am currently using the [UnboundID LDAP SDK], which have [LDIF-specific APIs]. 3 . I assume that [Apache LDAP API API] as well, but I have not used them.

+1
source share

All Articles