#!/usr/bin/perl -w

use strict;
use POSIX qw(strftime);

sub do_system {
	my $cmd = shift;
	print "$cmd\n";
	my $result = system($cmd);
	return $result == 0;
}	

my $ldif_backup = "/home/backup/ldap.ldif.gz";
my $binary_backup = "/home/backup/ldap.tar.gz";


# copy slapd.conf file
do_system("cp -p /etc/openldap/slapd.conf /etc/openldap/slapd.conf.copy")
	|| die "$0: Error copying slapd.conf\n";
do_system("echo -e \"\\nreadonly on\\n\" >> /etc/openldap/slapd.conf")
	|| die "$0: Appending readonly on";
# Restart ldap in readonly
do_system("/sbin/service ldap restart")
	|| die "$0: Restarting ldap readonly";
# backup as LDIF-file
do_system("umask 0077; /usr/sbin/slapcat | gzip > $ldif_backup") ||
	warn "$0: Error slapcat to $ldif_backup\n";
# backup binary files
do_system("umask 0077; /bin/tar cfz $binary_backup /var/lib/ldap") ||
	warn "$0: Error tar to $binary_backup\n";
# Copy back original file and restart ldap
do_system("cp -p /etc/openldap/slapd.conf.copy /etc/openldap/slapd.conf")
	|| die "$0: Error copying back original slapd.conf file";
do_system("/sbin/service ldap restart")
	|| die "$0: Error restarting ldap writable";
