#!/bin/bash

BACKDIR="/home/ambae/backups/kolab"
KOLABDIR="/kolab"

#Stop all services
/kolab/bin/openpkg rc all stop

sleep 60

#$1->date to restore
cd $BACKDIR/$1

#IMAP restore
tar -xvzpf var_imapd.tgz
rsync -avz kolab/var/imapd/ $KOLABDIR/var/imapd/ && rm -Rf kolab/

#OpenLDAP restore
tar -xvzpf ldap_conf.tgz
gzip -d ldap_db.ldif.gz
rsync -avz kolab/etc/openldap/ $KOLABDIR/etc/openldap/ && rsync -avz kolab/var/openldap/ $KOLABDIR/var/openldap/ && rm -Rf kolab/
cp $KOLABDIR/var/openldap/openldap-data/DB_CONFIG /tmp/
rm -rf $KOLABDIR/var/openldap/openldap-data/*
cp /tmp/DB_CONFIG $KOLABDIR/var/openldap/openldap-data/
/kolab/sbin/slapadd -l ldap_db.ldif && gzip ldap_db.ldif
chown -R kolab:kolab $KOLABDIR/var/openldap/openldap-data

#Kolab restore
tar -xvzpf kolab_conf.tgz
rsync -avz kolab/etc/ $KOLABDIR/etc/ && rm -Rf kolab/

#Spamassassin restore
gzip -d sa-bayes-db.txt.gz
/kolab/bin/sa-learn --restore sa-bayes-db.txt && gzip sa-bayes-db.txt

sleep 60

#Start all services
/kolab/bin/openpkg rc all start
