Skip to content

khoirulyahya/mysql-docker-backup

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

mysql-docker-backup

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.

What It Does

  1. Dumps a MySQL database from a running Docker container using mysqldump
  2. Compresses the dump with gzip
  3. Uploads the backup file to a specified Google Drive folder
  4. Rotates old backups on Google Drive (keeps N most recent)
  5. Cleans up old local backup files (keeps N most recent)
  6. Sends a status report to a Telegram channel with per-step results

Telegram Report Example

📦 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

File Structure

mysql-docker-backup/
├── README.md
├── telegram.sh
└── db-backup.sh

Requirements

  • 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

Setup

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.sh

2. 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=5

4. 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 at localhost:8085 waiting 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 -e

Example — run daily at 2 AM:

0 2 * * * /usr/local/bin/backup-scripts/db-backup.sh

Notes

  • telegram.sh is kept as a separate file so it can be sourced by multiple scripts without duplication
  • The script uses set -uo pipefail for 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

About

Automated MySQL backup from Docker container with Google Drive upload, log rotation, and Telegram reporting

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages