LDAP 备份还原
备份
OpenLDAP
中数据备份一般分为三种方式,一种通过 slapcat
指令进行备份,一种通过 phpLDAPadmin
控制台进行备份,另一种通过数据目录来实现备份。本次只介绍最简单直接的办法–使用 slapcat
slapcat
备份脚本
#!/usr/bin/env bash
LDAPBK=ldap-$( date +%Y%m%d-%H ).ldif
BACKUPDIR=/data/ldap_backups
BACKUP_EXEC=`which slapcat`
PACKAGE=`which gzip`
checkdir(){
if [ ! -d "$BACKUPDIR" ]; then
mkdir -p ${BACKUPDIR}
fi
}
backuping(){
echo "Backup Ldap Start...."
${BACKUP_EXEC} -v -l ${BACKUPDIR}/${LDAPBK}
${PACKAGE} -9 $BACKUPDIR/$LDAPBK
}
checkdir
backuping
定时备份
具体时间自己把控
crontab -e
00 */2 * * * /path/ldap_backup.sh
还原
模拟数据丢失
# 获取所有用户配置条目
ldapsearch -x -Z -LLL -H ldap://172.16.10.220 -b 'ou=People,dc=laoshiren,dc=com' | wc -l
106
# 清空所有所有条目
ldapdelete -x -D "cn=manager,dc=laoshiren,dc=com" -w FCzxpJWCccuB -r "dc=laoshiren,dc=com"
# 确认条目
ldapsearch -x -Z -LLL -H ldap://172.16.10.220 -b 'ou=People,dc=laoshiren,dc=com' | wc -l
0
还原数据
systemctl stop slapd && systemctl status slapd
# 清空原openldap数据
rm -fr /var/lib/ldap/*
/usr/sbin/slapadd -l /data/ldap_backups/ldap-20190109-21.ldif
# 输出
5c36b84d The first database does not allow slapadd; using the first available one (2)
5c36b84d hdb_db_open: warning - no DB_CONFIG file found in directory /var/lib/ldap: (2).
Expect poor performance for suffix "dc=laoshiren,dc=com".
_#################### 100.00% eta none elapsed none fast!
Closing DB...
# 拷贝数据配置文件
cp /usr/share/openldap-servers/DB_CONFIG.example /var/lib/ldap/DB_CONFIG
# 赋权
chown ldap:ldap -R /var/lib/ldap/
# 启动 slapd
systemctl start slapd && systemctl status slapd
验证数据
# 验证条目
ldapsearch -x -Z -LLL -H ldap://172.16.10.220 -b 'ou=People,dc=laoshiren,dc=com' | wc -l
106
# 验证登录
ssh -i ~/.ssh/id_rsa clin@172.16.10.10
Last login: Wed Jan 9 21:43:07 2019 from 172.16.10.1
-sh-4.2$
恢复完成