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-databasesPortability, 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 output | High 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 --versionBackup account को चुने हुए objects/options के लिए minimum privileges दें। Requirements MySQL version, views, locking mode और tablespaces पर बदल सकती हैं। Dedicated least-privileged account से exact command test करें; routinely root use न करें।
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.sqlFirst command form में database name argument है, लेकिन output उसे create/select जरूरी नहीं करता। --databases बाद के names को databases मानकर database-level statements लिखता है; इससे restore command बदलता है। Output location explicit करने के लिए --result-file रखें।
--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| Object | Option/behavior |
|---|---|
| Table definitions/data | Normally included |
| Views | Definitions; definer/security important |
| Triggers | Default included; explicit flag intent बताता है |
| Procedures/functions | --routines |
| Scheduled events | --events |
| Accounts/grants | Separate 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 0Creation के बाद 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- Dump/restore exit codes और stderr check करें।
- Manifest से byte size और checksum verify करें।
- Warnings, SQL modes, charset और version compatibility देखें।
- Object inventory, row counts और business aggregates compare करें।
- Views, triggers, routines/events verify करें; events approval तक disabled रहें।
- External side effects block करके app smoke tests चलाएँ।
- Restore duration और recovery point record करें।
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 संदर्भ
- MySQL 8.4: mysqldump
- MySQL 8.4: Reloading SQL-Format Backups
- MySQL 8.4: Dumping Definitions and Data Separately
Command forms, object options, transaction limits और reload behavior official MySQL manual से checked हैं। Deployed client के लिए mysqldump --help confirm करें और compatible isolated server पर test करें।