Google Drive auto-backup for React Native on Android
Tags: reactnative, android, backup
Published: 2022-12-28 13:28:10

---
title: Google Drive auto-backup for React Native on Android
published: true
description: 
tags: reactnative, android, backup
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tra2i2g2jndn4f9gg7zq.png
---






What if async storage values were to persist when a user changed devices or installed an app again?

You won't believe it, but it works with Android right out of the box!

---


First we need to enable a backup option in the `android/app/src/main/AndroidManifest.xml` file:

```xml
    <application
      android:allowBackup="true"
      android:dataExtractionRules="@xml/backup_rules"
```

> docs: [`android:allowBackup=["true" | "false"]`](https://developer.android.com/guide/topics/manifest/application-element#allowbackup)

Then create `android/app/src/main/res/xml/backup_rules.xml` file with the list of files that should be added to backup:

```xml
<?xml version="1.0" encoding="utf-8"?>
<data-extraction-rules>
  <cloud-backup>
    <!-- your rules -->
  </cloud-backup>
  <device-transfer>
    <!-- your rules -->
  </device-transfer>
</data-extraction-rules>
```



docs: [Backup and restore](https://developer.android.com/about/versions/12/behavior-changes-12#backup-restore)

---


### Testing

More rules are listed below, but first, let's look at how to test them.

1. You can use Simulator or Real device 
2. Need to enable Backups: `Setting` => `System` => `Backup`
3. Then using `adb` you can create/restore backup:
```sh
# Replace `com.mycompany.myapp` with your app package!!!

adb shell bmgr backupnow com.mycompany.myapp # Create backup

adb shell bmgr restore 1 com.mycompany.myapp
                    # ^^^ use a fake id `1` to show list of available sets
# No matching restore set token.  Available sets:
#  3a945fc11efadf47 : Google Pixel 7 Pro
# done
 
adb shell bmgr restore 3a945fc11efadf47 com.mycompany.myapp # Now use a real id, to restore backup
```


---

### Rules 

I've compiled a list of guidelines for the most popular React Native packages for your convenience.

[`@react-native-async-storage/async-storage`](https://github.com/react-native-async-storage/async-storage) 

```xml
<include domain="database" path="RKStorage"/>
<include domain="database" path="RKStorage-journal"/>
```



[`react-native-shared-preferences`](https://github.com/sriraman/react-native-shared-preferences) 
```js
SharedPreferences.setName("my_name"); // js part
```

```xml
<include domain="sharedpref" path="my_name.xml" /> // android part
```



[`react-native-mmkv`](https://github.com/mrousavy/react-native-mmkv)
```xml
<include domain="file" path="./mmkv" />
```



[`react-native-webview`](https://www.npmjs.com/package/react-native-webview) cookies
```xml
<include domain="root" path="./app_webview"/>
```


[`realm-js`](https://github.com/realm/realm-js)
```xml
<include domain="file" path="./default.realm" />
<include domain="file" path="./default.realm.lock" />
<include domain="file" path="./default.realm.management" />
<include domain="file" path="./default.realm.note" />
```


[`@nozbe/watermelondb`](https://github.com/Nozbe/WatermelonDB)
```xml
<include domain="root" path="./watermelon.db"/>
<include domain="root" path="./watermelon.db-wal"/>
<include domain="root" path="./watermelon.db-shm"/>
```

---

### How create own rule

```sh
adb shell

run-as com.mycompany.myapp # Replace `com.mycompany.myapp` with your app package!!!

find . -type f # will print all files

# 1) For files in directory `./files/*`
#     <include domain="file" path="./mmkv" />
#                                  ^^^^^^ ./files/mmkv

# 2) For `./shared_prefs/*.xml`
#    <include domain="sharedpref" path="my_name.xml" />
#                                       ^^^^^^^^^ ./shared_prefs/my_name.xml

# ...
```