Skip to content

Instantly share code, notes, and snippets.

@molidev8
Last active August 30, 2022 10:39
Show Gist options
  • Save molidev8/49dba8ac678ba1348f9b84a4cc6d292e to your computer and use it in GitHub Desktop.
Save molidev8/49dba8ac678ba1348f9b84a4cc6d292e to your computer and use it in GitHub Desktop.
Backing/restore of the SQL database
/**
* Initiates the backup process
* @return true in case the upload went well, false otherwise
*/
fun doBackup(): Boolean = try {
LocalRecipeDatabase.getInstance(context).close()
val output = FileOutputStream(backupDir?.path + "/recipe-vault-backup.zip")
zipFiles(output, context.getDatabasePath(DATABASE_NAME), photosDir)
val zipFile = FileInputStream(backupDir?.listFiles()?.find { it.name == "recipe-vault-backup.zip" })
uploadToDropbox(zipFile)
} catch (e: FileNotFoundException) {
throw Exception()
} catch (e: Exception) {
throw Exception()
}
/**
* Restores the local Room database with the backup downloaded from Dropbox
*/
private fun restoreRoomDatabase() {
val dbPath = context.getDatabasePath(DATABASE_NAME)
val backupFile = backupDir?.listFiles()?.find { it.name == "recipes_db" }
try {
backupFile?.copyTo(dbPath, true)
} catch (e: Exception) {
// Do something
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment