The ability of rsync to replace write-protected files (e.g., files with a-w permissions) during a backup relies on how Unix/Linux filesystem permissions work, combined with rsync's strategy of atomic updates using temporary files. Here's a breakdown of how it works:
1. Filesystem Permissions: Files vs. Directories
- File permissions (
rw- for owner/group/others) control whether the file's contents can be modified.
- Directory permissions control whether you can:
- Create new files (write permission).
- Delete/rename files (write + execute permission).
If a file is read-only (e.g., -r--r--r--), you cannot modify its contents directly. However, you can delete or rename it if you have write/execute permissions on the directory containing the file.
2. How rsync Updates Files
rsync does not modify files in place. Instead, it uses an atomic update workflow:
Step-by-Step Process:
-
Create a Temporary File
rsync writes the updated content to a new temporary file in the destination directory (e.g., .filename.tmp).
Example:
.document.txt.tmp
-
Set Permissions on the Temporary File
If --chmod=a-w is used, rsync strips write permissions from the temporary file before finalizing it.
-
Rename the Temporary File
rsync atomically replaces the old file with the new one using rename():
mv .document.txt.tmp document.txt
- This operation does not require write access to the old file (
document.txt), only to the directory containing it.
- The
rename() operation unlinks the old file (breaking hard links) and points the filename to the new inode.
3. Why This Works with Write-Protected Files
Let’s say latest/document.txt has permissions -r--r--r-- (no write access for anyone). Even though the file itself is read-only, rsync can still replace it because:
- The directory containing
document.txt (e.g., /snapshots/username/latest/) is still writable to the user running rsync.
- The
rename() operation only requires directory-level write/execute permissions, not file-level write permissions.
4. Example Scenario
Suppose you have this file structure:
/snapshots/username/latest/document.txt # Read-only (-r--r--r--)
/snapshots/username/2024-05-20/document.txt # Hard-linked to the same inode
When rsync detects a change in the source (/home/username/files/document.txt), it does the following:
-
Creates a temporary file in latest/:
/snapshots/username/latest/.document.txt.tmp # New inode, read-only
-
Atomically replaces the old file:
mv /snapshots/username/latest/.document.txt.tmp \
/snapshots/username/latest/document.txt
- The old inode (shared with
2024-05-20/document.txt) is preserved.
- The new inode becomes part of
latest/document.txt.
5. Why Snapshots Remain Unchanged
Because rsync uses rename() instead of in-place updates:
- The historical snapshot (
2024-05-20/document.txt) still points to the original inode (unchanged).
- The
latest/document.txt now points to the new inode (updated content).
- Hard links are broken automatically, preserving the integrity of snapshots.
6. Script Safety with chmod u-w
In your script:
OPTS="... --chmod=a-w"
chmod u-w $SNAP/$DATETAG
--chmod=a-w ensures all files in latest/ are read-only.
chmod u-w $SNAP/$DATETAG makes the snapshot directory read-only for the owner.
This adds extra protection:
- Prevents accidental modification of files in
latest/ or snapshots.
- Ensures
rsync can still update latest/ because the directory itself remains writable (only the files inside it are read-only).
Summary
rsync can replace write-protected files because:
- It creates a new temporary file in the destination directory.
- It uses
rename() to swap the new file into place, which only requires directory-level write permissions.
- The original file’s permissions (e.g., read-only) do not block this process, as the file is not modified in place.
This approach ensures safe, atomic updates while preserving historical snapshots via hard links.