Emacs backup file recovery and manual integrity verification
0 reputation · 26 Feb 2021, 20:40 UTC
0 reputation · 26 Feb 2021, 20:40 UTC
Restore a buffer from an Emacs backup or auto-save file and confirm that the recovered content is complete before saving it over the original file.
Emacs writes tilde backups and #file# auto-save files according to make-backup-files, version-control, auto-save-default, and backup-directory-alist. recover-file can visit a newer auto-save, but Emacs does not automatically checksum, validate, or sandbox recovered data. Permissions follow the process umask and directory settings, so backups in shared locations may be readable by others. Numbered backup rotation can remove older revisions. Behavior varies by Emacs release.
Because no built-in integrity gate exists, the user must decide when a backup is trustworthy.
35275 reputation · 27 Feb 2021, 01:16 UTC
Use C-x C-w (write-file) to overwrite the current buffer with the auto-save contents, or M-x recover-file to visit the most recent auto-save file in a new buffer. Both commands are built-in and require no additional configuration.
The following variables determine where Emacs stores backup and auto-save files:
make-backup-files – When non-nil, creates filename~ tilde backups.version-control – When non-nil, enables numbered backups (e.g., filename~1~, filename~2~).backup-directory-alist – A list of cons cells mapping directory patterns to backup locations. Useful for centralizing backups outside project directories.auto-save-default – When non-nil, creates #filename# auto-save files.auto-save-file-name-transforms – Controls where auto-save files are written, similar to backup-directory-alist.Emacs does not enforce custom permissions on backup files. Files inherit the umask of the Emacs process and the permissions of the containing directory. In shared environments, backup files may be readable by other users. To restrict access:
umask 077).backup-directory-alist to point to ~/.emacs.d/backups or similar.before-save-hook function to call set-file-modes on the backup after creation.Emacs provides no built-in mechanism to require content comparison before recover-file replaces the buffer. Perform manual verification as follows:
M-x diff-buffer-with-file on the backup file (filename~ or #filename#) to compare the current buffer with the file on disk.C-x C-s.Teams should establish protocols for verifying recovered data:
sha256sum filename~ or shasum -a 256 filename~.700 for user-only access).kept-old-versions and kept-new-versions control how many numbered backups are retained.Before relying on recovery, confirm your configuration:
C-h v make-backup-files RET
C-h v backup-directory-alist RET
C-h v auto-save-default RET
Create a test file, make edits, kill the buffer without saving, then run M-x recover-file to verify the process works in your environment.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.