With a little side of applesauce...

Wednesday, December 14, 2011

Migrating LDAP groupOfNames to groups table in Postgresql

I love LDAP! Well... I hate LDAP too... But, LDAP is not the directory service for my current position, so I am moving my groupOfNames to a Postgresql database. I usually do this with BASH, but thought I would dust off the PERL chops today and wrote this:

sqlinserts_from_ldif.pl:



#!/usr/bin/perl
$f = 'webpos_groups.ldif';

# open file
open(FILE, $f) or die("Unable to open file");

$groupname = '';
while (<FILE>)
{
        chomp;
        $member = '';

        if ( $_ =~ m/dn:/)
        {
                @gn = split(',', $_);
                @gn2 = split('=', $gn[0]);
                $groupname = $gn2[1];
        }
        if ( $_ =~ m/member:/)
        {
                @m = split(',', $_);
                @m2 = split('=', $m[0]);
                $member = $m2[1];
        }
        ## print our sql
        if ( $member ne '' ) {
                $sqlinsert = '';
                $sqlinsert = "INSERT INTO groups (groupid, member) VALUES ((SELECT loc_id FROM loc WHERE dept_name = '" . $groupname . "'), '" . $member . "');";
                print "$sqlinsert\n";
        }
}
# close file
close(FILE);


I exported my LDAP group container to LDIF, then ran this PERL file against it to generate a SQL file I could upload into PostgreSQL.


./sqlinserts_from_ldif.pl > groups.sql


See... I can't leave BASH altogether :)


No comments: