3

My app uses a small SQLite database that I'd like to have backed up. I'm assuming this won't happen automatically, without my coding for it using fullBackupContent, shown below. How do I modify the content of my backupscheme.xml to set the include path correctly? I prefer to set the db location at runtime.

My backupscheme.xml looks like

<?xml version="1.0" encoding="utf-8"?>
<full-backup-content >
    <include domain="database" path="device_info.db"/>
</full-backup-content

My manifest contains:

    <application
    android:allowBackup="true"
    android:icon="@drawable/time_machine_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:name="mypackage.myapp"
    android:fullBackupContent="@xml/backupscheme" 
    > 
3
  • Um, if your database is named device_info.db, it would seem like you have the right stuff for it to get backed up. Commented Mar 4, 2016 at 16:48
  • Well, that's not the name, but I thought I had to populate path with the full path, something like /data/mypackage.myapp/databases/mydatabase.db Commented Mar 4, 2016 at 16:54
  • why do you think that it wont happen automatically? i though that if you define android:allowBackup="true", everything is backed up included databases not? Commented Mar 22, 2018 at 15:41

1 Answer 1

4
<include domain="database" path="device_info.db"/>

Here, the domain indicates the root directory in which the path is interpreted. database maps to where SQLite databases are stored by default, if you use:

  • getDatabasePath() on Context
  • SQLiteOpenHelper with just a plain filename
  • openOrCreateDatabase() with just a plain filename

In that case, the filename should be your path value.

If your database is stored somewhere else for some reason, the <include> directive would need some adjustment.

Sign up to request clarification or add additional context in comments.

11 Comments

That simplifies things. Thank you. Do you know when/how often the auto backup actually occurs? Is there a way to force the issue?
@PhilO: For testing, follow the instructions. In the "real world", I have no idea when/how often the backup occurs, other than it is tied to the conditions listed in the documentation.
@CommonsWare well, that's the thing. In my tests, I have found that I'm not missing any data which leads me to speculate that maybe the backup functionality does something like PRAGMA wal_checkpoint(2) stackoverflow.com/questions/12928294/…
Just to give some more details when backing up the app where the .db, .db-shm and .db-wal files exist (but only the .db specified in backup rules XML): When I use ADB to trigger the backup, then do Clear data, then restore, I notice both the .db and .db-wal files are restored in original form/size (i.e. no checkpoint occurred). The .db-shm is not restored, but I guess that's not needed anyway.
As a side note, not all wal_checkpoint commands delete the -wal file. For example, PRAGMA wal_checkpoint(TRUNCATE) results in a 0 length -wal file. Regardless, no type of wal_checkpoint occurs during backup/restore.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.