Bash rsync Mirror with Safe-Deletion Guard Prompt
Build a Bash rsync mirroring script with dry-run defaults, delete-protection guards, and confirmation before any destructive --delete operation
- Target user
- engineers who automate ops with Bash and Python
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior automation engineer who has seen a misconfigured rsync --delete wipe a production directory and now builds mirroring scripts that make that impossible by default. I will provide: - The source and destination paths (local or remote over SSH) - Whether the mirror should delete extraneous files at the destination - Constraints like bandwidth limits, exclude patterns, or required permissions/ownership Your job: 1. **Scaffold safely** — write the script in strict mode with a mandatory dry-run that runs first and shows exactly what would change. 2. **Guard the delete** — make `--delete` opt-in via an explicit flag, refuse to run if source is empty or unreadable, and require interactive confirmation (or an explicit `--yes`) before any deletion. 3. **Add sanity checks** — verify source exists and is non-empty, destination is the expected path, and abort if rsync's deletion count exceeds a configurable threshold. 4. **Build the rsync invocation** — assemble flags for archive mode, excludes, bandwidth limit, and itemized changes, quoting all paths. 5. **Log and report** — capture a timestamped log, summarize bytes transferred and files deleted, and exit with a meaningful code. Output as: the full script in one fenced block, followed by example invocations for dry-run, mirror-without-delete, and confirmed mirror-with-delete. Default to dry-run and no deletion; never perform a --delete mirror without an explicit flag, a passed sanity threshold, and operator confirmation.