-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from projectsyn/backup
Add how-to page to backup and restore Keycloak
- Loading branch information
Showing
3 changed files
with
202 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
= Restore Keycloak from a Backup | ||
|
||
The following steps will guide you through restoring a backup of Keycloak. | ||
|
||
[IMPORTANT] | ||
==== | ||
This guide only covers how to restore the built-in database. | ||
If you use an external database, please consult the documentation of your database provider on how to backup and restore it. | ||
==== | ||
|
||
[NOTE] | ||
==== | ||
You can only restore a database that has been backed up. | ||
Please refer to the xref:how-tos/use-built-in-db.adoc#_enable_backups[built-in database] setup guide on how to enable backups. | ||
==== | ||
|
||
==== | ||
Requirements | ||
* `kubectl` With access to the cluster running Keycloak | ||
* `base64` | ||
* https://restic.net/[`restic`] | ||
* (optional) https://stedolan.github.io/jq/[`jq`] | ||
* `pwgen` | ||
* `vault` | ||
==== | ||
|
||
. Configure access to backups | ||
+ | ||
[source,bash] | ||
---- | ||
# The namspace containing the Keycloak instance. Change if necessary. | ||
export NAMESPACE=syn-keycloak | ||
|
||
export reposecret=$( \ | ||
kubectl -n $NAMESPACE get schedule backup \ | ||
-o go-template="{{.spec.backend.repoPasswordSecretRef.name}}" \ | ||
) | ||
export s3secret=$( \ | ||
kubectl -n $NAMESPACE get schedule backup \ | ||
-o jsonpath="{.spec.backend.s3.accessKeyIDSecretRef.name}" \ | ||
) | ||
|
||
export RESTIC_REPOSITORY=$( \ | ||
kubectl -n $NAMESPACE get schedule backup \ | ||
-o go-template="s3:{{.spec.backend.s3.endpoint}}/{{.spec.backend.s3.bucket}}/" \ | ||
) | ||
export RESTIC_PASSWORD=$( \ | ||
kubectl -n $NAMESPACE get secrets $reposecret \ | ||
-o jsonpath={.data.password} \ | ||
| base64 -d \ | ||
) | ||
export AWS_ACCESS_KEY_ID=$( \ | ||
kubectl -n $NAMESPACE get secrets $s3secret \ | ||
-o jsonpath="{.data.username}" \ | ||
| base64 -d \ | ||
) | ||
export AWS_SECRET_ACCESS_KEY=$( \ | ||
kubectl -n $NAMESPACE get secrets $s3secret \ | ||
-o jsonpath="{.data.password}" \ | ||
| base64 -d \ | ||
) | ||
---- | ||
|
||
. List backups and choose the one to restore | ||
+ | ||
[source,bash] | ||
---- | ||
restic snapshots | ||
|
||
export SNAPSHOT_ID=XXXXXX # Choose a snapshot id from the list | ||
---- | ||
+ | ||
[TIP] | ||
==== | ||
To choose the last available backup you can simply run | ||
[source,bash] | ||
---- | ||
export SNAPSHOT_ID=$(restic snapshots --json --latest 1 --path /$NAMESPACE-keycloak-postgresql.sql | jq -r '.[0].id') | ||
---- | ||
==== | ||
|
||
. Disable ArgoCD auto sync | ||
+ | ||
[source,bash] | ||
---- | ||
# The ArgoCD app of the Keycloak instance. Change if necessary. | ||
export ARGO_APP=keycloak | ||
|
||
kubectl -n syn patch apps root --type=json \ | ||
-p '[{"op":"replace", "path":"/spec/syncPolicy", "value": {}}]' | ||
kubectl -n syn patch apps ${ARGO_APP} --type=json \ | ||
-p '[{"op":"replace", "path":"/spec/syncPolicy", "value": {}}]' | ||
---- | ||
|
||
. Scale down Keycloak | ||
+ | ||
[source,bash] | ||
---- | ||
kubectl -n $NAMESPACE patch statefulset keycloak --type=json \ | ||
-p '[{"op":"replace", "path":"/spec/replicas", "value": 0}]' | ||
|
||
# Wait until statefulset has been scaled down | ||
kubectl -n $NAMESPACE get statefulset keycloak -w | ||
---- | ||
|
||
. Load the backup and restore it | ||
+ | ||
[source,bash] | ||
---- | ||
export POD=keycloak-postgresql-0 | ||
|
||
restic dump "${SNAPSHOT_ID}" /$NAMESPACE-keycloak-postgresql.sql \ | ||
| kubectl -n $NAMESPACE exec -i $POD \ | ||
-- sh -c 'PGPASSWORD="${POSTGRES_PASSWORD}" psql --set ON_ERROR_STOP=on -U "${POSTGRES_USER}" ${POSTGRES_DB}' | ||
---- | ||
|
||
. Re-enable ArgoCD auto sync and scale up Keycloak | ||
+ | ||
[source,bash] | ||
---- | ||
kubectl -n syn patch apps root --type=json \ | ||
-p '[{ | ||
"op":"replace", | ||
"path":"/spec/syncPolicy", | ||
"value": {"automated": {"prune": true, "selfHeal": true}} | ||
}]' | ||
|
||
# Wait until Keycloak has started sucessfully | ||
kubectl -n $NAMESPACE get statefulset keycloak -w | ||
---- | ||
|
||
[NOTE] | ||
==== | ||
This guide assumes that you have direct access to the S3 bucket holding the backup. | ||
If the access is restricted to the Kubernetes cluster, you will need to adapt these steps. | ||
You could: | ||
. Perform the commands in a container running on the cluster | ||
. Restore the database backup to a PVC and copy it over | ||
Consult the official https://k8up.io/k8up/2.1/how-tos/restore.html#_restore_from_s3_to_pvc[K8up documentation] on options for restoring backups. | ||
==== | ||
|
||
|
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
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