title: Android backup/restore: how to backup an internal database?
body: <p>I have implemented a <code>BackupAgentHelper</code> using the provided <code>FileBackupHelper</code> to backup and restore the native database I have. This is the database you typically use along with <code>ContentProviders</code> and which resides in <code>/data/data/yourpackage/databases/</code>.</p>
  
  <p>One would think this is a common case. However the docs aren't clear on what to do: <a href="http://developer.android.com/guide/topics/data/backup.html" rel="noreferrer">http://developer.android.com/guide/topics/data/backup.html</a>. <strong>There is no <code>BackupHelper</code> specifically for these typical databases. Hence I used the <code>FileBackupHelper</code></strong>, pointed it to my .db file in "<code>/databases/</code>", introduced locks around any db operation (such as <code>db.insert</code>) in my <code>ContentProviders</code>, and even tried creating the "<code>/databases/</code>" directory before <code>onRestore()</code> because it does not exist after install.</p>
  
  <p>I have implemented a similar solution for the <code>SharedPreferences</code> successfully in a different app in the past. However when I test my new implementation in the emulator-2.2, I see a backup being performed to <code>LocalTransport</code> from the logs, as well as a restore being performed (and <code>onRestore()</code> called). <strong>Yet, the db file itself is never created.</strong></p>
  
  <p>Note that this is all after an install, and before first launch of the app, after the restore has been performed. Apart from that my test strategy was based on <a href="http://developer.android.com/guide/topics/data/backup.html#Testing" rel="noreferrer">http://developer.android.com/guide/topics/data/backup.html#Testing</a>.</p>
  
  <p>Please also note I'm not talking about some sqlite database I manage myself, nor about backing up to SDcard, own server or elsewhere. </p>
  
  <p>I did see a mention in the docs about databases advising to use a custom <code>BackupAgent</code> but it does not seem related:</p>
  
  <blockquote>
    <p>However, you might want to extend
    BackupAgent directly if you need to:
        * Back up data in a database. If you have an SQLite database that you
    want to restore when the user
    re-installs your application, you need
    to build a custom BackupAgent that
    reads the appropriate data during a
    backup operation, then create your
    table and insert the data during a
    restore operation.</p>
  </blockquote>
  
  <p><strong>Some clarity please.</strong></p>
  
  <p>If I really need to do it myself up to the SQL level, then I'm worried about the following topics:</p>
  
  <ul>
  <li><p>Open databases and transactions. I have no idea how to close them from such a singleton class outside of my app's workflow.</p></li>
  <li><p>How to notify the user that a backup is in progress and the database is locked. It might take a long time, so I might need to show a progress bar.</p></li>
  <li><p>How to do the same on restore. As I understand, the restore might happen just when the user has already started using the app (and inputting data into the database). So you can't presume to just restore the backupped data in place (deleting the empty or old data). You'll have to somehow join it in, which for any non-trivial database is impossible due to the id's.</p></li>
  <li><p>How to refresh the app after the restore is done without getting the user stuck at some - now - unreachable point.</p></li>
  <li><p>Can I be sure the database has already been upgraded on backup or restore? Otherwise the expected schema might not match.</p></li>
  </ul>