Area: Deployment
Sub-Area: Data Management
Applies to: OSS
Issue
You want to back up DataHub metadata, read it in bulk, migrate it between environments, or restore it after a failure.
The database, search cluster, Kafka, kubectl and Helm steps below apply only to self-hosted DataHub. DataHub Cloud customers can't reach the managed database or search cluster. If you use DataHub Cloud, contact DataHub Support for backups, migrations and restores.
You Might Be Asking
- How do I back up DataHub metadata?
- Can I export metadata to files?
- How do I migrate metadata between environments?
- Why does
datahub get --to-fileordatahub put -d @file.jsonfail?
Cause
Several commonly used commands don't work the way they are often written:
-
datahub gethas no--to-fileoption. It fetches one exact URN, doesn't accept wildcards, and prints JSON to stdout.allandlistare not aspect names. -
datahub putwrites one aspect to one URN. It can't export anything.-dtakes a plain file path or-for stdin, with no@. It has no-foption. - The versioned metadata lives in one SQL table,
metadata_aspect_v2. There is nometadata_indextable in the backup guide. - Search and graph data live in Elasticsearch or OpenSearch. With the default empty
INDEX_PREFIX, index names such asdatasetindex_v2,graph_service_v1andsystem_metadata_service_v1don't start withdatahub, so adatahub*snapshot pattern misses them. - Paging through the search API stops at 10,000 results by default.
Solution
1. Back up (self-hosted)
DataHub stores versioned aspects in the SQL table metadata_aspect_v2. Timeseries aspects, search indexes and graph relationships live in Elasticsearch or OpenSearch.
Dump only that table. Restore it with mysql:
mysqldump -u <username> -p datahub metadata_aspect_v2 > metadata_aspect_v2_backup.sql
mysql -h <host> -u <user> -p datahub < metadata_aspect_v2_backup.sql
You can also use RDS snapshots. See Backup and restore.
2. Search indices
You have two options after restoring the SQL table:
- Rebuild the search and graph indices from SQL. Use
datahub docker quickstart --restore-indicesfor quickstart,./docker/datahub-upgrade/datahub-upgrade.sh -u RestoreIndicesfor Docker, orkubectl create job --from=cronjob/<release>-datahub-restore-indices-job-template <name>for Helm. The CLI also hasdatahub check restore-indices --urn <urn or % pattern>. See Restore indices. - Restore Elasticsearch snapshots. For a filesystem repository, first add
path.repo(for example["/mnt/es-backups"]) toelasticsearch.ymlon all nodes. Then register the repository withPUT _snapshot/<repo>, and take a snapshot withPUT _snapshot/<repo>/snapshot_YYYY_MM_DD?wait_for_completion=true. Snapshot and restore all DataHub indices, not only names starting withdatahub.
3. Quickstart backup
For a local quickstart, datahub docker quickstart --backup writes ~/.datahub/quickstart/backup.sql. datahub docker quickstart --restore restores the database and the search indexes from that file. See Quickstart.
4. Read one entity
datahub get fetches one exact URN and prints JSON to stdout. Redirect with > to save it. Leave out -a to get all aspects. See the CLI Guide.
datahub get --urn "<dataset urn>" > single_dataset.json
datahub get --urn "<dataset urn>" -a ownership > ownership.json
If you pass -a and the URN doesn't exist, the CLI raises urn <urn> not found.
5. Read many entities
Don't page with start and count past 10,000 results. Use the OpenAPI v3 scroll endpoint (POST /openapi/v3/entity/scroll) or the GraphQL scrollAcrossEntities query. For the scroll endpoint, count (default 10) and pitKeepAlive (default 5m) are query parameters, not body fields. The filter has this shape:
{"and":[{"criteria":[{"field":"<field>","values":["<value>"],"condition":"<condition>","negated":false}]}]}
6. Write one aspect
datahub put writes one aspect to one entity. --urn, -a and -d are all required, and -d is a plain file path or - for stdin:
datahub put --urn "<urn>" -a <aspectName> -d aspect.json
datahub put ignores unknown options. It writes to the server set in DATAHUB_GMS_URL or ~/.datahubenv, so check where it points before you run it.
datahub put sends the whole aspect as one UPSERT, and an UPSERT replaces the whole aspect. To change one field, either read the full aspect, change it and write it back, or use PATCH, which changes only the fields you name. Keep a copy of the current aspect first. See Why use PATCH.
7. Migrate between instances
Use the datahub ingestion source. It needs direct access to the source instance's database, Kafka broker and schema registry, and the source database should have an index on metadata_aspect_v2(createdon). See the datahub source. If your source is DataHub Cloud, you don't have that access, so contact DataHub Support.
Alternatively, back up metadata_aspect_v2, restore it on the target, then restore snapshots or rebuild the indices as in step 2.
To keep a glossary in Git and apply it to several environments, use the datahub-business-glossary source. See business glossary source.
8. Import files with the file source
The file source key is path. filename is deprecated and renamed to path:
source:
type: file
config:
path: ./datahub_export.json
9. Which aspects to back up
Dataset aspects include editableDatasetProperties, editableSchemaMetadata, ownership, globalTags, glossaryTerms, domains and institutionalMemory.
Test your restore procedure regularly.