Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 105

mysqldump Backup Command

mysqldump क्या है?

mysqldump command-line MySQL client है जो logical dump बनाता है। SQL-format output में normally schema objects recreate करने और rows insert करने वाले statements होते हैं। यह raw InnoDB data files copy नहीं करता।

mysqldump [connection-options] [dump-options] database [tables]
mysqldump [connection-options] [dump-options] --databases db1 db2
mysqldump [connection-options] [dump-options] --all-databases

Portability, readable SQL, database/table selection या cross-environment migration में logical dump उपयोगी है। इसे automatically complete DR solution न मानें: configuration, accounts/grants, encryption keys, binary logs और external files की separate protection चाहिए हो सकती है।

लाभसीमा
Portable, selective, reviewable outputHigh volume पर dump/replay slow
SQL objects और rows recreate करता हैRestore में indexes rebuild हो सकते हैं
Server files copy नहीं करने पड़तेConsistency engines/options पर निर्भर

Credentials Securely Configure करें

Password को command line पर -p के बाद न लिखें। वह history, process inspection या logs में leak हो सकता है। Approved secret manager use करें या host-local encrypted login path interactively बनाएँ:

mysql_config_editor set --login-path=backup \
  --host=db.example.internal \
  --user=backup_operator --password

mysql --login-path=backup -e "SELECT VERSION();"
mysqldump --login-path=backup --version

Backup account को चुने हुए objects/options के लिए minimum privileges दें। Requirements MySQL version, views, locking mode और tablespaces पर बदल सकती हैं। Dedicated least-privileged account से exact command test करें; routinely root use न करें।

Artifact security: dump में personal data, password hashes और business secrets हो सकते हैं। Permissions restrict, transfer/storage encrypt और access audit करें। Production dump public web folder या source repository में न रखें।

Common mysqldump Commands

# एक database; CREATE/USE भी output में
mysqldump --login-path=backup \
  --databases school --result-file=school.sql

# एक database की एक table
mysqldump --login-path=backup \
  school students --result-file=students.sql

# दो databases
mysqldump --login-path=backup \
  --databases school library --result-file=two_databases.sql

# सभी databases और non-default stored objects
mysqldump --login-path=backup \
  --all-databases --routines --events --triggers \
  --result-file=all_databases.sql

First command form में database name argument है, लेकिन output उसे create/select जरूरी नहीं करता। --databases बाद के names को databases मानकर database-level statements लिखता है; इससे restore command बदलता है। Output location explicit करने के लिए --result-file रखें।

Windows: PowerShell पर --result-file कुछ versions की UTF-16 redirection problem से बचाता है। Created file inspect और restore test जरूर करें।

Consistent InnoDB Dump बनाएँ

mysqldump --login-path=backup \
  --single-transaction --quick \
  --routines --events --triggers \
  --databases school \
  --result-file=school_consistent.sql

--single-transaction ऐसी transaction isolation sequence चलाता है जिससे InnoDB tables consistent snapshot से read हों। --quick entire table client memory में buffer करने की बजाय rows one-by-one retrieve करता है। InnoDB-focused logical dump के लिए यह strong default है।

  • MyISAM जैसी nontransactional tables dump के बीच बदल सकती हैं।
  • Dumped tables पर साथ में ALTER, CREATE, DROP, RENAME या TRUNCATE न चलाएँ।
  • Long snapshot पुरानी row versions retain करके storage pressure बढ़ा सकता है।
  • Tight PITR RPO के लिए binary logs फिर भी चाहिए।

Mixed-engine dump को single lock boundary चाहिए तो controlled write pause और required privileges plan करें। Production से पहले staging copy पर impact measure करें।

Schema, Data और Stored Objects चुनें

# केवल definitions, rows नहीं
mysqldump --login-path=backup \
  --no-data --databases school --result-file=school_schema.sql

# केवल rows, CREATE TABLE नहीं
mysqldump --login-path=backup \
  --no-create-info school --result-file=school_data.sql

# Recovery-relevant stored objects explicit
mysqldump --login-path=backup \
  --routines --events --triggers \
  --databases school --result-file=school_objects.sql
ObjectOption/behavior
Table definitions/dataNormally included
ViewsDefinitions; definer/security important
TriggersDefault included; explicit flag intent बताता है
Procedures/functions--routines
Scheduled events--events
Accounts/grantsSeparate reviewed plan रखें

Definition-only/data-only files review या phased migration में useful हैं, पर restore order और foreign keys plan करें। Dump executable SQL है; casual editing ownership या data बदल सकती है।

Selected Tables या Rows Dump करें

