- Gmail to SQLite एक Python application है जो Gmail messages को local SQLite database में sync करता है, ताकि उन्हें analysis और archive के लिए इस्तेमाल किया जा सके
- इसका default behavior incremental sync है, जो केवल नए messages डाउनलोड करता है; full sync option के साथ सभी messages डाउनलोड किए जा सकते हैं और deleted status भी detect किया जा सकता है
- message import में multithreaded parallel processing का उपयोग होता है, और इसमें exponential backoff आधारित automatic retry तथा CTRL+C handling जैसे error और shutdown response शामिल हैं
- इसे चलाने के लिए Python 3.8 या उससे ऊपर, Gmail API enabled Google Cloud Project, और OAuth 2.0
credentials.json file की आवश्यकता होती है
- सहेजे गए data में sender, recipient, label, body, size, read status, outgoing status, deleted status आदि शामिल होते हैं, जिससे SQL के जरिए Gmail usage patterns का सीधे analysis किया जा सकता है
Gmail messages के लिए local sync tool
- Gmail to SQLite एक Python application है जो Gmail messages को local SQLite database में store करता है
- इसका उद्देश्य Gmail data को analyze और archive करने योग्य बनाना है
- पूरे codebase में type hints लागू किए गए हैं, जिससे type safety मिलती है
Sync का तरीका और reliability
- default sync incremental sync के रूप में काम करता है, इसलिए केवल नए messages डाउनलोड होते हैं
--full-sync option का उपयोग करने पर सभी messages sync होते हैं और Gmail से deleted messages भी detect किए जाते हैं
- message import multithreaded parallel processing के साथ किया जाता है, जिससे performance बेहतर होती है
- error handling में automatic retry और exponential backoff शामिल हैं
- CTRL+C दबाने पर graceful shutdown process शुरू होती है
- नए tasks स्वीकार करना बंद हो जाता है
- चल रहे tasks के पूरा होने का इंतज़ार किया जाता है
- पूरे हो चुके tasks की progress save की जाती है
- process सामान्य रूप से बंद हो जाती है
- CTRL+C एक बार और दबाने पर तुरंत exit हो जाता है
Installation और prerequisites
- runtime environment में Python 3.8 या उससे ऊपर आवश्यक है
- Gmail API enabled Google Cloud Project आवश्यक है
- OAuth 2.0 authentication file
credentials.json project root में मौजूद होनी चाहिए
- installation flow में repository को clone करने के बाद
uv sync से dependencies install की जाती हैं
- Gmail API authentication setup के लिए Google Cloud Console में project बनाना या चुनना होता है, Gmail API enable करना होता है, फिर Desktop application के लिए OAuth 2.0 credentials बनाकर उन्हें
credentials.json के रूप में save करना होता है
Command usage
- default incremental sync इस तरह चलाया जाता है
python main.py sync --data-dir ./data
# or: uv run main.py sync --data-dir ./data
- full sync और deleted message detection के लिए
--full-sync का उपयोग किया जाता है
python main.py sync --data-dir ./data --full-sync
- केवल किसी specific message को sync करने के लिए
sync-message और --message-id का उपयोग किया जाता है
python main.py sync-message --data-dir ./data --message-id MESSAGE_ID
- केवल deleted messages को detect और mark करने के लिए
sync-deleted-messages का उपयोग किया जाता है
python main.py sync-deleted-messages --data-dir ./data
- worker thread count को
--workers से set किया जा सकता है, और इसका default value CPU core count है
python main.py sync --data-dir ./data --workers 8
- command line arguments इस प्रकार हैं
command: required, और sync, sync-message, sync-deleted-messages में से एक
--data-dir: required, वह directory जहाँ SQLite database store होगा
--full-sync: optional, full sync को force करता है
--message-id: sync-message में required, sync किए जाने वाले specific message की ID
--workers: optional, worker threads की संख्या
--help: commands और options की help दिखाता है
SQLite schema और analysis examples
- बनने वाले SQLite database की
messages table में Gmail message analysis के लिए ज़रूरी fields शामिल हैं
message_id: unique Gmail message ID
thread_id: Gmail thread ID
sender: नाम और email वाला JSON sender information
recipients: to, cc, bcc type के अनुसार recipient JSON
labels: Gmail label array
subject: message subject
body: plain text message body
size: bytes में message size
timestamp: message timestamp
is_read: read status
is_outgoing: क्या message user ने भेजा है
is_deleted: क्या message Gmail से deleted है
last_indexed: आखिरी sync timestamp
- sender के अनुसार email count aggregate किया जा सकता है
SELECT sender->>'$.email', COUNT(*) AS count
FROM messages
GROUP BY sender->>'$.email'
ORDER BY count DESC
- unread emails को sender के अनुसार aggregate करके यह देखा जा सकता है कि कौन से senders बहुत अधिक गैर-ज़रूरी emails भेजते हैं
SELECT sender->>'$.email', COUNT(*) AS count
FROM messages
WHERE is_read = 0
GROUP BY sender->>'$.email'
ORDER BY count DESC
strftime का उपयोग करके साल, महीना, दिन, weekday, और hour के आधार पर email count aggregate किया जा सकता है
SELECT strftime('%Y', timestamp) AS period, COUNT(*) AS count
FROM messages
GROUP BY period
ORDER BY count DESC
- body में
newsletter या unsubscribe शामिल होने वाले mails खोजकर newsletters को sender के अनुसार group किया जा सकता है
SELECT sender->>'$.email', COUNT(*) AS count
FROM messages
WHERE body LIKE '%newsletter%' OR body LIKE '%unsubscribe%'
GROUP BY sender->>'$.email'
ORDER BY count DESC
- sender के अनुसार total mail size और बड़े email senders को MB unit में देखा जा सकता है
SELECT sender->>'$.email', sum(size)/1024/1024 AS size
FROM messages
GROUP BY sender->>'$.email'
ORDER BY size DESC
- खुद को भेजे गए mails की संख्या
recipients JSON और sender email condition से calculate की जा सकती है
SELECT count(*)
FROM messages
WHERE EXISTS (
SELECT 1
FROM json_each(messages.recipients->'$.to')
WHERE json_extract(value, '$.email') = 'foo@example.com'
)
AND sender->>'$.email' = 'foo@example.com'
- received mails में sender के अनुसार कुल size को descending order में देखा जा सकता है
SELECT sender->>'$.email', sum(size)/1024/1024 as total_size
FROM messages
WHERE is_outgoing=false
GROUP BY sender->>'$.email'
ORDER BY total_size DESC
- deleted messages को
is_deleted=1 condition से query किया जाता है
SELECT message_id, subject, timestamp
FROM messages
WHERE is_deleted=1
ORDER BY timestamp DESC
1 टिप्पणियां
Hacker News की टिप्पणियां
उत्सुकता है कि स्कीमा में कुछ खास हेडर अलग से क्यों निकाले गए।
recipients,subject,senderको JSON फ़ील्ड के रूप में भी रखा जा सकता है, लेकिनheadersनाम के एक ही फ़ील्ड में सब कुछ, साथ में मैसेज के बाकी हेडर भी, डाले जा सकते हैंअगर वजह performance है, तो
headersको एक single JSON blob के रूप में रखकर ज़रूरी फ़ील्ड्स को generated columns बनाया जा सकता है। जैसेsubjectकोjson_extract("headers", '$.Subject')से बनाया जा सकता है और उस पर index लगाया जा सकता हैयह मॉडल काफ़ी शक्तिशाली था, क्योंकि यूज़र अपनी query के लिए ज़रूरी indexed generated columns को
ALTER TABLEसे जोड़ सकता था। DKIM status के लिए भी"Dkim-Signature"निकालकर column और index बनाया जा सकता है, फिरGROUP BYकिया जा सकता हैCREATE INDEX subjectidx ON messages(json_extract(headers, '$.Subject'))जैसा बनाने पर, उस expression को refer करने वाली जगहों पर index इस्तेमाल होगाइस तरह index बनाने के बाद main table को
ALTERकरके generated column जोड़ने के बजाय उस expression का इस्तेमाल करने वालाVIEWबनाना ज़्यादा उपयोगी लगाआम तौर पर जिन columns का लगातार इस्तेमाल होना है, उन्हें अलग निकालना बेहतर लगता है। email headers जैसे stable target के मामले में तो और भी;
headerscolumn schema change को थोड़ा आसान बना सकता है, लेकिन यह write-time की तकलीफ़ को read-time की तकलीफ़ में बदलने जैसा है, और चुपचाप fail होने की गुंजाइश भी छोड़ता हैकरीब दो महीने बाद जब वास्तव में ज़रूरी fields दिखने लगते हैं, तो उन्हें JSON से भर देता हूं और API को लगातार up to date रखने देता हूं, या view बना देता हूं। “बस सब कुछ MongoDB में डाल दो” या “बस file system में रख दो” वाली growth pains से बचने में यह काफ़ी मददगार रहा, और cost भी ज़्यादा नहीं थी
dkimcolumn कोNOT NULLdefine किया गया है; उत्सुकता है कि अगर किसी email message मेंDkim-Signatureheader न हो तो क्या होता हैकुछ साल पहले Gmail जैसे large-scale email visualization tool बनाया था: https://github.com/terhechte/postsack
उत्सुकता है कि size option भी है या नहीं। मैं देखना चाहता हूं कि कौन-सा sender मेरी storage सबसे ज़्यादा इस्तेमाल कर रहा है। और website का SSL certificate expire हो चुका है
इसके उलट qdirstat cache files बनाना आसान है, इसलिए file जैसे दिखने वाले कई targets को visualize करने के लिए उनका इस्तेमाल किया जा सकता है
अब app-specific password से भी login नहीं किया जा सकता, और OAuth client बनाकर OAuth flow से गुजरना पड़ता है—यह सच में अफ़सोसजनक है। मेरा अपना email होते हुए भी Google ने access के लिए इस्तेमाल होने वाला open standard मुझसे छीन लिया
खासकर मुझे लगातार ज़्यादा जानकारी मिल रही है कि मेरा freelance email recipient systems में spam में जा रहा है। हालांकि Google ecosystem की आदत कैसे छोड़ूं, यह समझ नहीं आता
हाल में अपने app https://github.com/rumca-js/Django-link-archive में Gmail integrate करने की कोशिश की, लेकिन इसमें बहुत ज़्यादा समय लग गया, और मैंने तय किया कि Gmail support करना worth it नहीं है
Gmail to SQLite credentials setup को 6 steps में समझाता है, लेकिन मेरे मामले में ऐसा नहीं था। 6 steps के बाद भी Google ने कहा कि app published नहीं है, इसलिए उसे publish करना होगा; फिर कहा कि मैं Workspace user नहीं हूं इसलिए इसे internal app नहीं रख सकता; और external app में बदलने पर कहा कि verification से पहले इसे इस्तेमाल नहीं किया जा सकता
verification process में domain, address, बाकी details, scopes के लिए justification, और app कैसे इस्तेमाल होता है यह समझाने वाला video तक मांगा गया; और कहा गया कि submitted data verify करने में समय लगेगा। पूरा setup एक maze जैसा है, और users को Google की मांगों वाली hurdles पार करवाना बहुत ज़्यादा है
जानना चाहता हूँ कि अभी मौजूद सबसे अच्छा open source Gmail backup software कौन-सा है। Attachments को सुरक्षित रखने सहित क्या किसी ने ऐसा setup बनाया है?
संदर्भ के लिए https://www.mailstore.com/en/products/mailstore-home/ भी है। यह open source नहीं है, लेकिन indexed GUI होने के कारण local mail search के लिए अच्छा है, और resume सिर्फ backup में होता है इसलिए बड़े restore आम तौर पर fail हो जाते हैं
हर कुछ महीनों में इसे चलाने का reminder लगा रखा है और local backup update कर लेता हूँ। याद के मुताबिक यह gzip-compressed mbox file के रूप में आता है
मुझे लगता है इसका नाम “Gmail to SQLite” नहीं बल्कि “IMAP to SQLite” जैसा होना चाहिए। समझ नहीं आता इसे किसी एक specific email provider से क्यों बाँधा गया है
IMAP कहीं ज्यादा मुश्किल और काफी धीमा है, और Google की bandwidth limits से भी बँधा रहता है
पता नहीं वह बहुत cold storage में था इसलिए timeout हुआ या नहीं। इसलिए समझ आता है कि Google की proprietary API इस्तेमाल करने वाला तरीका बेहतर काम कर सकता है
आजकल Google Takeout में mbox शामिल होता है, ठीक से काम करता है और काफी तेज भी है, लेकिन continuous updates नहीं देता। आखिरकार मैं दूसरे mail provider Infomaniak पर चला गया, और खुद को धन्यवाद दिया कि पहले से अपना mail domain इस्तेमाल कर रहा था
Full-text search भी enable कर सकें तो अच्छा होगा
कल मैंने भी यही चीज बनाई। वजह यह थी कि मैं recipient emails को domain के हिसाब से list करना चाहता था। Code गड़बड़ है, लेकिन यहाँ है: https://github.com/hugoferreira/gmail-sqlite-db
PostgreSQL-based IMAP server Archiveopteryx की थोड़ी याद आती है: https://github.com/aox/aox
AOX का schema हमेशा अच्छा लगता था, लेकिन असल में कभी ठीक से इस्तेमाल नहीं कर पाया। मुख्य use case daily driver IMAP server नहीं, बल्कि mail analysis और search था
यहाँ bandwidth cost क्या होगी, यह जानना चाहता हूँ। 40GB से बड़ा Gmail account रखने वाले के तौर पर जानना चाहता हूँ कि इस tool से transfer करने पर charge लगेगा या नहीं
इसे ठीक करना आसान है। Google Takeout शायद free है, इसलिए पहले download करके file parse कर सकते हैं। फिर भी तुरंत शुरू करने के लिहाज से यह tool ज्यादा तेज लगता है