Search code examples
androidandroid-backup-servicebackup-agent

BackupAgentHelper: onRestore not getting called


I installed the example BackupRestore from this link.

https://android.googlesource.com/platform/development/+/0b3758ea4e53f9bfd0b112eaa4a7dd7b7f4040f5/samples/BackupRestore?autodive=0%2F%2F%2F%2F%2F%2F%2F%2F%2F%2F

I am unable to restore the data.

OnBackup and OnCreate get called, but OnRestore does not.

I am able to backup: OnCreate and OnBackup get hit, but OnRestore does not.

@Override
public void onRestore(BackupDataInput data, int appVersionCode,
                      ParcelFileDescriptor newState) throws IOException {
    // Hold the lock while the FileBackupHelper restores the file from
    // the data provided here.
    synchronized (BackupRestoreActivity.sDataLock) {
        super.onRestore(data, appVersionCode, newState);
    }
}



   public void onRestoreButtonClick(View v) {
        Log.v(TAG, "Requesting restore of our most recent data");
        mBackupManager.requestRestore(
                new RestoreObserver() {
                    public void restoreFinished(int error) {
                        /** Done with the restore!  Now draw the new state of our data */
                        Log.v(TAG, "Restore finished, error = " + error);
                        populateUI();
                    }
                }
        );
    }

I have tried using the adb tools (besides requestRestore) and it is not working either.

adb shell bmgr restore [package]
adb uninstall [apkfile]
adb  install [apkfile]

When I run restore I get:

restoreStarting: 1 packages
restoreFinished: 0
done

But OnRestore does not get hit, and the data is not restored.

I am using Android Studio 3.0. And I have spent many hours on this. Can anyone shed some light?


Solution

  • After checking the logs, I noticed a message:

    I/Backup: [KeyValueRateLimiter] K/V backup for [package] aborted by rate limiter. next=1519165401410, current=1519088429345

    So that means the backups are not being made until 2/20/2018, 5:23:21 PM. There is a rate limit for backups with Google Transport, so I am going to try Local Transport which has no rate limit.

    Update: Switching to Local Transport seems to work. It was able to restore the values after an app uninstall/reinstall on my device.

    Here is what I did to get it to work:

    • Copy the source files from the BackupRestore project into a new project.
    • Generate the Backup key for the manifest file from this website: https://developer.android.com/google/backup/signup.html
    • Run adb shell bmgr list transports to see the transports.
    • Run adb shell bmgr transport android/com.android.internal.backup.LocalTransport to change the transport to Local (Google transport will impose a rate limit)
    • Run adb shell bmgr backup [package] -- to backup app
    • Run adb shell dumpsys backup to see the pending backup files
    • adb shell bmgr run

    • adb uninstall [package name]

    • adb install -t [apk file] (for debugging I used -t)

    • Check that the values were restored during the install.

    • You can also click on Restore last Data button and the OnRestore event will fire, the last backup will be restored.

    I struggled with this. I hope this helps someone.