Home Other Backing up your telegram correspondence

Backing up your telegram correspondence

by admin

Let me start off by saying that I’m not urging anyone to flee the messenger-what-you-can’t-name. Quite the opposite, in fact. It’s just that it’s much more peaceful to sleep when you have one cloud backed up in another cloud. Or else the clouds periodically fall to a sinful earth, and losing years of correspondence is very frustrating. And a friend asked me how to makea backup, and I couldn’t find any clear instructions.

Be forewarned straight away – you need Linux for backup, or at least a virtual machine with it. If you’re handy, you can do it on Mac OS, and maybe even on the stump that’s built into Windows these days – but I can’t guarantee anything.

Let’s go!

We will back up using two nice programs :

We will take the sources from verified sources from git, so the first thing we need is a client :

sudo apt install git

Next, create yourself some directory where you will create your own mayhem, e.g.

mkdir telegram-backup

Then clone yourself a console client and navigate to its :

git clone --recursive https://github.com/vysheng/tg.git cd tg

Now install all sorts of dependencies to build it. If you don’t have Ubuntu, the program page describes alternative build methods :

sudo apt-get install libreadline-dev libconfig-dev libssl-dev lua5.2 liblua5.2-dev libevent-dev libjansson-dev libpython-dev make

Then the classics

/configuremake

Now run the client, passing it as a parameter the public key, which lies here :

bin/telegram-cli -k tg-server.pub

If all went well, you will see about the following :

Telegram-cli version 1.4.1, Copyright © 2013-2015 Vitaly Valtman
Telegram-cli comes with ABSOLUTELY NO WARRANTY; for details type show_license'. This is free software, and you are welcome to redistribute it under certain conditions; type show_license’ for details.
Telegram-cli uses libtgl version 2.1.0
Telegram-cli includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit. ( http://www.openssl.org/ )
I: config dir=[/home/jehy/.telegram-cli]
[/home/jehy/.telegram-cli] created
[/home/jehy/.telegram-cli/downloads] created

Next, from the console you will be prompted for a phone number, enter it :

phone number: 79166666666

And they will ask for a code, which will come in a text message or in another open client, the message from the client looks like this :

Your login code: 123456
This code can be used to log in to your Telegram account. We never ask it for anything else. Do not give it to anyone, even if they say they’re from Telegram!!!
If you didn’t request this code by trying to log in on another device, simply ignore this message.

Enter the code:

code ('CALL' for phone code):123456

And enter your password (if you have one set, which is generally a good idea):

password :xxx

Next, if things go well, you will see that you have logged into the client and you start getting messages:

User Jehy updated flags
User Jehy updated flags
[19:41] Alexander Zharov > > > Hello!
[19:41] Alexander Zharov > > > How are you?

Great! Now turn off the client (Ctrl+C) and start it in server mode :

bin/telegram-cli --json -P 9009

Then leave this console alone, open the second one, go to our telegram-backup folder and put the second program there :

git clone https://github.com/tvdstaaij/telegram-history-dump.git

Go to his directory

cd telegram-history-backup

And run

ruby telegram-history-dump.rb

If all is well, we will see that a backup has started, it will look like this :

ruby telegram-history-dump.rb
I, [2018-04-26T19:43:38.286782 #11274] INFO — : Attaching to telegram-cli control socket at localhost:9009
I, [2018-04-26T19:43:39.689541 #11274] INFO — : Skipping 34 dialogs: "Mila_Milan", "Stormy_Daniels", "Amila_Miley", "Sasha_Gray"
I, [2018-04-26T19:43:39.689946 #11274] INFO : Backing up dialogs 181: "user#1299351", "Canela_Skin", "Dana_DeAmond", "Lita_Phoenix", "Erica_Lauren", "Naomi_Woods" “Nowyny"
I, [2018-04-26T19:43:39.690609 #11274] INFO — : Dumping "user#122679361" (range 1-100)
I, [2018-04-26T19:43:40.975234 #11274] INFO — : Dumping "Jessa_Rhodes" (range 1-100)
I, [2018-04-26T19:43:42.262817 #11274] INFO — : Dumping "Anya_Ivu" (range 1-100)
I, [2018-04-26T19:43:43.548510 #11274] INFO — : Dumping "Brandi_Bae" (range 1-100)
I, [2018-04-26T19:44:42.541472 #11274] INFO – : Dumping "Infinite_let" (range 101-200)
I, [2018-04-26T19:44:43.541788 #11274] INFO – : Dumping "Infinite_let" (range 201-300)
I, [2018-04-26T19:44:44.542101 #11274] INFO — : Dumping "Infinite_leto" (range 301-400)
I, [2018-04-26T19:44:45.542446 #11274] INFO – : Dumping "Infinite_leto" (range 401-500)

Have patience – telegram has its own limits, and that’s why history is not pumped out fast – about 100 messages per second from a user or group.

When the program finishes its work, an interesting question arises – what and where did we downloaded? To figure this out, do from the program’s directory

cd output/json

There will be a bunch of JSON formatted files, each containing correspondence with some contact. Of course, it’s quite hard to read, but you can easily export from JSON to any other readable format. By the way, you could also specify a different format when exporting the history – for example, to have an HTML page per contact generated for you.
And if you like hardcore, you can just comb through the history, like this :

grep "porn" . -R./KotoNod.jsonl:{"event":"message", "id":"02040050001310087a8e0200000000000000000000000000", "flags":228, "from":{"id":"$0120000078745a00625f8a35c922bb6c", "peer_type":"user", "peer_id":3328156, "print_name":"Jehy", "flags":721597, "first_name":"Jehy", "when":"2018-04-26 20:09:54", "last_name":"", "phone":"7123456", "username":"Jehy_rus"}, "to":{"id":"$02000001005310020000000000000000", "peer_type":"chat", "peer_id":115391548, "print_name":"KotoNod", "flags":1, "title":"TootoNod", "admin":{"id":"$01000000001000000000001000000000", "peer_type":"user", "peer_id":0, "print_name":"user#0"}, "members_num":3}, "out":true, "unread":false, "service":false, "date":1504003822, "text":"https://tjournal.ru/58619-polzovatel-reddit-zaarhiviroval-dva-petabayta-porno-chtoby-proverit-bezlimitnoe-oblachnoe-hranilishche-amazon?from=digestnn@navalny are you?", "media":{"type":"webpage", "url":"https://tjournal.ru/58619-polzovatel-reddit-zaarhiviroval-dva-petabayta-porno-chtoby-proverit-bezlimitnoe-oblachnoe-hranilishche-amazon?from=digest", "title":"A Reddit user archived two petabytes of porn tocheck out Amazon's unlimited cloud storage", "description":"He tested the service for six months in order to find limitations in it."}}

Like this. That said, I remind you :

  • Naturally, the history of private chats is not saved;
  • Media files are not saved either. Maybe it can be done somehow, but I didn’t really care;
  • Please keep backups of your correspondence in safe places. Otherwise, years of correspondence can find anything at all.
  • It is quite possible that you will find somewhere more convenient export tool or prepared docker image with all this software or anything else. Be careful, check such tools three times.

In general I have a feeling that soon everything will be like in 90`s – music, movies and files on local machines, and only programmers can access Internet. Not to say that it makes me happy.

Not-so-helpful links with alternative options :

  • Amazing recommendation from the telegram wiki is to scroll through your entire chat in the web client and then save it with a printable pdf;
  • Crafty client javascript for exporting data directly from the web client;

You may also like