Local backup with synced Realm


#1

I’d like to allow users to store backups in the app. My app is offline first. This feature has to work when they are not logged in as well as when they are logged in.

The case where they have never logged in is easily manageable, so far I can see. Just copy the Realm to another location and when restoring I replace The Realm at my default location with the backup Realm.

But what to do when there’s a synced Realm? Can I do a clientside backup of this (of course only the Realm pertinent to the user… in my app each user has their own Realm, no shared data)? If yes, how?

I know that there’s the possibility to do server side backups too, but I don’t want to have to manage all this myself - the idea is that if the db of single users get corrupted they can restore their Realms themselves and of course without affecting the db for other users.


#2

What is the purpose of the synced backup?
Like, you have a backup, synced to the cloud…
The other thing is: what happens when a user restore an older backup? The newer data will be deleted? As a user, I will not really know whats going on, and to be honest - most of them don’t really care.

On the other side, I think its doable in the same way you made a synced realm from an offline realm. The only difference is that you delete the online data first, then re-add the backupped data.


#3

The purpose is merely to make a backup of the current data… The fact that it’s a synced Realm isn’t really relevant for the backup.

The reason that I want these backups is that I fear data inconsistency / corrupt db, in particular due to some missing features like semantic uniques and cascading deletes. While I of course am doing my best to prevent inconsistencies I want better safety and allow the user to go back to a backup if the database is corrupted.

I don’t know how to apply the same logic as for going from offline Realm to synced Realm, to create the backup, as I don’t know where the synced Realm is stored? I only know the path of the local Realm and it appears to not be in sync with the synced Realm (see https://forums.realm.io/t/syncing-synced-realm-back-to-local-realm/1780)


#4

You can copy back a synced realm to a local realm too :stuck_out_tongue:
Also, the synced realm probably has a lot of unnecessary data (like synced states etc), so it will better for backup.

Although it’s not too good copying everything every time, but it can work - maybe others will have a better idea :slight_smile:


#5

Okay, the question is what is the path of the synced Realm to copy it? Or do I have to create a new Realm and run a migration object by object like when migrating from the local Realm to the synced Realm?


#6

Jumping into the discussion here. I’m not familiar with a missing feature that you referenced: “semantic uniques”. Could you provide some details so that I can pass this along to product?

We also have conversion from local to sync realm (and vice versa) on the roadmap, so I imagine this could be used for the backup. You may even be able to do some kind of backup with the file system itself. Let me do a little more research on this.


#7

@mgeerling Ok, thanks.

The “semantic uniques” is what you call “composite keys”, e.g. here: https://github.com/realm/realm-core/issues/1370

I have uniques like used in SQL in mind, where you define a primary key (often just an autoincrement id or similar) and additionally a semantic primary key, like, say, a product name + brand. I’m aware of the workaround for this but it’s very cumbersome, see discussion here