#鸿蒙学习大百科#应用如何接入数据备份恢复?

应用如何接入数据备份恢复?

HarmonyOS
2024-10-22 15:36:18
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
努力向前进

module.json5

"extensionAbilities": [
  {
    "description": "$string:EntryAbility_desc",
    "icon": "$media:app_icon",
    "name": "BackupExtensionAbility",
    "type": "backup",//注册类型
    "visible": true,
    "metadata": [
      {
        "name": "ohos.extension.backup",
        "resource": "$profile:backup_config"
      }
    ],
    // 在BackupExtension.ts文件里自定义继承BackupExtensionAbility,重写其中的onBackup和onRestore方法。
    // 如果没有特殊要求可以空实现,则备份恢复服务会按照统一的备份恢复数据规则进行备份恢复。
    "srcEntrance": "./ets/BackupExtension/BackupExtension.ts",
  }
]

resources/base/profile/backup_config.json

{
  "allowToBackupRestore": true,
  "includes": [
    "/data/storage/el2/base/files/users/"
  ],
  "excludes": [
    "/data/storage/el2/base/files/users/hidden/"
  ],
  "fullBackupOnly": false,
  "restoreDeps": ""
}

自定义类继承的BackupExtensionAbility,通过重写其onBackup和onRestore方法,使其达到在备份预加工应用数据或者在恢复阶段加工待恢复文件。

import BackupExtensionAbility, {BundleVersion} from '@ohos.application.BackupExtensionAbility';

const TAG = `FileBackupExtensionAbility`;
export default class BackupExtension extends  BackupExtensionAbility {
  async onBackup ()   {
    console.log("onBackup ok");}

  async onRestore (bundleVersion : BundleVersion) {
    console.log( `onRestore ok ${JSON.stringify(bundleVersion)}`);
    console.log( `onRestore end`);
  }
}
分享
微博
QQ
微信
回复
2024-10-22 22:21:44
相关问题