Skip to content

Commit

Permalink
backup once a week and delete old files after backup
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Jan 1, 2025
1 parent 9c1c930 commit 6049b79
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,16 @@ class WebSocketServerService : Service() {
folder?.let {
val lastModifiedTime = folder.lastModified()
val currentTime = System.currentTimeMillis()
val twentyFourHoursAgo = currentTime - (24 * 60 * 60 * 1000)
val fiveDaysAgo = currentTime - (5 * 24 * 60 * 60 * 1000)

if (lastModifiedTime < twentyFourHoursAgo) {
Log.d(Citrine.TAG, "Deleting old backups")
folder.listFiles().forEach { file ->
if (file.lastModified() < fiveDaysAgo) {
file.delete()
}
}
val oneWeekAgo = currentTime - (7 * 24 * 60 * 60 * 1000)

if (lastModifiedTime < oneWeekAgo) {
Log.d(Citrine.TAG, "Backing up database")
scope.launch(Dispatchers.IO) {
ExportDatabaseUtils.exportDatabase(
database,
this@WebSocketServerService,
it,
database = database,
context = this@WebSocketServerService,
folder = it,
deleteOldFiles = true,
onProgress = {},
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.greenart7c3.citrine.utils

import android.content.Context
import android.util.Log
import androidx.documentfile.provider.DocumentFile
import com.anggrayudi.storage.file.CreateMode
import com.anggrayudi.storage.file.makeFile
import com.anggrayudi.storage.file.openOutputStream
import com.greenart7c3.citrine.Citrine
import com.greenart7c3.citrine.database.AppDatabase
import com.greenart7c3.citrine.database.toEvent
import java.util.Date
Expand All @@ -16,22 +18,34 @@ object ExportDatabaseUtils {
database: AppDatabase,
context: Context,
folder: DocumentFile,
deleteOldFiles: Boolean = false,
onProgress: (String) -> Unit,
) = withContext(Dispatchers.IO) {
val file = folder.makeFile(
context,
"citrine-${Date().time}.jsonl",
mode = CreateMode.CREATE_NEW,
)
val op = file?.openOutputStream(context)
op?.writer().use { writer ->
val events = database.eventDao().getAllIds()
events.forEachIndexed { index, it ->
database.eventDao().getById(it)?.let { event ->
val json = event.toEvent().toJson() + "\n"
writer?.write(json)
if (index % 100 == 0) {
onProgress("Exported ${index + 1}/${events.size}")
val events = database.eventDao().getAllIds()
if (events.isNotEmpty()) {
val fileName = "citrine-${Date().time}.jsonl"
val file = folder.makeFile(
context,
fileName,
mode = CreateMode.CREATE_NEW,
)
val op = file?.openOutputStream(context)
op?.writer().use { writer ->
events.forEachIndexed { index, it ->
database.eventDao().getById(it)?.let { event ->
val json = event.toEvent().toJson() + "\n"
writer?.write(json)
if (index % 100 == 0) {
onProgress("Exported ${index + 1}/${events.size}")
}
}
}
if (deleteOldFiles) {
Log.d(Citrine.TAG, "Deleting old backups")
folder.listFiles().forEach { file ->
if (file.name != fileName) {
file.delete()
}
}
}
}
Expand Down

0 comments on commit 6049b79

Please sign in to comment.