1e41f4b71Sopenharmony_ci# Backup and Restore Triggered by System Applications 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe backup and restore framework provides a solution for backing up and restoring data of applications and services on a device. You can follow the procedure below to enable an application to trigger data backup or restoration: 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci- [Obtain capability files](#obtaining-capability-files): Obtain capability files of all applications of the user in the system. The capability files are indispensable for data backup and restoration. 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ci- [Back up application data](#backing-up-application-data): Back up the application data based on the application information in the capability files. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci- [Restore application data](#restoring-application-data): Restore the application data based on the application information in the capability files. 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## How to Develop 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ciFor details about how to use the APIs, see [Backup and Restore](../reference/apis-core-file-kit/js-apis-file-backup-sys.md). 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ciBefore using the APIs, you need to: 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci1. Apply for the ohos.permission.BACKUP permission. For details, see [Requesting Permissions for system_basic Applications](../security/AccessToken/determine-application-mode.md#requesting-permissions-for-system_basic-applications). 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci2. Import **@ohos.file.backup**. 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci ```js 22e41f4b71Sopenharmony_ci import backup from '@ohos.file.backup'; 23e41f4b71Sopenharmony_ci ``` 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci## Obtaining Capability Files 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciObtain capability files of all applications of the current user. The capability files are indispensable for application data backup and restoration. 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ciThe capability file of an application contains the device type, device version, and basic application information, such as the application name, application size, application version, whether to allow backup and restoration, and whether to install the application during restoration. 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ciCall [backup.getLocalCapabilities()](../reference/apis-core-file-kit/js-apis-file-backup-sys.md#backupgetlocalcapabilities) to obtain the capability file. 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci```ts 34e41f4b71Sopenharmony_ciimport backup from '@ohos.file.backup'; 35e41f4b71Sopenharmony_ciimport common from '@ohos.app.ability.common'; 36e41f4b71Sopenharmony_ciimport fs from '@ohos.file.fs'; 37e41f4b71Sopenharmony_ciimport { BusinessError } from '@ohos.base'; 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci// Obtain the application file path. 40e41f4b71Sopenharmony_cilet context = getContext(this) as common.UIAbilityContext; 41e41f4b71Sopenharmony_cilet filesDir = context.filesDir; 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ciasync function getLocalCapabilities(): Promise<void> { 44e41f4b71Sopenharmony_ci try { 45e41f4b71Sopenharmony_ci let fileData = await backup.getLocalCapabilities(); 46e41f4b71Sopenharmony_ci console.info('getLocalCapabilities success'); 47e41f4b71Sopenharmony_ci let fpath = filesDir + '/localCapabilities.json'; 48e41f4b71Sopenharmony_ci fs.copyFileSync(fileData.fd, fpath); 49e41f4b71Sopenharmony_ci fs.closeSync(fileData.fd); 50e41f4b71Sopenharmony_ci } catch (error) { 51e41f4b71Sopenharmony_ci let err: BusinessError = error as BusinessError; 52e41f4b71Sopenharmony_ci console.error('getLocalCapabilities failed with err: ' + JSON.stringify(err)); 53e41f4b71Sopenharmony_ci } 54e41f4b71Sopenharmony_ci} 55e41f4b71Sopenharmony_ci``` 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**Capability file example** 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| Name | Type| Mandatory| Description | 60e41f4b71Sopenharmony_ci| -------------- | -------- | ---- | ---------------------- | 61e41f4b71Sopenharmony_ci| bundleInfos | Array | Yes | Application information. | 62e41f4b71Sopenharmony_ci| allToBackup | Boolean | Yes | Whether to allow backup and restoration. | 63e41f4b71Sopenharmony_ci| extensionName | String | Yes | Extension name of the application. | 64e41f4b71Sopenharmony_ci| name | String | Yes | Bundle name of the application. | 65e41f4b71Sopenharmony_ci| needToInstall | Boolean | Yes | Whether to install the application during data restoration.| 66e41f4b71Sopenharmony_ci| spaceOccupied | Number | Yes | Space occupied by the application data.| 67e41f4b71Sopenharmony_ci| versionCode | Number | Yes | Application version number. | 68e41f4b71Sopenharmony_ci| versionName | String | Yes | Application version name. | 69e41f4b71Sopenharmony_ci| deviceType | String | Yes | Type of the device. | 70e41f4b71Sopenharmony_ci| systemFullName | String | Yes | Device version. | 71e41f4b71Sopenharmony_ci 72e41f4b71Sopenharmony_ci```json 73e41f4b71Sopenharmony_ci{ 74e41f4b71Sopenharmony_ci"bundleInfos" :[{ 75e41f4b71Sopenharmony_ci "allToBackup" : true, 76e41f4b71Sopenharmony_ci "extensionName" : "BackupExtensionAbility", 77e41f4b71Sopenharmony_ci "name" : "com.example.hiworld", 78e41f4b71Sopenharmony_ci "needToInstall" : false, 79e41f4b71Sopenharmony_ci "spaceOccupied" : 0, 80e41f4b71Sopenharmony_ci "versionCode" : 1000000, 81e41f4b71Sopenharmony_ci "versionName" : "1.0.0" 82e41f4b71Sopenharmony_ci }], 83e41f4b71Sopenharmony_ci"deviceType" : "default", 84e41f4b71Sopenharmony_ci"systemFullName" : "OpenHarmony-4.0.0.0" 85e41f4b71Sopenharmony_ci} 86e41f4b71Sopenharmony_ci``` 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci## Backing Up Application Data 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ciYou can select the application data to be backed up based on the application information in the capability files. 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_ciThe Backup & Restore service packages the application data to be backed up. The package file handle is returned by the [onFileReady](../reference/apis-core-file-kit/js-apis-file-backup-sys.md#onfileready) callback registered when the **SessionBackup** instance is created. 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciYou can save the file to a local directory as required. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Example** 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci ```ts 99e41f4b71Sopenharmony_ci import backup from '@ohos.file.backup'; 100e41f4b71Sopenharmony_ci import common from '@ohos.app.ability.common'; 101e41f4b71Sopenharmony_ci import fs from '@ohos.file.fs'; 102e41f4b71Sopenharmony_ci import { BusinessError } from '@ohos.base'; 103e41f4b71Sopenharmony_ci 104e41f4b71Sopenharmony_ci // Obtain the sandbox path. 105e41f4b71Sopenharmony_ci let context = getContext(this) as common.UIAbilityContext; 106e41f4b71Sopenharmony_ci let filesDir = context.filesDir; 107e41f4b71Sopenharmony_ci // Create a SessionBackup instance for data backup. 108e41f4b71Sopenharmony_ci let g_session: backup.SessionBackup; 109e41f4b71Sopenharmony_ci function createSessionBackup(): backup.SessionBackup { 110e41f4b71Sopenharmony_ci let generalCallbacks: backup.GeneralCallbacks = { 111e41f4b71Sopenharmony_ci // onFileReady is called to return a data complete notification to the application. Avoid time-consuming implementations in onFileReady. You can use asynchronous threads to process data based on the file FD. 112e41f4b71Sopenharmony_ci onFileReady: (err: BusinessError, file: backup.File) => { 113e41f4b71Sopenharmony_ci if (err) { 114e41f4b71Sopenharmony_ci console.info('onFileReady err: ' + JSON.stringify(err)); 115e41f4b71Sopenharmony_ci } 116e41f4b71Sopenharmony_ci try { 117e41f4b71Sopenharmony_ci let bundlePath = filesDir + '/' + file.bundleName; 118e41f4b71Sopenharmony_ci if (!fs.accessSync(bundlePath)) { 119e41f4b71Sopenharmony_ci fs.mkdirSync(bundlePath); 120e41f4b71Sopenharmony_ci } 121e41f4b71Sopenharmony_ci // Calling copyFileSync causes one more memory copy. To reduce memory consumption, you can use the FD returned by onFileReady for data processing, and close the FD after data is processed. 122e41f4b71Sopenharmony_ci fs.copyFileSync(file.fd, bundlePath + `/${file.uri}`); 123e41f4b71Sopenharmony_ci fs.closeSync(file.fd); 124e41f4b71Sopenharmony_ci console.info('onFileReady success'); 125e41f4b71Sopenharmony_ci } catch (e) { 126e41f4b71Sopenharmony_ci console.error('onFileReady failed with err: ' + e); 127e41f4b71Sopenharmony_ci } 128e41f4b71Sopenharmony_ci }, 129e41f4b71Sopenharmony_ci onBundleBegin: (err: BusinessError<string|void>, bundleName: string) => { 130e41f4b71Sopenharmony_ci if (err) { 131e41f4b71Sopenharmony_ci console.info('onBundleBegin err: ' + JSON.stringify(err)); 132e41f4b71Sopenharmony_ci } else { 133e41f4b71Sopenharmony_ci console.info('onBundleBegin bundleName: ' + bundleName); 134e41f4b71Sopenharmony_ci } 135e41f4b71Sopenharmony_ci }, 136e41f4b71Sopenharmony_ci onBundleEnd: (err: BusinessError<string|void>, bundleName: string) => { 137e41f4b71Sopenharmony_ci if (err) { 138e41f4b71Sopenharmony_ci console.info('onBundleEnd err: ' + JSON.stringify(err)); 139e41f4b71Sopenharmony_ci } else { 140e41f4b71Sopenharmony_ci console.info('onBundleEnd bundleName: ' + bundleName); 141e41f4b71Sopenharmony_ci } 142e41f4b71Sopenharmony_ci }, 143e41f4b71Sopenharmony_ci onAllBundlesEnd: (err: BusinessError) => { 144e41f4b71Sopenharmony_ci if (err) { 145e41f4b71Sopenharmony_ci console.info('onAllBundlesEnd err: ' + JSON.stringify(err)); 146e41f4b71Sopenharmony_ci } else { 147e41f4b71Sopenharmony_ci console.info('onAllBundlesEnd'); 148e41f4b71Sopenharmony_ci } 149e41f4b71Sopenharmony_ci }, 150e41f4b71Sopenharmony_ci onBackupServiceDied: () => { 151e41f4b71Sopenharmony_ci console.info('onBackupServiceDied'); 152e41f4b71Sopenharmony_ci }, 153e41f4b71Sopenharmony_ci onResultReport: (bundleName: string, result: string) => { 154e41f4b71Sopenharmony_ci console.info('onResultReport bundleName: ' + bundleName); 155e41f4b71Sopenharmony_ci console.info('onResultReport result: ' + result); 156e41f4b71Sopenharmony_ci }, 157e41f4b71Sopenharmony_ci onProcess:(bundleName: string, process: string) => { 158e41f4b71Sopenharmony_ci console.info('onPross bundleName: ' + JSON.stringify(bundleName)); 159e41f4b71Sopenharmony_ci console.info('onPross result: ' + JSON.stringify(process)); 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci } 162e41f4b71Sopenharmony_ci let sessionBackup = new backup.SessionBackup(generalCallbacks); 163e41f4b71Sopenharmony_ci return sessionBackup; 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci async function sessionBackup (): Promise<void> { 167e41f4b71Sopenharmony_ci g_session = createSessionBackup(); 168e41f4b71Sopenharmony_ci // Select the application to be backed up based on the capability file obtained by backup.getLocalCapabilities(). 169e41f4b71Sopenharmony_ci // You can also back up data based on the application bundle name. 170e41f4b71Sopenharmony_ci const backupApps: string[] = [ 171e41f4b71Sopenharmony_ci "com.example.hiworld", 172e41f4b71Sopenharmony_ci ] 173e41f4b71Sopenharmony_ci await g_session.appendBundles(backupApps); 174e41f4b71Sopenharmony_ci console.info('appendBundles success'); 175e41f4b71Sopenharmony_ci } 176e41f4b71Sopenharmony_ci ``` 177e41f4b71Sopenharmony_ci 178e41f4b71Sopenharmony_ci## Restoring Application Data 179e41f4b71Sopenharmony_ci 180e41f4b71Sopenharmony_ciYou can select the application data to be restored based on the application information in the capability files. 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ciThe Backup and Restore service returns the FD of the application data to be restored in the [onFileReady](../reference/apis-core-file-kit/js-apis-file-backup-sys.md#onfileready) callback registered when the **SessionRestore** instance is created. The file handle is obtained by [getFileHandle](../reference/apis-core-file-kit/js-apis-file-backup-sys.md#getfilehandle). Then, the data to be restored is written to the file handle based on the [uri](../reference/apis-core-file-kit/js-apis-file-backup-sys.md#filemeta) returned. After the data is written, use [publishFile](../reference/apis-core-file-kit/js-apis-file-backup-sys.md#publishfile) to notify the service that the data write is complete. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ciWhen all the data of the application is ready, the service starts to restore the application data. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci**Example** 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci ```ts 189e41f4b71Sopenharmony_ci import backup from '@ohos.file.backup'; 190e41f4b71Sopenharmony_ci import fs from '@ohos.file.fs'; 191e41f4b71Sopenharmony_ci import { BusinessError } from '@ohos.base'; 192e41f4b71Sopenharmony_ci // Create a SessionRestore instance for data restoration. 193e41f4b71Sopenharmony_ci let g_session: backup.SessionRestore; 194e41f4b71Sopenharmony_ci let initMap = new Map<string, number>(); 195e41f4b71Sopenharmony_ci testFileNum = 123; // Number of files initialized. 196e41f4b71Sopenharmony_ci let testBundleName = 'com.example.myapplication'; // Test bundle name. 197e41f4b71Sopenharmony_ci initMap.set(testBundleName, testFileNum); 198e41f4b71Sopenharmony_ci let countMap = new Map<string, number>(); 199e41f4b71Sopenharmony_ci countMap.set(testBundleName, 0); // Initialization count. 200e41f4b71Sopenharmony_ci async function publishFile(file: backup.File): Promise<void> { 201e41f4b71Sopenharmony_ci let fileMeta: backup.FileMeta = { 202e41f4b71Sopenharmony_ci bundleName: file.bundleName, 203e41f4b71Sopenharmony_ci uri: '' 204e41f4b71Sopenharmony_ci } 205e41f4b71Sopenharmony_ci await g_session.publishFile(fileMeta); 206e41f4b71Sopenharmony_ci } 207e41f4b71Sopenharmony_ci function createSessionRestore(): backup.SessionRestore { 208e41f4b71Sopenharmony_ci let generalCallbacks: backup.GeneralCallbacks = { 209e41f4b71Sopenharmony_ci onFileReady: (err: BusinessError, file: backup.File) => { 210e41f4b71Sopenharmony_ci if (err) { 211e41f4b71Sopenharmony_ci console.info('onFileReady err: ' + JSON.stringify(err)); 212e41f4b71Sopenharmony_ci } 213e41f4b71Sopenharmony_ci // Set bundlePath based on the actual situation. 214e41f4b71Sopenharmony_ci let bundlePath: string = ''; 215e41f4b71Sopenharmony_ci if (!fs.accessSync(bundlePath)) { 216e41f4b71Sopenharmony_ci console.info('onFileReady bundlePath err : ' + bundlePath); 217e41f4b71Sopenharmony_ci } 218e41f4b71Sopenharmony_ci fs.copyFileSync(bundlePath, file.fd); 219e41f4b71Sopenharmony_ci fs.closeSync(file.fd); 220e41f4b71Sopenharmony_ci // After the data is transferred, notify the server that the files are ready. 221e41f4b71Sopenharmony_ci countMap[file.bundleName]++; 222e41f4b71Sopenharmony_ci if (countMap[file.bundleName] == initMap[file.bundleName]) { // Trigger publishFile when all the files of each bundle are received. 223e41f4b71Sopenharmony_ci publishFile(file); 224e41f4b71Sopenharmony_ci } 225e41f4b71Sopenharmony_ci console.info('onFileReady success'); 226e41f4b71Sopenharmony_ci }, 227e41f4b71Sopenharmony_ci onBundleBegin: (err: BusinessError<string|void>, bundleName: string) => { 228e41f4b71Sopenharmony_ci if (err) { 229e41f4b71Sopenharmony_ci console.error('onBundleBegin failed with err: ' + JSON.stringify(err)); 230e41f4b71Sopenharmony_ci } 231e41f4b71Sopenharmony_ci console.info('onBundleBegin success'); 232e41f4b71Sopenharmony_ci }, 233e41f4b71Sopenharmony_ci onBundleEnd: (err: BusinessError<string|void>, bundleName: string) => { 234e41f4b71Sopenharmony_ci if (err) { 235e41f4b71Sopenharmony_ci console.error('onBundleEnd failed with err: ' + JSON.stringify(err)); 236e41f4b71Sopenharmony_ci } 237e41f4b71Sopenharmony_ci console.info('onBundleEnd success'); 238e41f4b71Sopenharmony_ci }, 239e41f4b71Sopenharmony_ci onAllBundlesEnd: (err: BusinessError) => { 240e41f4b71Sopenharmony_ci if (err) { 241e41f4b71Sopenharmony_ci console.error('onAllBundlesEnd failed with err: ' + JSON.stringify(err)); 242e41f4b71Sopenharmony_ci } 243e41f4b71Sopenharmony_ci console.info('onAllBundlesEnd success'); 244e41f4b71Sopenharmony_ci }, 245e41f4b71Sopenharmony_ci onBackupServiceDied: () => { 246e41f4b71Sopenharmony_ci console.info('service died'); 247e41f4b71Sopenharmony_ci }, 248e41f4b71Sopenharmony_ci onResultReport: (bundleName: string, result: string) => { 249e41f4b71Sopenharmony_ci console.info('onResultReport bundleName: ' + bundleName); 250e41f4b71Sopenharmony_ci console.info('onResultReport result: ' + result); 251e41f4b71Sopenharmony_ci }, 252e41f4b71Sopenharmony_ci onProcess:(bundleName: string, process: string) => { 253e41f4b71Sopenharmony_ci console.info('onPross bundleName: ' + JSON.stringify(bundleName)); 254e41f4b71Sopenharmony_ci console.info('onPross result: ' + JSON.stringify(process)); 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci } 257e41f4b71Sopenharmony_ci let sessionRestore = new backup.SessionRestore(generalCallbacks); 258e41f4b71Sopenharmony_ci return sessionRestore; 259e41f4b71Sopenharmony_ci } 260e41f4b71Sopenharmony_ci 261e41f4b71Sopenharmony_ci async function restore01 (): Promise<void> { 262e41f4b71Sopenharmony_ci g_session = createSessionRestore(); 263e41f4b71Sopenharmony_ci const restoreApps: string[] = [ 264e41f4b71Sopenharmony_ci "com.example.hiworld", 265e41f4b71Sopenharmony_ci ] 266e41f4b71Sopenharmony_ci // You can obtain the capability file based on actual situation. The following is an example only. 267e41f4b71Sopenharmony_ci // You can also construct capability files as required. 268e41f4b71Sopenharmony_ci let fileData = await backup.getLocalCapabilities(); 269e41f4b71Sopenharmony_ci await g_session.appendBundles(fileData.fd, restoreApps); 270e41f4b71Sopenharmony_ci console.info('appendBundles success'); 271e41f4b71Sopenharmony_ci // After the applications to be restored are added, call getFileHandle() to obtain the handles of the application files to be restored based on the application name. 272e41f4b71Sopenharmony_ci // The number of application data files to be restored varies depending on the number of backup files. The following is only an example. 273e41f4b71Sopenharmony_ci let handle: backup.FileMeta = { 274e41f4b71Sopenharmony_ci bundleName: restoreApps[0], 275e41f4b71Sopenharmony_ci uri: "manage.json" 276e41f4b71Sopenharmony_ci } 277e41f4b71Sopenharmony_ci await g_session.getFileHandle(handle); 278e41f4b71Sopenharmony_ci handle.uri = "1.tar"; 279e41f4b71Sopenharmony_ci await g_session.getFileHandle(handle); 280e41f4b71Sopenharmony_ci console.info('getFileHandle success'); 281e41f4b71Sopenharmony_ci } 282e41f4b71Sopenharmony_ci ``` 283