|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Converts LDIF data to CASSANDRA CQL |
| 4 | +# |
| 5 | +# /opt/opendj/bin/ldapsearch -p 1389 -b ou=users,dc=am,dc=domain,dc=com -D "cn=Directory manager" -w password "(uid=*)" > export.ldif |
| 6 | +# cat export.ldif | keyspace=realm_test bash ldif2cassandra.sh > import.cql |
| 7 | +# cat import.cql | /opt/cassandra/bin/cqlsh |
| 8 | + |
| 9 | +keyspace=${keyspace:=realm_test} |
| 10 | + |
| 11 | +get_uid () |
| 12 | +{ |
| 13 | + res=`echo -e $1` |
| 14 | + if [[ $res =~ ^cn=(.[^,]+) ]] ; then #uid from DN |
| 15 | + res="${BASH_REMATCH[1]}" |
| 16 | + elif [[ $res =~ ^uid=(.[^,]+) ]] ; then #uid from DN |
| 17 | + res="${BASH_REMATCH[1]}" |
| 18 | + fi |
| 19 | + res=`echo -e $res | awk '{print tolower($0)}'` |
| 20 | +} |
| 21 | + |
| 22 | +block=""; |
| 23 | +while read line; do |
| 24 | + if [[ $line =~ (.+):(.*) ]] ; then #data line |
| 25 | + block="$block$line\n" |
| 26 | + continue |
| 27 | + elif [[ $line =~ ^\s+# ]] ; then #comment |
| 28 | + continue |
| 29 | + elif [[ $line =~ ^\s* ]] ; then #new line |
| 30 | + type="" |
| 31 | + if [[ $block =~ 'objectClass: person' ]] ; then |
| 32 | + type='user' |
| 33 | + elif [[ $block =~ 'objectClass: groupofuniquenames' ]] ; then |
| 34 | + type='group' |
| 35 | + fi |
| 36 | + if [ ! -z "$type" ] ; then |
| 37 | + uid="" |
| 38 | + |
| 39 | + if [ -z "$uid" ] ; then #uid from dn |
| 40 | + if [[ $block =~ dn:(.+) ]] ; then |
| 41 | + get_uid "${BASH_REMATCH[1]}" |
| 42 | + uid=$res |
| 43 | + fi |
| 44 | + fi |
| 45 | + echo "insert into \"$keyspace\".\"values\" (type,uid,field,value,change) VALUES ('$type',\$\$$uid\$\$,'uid',\$\$$uid\$\$,toTimestamp(now()));" |
| 46 | + while read line; do |
| 47 | + if [[ $line =~ ^dn: ]] ; then |
| 48 | + continue |
| 49 | + elif [[ $line =~ ^objectClass: ]] ; then |
| 50 | + continue |
| 51 | + elif [[ $line =~ ^uid: ]] ; then |
| 52 | + continue |
| 53 | + elif [[ ! $line =~ ^\s*$ ]] ; then |
| 54 | + if [[ $line == *::* ]] |
| 55 | + then |
| 56 | + attr=${line%%:*} |
| 57 | + value=`echo ${line#*: } | base64 --decode` |
| 58 | + else |
| 59 | + attr=${line%%:*} |
| 60 | + value=${line#*: } |
| 61 | + fi |
| 62 | + attr=`echo -e $attr | awk '{print tolower($0)}'` |
| 63 | + if [ $attr == 'uniquemember' ] || [ $attr == 'memberof' ] ; then |
| 64 | + get_uid "$value" |
| 65 | + value=$res |
| 66 | + fi |
| 67 | + echo "insert into \"$keyspace\".\"values\" (type,uid,field,value,change) VALUES ('$type',\$\$$uid\$\$,'$attr',\$\$$value\$\$,toTimestamp(now()));" |
| 68 | + fi |
| 69 | + done < <(echo -e $block) |
| 70 | + echo "" |
| 71 | + fi |
| 72 | + block=""; |
| 73 | + fi |
| 74 | +done |
| 75 | + |
| 76 | + |
0 commit comments