-
-
Save sudmed/5ac982281ac58a4253fab83ffa46e3c8 to your computer and use it in GitHub Desktop.
Shell script for automatted keycloak backups
This file contains hidden or 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
| #!/bin/bash | |
| # check, if another backup is currently running | |
| if [ `ps -ef|grep "keycloak.migration.action=export" |grep -v grep |wc -l` != 0 ] ; then | |
| echo "Another backup is currently running"; | |
| exit 1; | |
| fi | |
| # try to extract keycloak home from running keycloak instance | |
| KEYCLOAK_HOME=$(ps -ef|grep -v grep|grep jboss.home.dir|grep keycloak|sed 's/.*\(jboss.home.dir=\)//'|awk '{print $1}') | |
| KEYCLOAK_BACKUP_DIR=/opt/backup/keycloak-backup | |
| LOGFILE=/tmp/kc-$$.log | |
| rm -rf "$KEYCLOAK_BACKUP_DIR"/tmp || mkdir -p "$KEYCLOAK_BACKUP_DIR"/tmp | |
| "$KEYCLOAK_HOME"/bin/standalone.sh -Dkeycloak.migration.action=export \ | |
| -Dkeycloak.migration.provider=dir \ | |
| -Dkeycloak.migration.dir="$KEYCLOAK_BACKUP_DIR"\tmp \ | |
| -Dkeycloak.migration.usersPerFile=500 \ | |
| -Djboss.socket.binding.port-offset=99 \ | |
| -Djboss.as.management.blocking.timeout=900 \ | |
| > "$LOGFILE" & | |
| sleep 5 | |
| KEYCLOAK_PID=$(ps -ef|grep java|grep "keycloak.migration.dir="|awk '{print $2}') | |
| SUCCESS="Export finished successfully" | |
| FAILURE="seconds waiting for service container stability. Operation will roll back" | |
| echo "KEYCLOAK_PID=$KEYCLOAK_PID" | |
| while [ `grep "$SUCCESS" "$LOGFILE" | wc -l` == 0 ] ; do | |
| sleep 60 | |
| if [ `grep "$FAILURE" "$LOGFILE" | wc -l` != 0 ] ; then echo "killing keycloak with pid=$KEYCLOAK_PID"; kill $KEYCLOAK_PID; exit 1; fi; | |
| done | |
| kill $KEYCLOAK_PID || true | |
| # delete all files that have been modified more than 30 days ago | |
| find $KEYCLOAK_BACKUP_DIR -type f -mtime +30 -delete | |
| tar cfz "$KEYCLOAK_BACKUP_DIR/keycloak-backup-$(date +%Y-%m-%d).tar.gz" --remove-files -C "$KEYCLOAK_BACKUP_DIR"/tmp/* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment