I'm reading the documentation from Keycloak for the export operation in order to setup a backup operation (e.g. via a cron job) and what it says is that export is happending during server startup:
Export and import is triggered at server boot time and its parameters are passed in via Java system properties. It is important to note that because import and export happens at server startup, no other actions should be taken on the server or the database while this happens.
While it has a point, that no other operation is happening during the export operation I was wondering what is the best practice to keep backups of your existing keycloak data? Should the cron job for instance reboot the keycloak server every one per Week to keep the backup, or should I consider a backup operation on the database side (RDBMS)?
To export a realm, you can use the export command. Your Keycloak server instance must not be started when invoking this command. To export a realm to a directory, you can use the --dir <dir> option. When exporting realms to a directory, the server is going to create separate files for each realm being exported.
Supported databases By default, the server uses the dev-file database. This is the default database that the server will use to persist data and only exists for development use-cases.
Go inside "Keycloak/bin/" folder and run below command to export all realms data.
sh standalone.sh -Dkeycloak.migration.action=export -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=keycloak-export.json -Djboss.http.port=8888 -Djboss.https.port=9999 -Djboss.management.http.port=7777
This will export everything but change ports which your using in "Standard.xml" file. Also, if you want to backup users as well, you can look into this guide:
https://github.com/slim-hmidi/keycloak-export-realm
Export your realm data and you can always take your postgres database backup or you can run cron job to take backup.
0 * * * 0 pg_dump -U postgres dbname > ~/postgres/backups/dbname.bak
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With