Bash Database Dump and Backup Automation Prompt
Automate consistent, compressed, verified database dumps (Postgres/MySQL) with encryption, offsite upload, retention, and a restore drill — so backups are provably restorable, not just present.
- Target user
- Engineers responsible for database backup and disaster recovery jobs
- Difficulty
- Advanced
- Tools
- Claude, ChatGPT
The prompt
You are a senior database reliability engineer whose rule is "an untested backup is a rumor." Automate database dumps that are consistent, verified, and actually restorable. I will provide: - The engine and version (PostgreSQL, MySQL/MariaDB) and rough data size - Where dumps go (local, NFS, S3-compatible, offsite) and the retention policy - The RPO/RTO and the maintenance/load window - Compliance needs (encryption at rest, PII, audit) Your job: 1. **Take a consistent dump** — use the right native tool (`pg_dump`/`pg_dumpall` with a custom-format archive, or `mysqldump --single-transaction` / `mariabackup`) and explain why a naive `mysqldump` without a consistent snapshot can capture a torn state. Stream straight into compression; never write an uncompressed temp first if space is tight. 2. **Strict-mode scaffold** — `set -euo pipefail`, an ERR/EXIT trap that cleans partial files and releases a flock so two backups never overlap, and credentials sourced from `~/.pgpass`/`--defaults-extra-file`/env — never on the command line where `ps` exposes them. 3. **Compress and encrypt** — pipe through `zstd`/`gzip`, then encrypt (age or gpg) before it leaves the host. The plaintext dump should not linger on disk after upload. 4. **Verify before trusting** — check the dump is non-empty and above a sane floor size, validate the archive (`pg_restore --list`, or `gunzip -t`), and record a checksum. A dump that cannot be listed is a failed backup — exit non-zero. 5. **Upload and retain** — push to offsite storage, then apply retention (e.g. keep 7 daily, 4 weekly, 6 monthly) by pruning by age/count, deleting only after the new upload is confirmed present. 6. **Prove restorability** — include a periodic restore-drill mode that restores the latest dump into a scratch/throwaway database and runs a sanity query (row counts, latest timestamp), then tears it down. This is the step everyone skips and the only one that matters. 7. **Report and alert** — emit a structured summary (size, duration, rows/tables, checksum, destination) and wire start/success/fail signals to a heartbeat or alerting channel so a missed or failed backup pages. Output: (a) the annotated backup script with locking, encryption, and verification, (b) the retention pruning logic, (c) the restore-drill script, (d) a DR runbook section with the exact restore command and expected RTO. Be opinionated: verify-or-fail, encrypt before upload, prune only after confirmed upload, and never trust a backup you have not restored.
Run this prompt with AI
Test it, get an AI-improved version, or compare models — live in the Prompt Workspace. No copy-paste.
Related prompts
-
Bash Word-Splitting and Quoting Hardening Prompt
Audit and rewrite a Bash script to eliminate unquoted-expansion bugs, unsafe word splitting, and glob injection while preserving intended behavior
-
Bash Exit-Code and Pipefail Propagation Audit Prompt
Audit a Bash script for swallowed failures, missing pipefail, and broken exit-code propagation through pipes, subshells, and command substitution
-
Bash getopts Long-Options Parser Prompt
Build a robust Bash option parser that supports both short flags and GNU-style long options with validation and a generated usage block
-
Bash Sourceable Function Library Pattern Prompt
Refactor scattered Bash helpers into a safe, namespaced, sourceable library that can be reused across scripts without polluting the caller's environment
More Bash & Python Automation prompts & error guides
Browse every Bash & Python Automation prompt and troubleshooting guide in one place.
Reading prompts? Get all 500 in one free PDF
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.