Skip to main content

Data Initialization

If you are deploying Casdoor with other services as a complete application, you may want to provide an out-of-the-box feature for users. This means that users can directly use the application without any configuration.

In such a situation, you can use data initialization to register your service in Casdoor through a configuration file. This file can be pre-defined or dynamically generated by your own service.

Here we give a tutorial for importing or exporting config data.

Import Config Data

By default, if there is a configuration file named init_data.json at the root directory of Casdoor, it will be used to initialize data in Casdoor. You can also specify a custom path for the initialization file by setting the initDataFile parameter in conf/app.conf:

initDataFile = /path/to/your/init_data.json

If no custom path is specified, Casdoor will look for init_data.json in the root directory where Casdoor runs.

If you are using the official Docker image of Casdoor, here are some scripts that can help you to mount init_data.json into the container.

A template for init_data.json is provided at: init_data.json.template. Rename it to init_data.json before using it.

For Docker

If you deploy Casdoor with Docker, you can use the volume command to mount init_data.json into the container.

docker run ... -v /path/to/init_data.json:/init_data.json

For Kubernetes

If you deploy Casdoor with Kubernetes, you can use the configmap to store init_data.json.

apiVersion: v1
kind: ConfigMap
metadata:
name: casdoor-init-data
data:
init_data.json:

You can mount the data into Casdoor pods by mounting the configmap. You can modify your deployment as follows:

apiVersion: apps/v1
kind: Deployment
...
spec:
template:
...
spec:
containers:
...
volumeMounts:
- mountPath: /init_data.json
name: casdoor-init-data-volume
subPath: init_data.json
volumes:
- configMap:
name: casdoor-init-data
name: casdoor-init-data-volume

Export Config Data

You can export all Casdoor configuration data to a file for backup or migration purposes. There are two methods available:

If you're running Casdoor from a binary, use the -export flag to dump the database to a JSON file:

# Export to default location (init_data_dump.json)
./casdoor -export

# Export to a custom path
./casdoor -export -exportPath /path/to/backup.json

The export runs after database initialization but before the server starts, then exits automatically. This method works with any deployment method (binary, Docker, Kubernetes) and doesn't require Go toolchain or source code access.

Using Go Test

If you have access to the source code, you can use the test method:

go test ./object -v -run TestDumpToFile

This will generate init_data_dump.json in the same directory.

Migrating Data

After exporting, rename init_data_dump.json to init_data.json and place it in the root directory of your target Casdoor installation. On startup, Casdoor will automatically import the data.

References

All Casdoor objects supported by the data initialization are as follows:

ObjectGo StructDocumentation
organizationsstructdoc
applicationsstructdoc
usersstructdoc
certsstructdoc
providersstructdoc
ldapsstructdoc
modelsstruct
permissionsstructdoc
paymentsstructdoc
productsstructdoc
resourcesstructdoc
rolesstructdoc
syncersstructdoc
tokensstructdoc
webhooksstructdoc
groupsstructdoc
adaptersstructdoc
enforcersstruct
plansstructdoc
pricingsstructdoc
invitationsstructdoc
recordsstruct
sessionsstruct
subscriptionsstructdoc
transactionsstruct

If you still feel confused about filling out this template, you can call the RESTful API or use the debug mode of your browser to see the response of GetXXX to these objects. The responses are in the same format as init_data.json.