Open shanraisshan opened 6 years ago
Realm realm = Realm.getInstance(DBUtil.getRealmConfiguration());
StorageUtil.copy(fromFile, toFile);
Do not overwrite open Realm file.
well its a production code, and is working well on 2.2.0.0
public static void restoreDB() {
//realm.getPath() -> /data/data/com.appname/files/notes.db
Realm realm = Realm.getInstance(DBUtil.getRealmConfiguration());
File fromFile = PathUtil.getExternalDatabaseFile();
File toFile = new File(realm.getPath());
if (fromFile.exists())
StorageUtil.copy(fromFile, toFile);
realm.close();
}
Nevertheless, changing it to below, is not helping either
public static void restoreDB() {
RealmConfiguration configuration = DBUtil.getRealmConfiguration();
Realm realm = Realm.getInstance(configuration);
String path = realm.getPath();
realm.close();
Log.e("---->", Realm.getGlobalInstanceCount(configuration) + ""); //0
File fromFile = PathUtil.getExternalDatabaseFile();
File toFile = new File(path);
if (fromFile.exists())
StorageUtil.copy(fromFile, toFile);
}
Same error, I think there is an issue with my backed up database at external storage
Have you tried opening the external storage Realm file in the Realm Browser?
Or as an asset file directly (instead of through restoration)?
I am on windows, I'll try in few hours, then comment.
It is giving Please enter valid encryption key for realm file
Is there any way from which I can recover my data from database of version 1.0.0? How to check that whether database file is not corrupted?
public static void restoreDB() {
//realm.getPath() -> /data/data/com.appname/files/notes.db
Realm realm = Realm.getInstance(DBUtil.getRealmConfiguration());
File fromFile = PathUtil.getExternalDatabaseFile();
File toFile = new File(realm.getPath());
if (fromFile.exists())
StorageUtil.copy(fromFile, toFile);
realm.close();
}
Your restore function has a very big chance to damage the db file. Do never ever delete/overwrite the Realm file while there are Realm instances opened.
We might be able to supply a safe restore function with proper locks.
It might make sense to add a new API
Realm.restore(RealmConfiguaration config, File backupRealmFile)
for the restore purpose. @realm/java ?
How to check that whether my backed up database file (external storage) is corrupted or not?
Is there any way to view data from corrupted file, I can see gibberish + messages when opening db in text editor.
@shanraisshan
We will supply an API for restoring, something like:
RealmConfiguration.Builder()
.restoreFile(File path)
.build();
It will restore the Realm file if the config.getPath()
doesn't exist.
How to check that whether my backed up database file (external storage) is corrupted or not?
It should not be corrupted if you are using Realm.writeCopyTo()
and didn't change it manually. But if it is corrupted, getInstance()
will throw.
Okay, I will wait, I hope everything goes well. Thank you.
@beeender we can return aSHA
hash of the backup file, the user can provide it back when trying to restore again, this way we can ensure that the backup file was not tampered with.
// backup
String hash = realm.writeCopyTo(path);
// developer responsibility save the hash somewhere (Global SharedPref, text file, Drive etc...)
// restore
RealmConfiguration.Builder()
.restoreFile(File path, hash/*optional*/)
.build();
this way we can ensure that the backup file was not tampered with. @nhachicha is there any issue if file is tampered?
For Example: If user backed up a database with 1000 records, open it on MAC browser, insert 2500 more records, and wanted it to restore database with 3500 records.
Is this a problem?
@shanraisshan In theory, no, the hash should be used optionally as I mentioned, if you explicitly update the Realm then you lose the feature obviously.
The idea here is to give the developer options whether he/she wants to use the hash or not
(Note: you can still hash the file manually after you update the Realm shasum -a 256 backup.realm
then use it to restore the backup)
Goal:
App has backup functionality,
When click on backup: App copies/replaces database from Internal Storage to External Storage
When click on restore: App copies/replaces database from External Storage to Internal Storage
The app is working great on production.
Issue:
I personally was using the debug(not release) build of the app on my phone.
I had around 500 messages that was backed up on External Storage.
Today, I accidentally uninstall the app. :cry:
Expected Results:
Now, on installing latest version 2.2.0.0(either debug/release build), it should automatically backup the messages, but its is giving following error.
I am not sure know whether the notes.db file is corrupted or what happened
Stack Trace:
Code Sample:
Code for backup
Code for restore
Utilities
Version of Realm and tooling
Realm version(s): 4.1.1
Realm sync feature enabled: false
Android Studio version: 3.0.0
Which Android version and device: ? Nexus 5 (Marshmallow), Nexus 6P (Oreo)
Realm Backed Up Database Size (External Storage): 83.8 KB