1 page.title=Auto Backup for Apps 2 page.tags=backup, marshmallow, androidm 3 page.keywords=backup, autobackup 4 page.image=images/cards/card-auto-backup_2x.png 5 6 @jd:body 7 8 <div id="qv-wrapper"> 9 <div id="qv"> 10 <h2>In this document</h2> 11 <ol> 12 <li><a href="#Files">Files that are backed up</a></li> 13 <li><a href="#BackupLocation">Backup location</a></li> 14 <li><a href="#BackupSchedule">Backup schedule</a></li> 15 <li><a href="#RestoreSchedule">Restore schedule</a></li> 16 <li><a href="#EnablingAutoBackup">Enabling and disabling Auto Backup</a></li> 17 <li><a href="#IncludingFiles">Including and excluding files</a><ul> 18 <li><a href="#XMLSyntax">XML config syntax</a></li> 19 </ul></li> 20 <li><a href="#ImplementingBackupAgent">Implementing BackupAgent</a></li> 21 </ol> 22 23 <h2>Key classes</h2> 24 <ol> 25 <li>{@link android.app.backup.BackupAgent}</li> 26 <li><a href="{@docRoot}reference/android/R.attr.html">R.attr</a></li> 27 </ol> 28 29 </div> 30 </div> 31 32 Since Android 6.0 (API 23), Android has offered the <em>Auto Backup for Apps</em> feature as 33 a way for developers to quickly add backup functionality to their apps. Auto 34 Backup preserves app data by uploading it to the users Google Drive account. 35 The amount of data is limited to 25MB per user of your app and there is no 36 charge for storing backup data. 37 38 <h2 id="Files">Files that are backed up</h2> 39 <p>By default, Auto Backup includes files in most of the directories that are 40 assigned to your app by the system: 41 <ul> 42 <li>Shared preferences files. 43 <li>Files in the directory returned by {@link android.content.Context#getFilesDir()}. 44 <li>Files in the directory returned by {@link android.content.Context#getDatabasePath(String)}, 45 which also includes files created with the 46 {@link android.database.sqlite.SQLiteOpenHelper} class. 47 <li>Files in directories created with {@link android.content.Context#getDir(String,int)}. 48 <li>Files on external storage in the directory returned by 49 {@link android.content.Context#getExternalFilesDir(String)}.</li></ul> 50 51 <p>Auto Backup excludes files in directories returned by 52 {@link android.content.Context#getCacheDir()}, 53 {@link android.content.Context#getCodeCacheDir()}, or 54 {@link android.content.Context#getNoBackupFilesDir()}. The files saved 55 in these locations are only needed temporarily, or are intentionally 56 excluded from backup operations. 57 58 <p>You can configure your app to include and exclude particular files. 59 For more information, see the <a href="#IncludingFiles">Include and exclude files</a> 60 section. 61 62 <h2 id="BackupLocation">Backup location</h2> 63 <p>Backup data is stored in a private folder in the user's Google Drive account, 64 limited to 25MB per app. The saved data does not count towards the user's 65 personal Google Drive quota. Only the most recent backup is stored. When a 66 backup is made, the previous backup (if one exists) is deleted. 67 68 <p>Users can see a list of apps that have been backed up in the Google Drive app under 69 <strong>Settings -> Auto Backup for apps -> Manage backup</strong>. The 70 backup data cannot be read by the user or other applications on the device. 71 72 <p>Backups from each device-setup-lifetime are stored in separate datasets 73 as shown in the following examples: 74 <ul> 75 <li>If the user owns two devices, then a backup dataset exists for each device. 76 <li>If the user factory resets a device and then sets up the device with the 77 same account, the backup is stored in a new dataset. Obsolete datasets are 78 automatically deleted after a period of inactivity.</li></ul> 79 80 <p class="caution"><strong>Caution:</strong> Once the amount of data reaches 81 25MB, the app is banned from sending data to the 82 cloud, even if the amount of data later falls under the 25MB threshold. The ban 83 affects only the offending device (not other devices that the user owns) and 84 lasts for the entire device-setup-lifetime. For example, if the user removes and 85 reinstalls the application, the ban is still in effect. The ban is lifted when 86 the user performs factory reset on the device. 87 88 <h2 id="BackupSchedule">Backup schedule</h2> 89 <p>Backups occur automatically when all of the following conditions are met: 90 <ul> 91 <li>The user has enabled backup on the device in <strong>Settings</strong> > 92 <strong>Backup & Reset</strong>. 93 <li>At least 24 hours have elapsed since the last backup. 94 <li>The device is idle and charging. 95 <li>The device is connected to a Wi-Fi network. If the device is never connected 96 to a wifi network, then Auto Backup never occurs.</li></ul> 97 98 <p>In practice, these conditions occur roughly every night. To conserve network 99 bandwidth, upload takes place only if app data has changed. 100 101 <p>During Auto Backup, the system shuts down the app to make sure it is no longer 102 writing to the file system. By default, the backup system ignores apps that are 103 running in the foreground because users would notice their apps being shut down. 104 You can override the default behavior by setting the 105 <a href="{@docRoot}reference/android/R.attr.html#backupInForeground">backupInForeground</a> 106 attribute to true. 107 108 <p>To simplify testing, Android includes tools that let you manually initiate 109 a backup of your app. For more information, see 110 <a href="{@docRoot}guide/topics/data/testingbackup.html">Testing Backup and Restore</a>. 111 112 <h2 id="RestoreSchedule">Restore schedule</h2> 113 <p>Data is restored whenever the app is installed, either from the Play store, 114 during device setup (when the system installs previously installed apps), or 115 from running adb install. The restore operation occurs after the APK is 116 installed, but before the app is available to be launched by the user. 117 118 <p>During the initial device setup wizard, the user is shown a list of available backup 119 datasets and is asked which one to restore the data from. Whichever backup 120 dataset is selected becomes the ancestral dataset for the device. The device can 121 restore from either its own backups or the ancestral dataset. The device 122 prioritize its own backup if backups from both sources are available. If the 123 user didn't go through the device setup wizard, then the device can restore only from 124 its own backups. 125 126 <p>To simplify testing, Android includes tools that let you manually initiate 127 a restore of your app. For more information, see 128 <a href="{@docRoot}guide/topics/data/testingbackup.html">Testing Backup and Restore</a>. 129 130 <h2 id="EnablingAutoBackup">Enabling and disabling backup</h2> 131 <p>Apps that target Android 6.0 (API level 23) or higher automatically participate 132 in Auto Backup. This is because the 133 <a href="{@docRoot}reference/android/R.attr.html#allowBackup">android:allowBackup</a> 134 attribute, which enables/disables backup, defaults to <code>true</code> if omitted. 135 To avoid confusion, we recommend you explicitly set the attribute in the <code><application></code> 136 element of your <code>AndroidManifest.xml</code>. For example: 137 138 <pre class="prettyprint"><application ... 139 android:allowBackup="true"> 140 </app></pre> 141 142 <p>To disable Auto Backup, add either of the following attributes to the 143 application element in your manifest file: 144 145 <ul> 146 <li>set <code>android:allowBackup</code> to <code>false</code>. This completely disables data 147 backup. You may want to disable backups when your app can recreate its state 148 through some other mechanism or when your app deals with sensitive 149 information that should not be backed up.</li> 150 <li>set <code>android:allowBackup</code> to <code>true</code> and 151 <code>android:fullBackupOnly</code> to <code>false</code>. With these settings, 152 your app always participates in Key/Value Backup, even when running on devices that 153 support Auto Backup.</li></ul> 154 155 <h2 id="IncludingFiles">Including and excluding files</h2> 156 <p>By default, the system backs up almost all app data. For more information, 157 see <a href="#Files">Files that are backed up</a>. This section shows you how to 158 define custom XML rules to control what gets backed up. 159 160 <ol> 161 <li>In <code>AndroidManifest.xml</code>, add the <a href="{@docRoot}reference/android/R.attr.html#fullBackupContent">android:fullBackupContent</a> attribute to the 162 <code><application></code> element. This attribute points to an XML file that contains backup 163 rules. For example: 164 <pre class="prettyprint"><application ... 165 android:fullBackupContent="@xml/my_backup_rules"> 166 </app></pre></li> 167 <li>Create an XML file called <code>my_backup_rules.xml</code> in the <code>res/xml/</code> directory. Inside the file, add rules with the <code><include></code> and <code><exclude></code> elements. The following sample backs up all shared preferences except <code>device.xml</code>: 168 <pre><?xml version="1.0" encoding="utf-8"?> 169 <full-backup-content> 170 <include domain="sharedpref" path="."/> 171 <exclude domain="sharedpref" path="device.xml"/> 172 </full-backup-content></pre></li> 173 174 <h3 id="XMLSyntax">XML Config Syntax</h3> 175 <p>The XML syntax for the configuration file is shown below: 176 177 <pre class="prettyprint"><full-backup-content> 178 <include domain=["file" | "database" | "sharedpref" | "external" | "root"] 179 path="string" /> 180 <exclude domain=["file" | "database" | "sharedpref" | "external" | "root"] 181 path="string" /> 182 </full-backup-content></pre> 183 184 <p>Inside the <code><full-backup-content></code> tag, you can define <code><include></code> and <code><exclude></code> 185 elements: 186 187 <ul> 188 <li><code><include></code> - Specifies a file or folder to backup. By default, Auto Backup 189 includes almost all app files. If you specify an <include> element, the system 190 no longer includes any files by default and backs up <em>only the files 191 specified</em>. To include multiple files, use multiple <include> elements. 192 <p>note: Files in directories returned by <code>getCacheDir()</code>, <code>getCodeCacheDir()</code>, or 193 <code>getNoBackupFilesDir()</code> are always excluded even if you try to include them.</li> 194 195 <li><code><exclude></code> - Specifies a file or folder to exclude during backup. Here are 196 some files that are typically excluded from backup: <ul> 197 <li>Files that have device specific identifiers, either issued by a server or 198 generated on the device. For example, <a href="https://developers.google.com/cloud-messaging/android/start">Google Cloud Messaging (GCM)</a> needs to 199 generate a registration token every time a user installs your app on a new 200 device. If the old registration token is restored, the app may behave 201 unexpectedly. 202 <li>Account credentials or other sensitive information. Consider asking the 203 user to reauthenticate the first time they launch a restored app rather than 204 allowing for storage of such information in the backup. 205 <li>Files related to app debugging, such as <a href="{@docRoot}studio/run/index.html#instant-run">instant run files</a>. To exclude instant run files, add the rule <code><exclude 206 domain="file" path="instant-run"/></code> 207 <li>Large files that cause the app to exceed the 25MB backup quota.</li> </ul> 208 </li> </ul> 209 210 <p class="note"><strong>Note:</strong> If your configuration file specifies both elements, then the 211 backup contains everything captured by the <code><include></code> elements minus the 212 resources named in the <code><exclude></code> elements. In other words, 213 <code><exclude></code> takes precedence. 214 215 <p>Each element must include the following two attributes: 216 <ul> 217 <li><code>domain</code> - specifies the location of resource. Valid values for this attribute 218 include the following: <ul> 219 <li><code>root</code> - the directory on the filesystem where all private files belonging to 220 this app are stored. 221 <li><code>file</code> - directories returned by {@link android.content.Context#getFilesDir()}. 222 <li><code>database</code> - directories returned by {@link android.content.Context#getDatabasePath(String) getDatabasePath()}. 223 Databases created with {@link android.database.sqlite.SQLiteOpenHelper} 224 are stored here. 225 <li><code>sharedpref</code> - the directory where {@link android.content.SharedPreferences} 226 are stored. 227 <li><code>external</code> the directory returned by {@link android.content.Context#getExternalFilesDir(String) getExternalFilesDir()} 228 <p>Note: You cannot backup files outside of these locations.</li></ul> 229 <li><code>path</code>: Specifies a file or folder to include in or exclude from backup. Note 230 that: <ul> 231 <li>This attribute does not support wildcard or regex syntax. 232 <li>You can use <code>.</code> to reference the current directory, however, you cannot 233 reference the parent directory <code>..</code> for security reasons. 234 <li>If you specify a directory, then the rule applies to all files in the 235 directory and recursive sub-directories.</li></ul></li></ul> 236 237 <h2 id="ImplementingBackupAgent">Implementing BackupAgent</h2> 238 <p>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: 239 <ul> 240 <li>You want to receive notification of backup events such as, 241 {@link android.app.backup.BackupAgent#onRestoreFinished()} or {@link android.app.backup.BackupAgent#onQuotaExceeded(long,long)}. These callback methods are executed 242 even if the app is not running. 243 <li>You can't easily express the set of files you want to backup with XML rules. 244 In these very rare cases, you can implement a BackupAgent that overrides {@link android.app.backup.BackupAgent#onFullBackup(FullBackupDataOutput)} to 245 store what you want. To retain the system's default implementation, call the 246 corresponding method on the superclass with <code>super.onFullBackup()</code>.</li></ul> 247 248 <p class="note"><strong>Note:</strong> Your <code>BackupAgent</code> must 249 implement the abstract methods 250 {@link android.app.backup.BackupAgent#onBackup(ParcelFileDescriptor,BackupDataOutput,ParcelFileDescriptor) onBackup()} 251 and {@link android.app.backup.BackupAgent#onRestore(BackupDataInput,int,ParcelFileDescriptor) onRestore()}. 252 Those methods are used for Key/Value Backup. So if 253 you are not using Key/Value Backup, implement those methods and leave them blank. 254 255 <p>For more information, see 256 <a href="{@docRoot}guide/topics/data/keyvaluebackup.html#BackupAgent">Extending 257 BackupAgent</a>.