cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Data Governance
Join discussions on data governance practices, compliance, and security within the Databricks Community. Exchange strategies and insights to ensure data integrity and regulatory compliance.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Backing up your unity catalog metadata

DouglasMoore
Databricks Employee
Databricks Employee

Unity Catalog (UC) tracks the metadata and your cloud storage accounts store the your data. This python script will extract the metadata from {catalog}.information_schema into folders in a storage location. Take this and put into a notebook.  Make data backups from the cloud storage console. {catalog} can also include system, which will cover every catalog in UC. The UC configuration is recoverable from the information_schema metadata however I've not had time to make the recovery run in parallel and perform to reasonable expectations.

%python

storage_location = dbutils.widgets.get("storageLocation")
catalog_name = dbutils.widgets.get("catalogName")
(storage_location, catalog_name)
 
%python
#
# For every table in information schema, make a backup copy of it to the storage location and all of the other metadata
#
table_list = spark.catalog.listTables(f"`{catalog_name}`.information_schema")
for table in table_list:
print(f'backing up {table.catalog}.information_schema.{table.name} to {storage_location}/{table.name}...')
info_schema_table_df = spark.sql(f"SELECT * FROM {table.catalog}.information_schema.{table.name}")
info_schema_table_df.write.format("delta").mode("overwrite").save(f"{storage_location}/{table.name}")
print('backup complete')
1 REPLY 1

DoTA
Honored Contributor

Nice writeup - the "recovery hasn't performed to reasonable expectations" gap you mention is worth digging into a bit, because I think it's a fundamental issue with backing up information_schema rows rather than DDL.

 

information_schema is a live view of current state, not a set of replayable statements. When you write those rows to Delta and later try to "restore" from them, you end up having to reverse-engineer CREATE TABLE / GRANT / COMMENT statements from tabular metadata, which is slow and fragile - that's almost certainly what's fighting you on performance. The fix that's worked for me is to capture the DDL text directly instead of (or alongside) the info_schema snapshot:

 

- SHOW CREATE TABLE catalog.schema.table for table definitions (gives you a replayable CREATE statement, not just column metadata).

- SHOW GRANTS ON TABLE/SCHEMA/CATALOG ... for permissions, since grants aren't fully captured by the older information_schema views on every workspace - system.information_schema.grants or system.access.* (System Tables) is a more complete source if it's enabled on your account.

- Store these as plain text per-object rather than as a dataframe of metadata rows. Restore then becomes "replay the DDL," which is orders of magnitude faster than trying to reconstruct statements from rows at recovery time.

 

On the parallelization side: spark.catalog.listTables() and looping table-by-table is inherently serial. Since each backup is just a metadata query plus a small write, you can fan it out with a ThreadPoolExecutor (Python threads are fine here since the work is I/O-bound - waiting on the metastore and object storage, not CPU) instead of dbutils.notebook.run per catalog, which has heavier per-call overhead. That's the difference between backing up "system" (every catalog) taking hours vs. minutes.

 

Also worth noting for anyone using this as a real DR plan: this captures Unity Catalog's own metadata, but doesn't capture Volumes' underlying file listings or external location/storage credential definitions, which live outside information_schema entirely and need their own SHOW-based capture.