|
1 | 1 | (ns octo.github.pull |
2 | 2 | "pull backup into restore folder" |
3 | 3 | (:require |
4 | | - [octo.github.push :refer [format-]] |
| 4 | + [octo.common.pull :as cp] |
5 | 5 | [taoensso.timbre :as timbre] |
6 | 6 | [clojure.core.strint :refer (<<)] |
7 | | - [octo.common :refer (safe lazy-mkdir rclone-sync files)] |
8 | | - [clojure.edn :refer (read-string)] |
9 | | - [me.raynes.fs :as fs] |
10 | | - [clojure.java.io :refer (file)] |
11 | | - [taoensso.timbre :as timbre] |
12 | | - [clojure.java.shell :refer [sh with-sh-dir]])) |
| 7 | + )) |
13 | 8 |
|
14 | 9 | (timbre/refer-timbre) |
15 | 10 |
|
16 | | -(defn parse [s] (.parse (java.text.SimpleDateFormat. format-) s)) |
17 | | - |
18 | | -(defn parent |
19 | | - ([workspace] |
20 | | - (<< "~{workspace}/pull") ) |
21 | | - ([workspace repo] |
22 | | - (<< "~(parent workspace)/zbackup/~{repo}.zback")) |
23 | | - ([workspace repo name] |
24 | | - (<< "~(parent workspace)/repos/~{repo}/~{name}")) |
25 | | - ) |
26 | | - |
27 | | -(defn validate [extracted workspace repo] |
28 | | - (let [{:keys [total]} (read-string (slurp (<< "~{extracted}/check.edn"))) |
29 | | - restored (count (files (<< "~(parent workspace)/repos/~{repo}/")))] |
30 | | - (debug "validating restoration for" repo) |
31 | | - (when-not (== total restored) |
32 | | - (throw (ex-info "Failed to restore found" {:found restored :expected total}))))) |
33 | | - |
34 | | -(defn restore-bundles |
35 | | - [workspace repo] |
36 | | - (let [extracted (file (parent workspace) (.substring workspace 1) "repos" repo "bundles")] |
37 | | - (info "restoring bundles from" extracted) |
38 | | - (doseq [bundle (filter #(and (.isFile %) (.endsWith (.getName %) "bundle")) (file-seq extracted)) |
39 | | - :let [name (-> bundle (.getName) (.replace ".bundle" "")) target (parent workspace repo name)]] |
40 | | - (when (.exists (file target)) (fs/delete-dir target)) |
41 | | - (safe "git" "clone" (.getAbsolutePath bundle) target) |
42 | | - (with-sh-dir target (safe "git" "fsck" target))) |
43 | | - (validate extracted workspace repo) |
44 | | - (fs/delete-dir extracted))) |
45 | | - |
46 | | -(defn zbackup-restore |
47 | | - "Restore an org/repo" |
48 | | - [workspace password repo] |
49 | | - (let [base (<< "~(parent workspace repo)/backups/") |
50 | | - backups (filter #(.isFile %) (file-seq (file base))) |
51 | | - latest (.getAbsolutePath (last (sort-by #(-> % (.getName) (.replace repo "") parse) backups))) |
52 | | - target (<< "~(parent workspace)/tars/~{repo}.tar")] |
53 | | - (debug "zbackup restore") |
54 | | - (lazy-mkdir (<< "~(parent workspace)/tars")) |
55 | | - (safe "/bin/sh" "-c" (<< "zbackup --password-file ~{password} restore ~{latest} > ~{target}")) |
56 | | - (with-sh-dir (parent workspace) |
57 | | - (safe "tar" "xf" target)) |
58 | | - (lazy-mkdir (<< "~(parent workspace)/repos")) |
59 | | - (restore-bundles workspace repo))) |
60 | | - |
61 | 11 | (defn pull |
62 | 12 | [workspace {:keys [zbackup rclone] :as m} {:keys [org user]}] |
63 | | - (lazy-mkdir (parent workspace)) |
64 | 13 | (let [repo (or org user)] |
65 | | - (info "synching" repo) |
66 | | - (rclone-sync (<< "~(rclone :dest)/~{repo}.zback") (parent workspace repo)) |
67 | | - (info "purging" (<< "~(parent workspace)/repos/~{repo}")) |
68 | | - (fs/delete-dir (<< "~(parent workspace)/repos/~{repo}")) |
69 | | - (info "restoring" repo) |
70 | | - (when (empty? (.list (file (parent workspace repo)))) |
71 | | - (throw (ex-info "zbackup archive empty fix configuration, sync and push" {:repo repo}))) |
72 | | - (zbackup-restore workspace (zbackup :password-file) repo))) |
| 14 | + (cp/pull m (<< "~{workspace}/pull") repo))) |
0 commit comments