Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to backup Isar db in a cloud storage? #579

Closed
mysticeti opened this issue Aug 23, 2022 · 11 comments
Closed

How to backup Isar db in a cloud storage? #579

mysticeti opened this issue Aug 23, 2022 · 11 comments

Comments

@mysticeti
Copy link

Hi
I am currently researching for a db to be used in a simple app where backup of the db to a cloud storage(s3 or firebase storage) is necessary. Is it possible to backup isar db and if so how to do it? Could not find anything related to it in the doc.
Thanks

@simc
Copy link
Member

simc commented Aug 23, 2022

This highly depends on your use case. You can use the .exportJson() method and backup the json data. You can use isar.copyToFile() to get a compacted version of the database or you can manually sync the data with firebase.

@mysticeti
Copy link
Author

I was thinking of backing up the full isar db in a cloud storage. For instance, if I use sqlite3, there is a single db file that can uploaded to cloud storage either daily or weekly. I think copyToFile might be suitable but I could not find isar.copyToFile() in the doc https://pub.dev/documentation/isar/latest/isar/Isar-class.html

@simc
Copy link
Member

simc commented Aug 23, 2022

Isar is also just one file. Here is the method (you need the newest version): https://pub.dev/documentation/isar/3.0.0-dev.10/isar/Isar/copyToFile.html

@mysticeti
Copy link
Author

I can also open the compacted database as usual using Isar.open(directory: /backup_db.isar) and Isar db single file is .isar file right?

@simc
Copy link
Member

simc commented Aug 23, 2022

Correct!

@mysticeti
Copy link
Author

Thank you for helping me understand isar a bit more. Great work on isar. Hoping to integrate isar on an existing app.

@roiskhoiron
Copy link

roiskhoiron commented Jan 17, 2023

i have error when i try to open my file backup isar on directory ../Backups/TBY7Kf5h7rBKGbeNS.isar

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: IsarError: Cannot open Environment: MdbxError (20): Not a directory

anyone can help this issue ?

this is mycode :

Future<void> restore({required File file}) async {
    print('common restore ${file.path}');
    isar = await Isar.open([
      ExampleSchema1,
      ExampleSchema2,
    ], name: basename(file.path), inspector: true, directory: file.path);
  }

@alejandrogiubel
Copy link

alejandrogiubel commented Mar 3, 2023

i have error when i try to open my file backup isar on directory ../Backups/TBY7Kf5h7rBKGbeNS.isar

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: IsarError: Cannot open Environment: MdbxError (20): Not a directory

anyone can help this issue ?

this is mycode :

Future<void> restore({required File file}) async {
    print('common restore ${file.path}');
    isar = await Isar.open([
      ExampleSchema1,
      ExampleSchema2,
    ], name: basename(file.path), inspector: true, directory: file.path);
  }

Same here, any update on that?

@abdullahalamodi
Copy link

i have error when i try to open my file backup isar on directory ../Backups/TBY7Kf5h7rBKGbeNS.isar
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: IsarError: Cannot open Environment: MdbxError (20): Not a directory
anyone can help this issue ?
this is mycode :

Future<void> restore({required File file}) async {
    print('common restore ${file.path}');
    isar = await Isar.open([
      ExampleSchema1,
      ExampleSchema2,
    ], name: basename(file.path), inspector: true, directory: file.path);
  }

Same here, any update on that?

directory: (here should be the directory that contains that file not the file itself)

@abdullah-cse
Copy link

abdullah-cse commented Nov 4, 2023

@mysticeti, @abdullahalamodi How to backup Isar Db in File Storage, using copyToFile()?

//Getting user specified path using FilePicker Package
String? path = await FilePicker.platform.getDirectoryPath();
//Create an empty file on that path
final emptyFile=File(path);

//Now copy isar db to this emptyFile
final isar = await ref.watch(isarProvider.future);           
isar.copyToFile(emptyFile.path));

Though the user specified folder is empty, IsarError says that the file is exists.

Is there any better way to backup Isar DB file in Local Storeage using copyToFile()?

@mysticeti
Copy link
Author

Since this is a closed issue, I would suggest not to add message here. Please use discussions section https://github.com/isar/isar/discussions have questions or the issues section if you want to file something related to isar.

Code for opening saved isar file. You can call the below function once only ideally during app startup in main.dart.

Future<void> isarInit() async { final dir = await getApplicationDocumentsDirectory(); isarDB = await Isar.open([SettingsIsarSchema, SystemIsarSchema], inspector: true, directory: dir.path); }

Isar model for settings
@Collection() class SettingsIsar { Id id = Isar.autoIncrement; String? uid; @Enumerated(EnumType.name) DarkMode? darkMode;

Code for saving isar db in file storage locally, the below function is receiving a settings state model which would then be translated into isar obj and stored.

` Future saveCurrentSettings(
SettingsState currentSettingsState) async {

final settingsIsarObj = SettingsIsar()
..uid = currentSettingsState.uid
..darkMode = currentSettingsState.darkMode;

await isarDB.writeTxn(() async {
final settingsIsarAccess = isarDB.settingsIsars;
SettingsIsar? settingsIsarObjStoredCopy =
await settingsIsarAccess
.filter()
.uidEqualTo(settingsIsarObj.uid)
.findFirst();

// if authenticated uid is not stored then find whether default uid is there to replace with authenticated uid
settingsIsarObjStoredCopy ??= await settingsIsarAccess
    .filter()
    .uidEqualTo(defaultUID)
    .findFirst();

if (settingsIsarObjStoredCopy != null) {
  settingsIsarObj.id = settingsIsarObjStoredCopy.id;

  await settingsIsarAccess.put(settingsIsarObj);
} else {
  // data is being first put in the database
  await settingsIsarAccess.put(settingsIsarObj);
}

});
} `

For cloud backup, you can use firestore or an object storage such as firebase storage storing db file as json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants