Code Monkey home page Code Monkey logo

telegram-automated-db-backup's Introduction

Telegram automated db backup

Telegram automated db backup

This repo provides a Python script that does a daily backup and upload it automatically to a Telegram group chat. This way, you do not need to take care of where to save the backups or buying any cloud service.

How does it work?

The Python script it's based on:

  • pyrogram
  • unix commands (like mysqldump, zip)

Pyrogram is a python framework which allows you to use your Telegram account, so with this framework you can send a message, files, pictures etc. automatically without doing it manually.

Requirements

To use the script bak.py you need:

  • a Telegram account
  • python3
  • pyrogram
  • tgcrypto (optional, but recommended)
  • zip
  • mysql-client (for mysqldump)

To install these software in a Linux distro Debian-based you can run:

$ sudo apt install python3 python3-pip zip mysql-client

To install the python packages you can use the python package manager pip:

$ pip3 install pyrogram tgcrypto

Configuration

Please replace the following values with your personal data

# database connection config
mysql_localhost = "localhost"
mysql_username = "USERNAME"
mysql_password = "PASSWORD"

world_db = "acore_world"
char_db = "acore_characters"
auth_db = "acore_auth"

# zip config
zip_password = "db_zipped"
zip_name = "db-{:%Y%m%d}.zip".format(datetime.now()) # this will result in a string like "db-20211231.zip"

# replace "me" with a chat_id or username to change the chat
chat_id = "me"
# Telegram client config
username = "TELEGRAM_USERNAME"
api_id=1234567
api_hash="1234d401234d401234d401234d40"

Usage

To allow pyrogram to use your account you need to have an username on Telegram and create a Telegram 3rd "app" here: https://my.telegram.org/auth?to=apps. Generated the app, this app will have api_id and api_hash, put these data into the conf part inbak.py.

Afterward, fill the mysql credentials and the zip password and run the python script using

$ python3 bak.py

Note: alternatively, you can also run bak-per-table.py if you want to make a backup with one file per table instead of one file per db.

The first time, pyrogram will need from your telegram account a TOKEN that you will receive on Telegram, after this, pyrogram will generate a sqlite database in the same directory of bak.py to keep the session with your Telegram user.

Finally, you just need to run the script bak.py and your db will be pushed in your personal chat ("Saved Messages" / "me").

You can change the chat where push the zip archive, replacing "me" on line 30 with a chat_id or a Telegram @Username, if you don't know your chat_id you can ask @username_to_id_bot to get it.

Optional, run it daily using crontab

To run the script daily you can create a cronjob using crontab (usually it is installed as default on a Linux server). So open crontab using:

$ crontab -e

Fill the last line with:

0 0 * * * cd ~/root/path/project/; python3 bak.py

This will run the script daily on at midnight ๐Ÿ˜‰.

That's all! Open an issue if you need anything.

Credits

telegram-automated-db-backup's People

Contributors

helias avatar nicoaravena avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

telegram-automated-db-backup's Issues

Running on mariadb - Ubuntu 20.04

Error

unknown variable 'column-statistics=0'

Reason

The problem is that MariaDB doesn't support the column-statistics command.

Solution

To resolve this error, simply remove the column-statistics command from the line in the bak.py file. Backup is done normally.

bak.py won't start

I tried to start the bak.py many times now and I already reinstalled everything:

Traceback (most recent call last):
File "bak.py", line 1, in
from pyrogram import Client, Filters
ImportError: cannot import name 'Filters' from 'pyrogram' (/usr/local/lib/python3.8/dist-packages/pyrogram/init.py)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.