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" 
    > 
Phil O
  • 1,588
  • 4
  • 28
  • 52
  • Um, if your database is named `device_info.db`, it would seem like you have the right stuff for it to get backed up. – CommonsWare Mar 04 '16 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 – Phil O Mar 04 '16 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? – Emil Mar 22 '18 at 15:41

1 Answers1

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • That simplifies things. Thank you. Do you know when/how often the auto backup actually occurs? Is there a way to force the issue? – Phil O Mar 04 '16 at 17:18
  • @PhilO: For testing, [follow the instructions](http://developer.android.com/training/backup/autosyncapi.html#testing). 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](http://developer.android.com/training/backup/autosyncapi.html). – CommonsWare Mar 04 '16 at 17:41
  • Exactly what I'm doing now. Backup appears to work, but restore fails with Unable to restore package mypackage.myapp – Phil O Mar 04 '16 at 17:42
  • Do you think the restore failure has to do with a permission not having been granted? – Phil O Apr 07 '16 at 14:16
  • @PhilO: I have no idea, sorry. – CommonsWare Apr 07 '16 at 14:17
  • It is not clear whether you also need to include accompanying -shm and -wal files. In my rough testing this seems to be unnecessary, but I would like to see some official documentation on this. – Mark Jan 12 '21 at 07:53
  • @Mark: You should find that you are missing some data, if you do not include the `-shm` and `-wal` files when they exist. – CommonsWare Jan 12 '21 at 12:16
  • 1
    @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)` https://stackoverflow.com/questions/12928294/shm-and-wal-files-in-sqlite-db – Mark Jan 13 '21 at 13:06
  • @Mark: I certainly can't rule that out, though I was under the impression that those files would not be there after such a checkpoint. I agree that it would be better if there were more docs around this. – CommonsWare Jan 13 '21 at 14:54
  • 1
    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. – Mark Feb 09 '21 at 03:30
  • 1
    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. – Mark Feb 09 '21 at 03:33