Skip to main content
;;;; Create versioned backup file on every save into date hierachy  

;;;; Create YYYY-MM-DD subfolder and backup filename    
;;;; Based on: https://emacs.stackexchange.com/questions/28124/change-naming-scheme-for-backup-files/28145#28145    
(defun my-make-backup-file-name (FILE)  
    (let ((dirname (concat "~/Documents/Emacs/Emacs Backups/" (format-time-string "%Y-%m-%d/"))))   
    (setq backup-directory-alist '(("" . dirname))) 

    (if (not (file-exists-p dirname)) (make-directory dirname t))   

    (setq filenameWithExtension (file-name-nondirectory FILE))  
    (setq filenameWithoutExtension (file-name-sans-extension filenameWithExtension))    
    (setq filenameExtension (file-name-extension filenameWithExtension))    
    (concat dirname filenameWithoutExtension " (" (format-time-string "%Y-%m-%d %H-%M-%S-%3N") ")." filenameExtension)      
))  

(setq make-backup-file-name-function #'my-make-backup-file-name)    

;;;; Create versioned backup file on every save 
;;;; Based on: https://www.emacswiki.org/emacs/ForceBackups 
(setq version-control 'never    
    vc-make-backup-files t  
    backup-by-copying t)    

(defun force-backup-of-buffer ()    
    (when (not buffer-backed-up)    
        (setq buffer-backed-up t))  
    (let ((buffer-backed-up nil))   
        (backup-buffer))    
)   

(add-hook 'before-save-hook  'force-backup-of-buffer)   
;;;; Create versioned backup file on every save into date hierachy  

;;;; Create YYYY-MM-DD subfolder and backup filename    
;;;; Based on: https://emacs.stackexchange.com/questions/28124/change-naming-scheme-for-backup-files/28145#28145    
(defun my-make-backup-file-name (FILE)  
    (let ((dirname (concat "~/Documents/Emacs/Emacs Backups/" (format-time-string "%Y-%m-%d/"))))   
    (setq backup-directory-alist '(("" . dirname))) 

    (if (not (file-exists-p dirname)) (make-directory dirname t))   

    (setq filenameWithExtension (file-name-nondirectory FILE))  
    (setq filenameWithoutExtension (file-name-sans-extension filenameWithExtension))    
    (setq filenameExtension (file-name-extension filenameWithExtension))    
    (concat dirname filenameWithoutExtension " (" (format-time-string "%Y-%m-%d %H-%M-%S-%3N") ")." filenameExtension)      
))  

(setq make-backup-file-name-function #'my-make-backup-file-name)    

;;;; Create versioned backup file on every save 
;;;; Based on: https://www.emacswiki.org/emacs/ForceBackups 
(setq version-control 'never    
    vc-make-backup-files t  
    backup-by-copying t)    

(defun force-backup-of-buffer ()    
    (when (not buffer-backed-up)    
        (setq buffer-backed-up t))  
    (let ((buffer-backed-up nil))   
        (backup-buffer))    
)   

(add-hook 'before-save-hook  'force-backup-of-buffer)   
;;;; Create versioned backup file on every save into date hierachy  

;;;; Create YYYY-MM-DD subfolder and backup filename    
;;;; Based on: https://emacs.stackexchange.com/questions/28124/change-naming-scheme-for-backup-files/28145#28145    
(defun my-make-backup-file-name (FILE)  
    (let ((dirname (concat "~/Documents/Emacs/Emacs Backups/" (format-time-string "%Y-%m-%d/"))))   
    (setq backup-directory-alist '(("" . dirname))) 

    (if (not (file-exists-p dirname)) (make-directory dirname t))   

    (setq filenameWithExtension (file-name-nondirectory FILE))  
    (setq filenameWithoutExtension (file-name-sans-extension filenameWithExtension))    
    (setq filenameExtension (file-name-extension filenameWithExtension))    
    (concat dirname filenameWithoutExtension " (" (format-time-string "%Y-%m-%d %H-%M-%S-%3N") ")." filenameExtension)      
))  

(setq make-backup-file-name-function #'my-make-backup-file-name)    

;;;; Create versioned backup file on every save 
;;;; Based on: https://www.emacswiki.org/emacs/ForceBackups 
(setq version-control 'never    
    vc-make-backup-files t  
    backup-by-copying t)    

(defun force-backup-of-buffer ()    
    (when (not buffer-backed-up)    
        (setq buffer-backed-up t))  
    (let ((buffer-backed-up nil))   
        (backup-buffer))    
)   

(add-hook 'before-save-hook  'force-backup-of-buffer)   
;;;; Create versioned backup file on every save into date hierachy  

;;;; Create YYYY-MM-DD subfolder and backup filename    
;;;; Based on: https://emacs.stackexchange.com/questions/28124/change-naming-scheme-for-backup-files/28145#28145    
(defun my-make-backup-file-name (FILE)  
    (let ((dirname (concat "~/Documents/Emacs/Emacs Backups/" (format-time-string "%Y-%m-%d/"))))   
    (setq backup-directory-alist '(("" . dirname))) 

    (if (not (file-exists-p dirname)) (make-directory dirname t))   

    (setq filenameWithExtension (file-name-nondirectory FILE))  
    (setq filenameWithoutExtension (file-name-sans-extension filenameWithExtension))    
    (setq filenameExtension (file-name-extension filenameWithExtension))    
    (concat dirname filenameWithoutExtension " (" (format-time-string "%Y-%m-%d %H-%M-%S-%3N") ")." filenameExtension)      
))  

(setq make-backup-file-name-function #'my-make-backup-file-name)    

;;;; Create versioned backup file on every save 
;;;; Based on: https://www.emacswiki.org/emacs/ForceBackups 
(setq version-control 'never    
    vc-make-backup-files t  
    backup-by-copying t)    

(defun force-backup-of-buffer ()    
    (when (not buffer-backed-up)    
        (setq buffer-backed-up t))  
    (let ((buffer-backed-up nil))   
        (backup-buffer))    
)   

(add-hook 'before-save-hook  'force-backup-of-buffer)   
Source Link
Pete
  • 41
  • 3

Got it. This code creates a backup file on each save. File path example: ~/Documents/Emacs/Emacs Backups/2022-11-04/todo (2022-11-04 00-28-00-674).org.

;;;; Create versioned backup file on every save into date hierachy  

;;;; Create YYYY-MM-DD subfolder and backup filename    
;;;; Based on: https://emacs.stackexchange.com/questions/28124/change-naming-scheme-for-backup-files/28145#28145    
(defun my-make-backup-file-name (FILE)  
    (let ((dirname (concat "~/Documents/Emacs/Emacs Backups/" (format-time-string "%Y-%m-%d/"))))   
    (setq backup-directory-alist '(("" . dirname))) 

    (if (not (file-exists-p dirname)) (make-directory dirname t))   

    (setq filenameWithExtension (file-name-nondirectory FILE))  
    (setq filenameWithoutExtension (file-name-sans-extension filenameWithExtension))    
    (setq filenameExtension (file-name-extension filenameWithExtension))    
    (concat dirname filenameWithoutExtension " (" (format-time-string "%Y-%m-%d %H-%M-%S-%3N") ")." filenameExtension)      
))  

(setq make-backup-file-name-function #'my-make-backup-file-name)    

;;;; Create versioned backup file on every save 
;;;; Based on: https://www.emacswiki.org/emacs/ForceBackups 
(setq version-control 'never    
    vc-make-backup-files t  
    backup-by-copying t)    

(defun force-backup-of-buffer ()    
    (when (not buffer-backed-up)    
        (setq buffer-backed-up t))  
    (let ((buffer-backed-up nil))   
        (backup-buffer))    
)   

(add-hook 'before-save-hook  'force-backup-of-buffer)