Migration and backup
Two approaches to data migration
Backup and recovery (full). Creating a full copy of metadata of the entire system. Used for disaster recovery or to clone an environment (e.g., dev → test).
Export and import (partial). This is a transfer of individual objects (workbooks with their contents). Used for transfer of finished reports between environments (e.g., test → prod) or for migration from a cloud.
PostgreSQL database backups and recovery
The whole DataLens configuration (users, dashboards, and permissions) is stored in a PostgreSQL service database. A backup of this database is a backup of the entire system. The ./init.sh script provides convenient commands for this:
-
To create a backup:
./init.sh --dump-postgresThis command will create an SQL dump of the database and output it to
stdout, which will be forwarded to a file. Running this command regularly is the foundation of your backup strategy. -
To recover from a backup:
./init.sh --restore-postgres datalens_enterprise_<...>.dumpThis command will deploy the data from the SQL file you specify into a database.
Database restoration does not recover deleted workbooks because the
is_deletedflags are not overwritten.A backup is typically required when migrating DataLens to another server or in case of database incidents. To recover deleted workbooks, follow the procedure below.
-
To recover from a backup with a full preliminary cleanup of old database objects:
./init.sh --restore-postgres-with-clear datalens_enterprise_<...>.dump
Why CONTROL_API_CRYPTO_KEYis important
Restoring a backup to a new DataLens installation involves a critical caveat. Passwords and tokens in connections are encrypted with CONTROL_API_CRYPTO_KEY. This key is reset during a backup, and DataLens will not be able to decrypt old secrets. All your existing connections will fail: you will need to manually re-enter passwords.
When migrating via backup and recovery, it is important to transfer not only the database dump, but also the CONTROL_API_CRYPTO_KEY value from the Kubernetes secret of the old system to the new one. Do it by running this command:
./init.sh --kubectl get secret datalens-enterprise-secrets -o jsonpath='{.data.CONTROL_API_CRYPTO_KEY}' | base64 -d; echo
What to do if you accidentally deleted a workbook
Because backup via --restore-postgres does not recover deleted workbooks, you may need to use a separate workflow to restore a workbook that was accidentally deleted.
You can use the following helper commands:
# Run this command to get a list of deleted workbooks
./init.sh --pg sh -c 'psql -U "${POSTGRES_USER}" -d "${POSTGRES_DB_US}" -c "SELECT encode_id(workbook_id) AS workbook_id, title, deleted_at FROM workbooks WHERE deleted_at IS NOT NULL"'
# Once you have the workbook ID, substitute it for <WORKBOOK_ID> in the command below to restore the workbook:
./init.sh --kubectl exec -it deploy/datalens-enterprise-us -- node -e "fetch('http://localhost:8080/private/v2/workbooks/<WORKBOOK_ID>/restore',{method:'POST',headers:{'content-type':'application/json','x-us-master-token':process.env.MASTER_TOKEN}}).then(r=>r.json()).then(console.log)"
Exporting and importing workbooks
This mechanism is designed to transfer specific content. For it to work, the export function must be enabled on both installations. The process looks like this:
-
Source system
A user with permissions for the workbook selects Export. DataLens creates a ZIP archive with all objects inside the workbook.
-
Target system
User with the administrator or editor role selects Import and uploads the resulting archive.
Migration scenario: Yandex Cloud → On-premises
This is a frequent task when a company migrates its analytics into its corporate network. As there is no direct access to cloud DataLens' service database, the export and import method is used.
Migration plan:
-
Preparation
Deploy and configure DataLens On-premises, including authorization and network access to data sources.
-
Export
In cloud DataLens, export the required workbooks into JSON files one by one.
-
Import
In the On-premises environment, import the resulting JSON files.
-
Checking connections
Open the workbooks and verify that all connections are active. If not, edit their configuration. Check network availability, certificates, and the database address.
-
Rebinding
After importing, open the datasets and make sure they are using the new connections.
Use case
The administrator accidentally deletes an important workbook. They have a fresh database dump created before the deletion. They run the ./init.sh --restore-postgres datalens_enterprise_<...>.dump command, but still there is no workbook. Why?
- The dump is corrupted or was created after the workbook had already been deleted.
- To restore the workbook, the admin must first transfer the
CONTROL_API_CRYPTO_KEYfrom the dump, or the objects will not be decrypted and will be hidden. - The
--restore-postgrescommand does not overwriteis_deletedflags. - They must also run
./init.sh --restore-postgres-with-clearfor each deleted workbook individually.
Find out the answer
-
The dump is corrupted or was created after the workbook had already been deleted.
Incorrect. The scenario specifies that the dump is fresh and was created before the deletion. The issue is not with the dump.
-
To restore the workbook, the admin must first transfer the
CONTROL_API_CRYPTO_KEYfrom the dump, or the objects will not be decrypted and will be hidden.Incorrect. While the
CONTROL_API_CRYPTO_KEYkeys affect the decryption of passwords in connections, they do not hide or remove workbooks. -
The
--restore-postgrescommand does not overwriteis_deletedflags.Correct. A restore via
--restore-postgresdoes not recover deleted workbooks because it does not overwrite theis_deletedflags. In this scenario, you must either use--restore-postgres-with-clearor a command for targeted workbook recovery by its ID. -
They must also run
./init.sh --restore-postgres-with-clearfor each deleted workbook individually.Incorrect. The
--restore-postgres-with-clearcommand applies to the entire database, not individual workbooks. There is a separate workflow for targeted recovery of individual workbooks.
Results
You have mastered critical skills: backup, recovery, and migration. Now you know how to deploy and configure DataLens On-premises, as well as maintain its stable operation over the long term.