diff --git a/README.md b/README.md index d721738..e841280 100644 --- a/README.md +++ b/README.md @@ -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="your_email@example.com" + 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. diff --git a/backupSQL b/backupSQL index ea191a5..339ad69 100755 --- a/backupSQL +++ b/backupSQL @@ -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 \ No newline at end of file diff --git a/client.cnf b/client.cnf index fe3d4df..65efbd9 100644 --- a/client.cnf +++ b/client.cnf @@ -1,4 +1,4 @@ [client] -user = backuper -password = password -host = 127.0.0.1 +user=username +password=password +host=localhost diff --git a/config.cnf b/config.cnf new file mode 100644 index 0000000..f656fe7 --- /dev/null +++ b/config.cnf @@ -0,0 +1,14 @@ +# 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