Starting from API version 26.0.1, the capability of batch enterprise data deployment and restoration is available.
This capability allows you to query the size of user data to be backed up, back up and restore user data, query the list of apps for which data can be backed up, query the size of app sandbox data to be backed up, back up and restore app sandbox data, and cancel backup and restoration operations. It supports common enterprise apps and enterprise MDM apps to back up and restore app sandbox data and user data.
For details about the APIs, please refer to the API reference.
| API | Description |
|---|---|
| getUserBackupDataSize(): Promise<number> | Obtains the size of the user data to be backed up. This API uses a promise to return the result. |
| backupUserData(dest: string, progressCallback: BackupRestoreProgressCallback): Promise<void> | Backs up user data. This API uses a promise to return the result. |
| restoreUserData(src: string, progressCallback: BackupRestoreProgressCallback, options?: RestoreUserDataOptions): Promise<void> | Restores user data. This API uses a promise to return the result. |
| getAllowedBackupAndRestoreBundleList(): Promise<BundleInfo[]> | Obtains the list of apps whose data can be backed up or restored. This API uses a promise to return the result. |
| getBundlesBackupDataSize(bundleList: string[]): Promise<number> | Obtains the size of the app sandbox data to be backed up. This API uses a promise to return the result. |
| backupBundles(bundleList: string[], dest: string, progressCallback: BackupRestoreProgressCallback): Promise<void> | Backs up app sandbox data. This API uses a promise to return the result. |
| restoreBundles(bundleList: string[], src: string, progressCallback: BackupRestoreProgressCallback): Promise<void> | Restores app sandbox data. This API uses a promise to return the result. |
| cancelBackupRestore(): Promise<void> | Cancels the backup or restoration operation. This API uses a promise to return the result. |
Before requesting permissions, ensure that the basic principles for using permissions are met. Then, declare required permissions under requestPermissions in the module.json5 configuration file of the module of the project. Request the ohos.permission.ENTERPRISE_MANAGE_BACKUP permission. This capability is available only to common enterprise apps and enterprise MDM apps with this permission.
Example:
{
"requestPermissions": [
{
"name": "ohos.permission.ENTERPRISE_MANAGE_BACKUP"
}
]
} Import the required modules.
import { enterpriseDeployRestore } from '@kit.EnterpriseFoundationKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import { fileIo as fs } from '@kit.CoreFileKit';
import { hilog } from '@kit.PerformanceAnalysisKit'; Call backupUserData on the source device to back up the user data.
The user data to be backed up does not include Gallery data stored in the image directory. To back up Gallery data, you need to use the API for backing up app sandbox data.
Call getAllowedBackupAndRestoreBundleList on the source device to obtain the list of apps whose data can be backed up or restored.
Call getBundlesBackupDataSize on the source device to obtain the size of the app sandbox data to be backed up.
Call backupBundles on the source device to back up the sandbox data of specified apps. List of paths for app sandbox data backup:
"data/storage/el1/database/", "data/storage/el1/base/files/", "data/storage/el1/base/preferences/", "data/storage/el1/base/haps/*/files/", "data/storage/el1/base/haps/*/preferences/", "data/storage/el2/database/", "data/storage/el2/base/files/", "data/storage/el2/base/preferences/", "data/storage/el2/base/haps/*/files/", "data/storage/el2/base/haps/*/preferences/", "data/storage/el2/distributedfiles/", "data/storage/el5/database/", "data/storage/el5/base/files/", "data/storage/el5/base/preferences/", "data/storage/el5/base/haps/*/files/", "data/storage/el5/base/haps/*/preferences/"
Call restoreUserData on the target device to restore the user data from the backup.
The user data to be restored does not include Gallery data stored in the image directory. To restore Gallery data, you need to use the API for restoring app sandbox data.
Call restoreBundles on the target device to restore the app sandbox data from the backup.
During the backup or restoration, call cancelBackupRestore to cancel the current backup or restoration operation.
Call getUserBackupDataSize on the source device to obtain the size of the user data to be backed up.