A Python script that performs automated MySQL database backups and sends them to a Telegram chat.
- ✅ Full MySQL database backup using
mysqldump - ✅ Support for single database or all databases backup
- ✅ Maximum compression using LZMA/XZ algorithm (preset 9)
- ✅ Automatic file upload to Telegram
- ✅ Retry logic for network failures
- ✅ Comprehensive logging
- ✅ Environment variable configuration
- ✅ Automatic cleanup of temporary files
- Python 3.7 or higher
- MySQL client tools (
mysqldumpcommand) - A Telegram Bot Token
- Your Telegram Chat ID
Edit the .env file with your settings:
# Telegram Bot Configuration BOT_TOKEN=123456789:ABCdefGHIjklMNOpqrsTUVwxyz # Get from @BotFather BOT_DEST=123456789 # Your chat ID (get from @userinfobot) # Custom message (optional) CUST_MSG=Production Database Backup # MySQL Configuration MYSQL_HOST=localhost # MySQL server host MYSQL_PORT=3306 # MySQL server port MYSQL_USER=root # MySQL username MYSQL_PASSWORD=your_secure_password # MySQL password MYSQL_DATABASE= # Leave empty for all databases, or specify one # Temporary Directory (optional) TMP_DIR=/tmp # Temporary storage location
- Open Telegram and search for
@BotFather - Send
/newbotcommand - Follow the instructions to create your bot
- Copy the API token provided
- Search for
@userinfobotin Telegram - Start a conversation with it
- It will send you your chat ID
- Alternatively, send a message to your bot and check:
https://api.telegram.org/bot<YourBOTToken>/getUpdates
Ensure the MySQL user has the necessary permissions:
-- 1. Create backup user CREATE USER 'mysql_backup'@'%' IDENTIFIED BY 'YourSecurePassword123!'; -- 2. Grant necessary privileges for full backup with routines, triggers, and events GRANT SELECT, SHOW VIEW, TRIGGER, EVENT, ON *.* TO 'mysql_backup'@'%'; -- 3. Apply changes FLUSH PRIVILEGES; -- 4. Verify SHOW GRANTS FOR 'mysql_backup'@'%';
The easiest way to run this backup is using Docker. Pre-built images are available on GitHub Container Registry.
docker pull ghcr.io/YOUR_USERNAME/mysql-telegram-backup:latest
docker run --rm \ -e BOT_TOKEN="your_bot_token" \ -e BOT_DEST="your_chat_id" \ -e MYSQL_HOST="your_mysql_host" \ -e MYSQL_USER="your_mysql_user" \ -e MYSQL_PASSWORD="your_mysql_password" \ -e MYSQL_DATABASE="" \ ghcr.io/YOUR_USERNAME/mysql-telegram-backup:latest
- Create a
.envfile with your configuration - Update
docker-compose.ymlwith your GitHub username - Run:
docker-compose up
Use a cron job to run the Docker container:
# Add to crontab (crontab -e) 0 2 * * * docker run --network host --pull=always --rm --env-file /home/ubuntu/wavelog-backup.env ghcr.io/iu2frl/mysql-telegram-backup:latest
Or use a container orchestrator like Kubernetes with CronJob.
The script uses LZMA compression with the maximum preset (9) which provides:
- Best compression ratio (typically 70-90% reduction)
.xzfile extension- Slower compression but excellent space savings
- Ideal for database backups sent over networks
Logs are created in the temporary directory (default: /tmp) with the format:
YYYYMMDD_HHMMSS_mysql_backup.txt
The log file is also sent to Telegram after the backup completes.
Install MySQL client tools (see Prerequisites section)
Telegram has a 2GB file size limit. For larger databases, consider:
- Backing up individual databases separately
- Using a different file transfer method
- Splitting the backup into smaller parts
Check your MySQL credentials in the .env file
Increase the timeout value in the script or check your MySQL server accessibility
⚠️ Never commit your.envfile to version control⚠️ Keep your Telegram bot token secure⚠️ Use strong MySQL passwords⚠️ Consider encrypting backups with a password for sensitive data⚠️ Restrict MySQL user permissions to only what's needed for backups
MIT License - feel free to modify and use as needed.