I've implemented a BackupAgentHelper while using provided FileBackupHelper to backup and restore the native database I've. This is actually the database you typically use together with ContentProviders and which resides in /data/data/yourpackage/databases/.

You might think this can be a common situation. Nevertheless the paperwork aren't obvious on how to proceed: http://developer.android.com/guide/subjects/data/backup.html. There's no BackupHelper particularly of these typical databases. Hence I made use of the FileBackupHelper, pointed it to my .db file in "/databases/", introduced locks around any db operation (for example db.insert) during my ContentProviders, as well as attempted creating the "/databases/" directory before onRestore() because it doesn't exist after install.

I've implemented an identical solution for that SharedPreferences effectively inside a different application previously. However, if I test my new implementation within the emulator-2.2, I visit a backup being carried out to LocalTransport in the logs, in addition to a restore being carried out (and onRestore() known as). Yet, the db file is never produced.

Observe that this really is all after an install, and before first launch from the application, following the restore continues to be carried out. Aside from that my test strategy took it's origin from http://developer.android.com/guide/subjects/data/backup.html#Testing.

Please also note I am not speaking about some sqlite database I manage myself, nor about copying to SDcard, own server or elsewhere.

Used to do visit a mention within the paperwork about databases counseling to utilize a custom BackupAgent but it doesn't appear related:

However, you might like to extend BackupAgent directly if you want to: * Support data inside a database. For those who have an SQLite database that you simply wish to restore once the user re-installs the application, you'll need to construct a custom BackupAgent that reads the right data throughout a backup operation, then make your table and place the information throughout a restore operation.

Some clearness please.

Basically really should get it done myself as much as the SQL level, then I am concerned about the next subjects:

  • Open databases and transactions. I've no clue how you can close them from this type of singleton class outdoors of my app's workflow.

  • How you can inform the consumer that the backup is happening and also the database is locked. It could take a very long time, and so i should show a progress bar.

  • How you can perform the same on restore. When I understand, the restore can happen just once the user has began while using application (and typing data in to the database). Which means you can't presume to simply restore the backupped data in position (removing the empty or old data). You will need to in some way join it in, which for just about any non-trivial database doesn't seem possible because of the id's.

  • How you can refresh the application following the restore is performed without obtaining the user stuck at some - now - unreachable point.

  • Can One make sure the database was already upgraded on backup or restore? Otherwise the expected schema may not match.

After returning to my question, I could get results after searching at how ConnectBot will it. Thanks Kenny and Jeffrey!

It's really as simple as adding:

FileBackupHelper hosts = new FileBackupHelper(this,
    "../databases/" + HostDatabase.DB_NAME);
addHelper(HostDatabase.DB_NAME, hosts);

for your BackupAgentHelper.

The purpose I had been missing was the truth that you'd need to use a family member path with "../databases/".

Still, this really is in no way an ideal solution. The paperwork for FileBackupHelper mention for example: "FileBackupHelper ought to be used just with small configuration files, not large binary files.", the second being the situation with SQLite databases.

Let me have more suggestions, experience into what's expected people (what's the proper solution), and suggestions about how this may break.