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.
java ldap apacheds ldif
Emdadul sawon
source share