diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..722d5e7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.vscode diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..577d428 --- /dev/null +++ b/LICENSE @@ -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. \ No newline at end of file diff --git a/README.md b/README.md index e841280..5bf0f74 100644 --- a/README.md +++ b/README.md @@ -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="your_email@example.com" - 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. diff --git a/backup.cnf b/backup.cnf new file mode 100644 index 0000000..3ddf989 --- /dev/null +++ b/backup.cnf @@ -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 \ No newline at end of file diff --git a/backupSQL b/backupSQL index 339ad69..a5555a4 100755 --- a/backupSQL +++ b/backupSQL @@ -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 @@ -13,7 +13,7 @@ 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)") @@ -21,13 +21,21 @@ all_databases=$("$MYSQL" --defaults-extra-file="$CLIENT_CONFIG" -e "SHOW DATABAS # 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 } @@ -35,33 +43,55 @@ check_mysql_connection() { # 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 \ No newline at end of file +exit 0 diff --git a/config.cnf b/config.cnf deleted file mode 100644 index f656fe7..0000000 --- a/config.cnf +++ /dev/null @@ -1,14 +0,0 @@ -# Backup settings -BACKUP_DIR="/opt/backup/sql/" -BACKUP_RETENTION_DAYS=30 - -# Email settings -ADMIN_EMAIL="info@example.com" -MAIL_SUBJECT_SUCCESS="MySQL Backup Success" -MAIL_SUBJECT_ERRORS="MySQL Backup Errors" - -# SMTP settings -SMTP_HOST= -SMTP_PORT= -SMTP_USERNAME= -SMTP_PASSWORD= \ No newline at end of file