# Selected tables
mysqldump --login-path=backup school \
  students courses --result-file=academic_tables.sql

# एक table exclude करें
mysqldump --login-path=backup \
  --ignore-table=school.audit_archive \
  school --result-file=school_without_archive.sql

# WHERE match करने वाली rows
mysqldump --login-path=backup school orders \
  --where="order_date >= '2026-08-01'" \
  --result-file=recent_orders.sql

--where controlled extraction है, automatic relational subset नहीं। Parent/child rows, constraints और application invariants missing हो सकते हैं। Shell के अनुसार quote करें और predicate को पहले SELECT से test करें।

Unix-like system पर compression stream करते समय pipeline failure preserve करें:

set -o pipefail
mysqldump --login-path=backup --single-transaction \
  --quick --databases school | gzip -c > school.sql.gz
statuses=("${PIPESTATUS[@]}")
test "${statuses[0]}" -eq 0 && test "${statuses[1]}" -eq 0

Creation के बाद approved encryption/checksum tools use करें। Compressed file open होना verified restore नहीं है।

SQL Dump Restore और Verify करें

पहले isolated compatible server में restore करें। --databases या --all-databases file में database selection statements होते हैं:

mysql --login-path=restore < school_consistent.sql

# Interactive mysql client
SOURCE /approved/backup/school_consistent.sql;

mysqldump school students file में database selection न हो तो intended database deliberately create/select करें:

mysql --login-path=restore -e \
  "CREATE DATABASE school_restore CHARACTER SET utf8mb4;"
mysql --login-path=restore school_restore < students.sql
  1. Dump/restore exit codes और stderr check करें।
  2. Manifest से byte size और checksum verify करें।
  3. Warnings, SQL modes, charset और version compatibility देखें।
  4. Object inventory, row counts और business aggregates compare करें।
  5. Views, triggers, routines/events verify करें; events approval तक disabled रहें।
  6. External side effects block करके app smoke tests चलाएँ।
  7. Restore duration और recovery point record करें।
Golden rule: “mysqldump finished” job result है। “Isolated restore acceptance checks के साथ RTO में pass हुआ” recovery evidence है।

Scale, Replication और GTID संभालें

Representative data पर export और import दोनों benchmark करें। Logical restore में indexes/constraints recreate करने में समय लग सकता है। Very large database या strict RTO में one SQL file assume करने की बजाय MySQL Shell dump/loading utilities और supported physical tools compare करें।

  • DDL से अलग schedule करें; disk, I/O, replica lag और snapshot history monitor करें।
  • Destination storage अलग रखें ताकि full filesystem database crash न करे।
  • Database/table split तभी करें जब runbook dependencies preserve करे।
  • Full dump के बाद PITR के लिए binary logs रखें।

GTID environment में --set-gtid-purged=AUTO|ON|OFF|COMMENTED dump में GTID state लिखने को control करता है। सही choice target standalone, migration या replication topology पर निर्भर है। Internet से option copy न करें; DBA-approved runbook और clone validation follow करें।

Complete backup/restore plan बनाएँ, account पर least privilege लगाएँ और traffic से पहले restored scheduled events review करें।

Official संदर्भ

Command forms, object options, transaction limits और reload behavior official MySQL manual से checked हैं। Deployed client के लिए mysqldump --help confirm करें और compatible isolated server पर test करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

mysqldump किस काम आता है?
mysqldump MySQL client program है जो database definitions और data का logical representation, normally SQL statements में बनाता है। यह backup, migration, reviewable export और small-to-medium recovery में उपयोगी है।
क्या --single-transaction MySQL tables lock करता है?
InnoDB जैसी transactional tables के लिए यह पूरे dump के दौरान lock लगाए बिना consistent snapshot शुरू करता है। Nontransactional engines consistent नहीं होते और concurrent DDL dump बिगाड़ सकता है।
क्या triggers, routines और events default में आते हैं?
Triggers default में included हैं। Stored procedures/functions के लिए --routines और Event Scheduler events के लिए --events चाहिए। Recovery scope में हों तो तीनों flags explicit रखें।
mysqldump SQL file restore कैसे करें?
Compatible isolated target में mysql client या SOURCE command से load करें। --databases/--all-databases dump में database selection statements होते हैं; अन्य dump में target database create/select करें।
क्या mysqldump बहुत बड़े database के लिए सही है?
चल सकता है, पर logical export और index rebuild required RTO से धीमे हो सकते हैं। Representative restore benchmark करें और scale पर MySQL Shell dump utilities या supported physical backup evaluate करें।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.