如何在删除 Realm 文件之前备份 Android 中的 Realm DB。有什么办法可以恢复备份文件吗?
How to backup Realm DB in Android before deleting the Realm file. Is there any way to restore the backup file?
我正在开发一个 Android
应用程序,我将在将新数据复制到 Realm 之前删除 Realm
。有没有什么办法可以在删除数据之前备份数据,如果在使用 realm.copyToRealm()
时出现问题,可以将其恢复回来?
Realm.writeCopyTo
可能对这种情况有所帮助。您可以在此处找到 doc。
//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);
但在你的情况下,将你的 Realm 文件移动到一个地方进行备份,然后在你想要恢复它时将其移动回来,会更简单、更快捷。要获取 Realm 文件路径,请尝试:
realm.getPath();
这是领域数据库备份和恢复的示例代码。
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();
}
}
我正在开发一个 Android
应用程序,我将在将新数据复制到 Realm 之前删除 Realm
。有没有什么办法可以在删除数据之前备份数据,如果在使用 realm.copyToRealm()
时出现问题,可以将其恢复回来?
Realm.writeCopyTo
可能对这种情况有所帮助。您可以在此处找到 doc。
//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);
但在你的情况下,将你的 Realm 文件移动到一个地方进行备份,然后在你想要恢复它时将其移动回来,会更简单、更快捷。要获取 Realm 文件路径,请尝试:
realm.getPath();
这是领域数据库备份和恢复的示例代码。
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();
}
}