Automated MySQL backup script for Dockerized databases, with Google Drive upload, retention management, and Telegram status reporting.
Built for production use on Ubuntu VPS where the MySQL instance runs inside a Docker container.
- Dumps a MySQL database from a running Docker container using
mysqldump - Compresses the dump with gzip
- Uploads the backup file to a specified Google Drive folder
- Rotates old backups on Google Drive (keeps N most recent)
- Cleans up old local backup files (keeps N most recent)
- Sends a status report to a Telegram channel with per-step results
📦 Container MySQL Backup Report
Filename: mydb_2025-03-01_02-00-01.sql.gz
Export: Success
Upload: Success
GDrive Rotation: Success
Local Retention: keep last 7
GDrive Retention: keep last 5
mysql-docker-backup/
├── README.md
├── telegram.sh
└── db-backup.sh
- Docker (MySQL running in a container)
- gdrive CLI — authenticated and available in PATH
curl— for Telegram notifications- Cron or any scheduler to run the script automatically
1. Clone or copy the scripts to your server
cp telegram.sh /usr/local/bin/telegram.sh
cp backup-scripts/db-backup.sh /usr/local/bin/backup-scripts/db-backup.sh
chmod +x /usr/local/bin/telegram.sh
chmod +x /usr/local/bin/backup-scripts/db-backup.sh2. Configure telegram.sh
Edit the file and fill in your bot token and channel/chat ID:
TELEGRAM_BOT_TOKEN="your-telegram-bot-token"
TELEGRAM_CHANNEL_ID="your-channel-or-chat-id"3. Configure db-backup.sh
Edit the configuration block at the top of the file:
CONTAINER_NAME="your-mysql-container-name"
DB_NAME="your-database-name"
DB_USER="your-database-user"
DB_PASSWORD="your-database-password"
BACKUP_DIR="/var/lib/automysqlcontainerbackup"
LOG_FILE="/var/log/your-app-name/db-backup.log"
DRIVE_ID="your-google-drive-folder-id"
LOCAL_RETENTION=7
GDRIVE_RETENTION=54. Install and authenticate gdrive CLI
Download and install the binary:
cd /tmp
wget https://github.com/glotlabs/gdrive/releases/download/3.9.1/gdrive_linux-x64.tar.gz
tar -xzf gdrive_linux-x64.tar.gz
chmod +x gdrive
sudo mv gdrive /usr/local/bin/OAuth authentication on a headless server (no browser) requires two terminals:
- Terminal A — run
gdrive account add, which starts a local server atlocalhost:8085waiting for the OAuth callback - Your browser will be redirected to
http://127.0.0.1:8085/?code=...— this will fail to load because the server is on your VPS, not your local machine - Copy that full redirect URL from the browser address bar
- Terminal B — simulate the callback from inside the server using curl:
curl "http://127.0.0.1:8085/?code=YOUR_CODE_HERE&scope=..."Terminal A will complete the authentication once it receives the request.
5. Schedule with cron
crontab -eExample — run daily at 2 AM:
0 2 * * * /usr/local/bin/backup-scripts/db-backup.sh
telegram.shis kept as a separate file so it can be sourced by multiple scripts without duplication- The script uses
set -uo pipefailfor safer error handling — unset variables and pipeline failures are treated as errors - Google Drive rotation deletes by oldest
createdTime, so the most recent backups are always preserved - Local and GDrive retention limits are configured independently