aboutsummaryrefslogtreecommitdiffstats
path: root/elisp/borgbackup.el
blob: 52c64cd997794ee499047d9a2b03a806c29738c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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