@avivace for the original backup script
@dJani97 for providing a restore script
@Silenseo for his edited restore script
@NabiKAZ for his tip for faster speeds
@MohammadJouza for providing a guide
Step 1: Make sure you have Python 3 installed at your system https://www.python.org/downloads/
Step 2: In a terminal run these commands:
pip install telethon
Telethon is pure Python 3 MTProto API Telegram client library which is used by this script
pip install cryptg
Cryptg is an official Telethon extension to provide much faster cryptography for Telegram API requests (faster download speed for the media).
Step 3:
Get/create your own API_ID
and API_HASH
from https://my.telegram.org, under API development tools
Step 4:
Find your GROUP_CHAT_ID
Almost all clients: You can find it by tapping/right clicking any message at the group chat and selecting "Copy Link". The link should be something like
t.me/c/GROUP_CHAT_ID/11/11
if you have topics ort.me/c/GROUP_CHAT_ID/11
if you don't have.
Web clients: When you select your group (and a topic if enabled) the site address changes to something like
web.telegram.org/k/#-GROUP_CHAT_ID
Step 5:
Create a new folder for all the files that the backup will create. Then create a new text file inside that folder, paste the code of the script to the text file, change the API_ID
, API_HASH
and GROUP_CHAT_ID
at the beginning of the code to the values you got earlier and rename the text file from something.txt
to backup_script.py
Then open the terminal to that location
If you run Windows, to do this just hold ctrl+shift and right click the folder. Then select
Open in Terminal
orOpen Powershell window here
orOpen command window here
, whatever yuo see/prefer.
and run:
python backup_script.py
PS: If you also want to restore the backup, then do again the step 5 but this time for the edited restore script, which should be saved as restore_script.py
and executed as python restore_script.py
PS2: For more info check the thanks section
from telethon import TelegramClient, events, sync
from telethon.tl.types import InputChannel, PeerChannel
from telethon.tl.types import Channel
import time
# Replace API_ID and API_HASH with the values
# from https://my.telegram.org, under API development tools.
api_id = API_ID
api_hash = 'API_HASH'
# Replace GROUP_CHAT_ID with the one you want a backup
group_chat_id = GROUP_CHAT_ID
# Checks if you haven't added -100 at the beginning
group_chat_id = str(group_chat_id)
if group_chat_id[:4] != '-100':
group_chat_id = '-100' + group_chat_id
group_chat_id = int(group_chat_id)
# end of my addition
client = TelegramClient('session_name', api_id, api_hash)
client.start()
group = client.get_entity(PeerChannel(group_chat_id))
#messages = client.get_admin_log(group)
file1 = open("dump.json","w")
c = 0
m = 0
for event in client.iter_admin_log(group):
if event.deleted_message:
print("Dumping message",c, "(", event.old.id, event.old.date,")")
file1.write(event.old.to_json() + ",")
c+=1
if event.old.media:
m+=1
#print(event.old.media.to_dict()['Document']['id'])
client.download_media(event.old.media, str(event.old.id))
print(" Dumped media", m)
time.sleep(0.1)
How do I run this?
Please check https://docs.python.org/3/faq/