Skip to content

Commit

Permalink
Upgrade backupSql script with Telegram integration and colorized output.
Browse files Browse the repository at this point in the history
  • Loading branch information
klevze committed Feb 4, 2024
1 parent 8973ba6 commit 12588fd
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 88 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Gregor Klevze

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
182 changes: 125 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,110 +1,178 @@
# MySQL Backup Script

This script is designed to perform MySQL backups using `mysqldump` and send email notifications using `ssmtp`. It supports configuration through a dedicated configuration file.
This script is designed to perform MySQL backups using `mysqldump` and send notifications through Telegram. It supports configuration through dedicated configuration files.

## Features
## New Features

- Perform MySQL backups for specified databases.
- Email notifications on backup success and errors.
- Configurable backup directory and retention period.
- Ignore specified databases during the backup process.
- **Telegram Integration:** Receive backup status messages on Telegram.
- **Colorized Output:** The script now provides color-coded output for better readability.
- **Improved Configuration:** Additional options and improved organization in the configuration files.

## Installation
## Installation and Upgrade

### Prerequisites

- **MySQL Client Tools**: Ensure that `mysql` and `mysqldump` binaries are installed on your system.
- **ssmtp**: Install `ssmtp` for sending email notifications.
Ensure that the required dependencies are installed:

```bash
# On Debian/Ubuntu
sudo apt-get install ssmtp
- **MySQL Client Tools:** `mysql` and `mysqldump` binaries.
- **cURL:** Used for Telegram integration.

# On Red Hat-based systems
sudo yum install ssmtp
```
### Installation

### Configuration
1. **Clone the Repository:**

1. Clone the repository:
Clone the repository to your local machine:

```bash
git clone https://github.com/yourusername/mysql-backup-script.git
cd mysql-backup-script
git clone https://github.com/klevze/backupSQL.git
cd backupSQL
```

2. Create a configuration file named `config.cfg`:

```ini
# config.cfg
# Email settings
ADMIN_EMAIL="[email protected]"
MAIL_SUBJECT_SUCCESS="MySQL Backup Success"
MAIL_SUBJECT_ERRORS="MySQL Backup Errors"
2. **Create Configuration Files:**

# SMTP settings
SMTP_HOST="smtp.yourprovider.com"
SMTP_PORT="587"
SMTP_USERNAME="your_smtp_username"
SMTP_PASSWORD="your_smtp_password"
Create a configuration file named `backup.cnf` with the following content:

```bash
# Backup settings
BACKUP_DIR="/opt/backup/sql/"
BACKUP_RETENTION_DAYS=30
# Telegram
TELEGRAM_TOKEN=
TELEGRAM_CHATID=
TELEGRAM_SERVERID=ServerName
TELEGRAM_ENABLE=false
```

Adjust the settings according to your environment.
### Backup settings

3. Create a MySQL client configuration file named `client.cnf` with appropriate MySQL credentials:
The options `BACKUP_DIR` and `BACKUP_RETENTION_DAYS` in the configuration file play a crucial role in the MySQL backup script. Here's a description of each:
```ini
# client.cnf
1. **`BACKUP_DIR="/opt/backup/sql/"`**
[client]
user=your_mysql_user
password=your_mysql_password
host=your_mysql_host # Add the MySQL host if it's not the local host
- This option specifies the directory where the MySQL backups will be stored.
- In the provided example, the backups will be saved in the `/opt/backup/sql/` directory.
- It's important to ensure that the specified directory exists and that the script has the necessary permissions to write to it.
- Customize this option based on your server's file system structure and storage preferences.
# Additional MySQL configurations if needed
```
2. **`BACKUP_RETENTION_DAYS=30`**
Replace `your_mysql_user`, `your_mysql_password`, and `your_mysql_host` with your MySQL credentials.
- This option determines the retention period for the backups, specifically how many days the backups will be retained before being considered for deletion.
- In the provided example, backups older than 30 days will be removed from the backup directory.
- Adjust this value based on your backup retention policy and available storage capacity.
- A longer retention period provides a historical backup archive, while a shorter period might be necessary for systems with limited storage.
### Set Executable Permissions
These configuration options allow you to tailor the backup script to your specific storage and retention requirements. It's important to regularly monitor the available disk space and adjust the retention period accordingly to avoid unnecessary storage consumption.

Make sure the script is executable by running the following command:
### Telegram

```bash
chmod +x backupSql
TELEGRAM_TOKEN=
TELEGRAM_CHATID=
TELEGRAM_SERVERID=ServerName
TELEGRAM_ENABLE=false
```

Create a MySQL client configuration file named `client.cnf` with appropriate MySQL credentials:

```bash
[client]
user=your_mysql_user
password=your_mysql_password
host=localhost
```

### Set Executable Permissions

Make sure the script is executable by running the following command:

`chmod +x backupSql`

### Usage

Run the backup script:

```bash
./backupSql
```
`./backupSql`

### Schedule the Script with Cron

To run the script automatically at a specific time each day, you can use cron. Open the crontab file for editing:

```bash
crontab -e
```
`crontab -e`

Add the following line to run the script, for example, every day at midnight:

```bash
0 0 * * * /path/to/your/script/backupSql
```
`0 0 * * * /path/to/your/script/backupSql`

This cron expression represents "every day at midnight." Adjust the timing as needed. Save and exit the editor.

For additional help with cron expressions, you can use online tools like [CronTab Guru](https://crontab.guru/) to generate the appropriate schedule.

Remember to replace /path/to/your/script/ with the actual path to your script.
Remember to replace `/path/to/your/script/` with the actual path to your script.

Now, the backup script will be executed automatically according to the specified schedule.

# Setting Up a Telegram Bot

Telegram bots are special accounts designed to perform specific tasks. In this tutorial, we'll walk through the process of creating a Telegram bot and obtaining the necessary credentials to integrate it with your MySQL backup script.
## Step 1: Create a Telegram Account
If you don't have a Telegram account, download the Telegram app from [official website](https://telegram.org/) or your app store and create an account.

## Step 2: Create a New Telegram Bot

1. Open Telegram and search for the "BotFather" bot. This bot is responsible for creating and managing other bots.

2. Start a chat with the BotFather and use the `/newbot` command to create a new bot.

3. Follow the instructions to choose a name and username for your bot. Once completed, the BotFather will provide you with a unique API token for your bot.

## Step 3: Obtain Your Telegram Chat ID

To receive messages from your bot, you need to know your Telegram chat ID.

1. Start a chat with your newly created bot.

2. Send any message to the bot.

3. Open a web browser and go to the following URL, replacing `YOUR_BOT_TOKEN` with the actual token obtained from the BotFather:

`https://api.telegram.org/botYOUR_BOT_TOKEN/getUpdates`

