Advertisement
Guest User

Untitled

a guest
May 27th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1.  
  2. (defun snapshot-timemachine--osx-timemachine-parse-date (date)
  3. "Parse a DATE like \"2016-05-27-140240\" as a time object."
  4. (let* ((two "\\([0-9][0-9]\\)")
  5. (time-re
  6. (format "\\([0-9]\\{4\\}\\)-%s-%s-%s%s%s" two two two two two)))
  7. (cl-macrolet ((m (n) `(string-to-number (match-string ,n date))))
  8. (string-match time-re date)
  9. (encode-time (m 6) (m 5) (m 4) (m 3) (m 2) (m 1)))))
  10.  
  11.  
  12. (defun snapshot-timemachine-osx-timemachine-snapshot-finder (file)
  13. "Find snapshots of FILE made by Time Machine (OS X only).
  14. Uses the tmutil utility to get a list of Time Machine backups.
  15.  
  16. For example, say FILE is
  17. \"/home/thomas/.emacs.\"
  18.  
  19. And tmutil listbackups returns:
  20. \"/Volumes/TM/Backups.backupdb/mac/2015-12-23-120613\"
  21. \"/Volumes/TM/Backups.backupdb/mac/2016-01-03-151443\"
  22. ...
  23.  
  24. The snapshots of the file will be
  25. \"/Volumes/TM/Backups.backupdb/mac/2015-12-23-120613/ssd/home/thomas/.emacs\"
  26. \"/Volumes/TM/Backups.backupdb/mac/2016-01-03-151443/ssd/home/thomas/.emacs\"
  27. ...
  28.  
  29. Note the presence of \"ssd\", the name of the boot disk."
  30. ;; TODO check if Time Machine is enabled, not just if we're on OS X
  31. (if (not (executable-find "tmutil"))
  32. (error "Time Machine not found")
  33. (let ((backup-list (shell-command-to-string "tmutil listbackups"))
  34. (id 0)
  35. (file (expand-file-name file)) ;; e.g., replace ~ with /Users/user
  36. ;; TODO currently hardcoded
  37. (boot-disk "ssd"))
  38. (cl-loop for snapshot-dir in (split-string backup-list "\n" t)
  39. for date-str = (file-name-nondirectory snapshot-dir)
  40. for date = (snapshot-timemachine--osx-timemachine-parse-date
  41. date-str)
  42. for path = (format "%s/%s%s" snapshot-dir boot-disk file)
  43. when (file-exists-p path)
  44. collect (make-snapshot
  45. :id (cl-incf id)
  46. :name (number-to-string id)
  47. :file path
  48. :date date)))))
  49.  
  50. (setq snapshot-timemachine-snapshot-finder
  51. #'snapshot-timemachine-osx-timemachine-snapshot-finder)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement