-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
142 additions
and
206 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,59 @@ | ||
;; Run this script with "clj -T:build uber" | ||
(ns build | ||
(:require [clojure.tools.build.api :as b] | ||
[babashka.fs :as fs])) | ||
(:require [babashka.fs :as fs] | ||
[clojure.tools.build.api :as b])) | ||
|
||
(def lib 'com.github.schmir/prune-backups) | ||
(def git-branch (b/git-process {:git-args "branch --show-current"})) | ||
(def version (str (if (= git-branch "main") | ||
nil | ||
(str git-branch "-")) | ||
"v0.1." (b/git-count-revs nil))) | ||
(def version | ||
(str (if (= git-branch "main") nil (str git-branch "-")) "v0.1." (b/git-count-revs nil))) | ||
(def class-dir "target/classes") | ||
(def meta-clj (format "%s/prune_backups/meta.clj" class-dir)) | ||
(def basis (b/create-basis {:project "deps.edn"})) | ||
(def uber-file (format "target/%s.jar" (name lib))) | ||
(def uberscript-file (format "target/%s" (name lib))) | ||
|
||
(let [d (delay (b/delete {:path "target"}))] | ||
(defn clean [_] | ||
@d)) | ||
(let [d (delay (b/delete {:path "target"}))] (defn clean [_] @d)) | ||
|
||
(defn meta-ns | ||
[] | ||
(str "(ns prune-backups.meta)" \newline | ||
"(def version " (pr-str version) ")" \newline | ||
\newline)) | ||
(str "(ns prune-backups.meta)" \newline "(def version " (pr-str version) ")" \newline \newline)) | ||
|
||
(defn uber [_] | ||
(defn uber | ||
[_] | ||
(clean nil) | ||
(b/copy-dir {:src-dirs ["src" "resources"] | ||
:target-dir class-dir}) | ||
(b/copy-dir {:src-dirs ["src" "resources"], :target-dir class-dir}) | ||
(spit meta-clj (meta-ns)) | ||
(b/compile-clj {:basis basis | ||
:ns-compile '[prune-backups.cli] | ||
:class-dir class-dir}) | ||
(b/uber {:class-dir class-dir | ||
:uber-file uber-file | ||
:basis basis | ||
:main 'prune-backups.cli})) | ||
(b/compile-clj {:basis basis, :ns-compile '[prune-backups.cli], :class-dir class-dir}) | ||
(b/uber {:class-dir class-dir, :uber-file uber-file, :basis basis, :main 'prune-backups.cli})) | ||
|
||
(defn read-source | ||
[] | ||
(apply str (->> ["proto.clj" "rotate.clj" "tarsnap.clj" "zfs.clj" "cli.clj"] | ||
(map (fn [s] | ||
(str ";; --- " s \newline | ||
(slurp (str "src/prune_backups/" s)) | ||
\newline)))))) | ||
(apply str | ||
(->> ["proto.clj" "rotate.clj" "tarsnap.clj" "zfs.clj" "cli.clj"] | ||
(map (fn [s] | ||
(str ";; --- " s \newline (slurp (str "src/prune_backups/" s)) \newline)))))) | ||
|
||
(defn prelude | ||
[] | ||
(str | ||
"#!/usr/bin/env bb" \newline | ||
";; -*- mode: clojure -*-" \newline | ||
";; prune-backups " version \newline | ||
";;" \newline | ||
";; DO NOT EDIT THIS FILE" \newline | ||
";;" \newline | ||
"" \newline)) | ||
|
||
|
||
(defn uberscript [_] | ||
(str "#!/usr/bin/env bb" | ||
\newline | ||
";; -*- mode: clojure -*-" | ||
\newline | ||
";; prune-backups " | ||
version | ||
\newline | ||
";;" \newline | ||
";; DO NOT EDIT THIS FILE" \newline | ||
";;" \newline | ||
"" \newline)) | ||
|
||
|
||
(defn uberscript | ||
[_] | ||
(clean nil) | ||
(fs/create-dirs "target") | ||
(spit uberscript-file | ||
(str | ||
(prelude) | ||
(meta-ns) | ||
(read-source))) | ||
(spit uberscript-file (str (prelude) (meta-ns) (read-source))) | ||
(fs/set-posix-file-permissions uberscript-file "rwxr-xr-x")) | ||
|
||
(defn uberall [_] | ||
(uber nil) | ||
(uberscript nil) | ||
(spit "target/VERSION" version)) | ||
(defn uberall [_] (uber nil) (uberscript nil) (spit "target/VERSION" version)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,32 @@ | ||
{:paths ["src"] | ||
:deps {org.clojure/clojure {:mvn/version "1.11.1"} | ||
org.babashka/cli {:mvn/version "0.7.53"} | ||
babashka/fs {:mvn/version "0.5.20"} | ||
babashka/process {:mvn/version "0.5.21"}} | ||
:aliases {:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"} | ||
babashka/fs {:mvn/version "0.5.20"}} | ||
:ns-default build} | ||
:test | ||
{:extra-paths ["test"] | ||
:extra-deps {io.github.cognitect-labs/test-runner | ||
{:git/tag "v0.5.1" :git/sha "dfb30dd"}} | ||
:main-opts ["-m" "cognitect.test-runner"] | ||
:exec-fn cognitect.test-runner.api/test} | ||
{:paths ["src"], | ||
:deps {org.clojure/clojure {:mvn/version "1.11.1"}, | ||
org.babashka/cli {:mvn/version "0.7.53"}, | ||
babashka/fs {:mvn/version "0.5.20"}, | ||
babashka/process {:mvn/version "0.5.21"}}, | ||
:aliases {:build {:deps {io.github.clojure/tools.build {:mvn/version "0.9.6"}, | ||
babashka/fs {:mvn/version "0.5.20"}}, | ||
:ns-default build}, | ||
:test {:extra-paths ["test"], | ||
:extra-deps {io.github.cognitect-labs/test-runner {:git/tag "v0.5.1", | ||
:git/sha "dfb30dd"}}, | ||
:main-opts ["-m" "cognitect.test-runner"], | ||
:exec-fn cognitect.test-runner.api/test}, | ||
|
||
:outdated | ||
#_:clj-kondo/ignore | ||
{:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"} | ||
org.slf4j/slf4j-nop {:mvn/version "2.0.9"}} ;; silence SLF4J warning | ||
:main-opts ["-m" "antq.core"]} | ||
{:extra-deps {com.github.liquidz/antq {:mvn/version "RELEASE"}, | ||
org.slf4j/slf4j-nop {:mvn/version "2.0.9"}}, ;; silence SLF4J warning | ||
:main-opts ["-m" "antq.core"]}, | ||
|
||
:dev | ||
{:extra-paths ["test" "dev"] | ||
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.4.4"} | ||
lambdaisland/kaocha {:mvn/version "1.87.1366"} | ||
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J warning | ||
}} | ||
:dev {:extra-paths ["test" "dev"], | ||
:extra-deps {org.clojure/tools.namespace {:mvn/version "1.4.4"}, | ||
lambdaisland/kaocha {:mvn/version "1.87.1366"}, | ||
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J | ||
;; warning | ||
}}, | ||
|
||
:kaocha | ||
{:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"} | ||
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J warning | ||
} | ||
:main-opts ["-m" "kaocha.runner"]}} | ||
} | ||
:kaocha {:extra-deps {lambdaisland/kaocha {:mvn/version "1.87.1366"}, | ||
org.slf4j/slf4j-nop {:mvn/version "2.0.9"} ;; silence SLF4J | ||
;; warning | ||
}, | ||
:main-opts ["-m" "kaocha.runner"]}}} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,71 @@ | ||
(ns prune-backups.cli | ||
(:require [clojure.edn :as edn] | ||
(:require [babashka.cli :as cli] | ||
[clojure.edn :as edn] | ||
[clojure.string :as str] | ||
[prune-backups.meta :as meta] | ||
[prune-backups.rotate :as rotate] | ||
[prune-backups.proto :as proto] | ||
[prune-backups.rotate :as rotate] | ||
[prune-backups.tarsnap :as tarsnap] | ||
[prune-backups.zfs :as zfs] | ||
[babashka.cli :as cli]) | ||
[prune-backups.zfs :as zfs]) | ||
(:gen-class)) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
|
||
(def spec | ||
{:config | ||
{:desc "Path to edn config file"} | ||
:version | ||
{:desc "Show version information"} | ||
:help | ||
{:desc "Show help message" | ||
:alias :h}}) | ||
{:config {:desc "Path to edn config file"}, | ||
:version {:desc "Show version information"}, | ||
:help {:desc "Show help message", :alias :h}}) | ||
|
||
(defn print-help | ||
[] | ||
(println "Usage: prune-backups [options]") | ||
(println (cli/format-opts {:spec spec :order [:config :version :help]})) | ||
(println (cli/format-opts {:spec spec, :order [:config :version :help]})) | ||
(System/exit 0)) | ||
|
||
(defn find-archives-with-prefix | ||
[prefix archives] | ||
(filter (fn [a] (str/starts-with? (:archive a) prefix)) | ||
archives)) | ||
(filter (fn [a] (str/starts-with? (:archive a) prefix)) archives)) | ||
|
||
(defn rotate | ||
([cfg] | ||
(rotate cfg (proto/list-backups (:backup-set cfg)))) | ||
([cfg] (rotate cfg (proto/list-backups (:backup-set cfg)))) | ||
([cfg all-archives] | ||
(->> (:prefixes cfg) | ||
(mapcat (fn [prefix] | ||
(let [archives (find-archives-with-prefix prefix all-archives) | ||
res (rotate/rotate-backups archives :datetime (:rotate cfg))] | ||
(println (str (pr-str prefix) ": " (count res) " archives, destroying " (count (filter rotate/drop? res)))) | ||
res (rotate/rotate-backups archives :datetime (:rotate cfg))] | ||
(println (str (pr-str prefix) | ||
": " (count res) | ||
" archives, destroying " (count (filter rotate/drop? res)))) | ||
res)))))) | ||
|
||
(defn run | ||
[bs] | ||
(println (pr-str bs)) | ||
(let [archives (rotate bs) | ||
destroy (filter rotate/drop? archives)] | ||
destroy (filter rotate/drop? archives)] | ||
;; (println (count archives) "archives, destroying" (count destroy)) | ||
;; (pprint/pprint archives) | ||
(doseq [d destroy] | ||
(proto/destroy-backup (:backup-set bs) d)))) | ||
(doseq [d destroy] (proto/destroy-backup (:backup-set bs) d)))) | ||
|
||
(defn tarsnap-reader | ||
[m] | ||
(-> m | ||
(dissoc :configfile) | ||
(assoc :backup-set (tarsnap/->TarsnapBackups (:configfile m))))) | ||
(-> m (dissoc :configfile) (assoc :backup-set (tarsnap/->TarsnapBackups (:configfile m))))) | ||
|
||
(defn zfs-reader | ||
[m] | ||
(-> m | ||
(assoc :backup-set (zfs/->ZFS)))) | ||
(defn zfs-reader [m] (-> m (assoc :backup-set (zfs/->ZFS)))) | ||
|
||
(def custom-readers | ||
{:readers {'prune-backups/tarsnap tarsnap-reader | ||
'prune-backups/zfs zfs-reader}}) | ||
{:readers {'prune-backups/tarsnap tarsnap-reader, 'prune-backups/zfs zfs-reader}}) | ||
|
||
(defn -main [& args] | ||
(try | ||
(let [{:keys [help version config]} (cli/parse-opts args | ||
{:spec spec | ||
:restrict (keys spec)})] | ||
(when version | ||
(println meta/version) | ||
(System/exit 0)) | ||
(when help | ||
(print-help)) | ||
(defn -main | ||
[& args] | ||
(try (let [{:keys [help version config]} (cli/parse-opts args | ||
{:spec spec, :restrict (keys spec)})] | ||
(when version (println meta/version) (System/exit 0)) | ||
(when help (print-help)) | ||
|
||
(->> config slurp (edn/read-string custom-readers) run)) | ||
(System/exit 0) | ||
(finally | ||
(shutdown-agents)))) | ||
(->> config slurp (edn/read-string custom-readers) run)) | ||
(System/exit 0) | ||
(finally (shutdown-agents)))) | ||
|
||
(when (= *file* (System/getProperty "babashka.file")) | ||
(apply -main *command-line-args*)) | ||
(when (= *file* (System/getProperty "babashka.file")) (apply -main *command-line-args*)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
(ns prune-backups.proto) | ||
|
||
(defprotocol BackupSet | ||
(list-backups [this] "list all backups") | ||
(destroy-backup [this backup] "destroy the given backup")) | ||
(list-backups [this] | ||
"list all backups") | ||
(destroy-backup [this backup] | ||
"destroy the given backup")) |
This file contains 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
Oops, something went wrong.