Created
July 26, 2021 17:35
-
-
Save wstrange/e6fdaa53f9d678355abac9b132bcac41 to your computer and use it in GitHub Desktop.
scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Export backend data using ldif or ds-backup | |
# This is done offline, and is expected to be run by a job that runs to termination | |
set -ex | |
# Target to export data to | |
BACKUP_DIR=${BACKUP_DIR:-/backup} | |
# The backup type defaults to ldif. Use ds-backup for a directory backup command | |
BACKUP_TYPE=${BACKUP_TYPE:-ldif} | |
DEST="$BACKUP_DIR/$NAMESPACE/$BACKUP_TYPE" | |
mkdir -p $DEST | |
# The DS server version needs to match the JE data version | |
echo "Upgrading configuration and data..." | |
./upgrade --dataOnly --acceptLicense --force --ignoreErrors --no-prompt | |
# Calculate the list of backends. | |
mapfile -t BACK_ENDS < <(./bin/ldifsearch -b cn=backends,cn=config -s one config/config.ldif "(&(objectclass=ds-cfg-pluggable-backend)(ds-cfg-enabled=true))" ds-cfg-backend-id | grep "^ds-cfg-backend-id" | cut -c20-) | |
echo "Backends ${BACK_ENDS[@]}" | |
for B in "${BACK_ENDS[@]}"; do | |
if [[ "$B" =~ ^(rootUser|proxyUser|monitorUser|tasks|adminRoot)$ ]]; then | |
echo "Skipping system backend $B" | |
else | |
if [[ $BACKUP_TYPE == "ldif" ]]; | |
then | |
F="$DEST/$B.ldif.gz" | |
echo "Backing up $B to $F" | |
export-ldif --ldifFile $F --backendId $B --offline --compress | |
else | |
echo "Backing up $B to $DEST" | |
dsbackup --offline create --backupLocation "$DEST" --backendName $B | |
fi | |
fi | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# restore data | |
# This is done offline, and is expected to be run by a job that runs to termination. | |
# The first argument ($1) is the directory to search for the files to restore | |
# All files found at the directory will be restored. If you only want to partially restore files | |
# you must modify this script or delete the files from the restore source. | |
set -x | |
# Backup type is ldif or ds-backup | |
BACKUP_TYPE="${BACKUP_TYPE:-ldif}" | |
SOURCE="/backup/$NAMESPACE/$BACKUP_TYPE" | |
[ -d "$SOURCE" ] || { | |
echo "Directory $SOURCE DOES NOT exist." | |
exit 1 | |
} | |
# Call the entrypoint - to make sure the proto backends are restored | |
/opt/opendj/docker-entrypoint.sh initialize-only | |
echo "Restoring files found in $SOURCE:" | |
ls -lR $SOURCE | |
if [[ $BACKUP_TYPE == "ldif" ]]; then | |
LDIF=$(cd $SOURCE; ls *.ldif*) | |
for F in $LDIF | |
do | |
# Strip the .ldif or .ldif.gz from the filename to get backend name | |
BACKEND=$(echo $F | sed -e s/\.ldif\.*//g) | |
if [[ $F =~ ".gz" ]]; then | |
echo "=> Importing compressed $F to $BACKEND" | |
import-ldif --clearBackend --ldifFile "${SOURCE}/$F" --backendId ${BACKEND} --offline --isCompressed | |
else | |
echo "=> Importing $F to $BACKEND" | |
import-ldif --clearBackend --ldifFile "${SOURCE}/$F" --backendId ${BACKEND} --offline | |
fi | |
done | |
else | |
# use ds-restore command | |
# For debugging: | |
# dsbackup --offline list --backupLocation $SOURCE | |
# Get a list of all the last backups | |
BACKENDS=$(dsbackup --offline list --backupLocation $SOURCE --last | grep "Backup ID" | awk '{print $NF}' ) | |
for B in $BACKENDS | |
do | |
echo "Restoring id $B" | |
echo dsbackup --offline restore --backupLocation $SOURCE --backupId $B | |
dsbackup --offline restore --backupLocation $SOURCE --backupId $B | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment