1page.title=Auto Backup for Apps 2page.tags=backup, previewresources, androidm 3page.keywords=backup, autobackup, preview 4page.image=images/cards/card-auto-backup_2x.png 5@jd:body 6 7<div id="qv-wrapper"> 8 <div id="qv"> 9 <h2>In this document</h2> 10 <ol> 11 <li><a href="#overview">Overview</a></li> 12 <li><a href="#configuring">Configuring Data Backup</a></li> 13 <li><a href="#testing">Testing Backup Configuration</a></li> 14 <li><a href="#issues">Known Issues</a></li> 15 </ol> 16 </div> 17</div> 18 19<p> 20 Users often invest significant time and effort creating data and setting preferences within 21 apps. Preserving that data for users if they replace a broken device or upgrade to a new one is 22 an important part of ensuring a great user experience. Devices running the Android M Preview 23 system help ensure a good experience for users in these circumstances by automatically backing up 24 app data to Google Drive. The app data is automatically restored if a user changes or upgrades a 25 device. 26</p> 27 28<p> 29 Automatic backups are enabled for all apps installed on devices running the Android M Preview. No 30 additional app code is required. The system provides users with the ability to opt out of 31 automatic data backups. You can also choose to limit what data from your app is backed up. 32</p> 33 34<p> 35 This document describes the new system behavior and how to specify what data is backed up for 36 your app. 37</p> 38 39<h2 id="overview">Overview</h2> 40 41<p> 42 The automatic backup feature preserves the data your app creates on a user device by uploading it 43 to the user’s Google Drive account and encrypting it. There is no charge to you or the user for 44 data storage and the saved data does not count towards the user's personal Drive quota. During 45 the M Preview period, users can store up to 25MB per Android app. 46</p> 47 48<p> 49 Automatic backups occur every 24 hours, when the device is idle, charging, and connected to a 50 Wi-Fi network. When these conditions are met, the Backup Manager service uploads all available 51 backup data to the cloud. When the user transitions to a new device, or uninstalls and reinstalls 52 the backed up app, a restore operation copies the backed up data into the newly installed 53 app’s data directory. 54</p> 55 56<p class="note"> 57 <strong>Note:</strong> If your app uses the legacy 58 <a href="{@docRoot}google/backup/index.html">Android Backup service</a>, this new behavior 59 does not apply and the existing backup behavior works as usual. 60</p> 61 62 63<h3 id="auto-exclude">Automatically Excluded Data Files</h3> 64 65<p> 66 Not all app data should be backed up, such as temporary files and caches, so the automatic backup 67 service excludes certain data files by default: 68</p> 69 70<ul> 71 <li>Files in the directories referred to by the {@link android.content.Context#getCacheDir 72 getCacheDir()} and {@link android.content.ContextWrapper#getCodeCacheDir getCodeCacheDir()} 73 methods. 74 </li> 75 76 <li>Files located on external storage, unless they reside in the directory referred to by the 77 {@link android.content.Context#getExternalFilesDir getExternalFilesDir()} 78 method. 79 </li> 80 81 <li>Files located in the directory referred to by the 82 {@link android.content.Context#getNoBackupFilesDir getNoBackupFilesDir()} method. 83 </li> 84</ul> 85 86<h2 id="configuring">Configuring Data Backup</h2> 87 88<p> 89 The data created by any app installed on an M Preview device is backed up, except for the 90 automatically excluded files listed in the previous section. You can further limit and configure 91 what data gets backed up from your app using settings in your app manifest. 92</p> 93 94<h3 id="include-exclude">Including or Excluding Data</h3> 95 96<p> 97 Depending on what data your app needs and how you save it, you may need to set specific 98 rules for including or excluding certain files or directories. The automatic backup service 99 supports setting these backup rules through use of an XML configuration file and the app 100 manifest. In the app manifest, you can specify a backup scheme configuration file as shown in the 101 following example: 102</p> 103 104<pre> 105<?xml version="1.0" encoding="utf-8"?> 106<manifest xmlns:android="http://schemas.android.com/apk/res/android" 107 xmlns:tools="http://schemas.android.com/tools" 108 package="com.my.appexample"> 109 <uses-sdk android:minSdkVersion="MNC"/> 110 <uses-sdk android:targetSdkVersion="MNC"/> 111 <app ... 112<strong> android:fullBackupContent="@xml/mybackupscheme"></strong> 113 </app> 114 ... 115</manifest> 116</pre> 117 118<p> 119 In this example code, the <code>android:fullBackupContent</code> attribute specifies an XML file, 120 located in the <code>res/xml/</code> directory of your app development project, named 121 <code>mybackupscheme.xml</code>. This configuration file includes rules for what files are backed 122 up. The following example code shows a configuration file that excludes a specific file from 123 backups: 124</p> 125 126<pre> 127<?xml version="1.0" encoding="utf-8"?> 128<full-backup-content> 129 <exclude domain="database" path="device_info.db"/> 130</full-backup-content> 131</pre> 132 133<p> 134 This example backup configuration only excludes a specific database file from being backed up. 135 All other files are backed up. 136</p> 137 138<h4>Backup Configuration Syntax</h4> 139 140<p> 141 The backup service configuration allows you to specify what files to include or exclude from 142 backup. The syntax for the data backup configuration xml file is as follows: 143</p> 144 145<pre> 146<full-backup-content> 147 <include domain=["file" | "database" | "sharedpref" | "external" | "root"] path="string" /> 148 <exclude domain=["file" | "database" | "sharedpref" | "external" | "root"] path="string" /> 149</full-backup-content> 150</pre> 151 152<p> 153 The following elements and attributes allow you to specify the files to include and exclude from 154 backup: 155</p> 156 157<ul> 158 <li> 159 <code><include></code>. Use this element if you want to specify a set of resources to 160 back up, instead of having the system back up all data in your app by default. When you specify 161 an <code><include></code> tag, the system backs up <em>only the resources specified</em> 162 with this element. 163 </li> 164 165 <li> 166 <code><exclude></code>. Use this element to specify a set of resources to exclude from 167 backup. The system backs up all data in your app, except for resources specified with this 168 element. 169 </li> 170 171 <li> 172 <code>domain.</code> The type of resource you want to include or exclude from backup. The valid 173 values you can specify for this attribute include: 174 </li> 175 176 <li style="list-style: none"> 177 <ul> 178 <li> 179 <code>root</code>. Specifies that the resource is in the app’s root directory. 180 </li> 181 182 <li> 183 <code>file</code>. Corresponds to a resource in the directory returned by the 184 {@link android.content.Context#getFilesDir getFilesDir()} method. 185 </li> 186 187 <li> 188 <code>database</code>. Corresponds to a database returned by the 189 {@link android.content.Context#getDatabasePath getDatabasePath()} method or by using the 190 {@link android.database.sqlite.SQLiteOpenHelper} class. 191 </li> 192 193 <li> 194 <code>sharedpref</code>. Corresponds to a {@link android.content.SharedPreferences} object 195 returned by the {@link android.content.Context#getSharedPreferences getSharedPreferences()} 196 method. 197 </li> 198 199 <li> 200 <code>external</code>. Specifies that the resource is in external storage, and corresponds 201 to a file in the directory returned by the 202 {@link android.content.Context#getExternalFilesDir getExternalFilesDir()} method. 203 </li> 204 205 <li> 206 <code>path</code>. The file path to a resource that you want to include or exclude from 207 backup. 208 </li> 209 </ul> 210 </li> 211</ul> 212 213 214<h3 id="prohibit">Prohibiting Data Backups</h3> 215 216<p> 217 You can choose to prevent automatic backups of any of your app data by setting the 218 <code>android:allowBackup</code> attribute to <code>false</code> in the app element of 219 your manifest. This setting is illustrated in the following example code: 220</p> 221 222<pre> 223<?xml version="1.0" encoding="utf-8"?> 224<manifest xmlns:android="http://schemas.android.com/apk/res/android" 225 xmlns:tools="http://schemas.android.com/tools" 226 package="com.my.appexample"> 227 <uses-sdk android:minSdkVersion="MNC"/> 228 <uses-sdk android:targetSdkVersion="MNC"/> 229 <app ... 230<strong> android:allowBackup="false"></strong> 231 </app> 232 ... 233</manifest> 234</pre> 235 236 237<h2 id="testing">Testing Backup Configuration</h2> 238 239<p> 240 Once you have created a backup configuration, you should test it to make sure your app saves data 241 and can be restored properly. 242</p> 243 244 245<h4>Enabling Backup Logging</h4> 246 247<p> 248 To help determine how the backup feature is parsing your XML file, enable logging before 249 performing a test backup: 250</p> 251 252<pre class="noprettyprint"> 253$ adb shell setprop log.tag.BackupXmlParserLogging VERBOSE 254</pre> 255 256<h4>Testing Backup</h4> 257 258<p>To manually run a backup, first you must initialize the Backup Manager by calling the following 259 command: 260</p> 261 262<pre class="noprettyprint"> 263$ adb shell bmgr run 264</pre> 265 266<p> 267 Next, you manually backup your application using the following command, specifying the package 268 name for your app as the <code><PACKAGE></code> parameter: 269</p> 270 271<pre class="noprettyprint"> 272$ adb shell bmgr fullbackup <PACKAGE></pre> 273 274 275<h4>Testing Restore</h4> 276 277<p> 278 To manually initiate a restore after your app data is backed up, call the following command, 279 specifying the package name for your app as the <code><PACKAGE></code> parameter: 280</p> 281 282<pre class="noprettyprint"> 283$ adb shell bmgr restore <PACKAGE> 284</pre> 285 286<p class="warning"> 287 <b>Warning:</b> This action stops your app and wipes its data before performing the restore 288 operation. 289</p> 290 291<p> 292 You initiate the restore process for your app by uninstalling and reinstalling your app. The app 293 data is automatically restored from the cloud once the app installation is complete. 294</p> 295 296 297<h4>Troubleshooting Backups</h4> 298 299<p> 300 If you run into issues, you can clear the backup data and associated metadata by turning backup 301 off and on in the <strong>Settings > Backup</strong>, factory resetting the device, or by 302 calling this command: 303</p> 304 305<pre>$ adb shell bmgr wipe <TRANSPORT> <PACKAGE></pre> 306 307<p> 308 The <code><TRANSPORT></code> value must be prefixed by <code>com.google.android.gms</code>. 309 To get the list of transports, call the following command: 310</p> 311 312<pre>$ adb shell bmgr list transports</pre> 313 314<h2 id="issues">Known Issues</h2> 315 316<p>The following are known issues with the automatic backup service:</p> 317 318<ul> 319 <li><strong>Google Cloud Messaging</strong> - 320 For apps that use Google Cloud Messaging for push notifications, there is a known issue where 321 backing up the registration ID returned by Google Cloud Messaging registration can break push 322 notifications for the restored app. It is important to query the API for a new 323 registration ID after being installed on a new device, which is not be the case if the old 324 registration ID was backed up. To avoid this, exclude the registration id from the set of backed 325 up files. 326 </li> 327</ul> 328