Search code examples
androidsqlitegoogle-drive-android-api

Backup and sync SQLite database with Google Drive logic?


I want the "backup" part to be done in the background and would only start if the user is not using it. I am using android-priority-jobqueue and have made a Job object that would achieve this.

Here are my current problems :

  1. But now I am thinking of how I can make sure that the user is not using the app so that I can run the job. I am also thinking how I can backup or sync the database while the user is using the app like in multiple other apps.

  2. I don't know how I can restore the data back to the app without missing any data. Like for instance, there are two devices. Device 1 backed-up its database into Google Drive. Now, Device 2, being that it had no connection for a few hours, it created new entries into the database. Then it connected to internet and it has to get the other entries from the Google Drive without overwriting the entries it currently has.

I really am confused about when to sync, when to backup, and how they can be flawlessly achieved.

Can anybody please help me


Solution

  • For #2, if you are using GDAA to upload the file, you can pass an ExecutionOptions that allows you to handle conflicts. This lets you implement any arbitrary logic to resolve the upload conflict, e.g. maybe in your case it's to merge contents from both devices. However, note that currently conflict detection is only possible for updating an existing file, not for creating a new one.

    For more reference: https://developers.google.com/drive/android/completion#conflict https://developers.google.com/android/reference/com/google/android/gms/drive/ExecutionOptions.Builder.html#setConflictStrategy(int)

    Sample code for conflict resolution: https://github.com/googledrive/android-conflict