Android BackupManager 和使用同一帐户链接的多个设备

发布于 2024-12-21 12:18:38 字数 403 浏览 0 评论 0原文

我总是想知道 Android 的 BackupManager 将如何运行当同一个启用 BackupManager 的应用程序安装在链接到同一 Google 帐户的多个设备(例如智能手机和平板电脑)上时。 看来我不是唯一的人,但我不能没有找到任何关于此的规范。

您对这种情况有何体验?是否有任何官方资源描述该案例?

I always wonder how Android's BackupManager will act when the same BackupManager enabled App is installed on multiple devices (e.g. Smartphone and Tablet) linked to the same Google Account. It seems that I am not the only one, but I couldn't find any specification about this.

What's your experience with this scenario? Are there any official resources that describe that case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-12-28 12:18:38

这种机制没有任何面向用户的文档,也没有为应用程序开发人员提供的大量文档,因为它应该自动做正确的事情,但是 代码可用。以下所有信息均来自检查源代码和 bmgr 工具的记录选项。此答案经过调整,更加面向开发人员,来自用户友好的 我最初在 Android Stack Exchange 上写的答案。

我们来谈谈集合吧,宝贝

Android 的备份服务有一个称为集合的概念:在一个传输上从一台设备备份的所有数据的集合。每组都由唯一的字符串标识,例如设备上的 IMEI。备份应用程序(或已安装应用程序的列表)时,其备份数据将进入与其备份设备关联的数据集中。所有设置仍然特定于用户的 Google 帐户。如果您擦除设备并将其出售给其他人,他将无法访问该设备的设置,除非他可以登录您的 Google 帐户。

默认行为

当安装应用程序或设备恢复其应用程序列表时,备份系统首先在该设备的集合中查找该包的备份数据。如果它找不到任何内容(因为它是一个全新的设备,没有备份数据,或者因为该包从未安装在该设备上),它会将搜索范围扩展到其他集合。 (如果有选择,它将使用用于全设备恢复的最后一组。)

因此,当您设置新设备时,它将从旧设备的备份中恢复应用程序列表,并恢复旧设备备份中的每个应用程序。如果您在一台设备上安装了某个应用程序,然后将其安装在另一台设备上,则该应用程序将使用旧设备中的数据进行恢复。无论哪种情况,数据现在都会备份到设备的集合中,这意味着从现在起两个设备的备份数据是分开的。

将设备恢复出厂设置后,它将从该设备的最后一个备份(如果有)进行恢复,如果失败,则从其他设备的备份(如果有)进行恢复,但从那时起它将开始创建自己的一组。

bmgr:基本使用

bmgr 工具旨在用于调试和测试,并为您提供对备份/恢复过程的一些控制。您可以在 adb shell 中使用此工具来触发所选软件包的备份和恢复、擦除软件包的备份数据,甚至整个设备恢复。

不要尝试在设备上的 shell 中使用它,除非以 root 身份使用它:您需要系统级 android.permission.BACKUP 来用它做任何有趣的事情。

为了进行测试,您可以让包立即更新其备份数据:(

bmgr backup com.shadowburst.showr
bmgr run

或任何包名称)。要从备份数据恢复一个包,默认情况下会选择:

bmgr restore com.shadowburst.showr

这仅适用于已安装的包:它不会安装包来恢复数据。这两个命令仅用于测试,因为通常设备会在需要时备份和恢复数据。

更多控制

现在我们来看看备份系统在启动时无法执行的操作。要查看哪些备份数据集可用:

bmgr list sets

您将得到如下输出:

  3ff7800e963f25c5 : manta
  3f0e5c90a412cca7 : manta
  3dd65924a70e14c8 : TF101
  3baa67e9ce029355 : m0

左侧的 64 位十六进制数字称为令牌,它唯一标识该数据集。右边的东西是拥有该集合的设备的(相对)友好的名称。例如,ma​​nta 是 Nexus 10 的代号; TF101 指的是原来的华硕 Transformer。您可以通过指定其标记来从您选择的一组包中恢复数据:

