Home » Android » How to backup Realm DB in Android before deleting the Realm file. Is there any way to restore the backup file?

How to backup Realm DB in Android before deleting the Realm file. Is there any way to restore the backup file?

Posted by: admin June 15, 2020 Leave a comment


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 restore it back if something is wrong when using realm.copyToRealm() ?

How to&Answers:

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

Realm orgRealm = Realm.getInstance(orgConfig);
Realm backupRealm = Realm.getInstance(backupConfig);
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:



This is sample code for realm db backup & restore.

public class RealmMigration  {

private final static String TAG = RealmMigration.class.getName();

private Context context;
private Realm realm;

public RealmMigration(Context context) {
    this.realm = Realm.getInstance(BaseApplication.realmConfiguration);
    this.context = context;

public void backup() {

    File exportRealmFile = null;

    File exportRealmPATH = context.getExternalFilesDir(null);
    String exportRealmFileName = "default.realm";

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

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

        // if backup file already exists, delete it

        // copy current realm to backup file

    } catch (IOException e) {

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



public void restore() {

    File exportRealmPATH = context.getExternalFilesDir(null);
    String FileName = "default.realm";

    String restoreFilePath = 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 {
        File file = new File(context.getFilesDir(), outFileName);

        Log.d(TAG, "context.getFilesDir() = " + context.getFilesDir().toString());
        FileOutputStream outputStream = new FileOutputStream(file);

        FileInputStream inputStream = new FileInputStream(new File(oldFilePath));

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

private String dbPath(){

    return realm.getPath();