4. Look for the `"chat"` object in the response. The value of `"id"` inside the `"chat"` object is your Telegram chat ID.

## Step 4: Update Your Backup Configuration

Now that you have the Telegram bot token and chat ID, update your `backup.cnf` configuration file:

```ini
# Backup settings
BACKUP_DIR="/opt/backup/sql/"
BACKUP_RETENTION_DAYS=30
# Telegram
TELEGRAM_TOKEN=YOUR_BOT_TOKEN
TELEGRAM_CHATID=YOUR_CHAT_ID
TELEGRAM_SERVERID=ServerName
TELEGRAM_ENABLE=true
```

Replace `YOUR_BOT_TOKEN` and `YOUR_CHAT_ID` with the values obtained from the BotFather and your chat with the bot, respectively.

## Step 5: Enable Telegram Integration

Set `TELEGRAM_ENABLE=true` in your `backup.cnf` to enable Telegram integration.

```ini
# Telegram
TELEGRAM_ENABLE=true
```

## Step 6: Save and Run the Backup Script

Save your `backup.cnf` file and run the MySQL backup script. If everything is configured correctly, you should receive Telegram messages about the backup status.

----------

That's it! You've successfully set up a Telegram bot and integrated it with your MySQL backup script. Now, you'll receive status messages directly on Telegram.
9 changes: 9 additions & 0 deletions backup.cnf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Backup settings
BACKUP_DIR="/opt/backup/sql/"
BACKUP_RETENTION_DAYS=30

# Telegram
TELEGRAM_TOKEN=
TELEGRAM_CHATID=
TELEGRAM_SERVERID=ServerName
TELEGRAM_ENABLE=false
64 changes: 47 additions & 17 deletions backupSQL
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
TIMESTAMP=$(date +"%F")

# Include the backup configuration file
BACKUP_CONFIG="$(dirname "$0")/config.cnf"
BACKUP_CONFIG="$(dirname "$0")/backup.cnf"
source "$BACKUP_CONFIG"

# Include the client configuration file
Expand All @@ -13,55 +13,85 @@ MYSQL="/usr/bin/mysql"
MYSQLDUMP="/usr/bin/mysqldump"

# Define an array of databases to ignore during backup
IGNORED_DATABASES=("sys" "mysql" "information_schema" "performance_schema")
IGNORED_DATABASES=("sys" "mysql" "information_schema" "performance_schema" "phpmyadmin" "thewallpapers" "wallpapers76")

