#!/bin/bash -e

LDB_MODULES_PATH=/usr/lib64/samba/ldb
export LDB_MODULES_PATH

mkdir -p /home/lbkp/samba/{private,ldif/sam.ldb.d,gpo}
tar cf /home/lbkp/samba/sysvol.tar.lz4 --use-compress-program=lz4 -C /var/lib/samba/sysvol .
find /var/lib/samba/private/ -type f -name \*.ldb | xargs tdbbackup
pushd /var/lib/samba/private/ > /dev/null
find . -type f -name \*.bak | xargs cp --parents -t /home/lbkp/samba/private/
popd > /dev/null
find /var/lib/samba/private -type f -name \*.bak | xargs rm -f
find /home/lbkp/samba/private/ -type f -name \*.bak | while read F; do mv "$F" "${F%.bak}"; done
tar cf /home/lbkp/samba/private.tar.lz4 --use-compress-program=lz4 -C /home/lbkp/samba/private .
rm -rf /home/lbkp/samba/private/
for LDIF in $(find /var/lib/samba/private/ -type f -name \*.ldb); do
  ldbsearch --url=$LDIF | lz4 -c > /home/lbkp/samba/ldif/$(echo $LDIF | sed -e 's/\/var\/lib\/samba\/private//').ldif.lz4
done