4

Is there a command for recover the current file from the backup ?

I have a file ~/temp.txt and some backup files !home!djangoliv!temp.txt.~1~ ... !home!djangoliv!temp.txt.~5~, !home!djangoliv!temp.txt.~6~.

I would like to load backup files from the newer version to the older one (like git-time-machine for exemple)

Here my backup configuration

  (setq auto-save-default nil)
  (setq backup-directory-alist '(("." . "~/.emacs-backup-files/")))
  (setq version-control t       
        vc-make-backup-files t  
        backup-by-copying t     
        delete-old-versions -1) 
5
  • There's diff-backup, from which you could then reverse apply the patch to recover, but that command always uses the latest backup file.
    – p_wiersig
    Commented Dec 6, 2016 at 14:26
  • Thanks, not the complete answer, but interesting
    – djangoliv
    Commented Dec 6, 2016 at 14:32
  • github.com/lewang/backup-walker ?
    – phils
    Commented Dec 6, 2016 at 21:49
  • @phis Thanks backup-walker do the tricks
    – djangoliv
    Commented Dec 7, 2016 at 12:20
  • I've now added that as an answer, as this question just got bumped.
    – phils
    Commented Mar 6, 2017 at 23:26

2 Answers 2

6

Le Wang's backup-walker library facilitates this:

traverse incremental diffs between backup versions

  • open backup in traversal mode if the diff seems interesting

traverse backups

  • Once a backup is opened, traversing amongst backups is easy using the same keys.
  • the point kept the same as much as possible while traversing backups as much as possible by parsing diff output on the fly.

blame

  • find the version of backup where a line disappeared. Of course this works best if you have comprehensive backup history.

cleanup

  • optionally kill all open backups when quiting
2

How about this:

(defun revert-to-backup ()
  "Reverts to the latest backup file associated with the current buffer."
  (interactive)
  (let ((file (buffer-file-name)))
    (when file
      (let ((bak (file-newest-backup (buffer-file-name))))
        (if bak
            (progn
              (delete-region (point-min) (point-max))
              (insert-file-contents bak))
          (message "No backups found!"))))))

With this command you can revert the current buffer to its latest backup. The revert is not saved automatically, I'm assuming you might want to at least look at the content before hitting C-x C-s to save.

1
  • Thanks, but I want to go through all the backups from the most recent to the oldest.
    – djangoliv
    Commented Dec 7, 2016 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.