page.title=Auto Backup for Apps page.tags=backup, marshmallow, androidm page.keywords=backup, autobackup page.image=images/cards/card-auto-backup_2x.png @jd:body
By default, Auto Backup includes files in most of the directories that are assigned to your app by the system:
Auto Backup excludes files in directories returned by {@link android.content.Context#getCacheDir()}, {@link android.content.Context#getCodeCacheDir()}, or {@link android.content.Context#getNoBackupFilesDir()}. The files saved in these locations are only needed temporarily, or are intentionally excluded from backup operations.
You can configure your app to include and exclude particular files. For more information, see the Include and exclude files section.
Backup data is stored in a private folder in the user's Google Drive account, limited to 25MB per app. The saved data does not count towards the user's personal Google Drive quota. Only the most recent backup is stored. When a backup is made, the previous backup (if one exists) is deleted.
Users can see a list of apps that have been backed up in the Google Drive app under Settings -> Auto Backup for apps -> Manage backup. The backup data cannot be read by the user or other applications on the device.
Backups from each device-setup-lifetime are stored in separate datasets as shown in the following examples:
Caution: Once the amount of data reaches 25MB, the app is banned from sending data to the cloud, even if the amount of data later falls under the 25MB threshold. The ban affects only the offending device (not other devices that the user owns) and lasts for the entire device-setup-lifetime. For example, if the user removes and reinstalls the application, the ban is still in effect. The ban is lifted when the user performs factory reset on the device.
Backups occur automatically when all of the following conditions are met:
In practice, these conditions occur roughly every night. To conserve network bandwidth, upload takes place only if app data has changed.
During Auto Backup, the system shuts down the app to make sure it is no longer writing to the file system. By default, the backup system ignores apps that are running in the foreground because users would notice their apps being shut down. You can override the default behavior by setting the backupInForeground attribute to true.
To simplify testing, Android includes tools that let you manually initiate a backup of your app. For more information, see Testing Backup and Restore.
Data is restored whenever the app is installed, either from the Play store, during device setup (when the system installs previously installed apps), or from running adb install. The restore operation occurs after the APK is installed, but before the app is available to be launched by the user.
During the initial device setup wizard, the user is shown a list of available backup datasets and is asked which one to restore the data from. Whichever backup dataset is selected becomes the ancestral dataset for the device. The device can restore from either its own backups or the ancestral dataset. The device prioritize its own backup if backups from both sources are available. If the user didn't go through the device setup wizard, then the device can restore only from its own backups.
To simplify testing, Android includes tools that let you manually initiate a restore of your app. For more information, see Testing Backup and Restore.
Apps that target Android 6.0 (API level 23) or higher automatically participate
in Auto Backup. This is because the
android:allowBackup
attribute, which enables/disables backup, defaults to true
if omitted.
To avoid confusion, we recommend you explicitly set the attribute in the <application>
element of your AndroidManifest.xml
. For example:
<application ... android:allowBackup="true"> </app>
To disable Auto Backup, add either of the following attributes to the application element in your manifest file:
android:allowBackup
to false
. This completely disables data
backup. You may want to disable backups when your app can recreate its state
through some other mechanism or when your app deals with sensitive
information that should not be backed up.android:allowBackup
to true
and
android:fullBackupOnly
to false
. With these settings,
your app always participates in Key/Value Backup, even when running on devices that
support Auto Backup.By default, the system backs up almost all app data. For more information, see Files that are backed up. This section shows you how to define custom XML rules to control what gets backed up.
AndroidManifest.xml
, add the android:fullBackupContent attribute to the
<application>
element. This attribute points to an XML file that contains backup
rules. For example:
<application ... android:fullBackupContent="@xml/my_backup_rules"> </app>
my_backup_rules.xml
in the res/xml/
directory. Inside the file, add rules with the <include>
and <exclude>
elements. The following sample backs up all shared preferences except device.xml
:
<?xml version="1.0" encoding="utf-8"?> <full-backup-content> <include domain="sharedpref" path="."/> <exclude domain="sharedpref" path="device.xml"/> </full-backup-content>
The XML syntax for the configuration file is shown below:
<full-backup-content> <include domain=["file" | "database" | "sharedpref" | "external" | "root"] path="string" /> <exclude domain=["file" | "database" | "sharedpref" | "external" | "root"] path="string" /> </full-backup-content>
Inside the <full-backup-content>
tag, you can define <include>
and <exclude>
elements:
<include>
- Specifies a file or folder to backup. By default, Auto Backup
includes almost all app files. If you specify an <include> element, the system
no longer includes any files by default and backs up only the files
specified. To include multiple files, use multiple <include> elements.
note: Files in directories returned by getCacheDir()
, getCodeCacheDir()
, or
getNoBackupFilesDir()
are always excluded even if you try to include them.
<exclude>
- Specifies a file or folder to exclude during backup. Here are
some files that are typically excluded from backup: <exclude
domain="file" path="instant-run"/>
Note: If your configuration file specifies both elements, then the
backup contains everything captured by the <include>
elements minus the
resources named in the <exclude>
elements. In other words,
<exclude>
takes precedence.
Each element must include the following two attributes:
domain
- specifies the location of resource. Valid values for this attribute
include the following: root
- the directory on the filesystem where all private files belonging to
this app are stored.
file
- directories returned by {@link android.content.Context#getFilesDir()}.
database
- directories returned by {@link android.content.Context#getDatabasePath(String) getDatabasePath()}.
Databases created with {@link android.database.sqlite.SQLiteOpenHelper}
are stored here.
sharedpref
- the directory where {@link android.content.SharedPreferences}
are stored.
external
the directory returned by {@link android.content.Context#getExternalFilesDir(String) getExternalFilesDir()}
Note: You cannot backup files outside of these locations.
path
: Specifies a file or folder to include in or exclude from backup. Note
that: .
to reference the current directory, however, you cannot
reference the parent directory ..
for security reasons.
Apps that implement Auto Backup do not need to implement {@link android.app.backup.BackupAgent}. However, you can optionally implement a custom {@link android.app.backup.BackupAgent}. Typically, there are two reasons for doing this:
super.onFullBackup()
.Note: Your BackupAgent
must
implement the abstract methods
{@link android.app.backup.BackupAgent#onBackup(ParcelFileDescriptor,BackupDataOutput,ParcelFileDescriptor) onBackup()}
and {@link android.app.backup.BackupAgent#onRestore(BackupDataInput,int,ParcelFileDescriptor) onRestore()}.
Those methods are used for Key/Value Backup. So if
you are not using Key/Value Backup, implement those methods and leave them blank.
For more information, see Extending BackupAgent.