Skip to content

Instantly share code, notes, and snippets.

@MarcoBuster
Last active February 20, 2021 21:25
Show Gist options
  • Save MarcoBuster/11818ccba0d06a81485cf9e1f1b34cb7 to your computer and use it in GitHub Desktop.
Save MarcoBuster/11818ccba0d06a81485cf9e1f1b34cb7 to your computer and use it in GitHub Desktop.
Encrypt then backup to Telegram. Used to backup mailcow and other things
import os
import glob
from telethon.sync import TelegramClient
BACKUP_FOLDER="/your/backups/path"
KEY_FILE = "/your/key/path.txt" # echo "yourpassword" > key.txt
API_ID = 12345 # Telegram API ID (https://my.telegram.org)
API_HASH = "qwertyuiopasdfghjklzxcvbnm" # Telegram API hash (https://my.telegram.org)
BOT_TOKEN = "123456789:qwertyuiopasdfghjklzxcvbnmqwertyuiop" # Telegram bot token (@BotFather)
DEST_ID = -1001234567890 # Destination chat ID (hint: use a private channel)
bot = TelegramClient('bot', API_ID, API_HASH).start(bot_token=BOT_TOKEN)
backups = glob.glob(BACKUP_FOLDER + '/**')
latest = sorted(backups, key=os.path.getctime)[-1] + '/'
encrypted_fn = latest.split('/')[-2] + '.aes256'
os.system(f"tar -czf - {latest} | openssl enc -e -pass file:{KEY_FILE} -aes256 -pbkdf2 -out {encrypted_fn}")
bot.send_file(DEST_ID, encrypted_fn, force_document=True)
os.remove(encrypted_fn)
# To decrypt: openssl enc -d -aes256 -pbkdf2 -in $FILENAME > real.tar.gz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment