GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - iu2frl/MySQL-Telegram-Backup: Retrieves a backup from a MySQL server and sends the compressed output to a Telegram channel/chat

BranchesTags
Open more actions menu

Latest commit

 

History

10 Commits
10 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
.dockerignore
.dockerignore
 
 
.env.example
.env.example
 
 
.gitignore
.gitignore
 
 
Dockerfile
Dockerfile
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
docker-compose.yml
docker-compose.yml
 
 
mysql_telegram_backup.py
mysql_telegram_backup.py
 
 
requirements.txt
requirements.txt
 
 

Repository files navigation

MySQL Telegram Backup

A Python script that performs automated MySQL database backups and sends them to a Telegram chat.

Features

  • ✅ 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

Prerequisites

  • Python 3.7 or higher
  • MySQL client tools (mysqldump command)
  • A Telegram Bot Token
  • Your Telegram Chat ID

Configuration

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

Getting Your Telegram Bot Token

  1. Open Telegram and search for @BotFather
  2. Send /newbot command
  3. Follow the instructions to create your bot
  4. Copy the API token provided

Getting Your Chat ID

  1. Search for @userinfobot in Telegram
  2. Start a conversation with it
  3. It will send you your chat ID
  4. Alternatively, send a message to your bot and check:
https://api.telegram.org/bot<YourBOTToken>/getUpdates

MySQL User Permissions

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'@'%';

Usage

The easiest way to run this backup is using Docker. Pre-built images are available on GitHub Container Registry.

Pull the Docker image

docker pull ghcr.io/YOUR_USERNAME/mysql-telegram-backup:latest

Run with Docker

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

Run with Docker Compose

  1. Create a .env file with your configuration
  2. Update docker-compose.yml with your GitHub username
  3. Run:
docker-compose up

Scheduled Backups with Docker

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.

Compression Details

The script uses LZMA compression with the maximum preset (9) which provides:

  • Best compression ratio (typically 70-90% reduction)
  • .xz file extension
  • Slower compression but excellent space savings
  • Ideal for database backups sent over networks

Logging

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.

Troubleshooting

"mysqldump command not found"

Install MySQL client tools (see Prerequisites section)

"File too large for Telegram"

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

"Access denied for user"

Check your MySQL credentials in the .env file

Connection timeout

Increase the timeout value in the script or check your MySQL server accessibility

Security Notes

  • ⚠️ Never commit your .env file 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

License

MIT License - feel free to modify and use as needed.

About

Retrieves a backup from a MySQL server and sends the compressed output to a Telegram channel/chat

Resources

Readme
MIT license
Activity

Stars

1 star

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.