Management of Backup Folders and Data Directory Mapping in HarmonyOS
SameX
Posted on November 4, 2024
This article aims to deeply explore the data backup and directory management details of Huawei's HarmonyOS Next system (as of currently API12), and is summarized based on actual development practices.
Mainly as a carrier for technical sharing and exchange, it is inevitable that there are mistakes and omissions. Colleagues are welcome to put forward valuable opinions and questions for common progress.This article is original content. Any form of reprint must indicate the source and original author.
The backup and recovery framework in HarmonyOS Next is designed with a complete directory mapping and data isolation mechanism to support reliable backup and restoration of different application data. This article will systematically introduce the mapping relationship between application sandbox directories and backup recovery directories in HarmonyOS, backup strategies for user data and application data, and precautions for directory permission control, and provide some effective backup path configuration and management suggestions for reference.
I. Mapping relationship between sandbox directory and backup recovery directory
In the HarmonyOS Next system, the data of each application is stored in its independent application sandbox directory. The system isolates and manages the backup data of applications through the backup recovery directory to ensure the security and isolation of backup data.
1. Application sandbox directory
The application sandbox directory refers to the exclusive storage area allocated by the system for the application when it is installed. Application data such as user data, cache files, and databases are independently stored in this directory. This directory has the following characteristics:
- Data isolation: Data between different applications cannot be accessed by each other to ensure the security of application data.
-
Permission restriction: Only the application itself can read and write to its sandbox directory, and other applications have no right to access it.
### 2. Backup recovery directory
When the device is upgraded, reinstalled, or applications are migrated, the system will back up some data in the application sandbox to the backup recovery directory for data recovery. These directories are located in specific locations in the internal storage of the device, and the system will automatically restore the backup data to the corresponding path in the application sandbox directory.
### 3. Directory mapping relationship
The HarmonyOS Next system manages the access path of backup data through directory mapping. The typical directory mapping relationship is shown in the following table:
| Application data directory | Backup recovery directory | Directory mode |
| ------------------------- | ------------------------ | -------------- |
|
/data/user_de/{userId}/{package name}/
|/data/storage/el1/base/.backup/restore/{package name}/de/
|contextConstant.AreaMode.EL1
| |/data/user/{userId}/{package name}/
|/data/storage/el2/base/.backup/restore/{package name}/ce/
|contextConstant.AreaMode.EL2
| |/data/media/{userId}/Android/data/{package name}/
|/data/storage/el2/base/.backup/restore/{package name}/A/data/
|contextConstant.AreaMode.EL2
|
Among them, EL1 and EL2 modes correspond to different types of data areas of applications. Through the directory mapping relationship, HarmonyOS can efficiently manage the backup and recovery paths of applications.
II. Application data backup path configuration and strategy
HarmonyOS Next allows developers to specify the data directories that applications need to back up through configuration files, and perform backup and restoration through BackupExtensionAbility
. The backup strategy mainly includes directory selection, data filtering, permission control and other content.
1. Configure backup file path
The backup file path is managed by configuring the includes
and excludes
fields in backup_config.json:
- includes: Specify the directory or file path that needs to be backed up.
- excludes: Specify the directory or file path that needs to be excluded from backup. The following is an example configuration of backup_config.json:
{
"allowToBackupRestore": true,
"includes": [
"/data/storage/el2/base/files/"
],
"excludes": [
"/data/storage/el2/base/files/cache/"
],
"fullBackupOnly": false
}
2. Configuration strategy example
Suppose a music application needs to back up the user's favorite music list and playback history, but not the cache data. The following configuration can be made in backup_config.json:
{
"allowToBackupRestore": true,
"includes": [
"/data/storage/el2/base/files/user_favorites/",
"/data/storage/el2/base/files/play_history/"
],
"excludes": [
"/data/storage/el2/base/files/cache/"
],
"fullBackupOnly": false
}
The above configuration takes user favorite list and playback history as backup data, and excludes cache data from backup. In this way, when the device is migrated or the application is reinstalled, the user's core data can be retained without carrying unnecessary cache data.
III. Data directory and recovery directory mapping table
To help developers clearly understand the corresponding relationship between common application data directories and backup recovery directories, the following lists a common data directory mapping table:
| Data type | Sandbox directory example | Backup recovery directory example | Usage suggestion |
| ------------------- | ------------------------------------ | --------------------------------------------------- | ---------------------- |
| User data | /data/user_de/{userId}/{package name}/
| /data/storage/el1/base/.backup/restore/{package name}/de/
| Basic data visible to users |
| Application private data | /data/user/{userId}/{package name}/
| /data/storage/el2/base/.backup/restore/{package name}/ce/
| Private data independently stored by applications |
| Media data | /data/media/{userId}/Android/data/{package name}/
| /data/storage/el2/base/.backup/restore/{package name}/A/data/
| Picture, audio and other multimedia data |
Through this table, developers can more reasonably select the data directories that need to be backed up to ensure that various types of data are correctly loaded during the recovery process.
IV. Precautions: Permission control of backup path
During the data backup process, permission control is crucial to ensure the security and integrity of backup data. The following are some common permission control strategies:
1. Application data isolation
The backup data of each application is stored in an independent backup recovery directory and cannot be accessed by other applications, which conforms to the principle of sandbox data isolation. When configuring the backup path, developers should avoid backing up files containing sensitive information to avoid being misread or misused by the system.
2. Access permission restriction
In the HarmonyOS Next system, application backup and recovery functions require system permission authorization. Developers should use the permissions in BackupExtensionAbility reasonably and avoid excessive invocation of backup and recovery functions to prevent affecting system resources.
3. Correct management of recovery directory
During data recovery, it is necessary to ensure the mapping consistency between the recovery directory and the application sandbox directory. Any data stored in the application sandbox needs to be completely restored to the corresponding path during recovery to prevent data loss or path errors.
Summary
The backup folder and data directory mapping mechanism in HarmonyOS Next provides strong support for application data backup and recovery. By reasonably configuring the mapping relationship between application sandbox and recovery directory, backup strategy and permission control, developers can flexibly manage the application's data backup path and ensure the safe recovery of data in different device and system environments. These mechanisms not only improve the data security of users, but also provide effective technical support for device upgrades and data migrations.
Posted on November 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024
November 29, 2024
November 29, 2024
November 29, 2024