5

I'm currently looking to build a backup feature in my Android application. However I'm kinda struggling before even starting to implement it because I'm not sure what the correct way to go is.

I found some interesting articles on the net and so I came up with three possible solutions:

  1. Backup the entire DB file to the SD card
  2. Export the DB-data to an XML file on the SD card
  3. Use the Android backup mechanism to backup the entire DB to the Google cloud

Now I was wondering what you guys think about these 3 solutions, or do you know another (maybe an even better way to go) and what is in your eyes the best way to go?

Here are my remarks on the possible implementations:

  1. I don't know if the phone isn't rooted that it's possible to restore the DB file... Otherwise there aren't really any down sides for this I think...
  2. Handling XML files on the fly on Android phones is heavy so if it can be avoided it's best not to do it like that
  3. Using the Android backup mechanism the backup feature is only available if it's enabled by the user on the phone, and all the data should be copied to the cloud... Which in my case can be in some cases quite a lot...

I'm looking forward to see some input on this issue!

Thanks in advance!

Kr,

Dirk

dirkvranckaert
  • 1,364
  • 1
  • 17
  • 30

2 Answers2

0

You don't need to have a rooted phone to restore the database from a file on the SD card. Each application can write to it's own private directories, so you can just copy the file. As for 2, do you have any concrete numbers? Handling XML is fairly fast, and since it's backup/restore, it doesn't happen too often and users would expect that it takes some time, so it shouldn't be a problem. As usual, measure the actual time it takes and consider how much data you have, before you make any decisions.

Nikolay Elenkov
  • 52,576
  • 10
  • 84
  • 84
  • Ok but if 1) is possible then why should one even spend time on creating some generic stuff to backup all tables and their data and not just copy the entire database? Does that have any known disadvantages? – dirkvranckaert Sep 11 '11 at 21:00
  • It depends, if you don't need it, don't write it. Maybe you need to import your data in other programs, and XML/CSV/JSON might be a better choice. It's quite unlikely, but the SQLite version in Android 5.0 might be uncompatible with the current format, etc. – Nikolay Elenkov Sep 12 '11 at 02:05
0

I always use 1.). Here's a class of mine that does backup of a DB to SD-card. I'm using FileUtils from the Apache commons-io here, you need to change that if you don't use that jar. In addition there's need for a method in your SQLiteOpenHelper class (here MySQLiteOpenHelper.getDatabaseName()) that returns the name of your database file.

You will call that from within an AsyncTask in one of your activities...

public class MyDatabaseTools {
  private String appName = "";
  private String packageName = "";

  public boolean backup() {
    boolean rc = false;

    boolean writeable = isSDCardWriteable();
    if (writeable) {
      File file = new File(Environment.getDataDirectory() + "/data/" + packageName + "/databases/" + MySQLiteOpenHelper.getDatabaseName());

      File fileBackupDir = new File(Environment.getExternalStorageDirectory(), appName + "/backup");
      if (!fileBackupDir.exists()) {
        fileBackupDir.mkdirs();
      }

      if (file.exists()) {
        File fileBackup = new File(fileBackupDir, MySQLiteOpenHelper.getDatabaseName());
        try {
          fileBackup.createNewFile();
          FileUtils.copyFile(file, fileBackup);
          rc = true;
        } catch (IOException ioException) {
          //
        } catch (Exception exception) {
          //
        }
      }
    }

    return rc;
  }

  private boolean isSDCardWriteable() {
    boolean rc = false;

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
      rc = true;
    }

    return rc;
  }

    public MyDatabaseTools(final Context context, final String appName) {
        this.appName = appName;
        packageName = context.getPackageName();
    }
}
Harald Wilhelm
  • 6,656
  • 11
  • 67
  • 85
  • The example is similar to the ones I've found during my research. I will probably to this way. Thanks for the help! – dirkvranckaert Sep 11 '11 at 21:01
  • This doesn't mention restoring the database. – Stealth Rabbi Aug 12 '14 at 13:33
  • @Harald, i understand your answer. its helpfull for developement it. but can you share your suggestion for when we have to move db to another directory like uninstall time install time etc ? waiting for your positive feedback – Teraiya Mayur Jan 06 '17 at 06:11