Keycloak export in Docker and Exit
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
# If something goes wrong, this script does not run forever but times out | |
TIMEOUT_SECONDS=300 | |
# Logfile for the keycloak export instance | |
LOGFILE=/tmp/standalone.sh.log | |
# destionation export file | |
JSON_EXPORT_FILE=/tmp/realms-export-single-file.json | |
rm -f ${LOGFILE} ${JSON_EXPORT_FILE} | |
# Start a new keycloak instance with exporting options enabled. | |
# Use prot offset to prevent port conflicts with the "real" keycloak instance. | |
timeout ${TIMEOUT_SECONDS}s \ | |
/opt/jboss/keycloak/bin/standalone.sh \ | |
-Dkeycloak.migration.action=export \ | |
-Dkeycloak.migration.provider=singleFile \ | |
-Dkeycloak.migration.file=${JSON_EXPORT_FILE} \ | |
-Djboss.socket.binding.port-offset=99 \ | |
> ${LOGFILE} & | |
# Grab the keycloak export instance process id | |
PID="${!}" | |
# Wait for the export to finish | |
timeout ${TIMEOUT_SECONDS}s \ | |
grep -m 1 "Export finished successfully" <(tail -f ${LOGFILE}) | |
# Stop the keycloak export instance | |
kill ${PID} | |
# Optional reload the keycloak instance | |
/opt/jboss/keycloak/bin/jboss-cli.sh --connect command=:reload |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Move the script to the keycloak docker as a volume.
Get the name of keycloak container:
docker ps
Run the following command:
docker exec <keycloak_container_name_or_id> /bin/bash /tmp/keycloak-export-docker.sh
Ref:https://stackoverflow.com/a/60972882/7541412