-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
183 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,110 @@ | ||
# backupSQL | ||
# 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. | ||
|
||
## 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. | ||
|
||
## Installation | ||
|
||
### Prerequisites | ||
|
||
- **MySQL Client Tools**: Ensure that `mysql` and `mysqldump` binaries are installed on your system. | ||
- **ssmtp**: Install `ssmtp` for sending email notifications. | ||
|
||
```bash | ||
# On Debian/Ubuntu | ||
sudo apt-get install ssmtp | ||
|
||
# On Red Hat-based systems | ||
sudo yum install ssmtp | ||
``` | ||
|
||
### Configuration | ||
|
||
1. Clone the repository: | ||
|
||
```bash | ||
git clone https://github.com/yourusername/mysql-backup-script.git | ||
cd mysql-backup-script | ||
``` | ||
|
||
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" | ||
# SMTP settings | ||
SMTP_HOST="smtp.yourprovider.com" | ||
SMTP_PORT="587" | ||
SMTP_USERNAME="your_smtp_username" | ||
SMTP_PASSWORD="your_smtp_password" | ||
# Backup settings | ||
BACKUP_DIR="/opt/backup/sql/" | ||
BACKUP_RETENTION_DAYS=30 | ||
``` | ||
|
||
Adjust the settings according to your environment. | ||
|
||
3. Create a MySQL client configuration file named `client.cnf` with appropriate MySQL credentials: | ||
|
||
```ini | ||
# client.cnf | ||
[client] | ||
user=your_mysql_user | ||
password=your_mysql_password | ||
host=your_mysql_host # Add the MySQL host if it's not the local host | ||
# Additional MySQL configurations if needed | ||
``` | ||
|
||
Replace `your_mysql_user`, `your_mysql_password`, and `your_mysql_host` with your MySQL credentials. | ||
|
||
### Set Executable Permissions | ||
|
||
Make sure the script is executable by running the following command: | ||
|
||
```bash | ||
chmod +x backupSql | ||
``` | ||
|
||
### Usage | ||
|
||
Run the backup script: | ||
|
||
```bash | ||
./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 | ||
``` | ||
|
||
Add the following line to run the script, for example, every day at midnight: | ||
|
||
```bash | ||
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. | ||
|
||
Now, the backup script will be executed automatically according to the specified schedule. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,67 @@ | ||
#!/bin/bash | ||
|
||
# Set the timestamp | ||
TIMESTAMP=$(date +"%F") | ||
BACKUP_DIR="/path/to/backup/sql/" | ||
CONFIG_FILE="client.cnf" | ||
|
||
# Include the backup configuration file | ||
BACKUP_CONFIG="$(dirname "$0")/config.cnf" | ||
source "$BACKUP_CONFIG" | ||
|
||
# Include the client configuration file | ||
CLIENT_CONFIG="$(dirname "$0")/client.cnf" | ||
MYSQL="/usr/bin/mysql" | ||
MYSQLDUMP="/usr/bin/mysqldump" | ||
|
||
datum=`/bin/date +%Y%m%d` | ||
# Define an array of databases to ignore during backup | ||
IGNORED_DATABASES=("sys" "mysql" "information_schema" "performance_schema") | ||
|
||
# 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=() | ||
|
||
databases=`$MYSQL --defaults-extra-file=$CONFIG_FILE -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema)"` | ||
# Function to check MySQL connection | ||
check_mysql_connection() { | ||
if "$MYSQL" --defaults-extra-file="$CLIENT_CONFIG" -e "SELECT 1;" 2>/dev/null; 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 | ||
} | ||
|
||
for db in $databases; do | ||
echo "Backuping database: " $db | ||
$MYSQLDUMP --defaults-extra-file=$CONFIG_FILE --column-statistics=0 --single-transaction --force --opt --databases ${db} | gzip > "$BACKUP_DIR$db-${datum}.sql.gz" | ||
# Check MySQL connection before proceeding | ||
check_mysql_connection | ||
|
||
# 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" | ||
|
||
# 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" | ||
# Add the database to the list of databases with errors | ||
databases_with_errors+=("$db") | ||
fi | ||
else | ||
echo "Skipping backup for ignored database: $db" | ||
fi | ||
done | ||
|
||
### Remove files older than 30 days ### | ||
#for file in "$( /usr/bin/find $BACKUP_DIR -type f -mtime +30 )" | ||
#do | ||
# rm $file | ||
#done | ||
# Remove backups older than the specified retention period | ||
find "$BACKUP_DIR" -type f -mtime +$BACKUP_RETENTION_DAYS -exec rm {} \; | ||
|
||
# Send an email with 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" | ||
else | ||
echo "All backups completed successfully." | /usr/sbin/ssmtp -s "$MAIL_SUBJECT_SUCCESS" "$ADMIN_EMAIL" | ||
fi | ||
|
||
exit 0 | ||
exit 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
[client] | ||
user = backuper | ||
password = password | ||
host = 127.0.0.1 | ||
user=username | ||
password=password | ||
host=localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Backup settings | ||
BACKUP_DIR="/opt/backup/sql/" | ||
BACKUP_RETENTION_DAYS=30 | ||
|
||
# Email settings | ||
ADMIN_EMAIL="[email protected]" | ||
MAIL_SUBJECT_SUCCESS="MySQL Backup Success" | ||
MAIL_SUBJECT_ERRORS="MySQL Backup Errors" | ||
|
||
# SMTP settings | ||
SMTP_HOST= | ||
SMTP_PORT= | ||
SMTP_USERNAME= | ||
SMTP_PASSWORD= |