# Get a list of all databases and redirect stdout to /dev/null
all_databases=$("$MYSQL" --defaults-extra-file="$CLIENT_CONFIG" -e "SHOW DATABASES;" 2>/dev/null | grep -Ev "(Database)")

# Create an array to store databases with backup errors
databases_with_errors=()

# Function to send a message to Telegram
send_telegram_message() {
local message="$1"

curl -s -X POST "https://api.telegram.org/bot$TELEGRAM_TOKEN/sendMessage" \
-d "chat_id=$TELEGRAM_CHATID" \
-d "text=$message"
}

# Function to check MySQL connection
check_mysql_connection() {
if "$MYSQL" --defaults-extra-file="$CLIENT_CONFIG" -e "SELECT 1;" 2>/dev/null; then
if "$MYSQL" --defaults-extra-file="$CLIENT_CONFIG" -e "SELECT 1;" >/dev/null 2>&1; then
echo "MySQL connection successful."
else
echo "Error: Unable to connect to MySQL. Check credentials and permissions."
echo "Unable to connect to MySQL. Check credentials and permissions." | /usr/sbin/ssmtp -s "MySQL Connection Error" "$ADMIN_EMAIL"
exit 1
fi
}

# Check MySQL connection before proceeding
check_mysql_connection

# Print table header
printf "|---------------------------|---------------------------|\n"
printf "| %-25s | %-25s |\n" "Database" "Status"
printf "|---------------------------|---------------------------|\n"

# Loop through each database and perform a backup if not in the IGNORED_DATABASES array
for db in $all_databases; do
if [[ ! " ${IGNORED_DATABASES[@]} " =~ " $db " ]]; then
echo "Backing up database: $db"
status="Success"

# Perform mysqldump and check for errors
if "$MYSQLDUMP" --defaults-extra-file="$CLIENT_CONFIG" --default-character-set=utf8mb4 --single-transaction --force --opt --databases "$db" | gzip > "$BACKUP_DIR$db-$TIMESTAMP.sql.gz"; then
echo "Backup successful for database: $db"
else
echo "Error during backup for database: $db"
if ! "$MYSQLDUMP" --defaults-extra-file="$CLIENT_CONFIG" --default-character-set=utf8mb4 --single-transaction --force --opt --databases "$db" | gzip > "$BACKUP_DIR$db-$TIMESTAMP.sql.gz"; then
status="Error"
# Add the database to the list of databases with errors
databases_with_errors+=("$db")
fi

# Print colored table row
if [ "$status" == "Success" ]; then
message="Backup successful for database: $db"
printf "| \033[0;32m%-25s\033[0m | \033[0;32m%-25s\033[0m |\n" "$db" "$status"
elif [ "$status" == "Error" ]; then
message="Error during backup for database: $db"
printf "| \033[0;31m%-25s\033[0m | \033[0;31m%-25s\033[0m |\n" "$db" "$status"
else
message="Skipped backup for ignored database: $db"
printf "| \033[0;33m%-25s\033[0m | \033[0;33m%-25s\033[0m |\n" "$db" "Skipped"
fi

else
echo "Skipping backup for ignored database: $db"
# Print colored table row for ignored database
printf "| \033[0;33m%-25s\033[0m | \033[0;33m%-25s\033[0m |\n" "$db" "Skipped"
fi
done

# Remove backups older than the specified retention period
find "$BACKUP_DIR" -type f -mtime +$BACKUP_RETENTION_DAYS -exec rm {} \;
printf "|---------------------------|---------------------------|\n"

# Send an email with the list of databases that encountered errors
# Display the list of databases that encountered errors
if [ ${#databases_with_errors[@]} -gt 0 ]; then
echo "Databases with backup errors:" | /usr/sbin/ssmtp -s "$MAIL_SUBJECT_ERRORS" "$ADMIN_EMAIL"
printf '%s\n' "${databases_with_errors[@]}" | /usr/sbin/ssmtp -s "$MAIL_SUBJECT_ERRORS" "$ADMIN_EMAIL"
message="Databases with backup errors on $TELEGRAM_SERVERID:\n${databases_with_errors[@]}"
echo -e "\n$message"

# Send the message to Telegram if TELEGRAM_ENABLE is true
if [ "$TELEGRAM_ENABLE" == "true" ]; then
send_telegram_message "$message" >/dev/null 2>&1
fi
else
echo "All backups completed successfully." | /usr/sbin/ssmtp -s "$MAIL_SUBJECT_SUCCESS" "$ADMIN_EMAIL"
message="All backups completed successfully on $TELEGRAM_SERVERID."
echo -e "\n$message"
fi

exit 0
exit 0
14 changes: 0 additions & 14 deletions config.cnf

This file was deleted.

0 comments on commit 12588fd

Please sign in to comment.