How to implement backups in React Native

Unfortunately, React Native still lacks documentation on the mentioned part. This was the main motivation for me to share some information in this article.

You have two main options:

Use some of the services iOS and Android already offers which are super easy to use and provide for a great user experience Implement your own logic to backup and restore data

I will only go into detail on the first option, since this was the one which fitted me best. The app I developed does not require any kind of registration, so I would have had to build all this infrastructure just for backups. Also the UX has turned out to be better because the backups just run automatically and do not require any effort from the user.

If you want to go the second way (e.g. you do not want to trust Google and Apple with your backups), then you will anyway need to build some custom system for your current case.

Let’s break it down into our two platforms: iOS and Android

iOS Backups with iCloud

Good news: If you use AsyncStorage then iOS will actually backup this data out-of-the-box!

Still it’s always good to verify that everything works, right?

Unfortunately you cannot test iCloud backups in the simulator, so you will need an actual physical device to check. To test the automatic iCloud backups you can use the Offload app function of iOS.

If you have never heard of that function, don’t worry, me neither. Basically, you can uninstall an app on iOS while still keeping the app icon on your home screen. iOS will remove the app and backup the data, so when you tap the app icon again, it will automatically install the app one more time and restore all the data from your iCloud backup.

For more information on iCloud backups I recommend checking out this page.

Android Backups with Google Drive

This will not work without a tiny change in the AndroidManifest.xml , because per default the automatic backups are deactivated. You can activate the automatic backup by adding android:allowBackup="true" to your <application> !

<application

android:name=”.MainApplication”

android:label=”@string/app_name”

android:icon=”@mipmap/ic_launcher”

android:roundIcon=”@mipmap/ic_launcher_round”

android:allowBackup=”true”

android:theme=”@style/AppTheme”>

Testing backups on Android is a bit more complicated, but at least it works in the simulator as well!

First, you should make sure to enable backups in your simulator device!

Go to the settings in your simulator and search for “Backup”.

You should be able to find the option “Backup & reset”. In there you enable “Back up my data”. After that you are ready to go!

Now you can use the following adb commands to create a backup and restore a backup.

adb shell bmgr backupnow <PACKAGE>

adb shell bmgr restore <TOKEN> <PACKAGE>

Replace <PACKAGE> and <TOKEN> with your apps and backup data (the backup id will be 1 if you did it in the simulator).

After you created a backup you can also just uninstall and reinstall the app. It will work the same like bmgr restore . Just be aware, that Google doesn’t recommend using the bmgr restore command without a token, because this command works slightly different to the other restore commands.

To really verify that it works I recommend you doing some changes before you restore, because then you should see a rollback to the backup state. If you use the bmgr restore command, the app will close, wipe all the local data and restore it from the backup.

For more information on Google Drive backups I recommend checking out this page.