MySQL MariaDB Backup Script for daily backup
#!/bin/bash
SERVER_NAME="Name of the server"
DB_USER=''
DB_PASS=''
DB_HOST='localhost'
# Number of days to store the backup
KEEP_BACKUP_IN_DAY=30
BACKUP_LOCATION="/opt/backup/mysql"
DATABASE_LIST="test1 test2" # Add Database name with space
# Starting Backup Script
mkdir -p "$BACKUP_LOCATION"
for database_name in $DATABASE_LIST; do
echo "Currently taking backup of : $database_name"
BACKUP_PATH="$BACKUP_LOCATION/$database_name"
mkdir -p "$BACKUP_PATH"
BACKUP_FILE="$BACKUP_PATH/$database_name-`date +\%Y-\%m-\%d_\%H-\%M-\%S`.sql.gz"
mysqldump --user="$DB_USER" --password="$DB_PASS" --host="$DB_HOST" "$database_name" --triggers --routines --events | gzip -c > "$BACKUP_FILE"
if [ $? == 0 ]; then
echo 'Successfully Taken Dump'
else
echo "Unable to take backup of $database_name"
fi
done
# Delete old backups
find "$BACKUP_LOCATION" -mtime +$KEEP_BACKUP_IN_DAY -delete
Here
-
SERVER_NAME : Name of the server
-
DB_USER : Database username
-
DB_PASS : Database password
-
DB_HOST : Database hostname
-
KEEP_BACKUP_IN_DAY : How many days store the old backup
-
BACKUP_LOCATION : Where to store backup
-
DATABASE_LIST : Space separated database name, those are going take backup