directory-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Hal Deadman <hal.dead...@gmail.com>
Subject Re: DHCP using Apache directory server
Date Thu, 28 Jan 2016 21:58:58 GMT
There may be an easier way to get an LDIF dump but I have this method run
nightly by a Spring task in an LDAP related web application. I haven't
switched over to ApacheDS in production but I use this against 389-ds in
production and ApacheDS in development. This dumps people, groups and
organization units to a daily rolling log file (configured via logback). It
helps me sleep easier with my current single directory server and it is
what is making me comfortable enough to switch to ApacheDS, knowing that if
there is a corruption I can recover from a recent backup. I don't recall
why I used ldaptive instead of ApacheDS ldif functionality. This wouldn't
handle a really large directory because it's reading the entire directory
into memory and writing everything to a String before logging it. The
directory I am using it on has less than 1500 entries, mostly people.


import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.StringUtils;
import org.ldaptive.Connection;
import org.ldaptive.DefaultConnectionFactory;
import org.ldaptive.LdapEntry;
import org.ldaptive.Response;
import org.ldaptive.SearchFilter;
import org.ldaptive.SearchRequest;
import org.ldaptive.SearchResult;
import org.ldaptive.control.util.PagedResultsClient;
import org.ldaptive.io.LdifWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LdifDumper {
private static final Logger LDIF_BACKUP =
LoggerFactory.getLogger("LDIF_BACKUP");

private static final Logger logger =
LoggerFactory.getLogger(LdifDumper.class);
public void dumpLdif(DefaultConnectionFactory connectionFactory, String
baseDn) {

String[] objectClasses = { "domain", "organizationalunit",
"groupOfUniqueNames", "person" };

StringWriter writer = new StringWriter();
LdifWriter ldifWriter = new LdifWriter(writer);
try (Connection conn = connectionFactory.getConnection()) {
conn.open();
PagedResultsClient client = new PagedResultsClient(conn, 500);
for (int i = 0; i < objectClasses.length; i++) {
Map<String, LdapEntry> entryMap = new TreeMap<String, LdapEntry>();
SearchFilter filter = new SearchFilter("(objectclass=" + objectClasses[i] +
")");
SearchRequest request = new SearchRequest(baseDn, filter);
request.setSearchScope(org.ldaptive.SearchScope.SUBTREE);
Response<SearchResult> response = client.executeToCompletion(request);
SearchResult result = response.getResult();
Collection<LdapEntry> entries = result.getEntries();
for (LdapEntry ldapEntry : entries) {
String key = ldapEntry.getDn();
// hack - count commas so higher level entities sort first
// (e.g. for nested OUs - create ou=Groups before ou=Groups,ou=App1 )
int commaCount = StringUtils.countMatches(key, ",");
key = commaCount + key;
entryMap.put(key, ldapEntry);
}
// print out sorted entries
for (Map.Entry<String, LdapEntry> mapEntry : entryMap.entrySet()) {
ldifWriter.write(new SearchResult(mapEntry.getValue()));
}
}
LDIF_BACKUP.info(writer.toString());
} catch (org.ldaptive.LdapException | IOException e) {
logger.error("Error dumping users to ldif: " + e.getMessage(),e);
}
}
}



On Thu, Jan 28, 2016 at 12:00 PM, Emmanuel Lécharny <elecharny@gmail.com>
wrote:

> Le 28/01/16 15:46, Sherman Lilly a écrit :
> > How to do I do that?
>
> Assuming that you have a backup of your database content in a LDIF
> format, just delete the existing data from your disk (<apacheds
> root>/instance/<your instance>/partitions/<your database>)
>
> Typically, on my linux box :
>
> /opt/apacheds-2.0.0-M22-SNAPSHOT/instances/default/partitions/example :
> [root@brie example]# ll
> total 620
> -rw-r--r--. 1 root root     0 Jan 27 15:55 0.9.2342.19200300.100.1.1.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 0.9.2342.19200300.100.1.1.lg
> -rw-r--r--. 1 root root   222 Jan 27 15:55
> 0.9.2342.19200300.100.1.1-uid.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 0.9.2342.19200300.100.1.25.db
> -rw-r--r--. 1 root root   257 Jan 27 15:55
> 0.9.2342.19200300.100.1.25-dc.txt
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 0.9.2342.19200300.100.1.25.lg
> -rw-r--r--. 1 root root   273 Jan 27 15:55
> 1.3.6.1.4.1.18060.0.4.1.2.3-apachePresence.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.3.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.3.lg
> -rw-r--r--. 1 root root   264 Jan 27 15:55
> 1.3.6.1.4.1.18060.0.4.1.2.50-apacheRdn.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.50.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.50.lg
> -rw-r--r--. 1 root root   211 Jan 27 15:55
> 1.3.6.1.4.1.18060.0.4.1.2.5-apacheOneAlias.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.5.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.5.lg
> -rw-r--r--. 1 root root   208 Jan 27 15:55
> 1.3.6.1.4.1.18060.0.4.1.2.6-apacheSubAlias.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.6.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.6.lg
> -rw-r--r--. 1 root root   204 Jan 27 15:55
> 1.3.6.1.4.1.18060.0.4.1.2.7-apacheAlias.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.7.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.18060.0.4.1.2.7.lg
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.4203.666.1.7.db
> -rw-r--r--. 1 root root   246 Jan 27 15:55
> 1.3.6.1.4.1.4203.666.1.7-entryCSN.txt
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.4203.666.1.7.lg
> -rw-r--r--. 1 root root     0 Jan 27 15:55 1.3.6.1.4.1.5322.10.1.1.db
> -rw-r--r--. 1 root root   215 Jan 27 15:55
> 1.3.6.1.4.1.5322.10.1.1-krb5PrincipalName.txt
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 1.3.6.1.4.1.5322.10.1.1.lg
> -rw-r--r--. 1 root root   148 Jan 27 15:55 2.5.18.5-administrativeRole.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 2.5.18.5.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 2.5.18.5.lg
> -rw-r--r--. 1 root root     0 Jan 27 15:55 2.5.4.0.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 2.5.4.0.lg
> -rw-r--r--. 1 root root   184 Jan 27 15:55 2.5.4.0-objectClass.txt
> -rw-r--r--. 1 root root     0 Jan 27 15:55 2.5.4.11.db
> -rw-r--r--. 1 root root 41362 Jan 27 15:55 2.5.4.11.lg
> -rw-r--r--. 1 root root   257 Jan 27 15:55 2.5.4.11-ou.txt
> -rw-r--r--. 1 root root 40960 Jan 27 15:55 master.db
> -rw-r--r--. 1 root root     8 Jan 27 15:55 master.lg
>
>
> Just get rid of all that. They will be recreated when you inject the
> LDIF that contains your data.
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message