Save data serverless with Android Auto Backup

Photo by Charles Deluvio on Unsplash

Do you want to know the real first open of your application that persists across app uninstalls or when the user moves to a new phone? Or do you want to save any settings of the application (like Disk usage settings, Sync settings, etc) without losing it when the app is reinstalled?

The first answer that comes to mind is to create a web service that stores the data and a login process by email or phone number. But it’s hard to implement for the developer and even user to login for just some sort of insensitive data.

And maybe you are just familiar with Android development. Then what?

There is an easy way to solve this problem: Android Auto Backup

Warning : Any sensitive user data sould be kept secure with passwords and pin codes and Auto backup are not good for this type of data. For sensitive data You can use Google Sign-In or create your own webservice.

Auto Backup

So I will describe the Auto Backup API that is useful for simple usages, But for complex usages, you can use Android backup Service.

Auto backup is a file-based backup service. This means it can save your Shared Preferences files, Database files, and any other types of file. But be aware that each app has a 25MB of data limit per user/device.

Auto backup is enabled by default and it saves near to any file that is saved to apps private directory except the files in the cache directories. To disable the auto-backup set the android:allowBackupattribute to false in the <application> element of your manifest file:

<manifest ... >
...
<application android:allowBackup="false" ... >
...
</application>
</manifest>

Backups occur automatically when all of the following conditions are met:

  • The user has enabled backup on the device. In Android 9, this setting is in Settings > System > Backup. (It’s enabled by default)
  • At least 24 hours have elapsed since the last backup.
  • The device is idle.
  • The device is connected to a Wi-Fi network (if the device user hasn’t opted in to mobile-data backups).

Include and exclude files

This section shows you how to define custom XML rules to control what gets backed up.

  1. In AndroidManifest.xml, add android:fullBackupContent attribute to the <application> element. This attribute points to an XML file that contains backup rules.
<application ...
android:fullBackupContent="@xml/backup_rules">
</application>

2. Create the back_rules.xml file in the res/xml/ directory and add include/exclude rules as desired to it:

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content>
<include domain="sharedpref" path="."/>
<exclude domain="sharedpref" path="excluded_shared_pref.xml"/>
</full-backup-content>

The above declaration adds all files in the SharedPreferences directory except excluded_shared_pref.xmlfile.

The syntax of the fullBackupContent XML file is like below:

<full-backup-content>
<include domain=["file" | "database" | "sharedpref" | "external" | "root"]
path="string"
requireFlags=["clientSideEncryption" | "deviceToDeviceTransfer"] />
<exclude domain=["file" | "database" | "sharedpref" | "external" | "root"]
path="string" />
</full-backup-content>

includeand exclueelements both have domain, path and requireFlags attributes.

With thedomain attribute you can specify the directory of the file like below:

  • The sharedpref points to the directory where SharedPreferences are stored.
  • file points to the directories returned by getFilesDir().
  • database points to getDatabasePath() where database files are stored.
  • external points to the external files directory returned bygetExternalFilesDir()
  • And root points to the directory on the filesystem where all private files belonging to the app are stored.

With thepath attribute you can specify the file or folder to include or exclude.

We skip requireFlags here for brevity. You can read more about it in this guide.

So if you want to save a SharedPreferences or Database file, write an inclue rule, set it’s domain to sharedpref or database respectively and set the relative path of the file in the path attribute. The relative path is the file path related to where the domain path specifies, but usually, it’s just the file name(eg., my_dababase.db or my_shared_preferences.xml).

Note that if you specify an <include> element, the system no longer includes any files by default and backs up only the files specified.

Restore

The backup will get saved in users google drive And it has a 25MB storage limit per app/user. The backup file will not count on the user’s google drive storage limit.

The restore will happen when the app is installed on a device with the same google drive account(same Gmail). So when the app is launched for the first time its files are restored.

Test

To make sure your implementation works properly you can force the android device to backup files with the help of an adb command.

First, work with the app in order for the data you want to test to get created. Then force the device to make a backup by typing this command in a command-line:

(I tested this command with an actual device but it may work with an emulator too)

adb shell bmgr backupnow <Replace Your App's PACKAGENAME here>

Then just reinstall the app and it should have the data you want. Read this guide to know more about testing.

Happy coding. 😍

Android developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store