A Python-based system for automated MariaDB backups with retention management, Discord notifications, and Docker support.
- Automated Backups: Schedule backups at specific times or intervals.
- Retention Policy: Automatically manage disk space by keeping a set number of backups or limiting total backup size (per database).
- Discord Integration: Receive real-time notifications for successful backups or failures.
- Docker Support: Can perform backups by executing
mariadb-dumpinside Docker containers. - Compression: Backups are compressed using
gzipto save space. - Restore Capability: Easily restore databases from compressed backups via CLI.
- Colored Output: Readable terminal output for monitoring.
- Python 3.x
pyyamlandrequestslibraries- MariaDB client tools (
mariadb-dump,mariadb) OR Docker installed (if using the container option).
-
Clone the repository:
git clone <repository-url> cd "Database Backup"
-
Install dependencies:
pip install pyyaml requests
Configure the system using config.yml. A sample structure is provided below:
storage:
path: "./backups"
discord:
webhook_url: "YOUR_DISCORD_WEBHOOK_URL"
on_success: "Backup of {database} on {host} completed successfully."
on_failure: "Backup of {database} on {host} failed: {error}"
retention:
default:
keep_last: 10
max_gb: 5.0
overrides:
important_db:
keep_last: 30
max_gb: 20.0
servers:
- host: "test.errin.dev"
container: "db_server_one" # Optional: uses docker exec if specified
user: "backup_user"
password: "backup_password"
databases:
- name: "website_db"
timeout: 30 # Custom 30-second timeout for this specific DB
schedule: "02:00" # Daily at 02:00
- host: "127.0.0.1"
container: "db_server_two"
user: "backup_user"
password: "backup_password"
databases:
- "app_db"
interval_hours: 6 # Every 6 hours
timeout: 600 # Default 10-minute timeout for all DBs on this serverstorage:path: Directory where backups will be stored.
retention:default: Default policy for all databases.keep_last: Number of most recent backups to keep.max_gb: Maximum total size of backups for a single database.
overrides: Specific policies for individual databases.
servers:host: Database host address.container: (Optional) Docker container name if running MariaDB in Docker.user: Database user.password: Database password.databases: List of databases to backup. Can be a string or an object withnameandtimeout.timeout: (Optional) Default timeout in seconds for all databases on this server.schedule: (Optional) Daily backup time inHH:MMformat.interval_hours: (Optional) Backup interval in hours.
The main script is backup.py.
python3 backup.py listpython3 backup.py nowpython3 backup.py restore [host]/[backup_filename]
# Example: python3 backup.py restore 127.0.0.1/website_db-18-01-2026-1.sql.gzThis will keep the script running and execute backups according to the schedules/intervals in config.yml.
python3 backup.py daemonTo ensure the backup daemon runs 24/7 and starts automatically after a reboot, it is recommended to use systemd.
-
Create a service file: Create a file named
/etc/systemd/system/mariadb-backup.service(you may needsudo):[Unit] Description=MariaDB Backup Daemon After=network.target [Service] Type=simple User=your_username WorkingDirectory=/path/to/Database Backup ExecStart=/usr/bin/python3 backup.py daemon Restart=always RestartSec=10 [Install] WantedBy=multi-user.target
Note: Replace
your_usernameand/path/to/Database Backupwith your actual username and the absolute path to the project. -
Reload systemd:
sudo systemctl daemon-reload
-
Enable and start the service:
sudo systemctl enable mariadb-backup.service sudo systemctl start mariadb-backup.service -
Check status:
sudo systemctl status mariadb-backup.service
Run the unit tests using the provided script:
./run_tests.shMIT