bmgr restore 3ff7800e963f25c5 com.shadowburst.showr

您可以在命令末尾添加更多包名称以一次恢复多个包,也可以不指定包名称(仅指定标记)使用该集中的数据恢复每个包(也就是说,它执行完整系统恢复)。

最后,您可以从当前集中擦除包的数据:

bmgr wipe com.shadowburst.showr

这将使下一个备份操作从头开始。这在调试备份代码时可能很有用。

您无法让一台设备开始写入另一组设备,也无法一次性擦除一整组设备。

This mechanism doesn't have any user-facing documentation, nor a lot of documentation for app developers, since it's supposed to automatically do the right thing, but the code is available. All the information below comes from inspecting the source code and from the documented options of the bmgr tool. This answer is adapted to be more developer-oriented, from a user-friendly answer I originally wrote on the Android Stack Exchange.

Let's talk about sets, baby

Android's backup service has a concept called a set: the set of all data backed-up from one device on one transport. Each set is identified by a unique string, such as the IMEI on the device. When an app (or the list of installed apps) is backed up, its backup data go into the set associated with the device it's being backed up from. All the sets are still specific to the user's Google account. If you wipe your device and sell it to someone else, he won't be able to access that device's set unless he can log into your Google account.

Default behaviour

When an app is installed, or a device has its list of apps restored, the backup system first looks in that device's set for backup data for that package. If it doesn't find any (either because it's a completely new device with no backed-up data, or because that package has never been installed on that device), it'll expand the search to other sets. (If there's a choice, it'll use the last set that was used for a full-device restore.)

Thus, when you set up a new device, it'll restore the list of apps from an old device's backup, and restore each app from the old device's backup. If you had an app installed on one device and you install it on another device, the app will be restored with its data from the old device. In either case, the data are now backed up into the new device's set, which means that the backup data from the two devices are separate from now on.

After you factory-reset a device, it'll restore from that device's last backup if there is one, and failing that, from some other device's backup if there is one, but it will start to create its own set from then on.

bmgr: basic use

The bmgr tool is intended for debug and test, and gives you a little control over the backup/restore process. You can use this tool in an adb shell to trigger backups and restores of chosen packages, wipe packages' backed-up data, and even a whole-device restore.

Don't try to use it in an on-device shell except as root: you need the system-level android.permission.BACKUP to do anything interesting with it.

For testing, you can make a package update its backed-up data immediately:

bmgr backup com.shadowburst.showr
bmgr run

(or whatever the package name is). To restore one package from the backed-up data it would choose by default:

bmgr restore com.shadowburst.showr

This only works on already-installed packages: it won't install a package in order to restore the data. Both these commands are just for testing, since normally the device backs-up and restores data whenever it needs to.

More control

Now for the stuff that the backup system won't do on its on. To see what sets of backed-up data are available:

bmgr list sets

and you'll get some output like this:

  3ff7800e963f25c5 : manta
  3f0e5c90a412cca7 : manta
  3dd65924a70e14c8 : TF101
  3baa67e9ce029355 : m0

The 64-bit hex number on the left is called a token, and uniquely identifies the set. The thing on the right is a (relatively) friendly name for the device that owns the set. For example, manta is the code name for the Nexus 10; TF101 refers to the original Asus Transformer. You can restore a package's data from a set of your choice, by specifying its token:

bmgr restore 3ff7800e963f25c5 com.shadowburst.showr

You can add more package names to the end of the command to restore several packages at once, or you can specify no package name (just the token) to restore every package with data in that set (that is, it does a full-system restore).

Finally, you can wipe an package's data from the current set:

bmgr wipe com.shadowburst.showr

This will make its next backup operation start from scratch. This might be useful while debugging your backup code.

You can't make a device start writing into a different set, nor can you wipe a whole set in one go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文