Skip to content

Instantly share code, notes, and snippets.

@MrHassanMurtaza
Last active January 26, 2022 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrHassanMurtaza/73922ce2ec5e965feec2194e309c2e6d to your computer and use it in GitHub Desktop.
Save MrHassanMurtaza/73922ce2ec5e965feec2194e309c2e6d to your computer and use it in GitHub Desktop.
Keycloak export in Docker and Exit
# 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
@MrHassanMurtaza
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment