CMD Guide
HomeDatabasesSQL Fundamentals

Backup Database

What a database backup actually is

A backup is a recoverable copy you can fall back to when replication can't save you — a bad migration, an accidental DELETE, data corruption, or ransomware (replicas faithfully replicate the disaster). Two fundamentally different kinds, and the difference matters:

Logical backupPhysical backup
WhatExports the data as SQL/rowsCopies the on-disk data files
Toolsmysqldump, pg_dumppg_basebackup, Percona XtraBackup, SQL Server BACKUP DATABASE
RestoreRe-runs the SQL (slow on big DBs)Drop files back in place (fast)
Portable across versions/engines?YesNo — engine/version-specific
A base backup plus a continuous WAL/binlog log; recovery replays the log up to a chosen timestamp
A base backup plus a continuous WAL/binlog log; recovery replays the log up to a chosen timestamp

Full vs incremental, and the real power: point-in-time recovery

A full backup is everything; incremental/differential backups capture only what changed since the last one (cheaper, faster). But the capability that actually saves you is point-in-time recovery (PITR): keep a base backup plus the continuous transaction log (WAL in Postgres, binlog in MySQL), and you can restore to any instant — e.g. "the moment before the bad DELETE at 14:32" — by replaying the log up to that timestamp. This is the same WAL from the Transactions lesson, archived.

Correct, real commands

# Logical (portable)
mysqldump -u root -p mydb > mydb.sql            # MySQL
# Prefer InnoDB-consistent logical dump (no long table locks):
mysqldump -u root -p --single-transaction --routines --triggers mydb > mydb.sql
# --single-transaction starts a transaction and dumps from a consistent snapshot
# for InnoDB; without it, concurrent writes can make tables disagree mid-dump.
pg_dump mydb > mydb.sql                          # PostgreSQL (plain SQL)
pg_dump -Fc mydb > mydb.dump                     # PostgreSQL custom format (for pg_restore)
# Postgres dumps are MVCC-consistent by default for ordinary heap tables; still
# schedule them, encrypt them, and test restore — consistency ≠ tested recovery.

# Physical
pg_basebackup -D /backup -Ft -X stream           # PostgreSQL base backup + WAL
BACKUP DATABASE mydb TO DISK = 'D:\mydb.bak';     # SQL Server (real T-SQL)
Scheduling is not part of SQL. There is no BACKUP DATABASE ... WITH SCHEDULE='Daily at 2 AM' — you schedule backups with cron/pg_cron, SQL Server Agent, or your managed service's snapshot feature.

Pitfalls

Takeaways


Re-authored for correctness for this guide (the prior version used invented BACKUP … WITH SCHEDULE syntax). Follows the PostgreSQL & MySQL backup docs. See also: Transactions & ACID (the WAL), RPO/RTO & disaster recovery, Replication.

🎯 STRICT STANDOUT: Why / worked / when-not / failure / drills — Backup Database

Why this concept exists (judgment chain)

Backups exist for disasters replication cannot fix (bad DELETE, corruption, ransomware). Logical vs physical, full vs incremental, and PITR (base + WAL/binlog) are different tools; an untested backup is not a backup. Scheduling is ops, not SQL syntax.

Worked example with numbers or traced steps

Logical: pg_dump / mysqldump → portable SQL, slow restore at scale.
Physical: pg_basebackup / XtraBackup → fast, version-bound.
PITR: base Sun 02:00 + WAL → restore to Tue 14:32:05 before bad DELETE.
RPO target 5 min ⇒ archive WAL frequently enough; RTO ⇒ rehearse restore time.
3-2-1: 3 copies, 2 media types, 1 off-site; encrypt backups.

When NOT to use / named alternative

Do not treat replicas as backups — they apply the same DELETE. Do not invent BACKUP … WITH SCHEDULE SQL. Prefer logical dumps for small DBs/migrations; physical+PITR for large production. Skip continuous archiving only if you accept RPO = last full backup age.

Failure / ops fingerprint

Fingerprint: restore fails first time tried; binlog purge before base backup age; backup files unencrypted on object storage. Ops: quarterly restore game day; monitor backup age and WAL archive lag; alert if last successful backup > SLO.

Hostile-panel drills (defend the decision)

Q1. Logical vs physical backup?
Model answer: Logical exports rows/SQL (portable, slower restore); physical copies data files (fast, engine-specific).

Q2. What is PITR?
Model answer: Restore base backup then replay WAL/binlog to a chosen timestamp — recover to just before a mistake.

Q3. Why replication ≠ backup?
Model answer: Replicas copy corruption and deletes instantly; backups provide point-in-time independent history.

🤖 Don't fully get this? Learn it with Claude

Stuck on Backup Database? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Backup Database** (Databases) and want to truly understand it. Explain Backup Database from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Backup Database** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Backup Database** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Backup Database** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes