Dump entire database into file; dump data of one or more tables into a file

एक बिना argument वाला .dump पूरे database को replayable SQL के रूप में export करता है, .dump t1 t2 specific tables चुनता है, और .read (या एक shell redirect) dump को एक fresh database में restore करता है।

8 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Friday, शाम 4:55: सब कुछ backup कीजिए

Results week ख़त्म होता है। Exam cell का parting order: "एक file WHOLE database के साथ: students, marks, audit log, trigger, सब कुछ। और हमें दिखाइए यह असल में restore होती है।"

पिछले lesson आपने एक table dump की। Scale up करना tables पर loop नहीं है: आप trigger, indexes, order भूल जाएँगे।

Shell के पास एक शब्द का जवाब है: बिना argument वाला .dump: पूरा database एक replayable SQL script के रूप में।

Theory

पूरी recipe book

एक table का dump एक recipe card था। बिना argument वाला .dump पूरी recipe book लिखता है: हर table, हर index, हर trigger, dependency order में, ऊपर BEGIN TRANSACTION और आख़िर में COMMIT के साथ।

Transaction wrapper क्यों? ताकि एक restore all or nothing पके: अगर replay line 900 पर fail हो, नया database आधा-पका नहीं छोड़ा जाता। आपका Unit 1 transactions lesson, overtime काम करते हुए।

Follow along

Full backup, standard sequence

  1. sqlite3 college.db Backup करने वाले database पर shell खोलिए।
  2. .output full_backup.sql Shell जो कुछ print करता है वह file में भेजिए।
  3. .dump कोई argument नहीं = ENTIRE database: सभी tables, data, indexes, triggers, एक transaction में wrapped।
  4. .output stdout फिर .quit Screen printing restore कीजिए, shell छोड़िए। full_backup.sql आपकी recipe book है।
  5. One-liner alternative (कोई shell session नहीं): sqlite3 college.db .dump > full_backup.sql Terminal redirect .output का काम करता है; scripts और cron jobs में handy।

Theory

सिर्फ़ कुछ tables, और restore

Selected tables: .dump students marks सिर्फ़ वे दो लिखता है (कितने भी नाम)। नोटिस कीजिए: दूसरी tables के triggers और indexes शामिल नहीं होते; सिर्फ़ बिना argument वाला .dump सब कुछ guarantee करता है।

Restore SQL text को वापस एक database में बदलता है:

  • Terminal से: sqlite3 restored.db < full_backup.sql
  • Shell के अंदर: .read full_backup.sql

Fresh, empty file में restore कीजिए: उन tables वाले database में CREATE TABLE replay करना एक error पर रुक जाता है।

Quiz

आपको सिर्फ़ students और marks tables (data समेत) एक teammate के machine पर readable SQL के रूप में ले जानी हैं। कौन सा command वह file produce करता है?

  1. .dump students marks (.output transfer.sql के बाद)
  2. .dump बिना argument
  3. .schema students marks
  4. .read students marks
Show the answer

.dump students marks (.output transfer.sql के बाद)

.dump table names की एक list accept करता है और बिल्कुल वे export करता है, structure plus data। बिना argument वाला .dump audit log और बाक़ी सब कुछ भी घसीट लाता (काम करता है, पर ONLY को नज़रअंदाज़ करता है)। .schema बिना data के structure देता है, और .read RESTORE direction है: यह SQL files consume करता है, produce नहीं। Command को travel की direction से match करना ही पूरा सवाल है।

Think first

Restore अंदाज़ा लगाइए

एक student sqlite3 college.db < full_backup.sql चलाता है, ORIGINAL database में जिसमें अभी भी सभी tables हैं, restore करते हुए। tap करने से पहले: क्या होता है, और उन्हें क्या करना चाहिए था?

Show the answer

Replay शुरू होता है और तुरंत CREATE TABLE students... पर टकराता है जहाँ students पहले से मौजूद है: error, और transaction wrapper attempt rollback करता है। कुछ नहीं खोया, पर कुछ भी restore नहीं हुआ।

Correct practice: एक fresh file में restore कीजिए: sqlite3 restored_college.db < full_backup.sql, फिर एक SELECT से verify कीजिए, और सिर्फ़ तभी files swap कीजिए अगर replacing goal थी। Restores empty stages पर rehearsed होते हैं।

Watch out

Backup rules जो careers बचाते हैं

जो backup आपने कभी restore नहीं किया वह एक hope है, backup नहीं: हमेशा एक scratch file में test-replay कीजिए।

Partial dumps triggers भूल जाते हैं: .dump marks marks_log पर audit trigger नहीं ढोता; whole-database काम को बिना argument वाला .dump चाहिए।

.read paths relative होते हैं वहाँ से जहाँ आपने sqlite3 launch किया: 'file not found' आमतौर पर मतलब है आप ग़लत folder में खड़े हैं, file gone नहीं है।

Theory

यह subject के बाक़ी हिस्से से कहाँ मिलता है

Dump file pure SQL text है, तो Unit 1 का सब कुछ इसके अंदर लागू होता है: आप इसे खोल सकते हैं और वह CREATE TRIGGER पढ़ सकते हैं जो आपने लिखा, वे INSERTs जो आपके transaction ने commit किए। Unit 3 में, Python इस workflow को automate करेगा (iterdump() बिल्कुल programmatic dumps के लिए मौजूद है), और अगले CSV lessons आपको OTHER export format देते हैं: data-only, databases के बजाय spreadsheets के लिए।

Summary

Key takeaways

  • बिना argument वाला .dump = पूरा database SQL के रूप में: tables, data, indexes, triggers, एक transaction में।
  • .dump name1 name2 सिर्फ़ selected tables export करता है (उनके triggers कहीं और शामिल नहीं)।
  • Standard sequence: .output file.sql, .dump, .output stdout; या one-line: sqlite3 db .dump > file.sql।
  • sqlite3 fresh.db < file.sql से restore कीजिए, या shell के अंदर .read file.sql, एक EMPTY database में।
  • Transaction wrapper एक restore को all-or-nothing बनाता है।
  • हर backup को असल में एक बार restore करके test कीजिए।
  • Memory hook: पूरी recipe book, एक empty stage पर rehearsed।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Database backup and CSV handling

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Dump entire database into file; dump data of one or more tables into a file · Database Handling using Python · Gri-Learn