aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--elisp/borgbackup.el88
1 files changed, 88 insertions, 0 deletions
diff --git a/elisp/borgbackup.el b/elisp/borgbackup.el
new file mode 100644
index 0000000..52c64cd
--- /dev/null
+++ b/elisp/borgbackup.el
@@ -0,0 +1,88 @@
+;;; borgbackup.el --- Borg backup client -*- lexical-binding: t; -*-
+;;
+;; Copyright (C) 2023 Óscar Nájera
+;;
+;; Author: Óscar Nájera <hi@oscarnajera.com>
+;; Maintainer: Óscar Nájera <hi@oscarnajera.com>
+;; Created: October 26, 2023
+;; Modified: October 26, 2023
+;; Version: 0.0.1
+;; Homepage: https://git.oscarnajera.com/dotfiles/tree/elisp/borgbackup.el
+;; Package-Requires: ((emacs "27.1"))
+;;
+;; This file is not part of GNU Emacs.
+;;
+;;; Commentary:
+;;
+;; Description
+;;
+;;; Code:
+
+(require 'dash)
+
+(defcustom borgbackup-repos '(("ssh://borgbackup@sarah/./repos/ingrid" "Admin/sarah/borg/ingrid")
+ ("ssh://borgbackup@sarah/./repos/oscar" "Admin/sarah/borg/oscar")
+ ("ssh://backup/media/Backup/daily_backup/" "borgbackup"))
+ "List of lists where each entry has the repo path and then the pass entry."
+ :type list
+ :group 'borgbackup)
+
+(defun borgbackup--cmd (cmd repo pass-path &rest args)
+ (let ((process-environment (list (concat "BORG_PASSCOMMAND=pass show " pass-path)
+ (concat "SSH_AUTH_SOCK=" (getenv "SSH_AUTH_SOCK"))
+ (concat "HOME=" (getenv "HOME"))))
+ (err-file (make-temp-file "borg-err")))
+ (with-temp-buffer
+ (apply #'call-process "borg" nil (list (current-buffer) err-file) nil
+ cmd repo args)
+ (if (= 0 (file-attribute-size (file-attributes err-file))) ;; no error
+ (buffer-string)
+ (user-error (find-file err-file))))))
+
+(-let (((&plist :archives)
+ (json-parse-string
+ (borgbackup--cmd "list"
+ "ssh://borgbackup@sarah/./repos/ingrid"
+ "Admin/sarah/borg/ingrid" "--json")
+ :object-type 'plist)))
+ archives)
+
+(defun borgbackup-info (repo pass-path)
+ (interactive
+ (thread-first
+ (completing-read "Which repo do you want: " borgbackup-repos)
+ (assoc borgbackup-repos #'string=)))
+ (message
+ (borgbackup--cmd "info" repo pass-path)))
+
+(defun borgbackup--list (repo pass-path)
+ (-let (((&plist :archives)
+ (json-parse-string
+ (borgbackup--cmd "list" repo pass-path "--json")
+ :object-type 'plist)))
+
+ (with-current-buffer (get-buffer-create "*borg*")
+ (tabulated-list-mode)
+ (setq tabulated-list-format [("Archive" 40 t)
+ ("Date" 27 t)
+ ("ID" 64 t)])
+ (setq tabulated-list-padding 2)
+ (setq tabulated-list-entries
+ (cl-map 'list
+ (lambda (row)
+ (-let (((&plist :archive :time :id) row))
+ (list row (vector archive time id))))
+ archives))
+ (tabulated-list-init-header)
+ (tabulated-list-print)
+ (display-buffer (current-buffer)))))
+
+(defun borgbackup-list (repo pass-path)
+ (interactive
+ (thread-first
+ (completing-read "Which repo do you want: " borgbackup-repos)
+ (assoc borgbackup-repos #'string=)))
+ (borgbackup--list repo pass-path))
+
+(provide 'borgbackup)
+;;; borgbackup.el ends here