├── README.md ├── config.py ├── installer.sh ├── main.py └── requirements.txt /README.md: -------------------------------------------------------------------------------- 1 | # backup-bot -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/soheilkhaledabdi/backup-bot/64a33a3d9405c3561d8fa0b83d5ccd19a48749ef/config.py -------------------------------------------------------------------------------- /installer.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Create the configuration file 4 | read -p "Enter your Telegram API ID: " api_id 5 | read -p "Enter your Telegram API hash: " api_hash 6 | read -p "Enter your Telegram bot token: " bot_token 7 | read -p "Enter your Telegram chat ID: " chat_id 8 | read -p "Enter your MySQL host (default: localhost): " -i "localhost" mysql_host 9 | read -p "Enter your MySQL username: " mysql_user 10 | read -p "Enter your MySQL password: " mysql_password 11 | read -p "Enter your MySQL database name: " mysql_database 12 | 13 | 14 | 15 | # Create the configuration file 16 | echo "api_id = '$api_id' 17 | api_hash = '$api_hash' 18 | bot_token = '$bot_token' 19 | bot_chat_id = $chat_id 20 | mysql_host = '$mysql_host' 21 | mysql_user = '$mysql_user' 22 | mysql_password = '$mysql_password' 23 | mysql_database = '$mysql_database'" > config.py 24 | 25 | 26 | #install pip 27 | apt install python3-pip 28 | # Install dependencies 29 | pip3 install -r requirements.txt 30 | 31 | # Create a systemd service unit file 32 | echo "[Unit] 33 | Description=Telegram Bot 34 | After=network.target 35 | 36 | [Service] 37 | User=$USER 38 | WorkingDirectory=$(pwd) 39 | ExecStart=/usr/bin/python3 $(pwd)/main.py 40 | Restart=always 41 | 42 | [Install] 43 | WantedBy=multi-user.target" > telegram_bot.service 44 | 45 | # Move the service unit file to systemd directory 46 | sudo mv telegram_bot.service /etc/systemd/system/ 47 | 48 | # Reload systemd manager configuration 49 | sudo systemctl daemon-reload 50 | 51 | # Enable and start the service 52 | sudo systemctl enable telegram_bot.service 53 | sudo systemctl start telegram_bot.service 54 | 55 | # Display service status 56 | sudo systemctl status telegram_bot.service 57 | 58 | 59 | 60 | 61 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | import time 3 | import subprocess 4 | # import mysql.connector 5 | from pyrogram import Client 6 | from config import * 7 | 8 | 9 | 10 | # Function to perform database backup 11 | def backup_database(): 12 | # Generate a timestamp for the backup file 13 | timestamp = time.strftime("%Y%m%d-%H%M%S") 14 | backup_file = f"database_backup_{timestamp}.sql" 15 | 16 | # Construct the mysqldump command 17 | command = f"mysqldump -h {mysql_host} -u {mysql_user} -p{mysql_password} {mysql_database} > {backup_file}" 18 | 19 | # Execute the command 20 | subprocess.run(command, shell=True) 21 | 22 | # Send the backup file to Telegram 23 | with Client("secure swtich",api_id=api_id,api_hash=api_hash,bot_token=bot_token) as bot: 24 | bot.send_document(chat_id=bot_chat_id, document=backup_file) 25 | 26 | # Delete the backup file 27 | os.remove(backup_file) 28 | 29 | # Main function to schedule backups 30 | def main(): 31 | while True: 32 | try: 33 | # Perform the database backup 34 | backup_database() 35 | print("Backup sent successfully.") 36 | except Exception as e: 37 | print(f"Error: {e}") 38 | 39 | # Wait for an hour 40 | time.sleep(3600) 41 | 42 | if __name__ == '__main__': 43 | main() 44 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | pyrogram --------------------------------------------------------------------------------