Reputation: 432
I've looked in the API as best I can but I'm wondering if there is a way to back up a Realm .net db on device (Android, iOS etc) using export or something similar. Need that functionality more than cloud sync, which I know is another way to back up.
Any.net realm libray device-side backup implementation thoughts appreciated.
Upvotes: 0
Views: 114
Reputation: 2186
A Realm file is just a file - if it isn't open, you can back it up using the filesystem API. If a Realm instance is open, copying the file is not safe as it is not an atomic operation and the file contents may change while copying.
If you need to perform a backup while a Realm is open, you can use Realm.WriteCopy to do that. For example, something like this:
// This is the Realm you want to backup
var myRealm = Realm.GetInstance(...);
// Store the file in Backups/myRealm.backup
var myRealmPath = myRealm.Config.DatabasePath;
var folder = Path.GetDirectoryName(myRealmPath);
var backupPath = Path.Combine(folder, "Backups", "myRealm.backup");
var backupConfig = new RealmConfiguration(backupPath);
// You can also encrypt the backup
// backupConfig.EncryptionKey = new byte[] { 1, 2, 3, ..., 64 };
myRealm.WriteCopy(backupConfig);
Upvotes: 1