Skip to content Skip to sidebar Skip to footer

How To Backup Realm Db In Android Before Deleting The Realm File. Is There Any Way To Restore The Backup File?

I am working on an Android application where I will be deleting Realm before copying the new data to Realm. Is there any way I can take backup of the data before deleting it and re

Solution 1:

Realm.writeCopyTo might be helpful for this case. You can find doc here.

//Backup
Realm orgRealm = Realm.getInstance(orgConfig);
orgRealm.writeCopyTo(pathToBackup);
orgRealm.close();
//Restore
Realm.deleteRealm(orgConfig);
Realm backupRealm = Realm.getInstance(backupConfig);
backupRealm.writeCopyTo(pathToRestore);
backupRealm.close();
orgRealm = Realm.getInstance(orgConfig);

But in your case, it would be much simpler and faster to just move your Realm file to a place to backup, and move it back when you want to restore it. To get the Realm file path, try:

realm.getPath();

Solution 2:

This is sample code for realm db backup & restore.

publicclassRealmMigration  {

privatefinalstaticStringTAG= RealmMigration.class.getName();

private Context context;
private Realm realm;

publicRealmMigration(Context context) {
    this.realm = Realm.getInstance(BaseApplication.realmConfiguration);
    this.context = context;
}

publicvoidbackup() {

    FileexportRealmFile=null;

    FileexportRealmPATH= context.getExternalFilesDir(null);
    StringexportRealmFileName="default.realm";

    Log.d(TAG, "Realm DB Path = "+realm.getPath());

    try {
        // create a backup file
        exportRealmFile = newFile(exportRealmPATH, exportRealmFileName);

        // if backup file already exists, delete it
        exportRealmFile.delete();

        // copy current realm to backup file
        realm.writeCopyTo(exportRealmFile);

    } catch (IOException e) {
        e.printStackTrace();
    }

    Stringmsg="File exported to Path: "+ context.getExternalFilesDir(null);
        Toast.makeText(context, msg, Toast.LENGTH_LONG).show();
        Log.d(TAG, msg);


    realm.close();

}

publicvoidrestore() {

    //RestoreFileexportRealmPATH= context.getExternalFilesDir(null);
    StringFileName="default.realm";

    StringrestoreFilePath= context.getExternalFilesDir(null) + "/"+FileName;

    Log.d(TAG, "oldFilePath = " + restoreFilePath);

    copyBundledRealmFile(restoreFilePath, FileName);
        Log.d(TAG, "Data restore is done");

}

private String copyBundledRealmFile(String oldFilePath, String outFileName) {
    try {
        Filefile=newFile(context.getFilesDir(), outFileName);

        Log.d(TAG, "context.getFilesDir() = " + context.getFilesDir().toString());
        FileOutputStreamoutputStream=newFileOutputStream(file);

        FileInputStreaminputStream=newFileInputStream(newFile(oldFilePath));

        byte[] buf = newbyte[1024];
        int bytesRead;
        while ((bytesRead = inputStream.read(buf)) > 0) {
            outputStream.write(buf, 0, bytesRead);
        }
        outputStream.close();
        return file.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
    }
    returnnull;
}

private String dbPath(){

    return realm.getPath();
}

}

Post a Comment for "How To Backup Realm Db In Android Before Deleting The Realm File. Is There Any Way To Restore The Backup File?"