I have create a server in Apache Directory Studio. I also created a partition and inserted some entries to that server form Java. Now I want to Backup and Restore this data in and LDIF file programmatically. I am new to LDAP. So please show me a detailed way to Export and Import entries programmatically using java from my server into LDIF.
Current solution:
Now I am using this approach to 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 restore I am using 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 am not sure whether this is the correct way.
Problem of this solution:
When I backup my database, it writes entries into LDIF in a random way, so restore does not works until I fix the order of entries manually. I there any better way? Please someone help me.