to top

bmgr

bmgr is a shell tool you can use to interact with the Backup Manager on Android devices supporting API Level 8 or greater. It provides commands to induce backup and restore operations so that you don't need to repeatedly wipe data or take similar intrusive steps in order to test your application's backup agent. These commands are accessed via the adb shell.

For information about adding support for backup in your application, read Data Backup, which includes a guide to testing your application using bmgr.

Forcing a Backup Operation

Normally, your application must notify the Backup Manager when its data has changed, via dataChanged(). The Backup Manager will then invoke your backup agent's onBackup() implementation at some time in the future. However, instead of calling dataChanged(), you can invoke a backup request from the command line by running the bmgr backup command:

adb shell bmgr backup <package>

<package> is the formal package name of the application you wish to schedule for backup. When you execute this backup command, your application's backup agent will be invoked to perform a backup operation at some time in the future (via your onBackup() method), though there is no guarantee when it will occur. However, you can force all pending backup operations to run immediately by using the bmgr run command:

adb shell bmgr run

This causes a backup pass to execute immediately, invoking the backup agents of all applications that had previously called dataChanged() since the last backup operation, plus any applications which had been manually scheduled for backup via bmgr backup.

Forcing a Restore Operation

Unlike backup operations, which are batched together and run on an occasional basis, restore operations execute immediately. The Backup Manager currently provides two kinds of restore operations. The first kind restores an entire device with the data that has been backed up. This is typically performed only when a device is first provisioned (to replicate settings and other saved state from the user's previous device) and is an operation that only the system can perform. The second kind of restore operation restores a single application to its "active" data set; that is, the application will abandon its current data and revert to the last-known-good data that is held in the current backup image. You can invoke this second restore operation with the requestRestore() method. The Backup Manager will then invoke your backup agent's onRestore() implementation.

While testing your application, you can immediately invoke the restore operation (bypassing the requestRestore() method) for your application by using the bmgr restore command:

adb shell bmgr restore <package>

<package> is the formal Java-style package name of the application participating in the backup/restore mechanism, which you would like to restore. The Backup Manager will immediately instantiate the application's backup agent and invoke it for restore. This will happen even if your application is not currently running.

Other Commands

Wiping data

The data for a single application can be erased from the active data set on demand. This is very useful while you're developing a backup agent, in case bugs lead you to write corrupt data or saved state information. You can wipe an application's data with the bmgr wipe command:

adb shell bmgr wipe <package>

<package> is the formal package name of the application whose data you wish to erase. The next backup operation that the application's agent processes will look as though the application had never backed anything up before.

Enabling and disabling backup

You can see whether the Backup Manager is operational at all with the bmgr enabled command:

adb shell bmgr enabled

This might be useful if your application's backup agent is never being invoked for backup, to verify whether the operating system thinks it should be performing such operations at all.

You can also directly disable or enable the Backup Manager with this command:

adb shell bmgr enable <boolean>

<boolean> is either true or false. This is equivalent to disabling or enabling backup in the device's main Settings UI.

Warning! When backup is disabled, the current backup transport will explicitly wipe the entire active data set from its backend storage. This is so that when a user says they do not want their data backed up, the Backup Manager respects that wish. No further data will be saved from the device, and no restore operations will be possible, unless the Backup Manager is re-enabled (either through Settings or through the above bmgr command).