Browse Source

enhance: keep the latest 12 hourly backups

Tienson Qin 1 year ago
parent
commit
5b7cd68b78
1 changed files with 33 additions and 7 deletions
  1. 33 7
      src/main/frontend/handler/export.cljs

+ 33 - 7
src/main/frontend/handler/export.cljs

@@ -224,6 +224,16 @@
       (.setAttribute anchor "download" (file-name (str repo "_roam") :json))
       (.click anchor))))
 
+(defn- truncate-old-versioned-files!
+  "reserve the latest 12 version files"
+  [^js backups-handle]
+  (p/let [files (utils/getFiles backups-handle true)
+          old-versioned-files (drop 12 (reverse (sort-by (fn [^js file] (.-name file)) files)))]
+    (p/map (fn [^js files]
+             (doseq [^js file files]
+               (.remove (.-handle file))))
+           old-versioned-files)))
+
 (defn backup-db-graph
   [repo]
   (when (and repo (= repo (state/get-current-repo)))
@@ -231,12 +241,25 @@
       (p/let [handle (idb/get-item (str "file-handle/" backup-folder))
               repo-name (sqlite-common-db/sanitize-db-name repo)]
         (if handle
-          (p/let [graph-dir-handle (.getDirectoryHandle handle repo-name #js {:create true})
-                  backup-handle (.getFileHandle graph-dir-handle "db.sqlite" #js {:create true})
-                  data (persist-db/<export-db repo {:return-data? true})
-                  _ (utils/writeFile backup-handle data)]
-            (println "Successfully created a backup for" repo-name "at" (str (js/Date.)) ".")
-            true)
+          (->
+           (p/let [graph-dir-handle (.getDirectoryHandle handle repo-name #js {:create true})
+                   backups-handle (.getDirectoryHandle graph-dir-handle "backups" #js {:create true})
+                   backup-handle ^js (.getFileHandle graph-dir-handle "db.sqlite" #js {:create true})
+                   file ^js (.getFile backup-handle)
+                   file-content (.text file)
+                   data (persist-db/<export-db repo {:return-data? true})
+                   decoded-content (.decode (js/TextDecoder.) data)]
+             (when (not= file-content decoded-content)
+               (p/do!
+                (when (> (.-size file) 0)
+                  (.move backup-handle backups-handle (str (util/time-ms) ".db.sqlite")))
+                (truncate-old-versioned-files! backups-handle)
+                (p/let [new-backup-handle ^js (.getFileHandle graph-dir-handle "db.sqlite" #js {:create true})]
+                  (utils/writeFile new-backup-handle data))
+                (println "Successfully created a backup for" repo-name "at" (str (js/Date.)) ".")
+                true)))
+           (p/catch (fn [error]
+                      (js/console.error error))))
           (p/do!
             ;; handle cleared
            (notification/show! "DB backup failed, please go to Export and specify a backup folder." :error)
@@ -258,5 +281,8 @@
       (when backup-now? (backup-db-graph repo))
 
     ;; run backup every hour
-      (let [interval (js/setInterval #(backup-db-graph repo) (* 1 60 60 1000))]
+      (let [interval (js/setInterval #(backup-db-graph repo)
+                                     ;; (* 1 60 60 1000)
+                                     (* 1 10 1000)
+                                     )]
         (reset! *backup-interval interval)))))