This script is designed to perform MySQL backups using mysqldump
and send notifications through Telegram. It supports configuration through dedicated configuration files.
- 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.
Ensure that the required dependencies are installed:
- MySQL Client Tools:
mysql
andmysqldump
binaries. - cURL: Used for Telegram integration.
-
Clone the Repository:
Clone the repository to your local machine:
git clone https://github.com/klevze/backupSQL.git cd backupSQL
-
Create Configuration Files:
Create a configuration file named
backup.cnf
with the following content:# Backup settings BACKUP_DIR="/opt/backup/sql/" BACKUP_RETENTION_DAYS=30 # Telegram TELEGRAM_TOKEN= TELEGRAM_CHATID= TELEGRAM_SERVERID=ServerName TELEGRAM_ENABLE=false
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:
BACKUP_DIR="/opt/backup/sql/"
- 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.
BACKUP_RETENTION_DAYS=30
- 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.
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.
TELEGRAM_TOKEN=
TELEGRAM_CHATID=
TELEGRAM_SERVERID=ServerName
TELEGRAM_ENABLE=false
Create a MySQL client configuration file named client.cnf
with appropriate MySQL credentials:
[client]
user=your_mysql_user
password=your_mysql_password
host=localhost
Make sure the script is executable by running the following command:
chmod +x backupSql
Run the backup script:
./backupSql
To run the script automatically at a specific time each day, you can use cron. Open the crontab file for editing:
crontab -e
Add the following line to run the script, for example, every day at midnight:
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 to generate the appropriate schedule.
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.
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.
If you don't have a Telegram account, download the Telegram app from official website or your app store and create an account.
-
Open Telegram and search for the "BotFather" bot. This bot is responsible for creating and managing other bots.
-
Start a chat with the BotFather and use the
/newbot
command to create a new bot. -
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.
To receive messages from your bot, you need to know your Telegram chat ID.
-
Start a chat with your newly created bot.
-
Send any message to the bot.
-
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
- Look for the
"chat"
object in the response. The value of"id"
inside the"chat"
object is your Telegram chat ID.
Now that you have the Telegram bot token and chat ID, update your backup.cnf
configuration file:
# 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.
Set TELEGRAM_ENABLE=true
in your backup.cnf
to enable Telegram integration.
# Telegram
TELEGRAM_ENABLE=true
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.