← Back to writing

Technical note · python / backup / sha256

Compare a source and backup with Backup Compare

Use the read-only Python folder comparator, understand exit codes, empty-directory semantics and safety limits, and download the source.

Backup Compare examines the question in Monday's article: the backup folder exists, but which paths and contents match the source? This small read-only command-line tool uses the Python standard library. It does not copy, delete, repair or back up files.

Python 3.9+ is the target; no additional Python package is needed. All regular files on both sides are read in 1 MiB chunks for SHA-256. Hashes are compared at shared paths; do not assume that only shared files are read.

Review the implementation, tests and README files in the GitHub repository and source code. The ZIP below remains an additional download option.

Choose the folders and working directory

The source is the reference; the backup is the second folder being checked. Both roots must be existing, separate directories, neither identical nor nested. Do not use “..” root components. Wait for copying or synchronization to finish and pause writers when practical.

Get the code from GitHub or the additional ZIP below. Open a terminal in the project root containing backup_compare.py and test_backup_compare.py. The commands below assume that working directory. Replace SOURCE and BACKUP with the absolute paths you selected, keeping spaces inside quotes.

sh
python3 --version
python3 backup_compare.py "SOURCE" "BACKUP"
python3 backup_compare.py "SOURCE" "BACKUP" --json

Example for Windows PowerShell when the Python launcher is available:

powershell
py -3 backup_compare.py "C:\Example\Source" "D:\Example\Backup" --json
$LASTEXITCODE

In Bash, inspect echo $? immediately after the command. JSON is written to the terminal. If redirecting it to a file, keep that file outside both compared roots. Start with synthetic files.

Understand the lists

  • missing: present in the source, absent from the backup; may include file or directory paths.
  • extra: present only in the backup; not an automatic deletion instruction.
  • different: content or file/directory type differs at a shared relative path.
  • same: regular files with matching SHA-256 hashes; excludes directories.

Empty directories are classified separately under empty_directories. Here, missing means an empty source path is not also an empty directory in the backup. If both sides contain folder but the backup folder is nonempty, the top-level missing can be empty while empty_directories.missing contains folder. extra is the reverse case; . denotes the root.

Read completeness first

Exit 0 means the comparison completed without differences in scope. Exit 1 means it completed and found differences. Exit 2 means an error or incomplete check and takes precedence over differences.

Read complete, issues and the exit code together. All same lists are cleared in an incomplete run. Definite missing/extra claims are suppressed for affected subtrees; other findings are partial. Invalid command-line usage also exits 2 without a JSON report.

Detected links/reparse points, special files and unreadable entries make the check incomplete. Ancestor components of root paths are checked too. POSIX uses component-wise descriptor opening with O_NOFOLLOW protection.

On Windows and other fallback opening paths, a race can remain between checking for links and opening the path. There is no promise that links can never be followed under every condition. This is not a security boundary for a maliciously changing filesystem.

The tool checks for observed file and directory changes around reading, but does not create an atomic snapshot. Unobserved changes or changes after the final check can escape detection. Use stable folders you trust; stronger assurance requires fixed snapshots.

Scope of the evidence

Reads may update access times. The comparison excludes ACLs/permissions, ownership, xattrs, alternate streams/resource forks, sparse allocation and hardlink topology. Very deep trees can return incomplete/exit 2; result memory grows with entry count.

Matching hashes rely on SHA-256's usual collision assumption. A match does not guarantee restoration. Open important files in a separate test location and exercise the actual restore process as well.

Source code and tests

GitHub repository: BackupCompare. The implementation, synthetic tests and current TR/EN documentation are maintained here. The 19 October Linux test evidence records the production source hash and the 20/20 + 7/7 result.

Additional ZIP download

Download the Backup Compare source and usage package. The ZIP contains only tool source, synthetic tests and usage documentation. Extract it and open a terminal in backup-compare:

sh
python3 -B test_backup_compare.py
python3 -B review/test_review.py

Existing local evidence shows 20 development and 7 independent tests passing on Linux/Python 3.14.8. TESTING.md includes the source SHA-256. There is no successful Windows/macOS or Python 3.9 execution record; a target platform is not a tested platform.

Tests use temporary synthetic directories only. Before checking your files, verify the source/backup order and keep reports outside both roots. Review private filenames and paths before sharing a report.

← Back to writingmuhammet.saridogan.work@gmail.com