DEV Community

HarmonyOS
HarmonyOS

Posted on

How to Back Up a Relational Database in a Specified Folder?

Read the original article:How to Back Up a Relational Database in a Specified Folder?

Context

How to back up a relational database in a specified folder within a Huawei HarmonyOS application. The relational database here is based on SQLite and implemented via ArkTS for data persistence. The solution involves using rdbStore.backUp() for backup and fs.moveFile to relocate the backup file if needed.

Description

How to back up a relational database in a specified folder?

Solution

1. By default, ArkTS stores database files in the database directory. The path can be retrieved via the application context: ${this.getUIContext().getHostContext()?.databaseDir}/rdb. After execution, the DB file is generated at: ${this.getUIContext().getHostContext()?.databaseDir}/rdb/RdbTest.db.

const STORE_CONFIG: relationalStore.StoreConfig = {
  name: "RdbTest.db",
  securityLevel: relationalStore.SecurityLevel.S1
}
let rdbStore = await relationalStore.getRdbStore(context, STORE_CONFIG)
Enter fullscreen mode Exit fullscreen mode

2. Generating a Backup File:

  • Use rdbStore.backUp(name) to create a backup (stored in the same directory as the source DB).
  • To move the backup to a custom location, use fileIo.moveFile.
  • Restore using rdbStore.restore('RdbTest_Back.db.back').
rdbStore.backUp('RdbTest_Back.db.back') 
fileIo.moveFile(`${this.getUIContext().getHostContext()?.databaseDir}/rdb/RdbTest_Back.db`,
    `${this.getUIContext().getHostContext()?.filesDir/RdbTest_Back.db.back`)
Enter fullscreen mode Exit fullscreen mode
  • Saving to Public Directory: Use picker.DocumentViewPicker to save the backup to a public directory
const documentSaveOptions = new picker.DocumentSaveOptions();
documentSaveOptions.newFileNames = ["RdbTest_back.db"];
documentSaveOptions.fileSuffixChoices = ['.db'];
let uris: Array<string> = [];
// Please obtain the context within the component to ensure that `this.getUIContext().getHostContext()` returns a result of type `UIAbilityContext`.
let context = this.getUIContext().getHostContext() as common.UIAbilityContext;
const documentViewPicker = new picker.DocumentViewPicker(context);
documentViewPicker.save(documentSaveOptions).then((documentSaveResult: Array<string>) => {
    uris = documentSaveResult;
    console.info('documentViewPicker.save to file succeed and uris are:' + uris);
}).catch((err: BusinessError) => {
    console.error(`Invoke documentViewPicker.save failed, code is ${err.code}, message is ${err.message}`);
})
Enter fullscreen mode Exit fullscreen mode

rdbStore.backUp does not allow specifying a custom backup directory. Instead, use:

  • fileIo.moveFile to relocate the backup within sandbox directories.
  • picker.DocumentViewPicker to save backups to public directories.

Key Takeaways

  • ArkTS databases default to a specific directory unless specified otherwise.
  • Backups via rdbStore.backUp() are stored in the same directory as the source DB.
  • Use fileIo.moveFile to move backups to custom locations.
  • picker.DocumentViewPicker enables saving backups to public directories.

Written by Taskhyn Maksim

Top comments (0)