This article aims to deeply explore the technical details of data backup in Huawei's HarmonyOS Next system (as of API 12 currently) and is summarized based on actual development practices.
As a carrier for technical sharing and exchange, it is inevitable that there may be errors 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.
In mobile application development, data backup and restoration is an important mechanism to ensure the security of user data. HarmonyOS Next provides a reliable backup and restoration framework for applications, supporting the storage of application data in a secure backup directory to deal with scenarios such as device upgrades and application reinstallations. This article will systematically introduce the data backup concepts, framework composition, and core component BackupExtensionAbility in HarmonyOS Next to help developers quickly understand and apply this mechanism.
I. Overview of data backup in HarmonyOS Next
Data backup refers to storing key information and user data of an application in a secure backup directory for restoration when the application is reinstalled or the device is migrated. The data backup framework in HarmonyOS Next ensures the independence and security of application data by supporting application sandboxes and mapping management of backup and restoration directories:
- Backup directory mapping: The system maps an independent backup and restoration directory for each application in the internal storage space to ensure that data between different applications does not interfere with each other.
- Data backup scenarios: Include application uninstallation and reinstallation, device upgrade and migration, etc. HarmonyOS Next supports automatic data restoration after an OTA upgrade.
- Security isolation mechanism: HarmonyOS provides an application sandbox to ensure the security of backup data and prevent unauthorized applications from accessing the data.
II. Introduction to BackupExtensionAbility
In the data backup framework of HarmonyOS Next, BackupExtensionAbility is a core component used to define and implement the backup and restoration logic of application data. BackupExtensionAbility is a derived class of ExtensionAbility and runs as a non-interface component with the following key characteristics:
- Lifecycle: BackupExtensionAbility starts with the backup task and exits after the task is completed, ensuring that the backup process does not affect the normal operation of the application.
-
Main methods:
- onBackup: Used to define the logic during data backup, such as storage path, data format conversion, etc.
- onRestore: Used to define the logic of data restoration, such as file verification, data migration, etc.
- Synchronous interface: The onRestore method is a synchronous interface. It is necessary to ensure synchronous waiting for all asynchronous operations inside the method to ensure the integrity of data restoration.
- Custom implementation: Developers can customize backup and restoration operations of different data formats and logics by overriding onBackup and onRestore.
The following is a basic implementation example of BackupExtensionAbility:
import { BackupExtensionAbility, BundleVersion } from '@kit.CoreFileKit';
const TAG = `BackupExtensionAbility`;
// Define the BackupExtensionAbility class
export default class BackupExtension extends BackupExtensionAbility {
// Data backup
onBackup() {
console.log(TAG, `onBackup invoked, starting data backup...`);
}
// Data restoration
async onRestore(bundleVersion: BundleVersion): Promise<void> {
console.log(TAG, `onRestore invoked for version ${JSON.stringify(bundleVersion)}`);
if (bundleVersion.name.startsWith("0.0.0.0")) {
console.log(TAG, `Handling data migration for HarmonyOS to HarmonyOS NEXT scenario`);
} else {
console.log(TAG, `Other migration scenario`);
}
}
}
III. Usage scenarios of backup and restoration
The data backup and restoration mechanism of HarmonyOS Next is applicable to multiple typical scenarios to ensure that users can still use historical data smoothly after operations such as application uninstallation and device upgrade. The following are some common scenarios:
- Application reinstallation: When a user uninstalls and reinstalls an application, the system will restore user data from the backup directory to keep user data from being lost.
- Device upgrade: When a device is upgraded from an old version of HarmonyOS to HarmonyOS Next, the original application data is automatically migrated to the new system.
- Data migration: Data transfer between the same device or multiple devices to ensure the consistency of application data between different devices.
To achieve data migration in the above scenarios, developers can set different backup logics and restoration mechanisms in BackupExtensionAbility, such as strategies for data restoration based on device environment and version control.
IV. Composition of the backup and restoration framework
The backup and restoration framework of HarmonyOS Next is composed of the following major modules to ensure the smooth progress of data backup and restoration:
- Backup and restoration directory Each application has an independent backup and restoration directory in the internal storage space for storing data to be backed up. When an application is uninstalled or the system is upgraded, the system will automatically restore according to the data in the backup and restoration directory.
-
Data storage distinction:
- Application sandbox directory: Used to isolate application data and ensure the independence of application files.
- Restoration directory: After data backup, the system will save application data in a specific restoration directory for reinstallation and migration of data.
| Directory type | Example path | Description |
| -------- | -------- | ---- |
| User file backup directory | /data/storage/el1/base/.backup/restore/user/
| Used to store user files |
| Application data backup directory | /data/storage/el1/base/.backup/restore/app/
| Used to store application data |
BackupExtensionAbility
The core of the backup and restoration framework, implementing specific data backup and restoration logic through BackupExtensionAbility.Backup task management
The backup and restoration framework supports the scheduling of multiple backup tasks and automatically triggers backup and restoration operations when users operate or the system is upgraded. For example, backup tasks are triggered when an application is uninstalled and reinstalled, and data restoration tasks are triggered when the device system version is upgraded.Permission control and security isolation
The system assigns an independent backup directory to each application and strictly controls access permissions to ensure the security and isolation of application data. During the data restoration stage, the system automatically loads backup data into the sandbox directory to prevent external applications from accessing sensitive data without authorization.
Summary
The data backup and restoration framework of HarmonyOS Next provides a strong guarantee for the reliability and security of application data through mechanisms such as providing a secure and isolated backup and restoration directory, flexibly configured BackupExtensionAbility, and backup task management. We can configure suitable backup strategies and restoration logics according to specific business needs to meet users' seamless data migration and usage experience between different devices and systems.
Top comments (0)