diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-07-13 15:39:16 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2012-07-13 15:39:16 +0300 |
commit | 35212ddd3ceb7d3b20957b9ce6786f4ddd716bbe (patch) | |
tree | 5d52d7295c639f0c905d0461f1dfe237b97b51e5 /lib/beam/ldap.sh | |
parent | dd89ed279f6948fab60319262ffc4d25da8ecd10 (diff) | |
download | beam-35212ddd3ceb7d3b20957b9ce6786f4ddd716bbe.tar.gz beam-35212ddd3ceb7d3b20957b9ce6786f4ddd716bbe.tar.bz2 |
Add a LDAP backup module.
* lib/beam/ldap.sh: New file.
* lib/beam/Makefile.am (libbackup_SCRIPTS,EXTRA_DIST): Add ldap.sh.
* beam.conf: Update, mention new backup type.
* doc/beam.conf.5in: Update.
* lib/beam/common.in (load_config): If backup_archive_dir is not set,
and S3 backup is configured, set backup_archive_dir to $backup_mp_s3.
(beam_logrotate): Add missingok to the logrotate configuration.
* lib/beam/fs.sh: List configuration keywords.
* lib/beam/mysql.sh: Likewise.
* lib/beam/postgres.sh: Likewise.
* lib/beam/s3.sh: Likewise.
Diffstat (limited to 'lib/beam/ldap.sh')
-rwxr-xr-x | lib/beam/ldap.sh | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/lib/beam/ldap.sh b/lib/beam/ldap.sh new file mode 100755 index 0000000..f6e56b8 --- /dev/null +++ b/lib/beam/ldap.sh @@ -0,0 +1,164 @@ +#! /bin/sh +# This file is part of BEAM +# Copyright (C) 2012 Sergey Poznyakoff +# +# BEAM is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3, or (at your option) +# any later version. +# +# BEAM is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with BEAM. If not, see <http://www.gnu.org/licenses/>. + +# Configuration keywords: +# +# item_type=ldap [mandatory] +# item_database_directory=string [mandatory] +# item_database_number=n [optional *] +# item_database_suffix=suf [optional *] +# item_uri=str [optional] +# item_user=name [optional] +# item_slapcat_options=string [optional internal] +# item_slapadd_options=string [optional internal] +# +# * - mutually exclusive keywords. + + +# ldap_check item +ldap_check() { + local rc=0 dbno dbsuf dbdir + eval dbno=\$${1}_database_number \ + dbsuf=\$${1}_database_suffix \ + dbdir=\$${1}_database_directory + if test -n "$dbno" && test -n "$dbsuf"; then + error "$1: both ${1}_database_number and ${1}_database_suffix are set" + rc=1 + fi + if test -z "$dbdir"; then + error "${1}_database_directory not configured" + rc=1 + fi + if ! test -d "$dbdir"; then + error "LDAP database directory $dbdir does not exist" + error "(set by ${1}_database_directory)" + fi + return $rc +} + +# ldap_list item prefix +ldap_list() { + local dbid + + eval dbid=\$${1}_database_number + if [ -n "$dbid" ]; then + dbid=" number $dbid" + else + eval dbid=\$${1}_database_suffix + if [ -n "$dbid" ]; then + dbid=" suffixed with $dbid" + fi + fi + + echo "${2}LDAP database"$dbid +} + +# ldap_backup item +ldap_backup() { + local options dbno dbsuf uri + + logit "backing up LDAP database $1" + + eval options=\$${1}_slapcat_options \ + dbno=\$${1}_database_number \ + dbsuf=\$${1}_database_suffix + + if [ -n "$dbno" ]; then + options="$options -n$dbnum" + elif [ -n "$dbsuf" ]; then + options="$options -b$slapcat" + fi + + eval uri=\"\$${1}_uri\" + if [ -n "$uri" ]; then + options="$options -H\"$uri\"" + fi + + if [ -z "$dry_run" ]; then + slapcat $options > $backup_snapshot_dir/$1-$week-$round-$level + else + echo "slapcat $options > $backup_snapshot_dir/$1-$week-$round-$level" + fi + + if [ $? -ne 0 ]; then + tarerror=$((tarerror + 1)) + logit "failed" + else + logit "creating $1-$week-$round-$level.$tar_suffix" + $dry_run tar $verbose $taroptions \ + -f $backup_archive_dir/$1-$week-$round-$level.$tar_suffix \ + -C $backup_snapshot_dir $1-$week-$round-$level + tarcode $? + $dry_run rm $backup_snapshot_dir/$1-$week-$round-$level + fi +} + +# ldap_restore item +ldap_restore() { + local options dbno dbsuf dbdir u user + + logit "extracting LDAP database dump $1" + + eval options=\$${1}_slapadd_options dbno=\$${1}_database_number \ + dbsuf=\$${1}_database_suffix dbdir=\$${1}_database_directory \ + user=\$${1}_user + + if test -z "$dbdir"; then + error "${1}_database_directory not configured" + tarerror=$((tarerror + 1)) + return + fi + if [ -n "$dbno" ]; then + options="$options -n$dbnum" + elif [ -n "$dbsuf" ]; then + options="$options -b$slapcat" + fi + + u=$(umask) + trap "umask $u" 1 2 3 13 15 + umask 077 + $dry_run tar $verbose $taroptions \ + -f $backup_archive_dir/$1-$week-$round-$level.$tar_suffix + e=$? + tarcode $e + if [ $e -eq 0 ]; then + prevdbtar=$backup_snapshot_dir/ldap-$1-$(date +%Y%m%dT%H%M%S).tar + logit "archiving the previous database contents to $prevdbtar" + $dry_run tar $verbose -c -f $prevdbtar $dbdir + e=$? + tarcode $e + umask $u + if [ $e -ne 0 ]; then + error "Failed to backup the prior database contents to $prevdbtar" + return + fi + logit "removing old database files in $dbdir" + $dry_run rm $dbdir/* + logit "restoring database $1 from the dump" + if test -n "$user"; then + su $user -c "$dry_run slapadd $options $verbose -l $1-$week-$round-$level" + su $user -c "$dry_run slapindex" + else + $dry_run slapadd $options $verbose -l $1-$week-$round-$level + $dry_run slapindex + fi + # FIXME: error checking + $dry_run rm $1-$week-$round-$level + fi + umask $u + trap - 1 2 3 13 15 +} |