Watch keynotes and sessions from MongoDB.live, our virtual developer conference.

Automatic Realm Cloud Backup

Hi,

I have spent ~2 hours looking for solutions to create an automatic backup of my Realm Cloud database. Can you please post a link to documentation on how to accomplish this?

Thanks.

@Austin_Teague Automatic backup is built into the Realm Cloud database in case of the need to restore. If you are wanting to build your own backup from Realm Cloud to some other storage then you can use our server SDKs, such as node.js, to download and sync the realm, open it, and then iterate through each object and then write it to some other location. For instance, you could use JSON.stringify, write to a flat file, and then upload to S3 as part of a cron job or similar.

2 Likes

Thanks @Ian_Ward! I’ve done just that and it very easy. Is there any plan to make the Realm Cloud backups quickly accessible? Right now it appears I have to request it using a support ticket.

@Austin_Teague Not at this time but once the merged MongoDB and Realm product is launched you will be able to backup your data on Atlas which has much more extensive backup and restore capabilities.

1 Like

Post here my solution that I use for backup my realm-cloud realms:

async function runBackup (backupDir) {
  var credentials = Realm.Sync.Credentials.usernamePassword('admin', process.env.REALM_ADMIN_PW, false)

  var adminUser = await Realm.Sync.User.login(SyncAuthURL, credentials)

  // __Admin Realms
  fs.mkdirSync(backupDir)
  addLog(' > Downloading Realms...')
  
  const config = adminUser.createConfiguration({ sync: { fullSynchronization: true, url: CommonRealmURL + '/__admin' }})
  const adminRealm = await Realm.open(config)
  const realmFiles = adminRealm.objects('RealmFile')
  var realm = null
  for (const realmFile of realmFiles) {
    const backupRealmFile = `${backupDir}${realmFile.path}.realm`
    await fs.promises.mkdir(path.dirname(backupRealmFile), { recursive: true })
    const cfg = adminUser.createConfiguration({ sync: { fullSynchronization: true, url: CommonRealmURL + realmFile.path }})
    realm = await Realm.open(cfg)
    realm.writeCopyTo(backupRealmFile)
    realm.close()
  }
}

@rouuuge Awesome, thanks. That’s certainly one solution. You can then open this in Realm Studio and export it as a CSV. Then open the desired Realm and import data via CSV.

We decided to create the CSV direction from within the function.

Thanks for the info though! Realm support was also able to access up to 30 days of backups since we use their Realm Cloud. I just needed to submit a support ticket since they currently don’t have a self-service option.

1 Like