如何在删除Realm文件之前在Android中备份Realm DB。有没有办法恢复备份文件?

时间:2015-07-01 21:04:47

标签: android realm realm-database

我正在开发Android应用程序,在将新数据复制到Realm之前,我将删除Realm。有没有什么办法可以在删除之前备份数据并在使用realm.copyToRealm()时出现问题时将其还原?

2 个答案:

答案 0 :(得分:14)

ext { githubProjectName = 'eureka' awsVersion='1.9.3' servletVersion='2.5' jerseyVersion='1.11' governatorVersion='1.3.3' archaiusVersion='0.6.5' blitzVersion='1.34' mockitoVersion='1.9.5' junit_version='4.10' mockserverVersion='3.9.2' jetty_version='7.2.0.v20101020' } dependencies { compile "com.sun.jersey:jersey-core:$jerseyVersion" compile "com.sun.jersey:jersey-client:$jerseyVersion" compile 'com.sun.jersey.contribs:jersey-apache-client4:1.11' compile 'org.apache.httpcomponents:httpclient:4.2.1' } 可能对此案有所帮助。你可以在这里找到doc

Realm.writeCopyTo

但在您的情况下,将Realm文件移动到备份位置会更加简单快捷,并在您想要还原它时将其移回。要获取Realm文件路径,请尝试:

//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);

答案 1 :(得分:7)

这是领域db backup& amp;的示例代码。还原

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
        exportRealmFile.delete();

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

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

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


    realm.close();

}

public void restore() {

    //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);
        }
        outputStream.close();
        return file.getAbsolutePath();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

private String dbPath(){

    return realm.getPath();
}

}