Last active
August 30, 2022 10:39
-
-
Save molidev8/49dba8ac678ba1348f9b84a4cc6d292e to your computer and use it in GitHub Desktop.
Backing/restore of the SQL database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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