#!/bin/bash # #################################################################### ## Shell script to backup all MySql database with single User ## ## MySQL Database Backup Script ## Written By: Amol Jhod ## URL: https://www.itsupportwale.com/blog/learn-how-to-backup-up-all-mysql-databases-using-a-single-user-with-a-simple-bash-script ## Last Update: Apr 25, 2019 ## ## For more scripts please visit : www.itsupportwale.com ## ##################################################################### ##################################################################### #### Caution : This script is takes backup of all databases ######### ############# on which the given user is having access. ########### ############## And Delete the backups older then BKP_DAYS ########## ##################################################################### ##################################################################### ########### You Have to Update the Below Values ##################### ##################################################################### # # BKP_USER="mysqlbackupuser" # Enter the username for backup BKP_PASS="$(cat $HOME/.password.txt)" # Enter the password of the backup user # BKP_DEST="$HOME/Documents/MySQL" # Enter the Backup directory,change this if you have someother location # ## Note: Scripts will delete all backup which are older then BKP_DAYS## # BKP_DAYS="2" # Enter how many days backup you want, # ########### Use This for only local server ############################# MYSQL_HOST="localhost" # # ######################################################################## ########### Thats Enough!! NO NEED TO CHANGE THE BELOW VALUES ########## ######################################################################## # ##################### Get Backup DATE ################################## # BKP_DATE="$(date +"%A_%d-%m-%Y_%H-%M-%S")"; #BKP_DATE="$(date +"%A_%d-%m-%Y_%T")"; # ########## Ignore these default databases shen taking backup ############ # IGNORE_DB="information_schema mysql performance_schema" # ########## Creating backup dir if not exist ############################# # [ ! -d $BKP_DEST ] && mkdir -p $BKP_DEST || : # ################# Autodetect the linux bin path ######################### MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" # notification() { if [ $3 -eq 0 ]; then sound="Glass" message="Envoi terminé sur $2 !" else sound="Basso" message="Echec lors de l'envoi sur $2 : erreur $result" fi if [[ "$OSTYPE" == "darwin"* ]] && [ -x "$(command -v terminal-notifier)" ]; then terminal-notifier -title "$1" -message "$message" -sound "$sound" fi } ###################### Get database list ################################ # DB_LIST="$($MYSQL -u $BKP_USER -h $MYSQL_HOST -p$BKP_PASS -Bse 'SHOW DATABASES')" # for db in $DB_LIST do skipdb=-1 if [ "$IGNORE_DB" != "" ]; then for i in $IGNORE_DB do [ "$db" == "$i" ] && skipdb=1 || : done fi if [ "$skipdb" == "-1" ] ; then BKP_FILENAME="$BKP_DEST/$db.$BKP_DATE.sql" BKP_GZ_FILENAME="$BKP_DEST/$db.$BKP_DATE.sql.gz" # ################ Using MYSQLDUMP for bakup and Gzip for compression ################### # #$MYSQLDUMP -u $BKP_USER -h $MYSQL_HOST -p$BKP_PASS $db | $GZIP -9 > $BKP_FILENAME $MYSQLDUMP -u $BKP_USER -h $MYSQL_HOST -p$BKP_PASS -r$BKP_FILENAME $db $GZIP -9 $BKP_FILENAME server1="ftp.cluster011.ovh.net:www/backup/SilverBook/Bases_MySQL/" scp "$BKP_GZ_FILENAME" funnymac@"$server1" result=$? notification "Backup MySQL: base $db" "$server1" $result sleep 1 server2="clicclac.synology.me:/volume1/Backup/SilverBook/Bases_MySQL/" scp -P42666 -p "$BKP_GZ_FILENAME" bruno@"$server2" result=$? notification "Backup MySQL: base $db" "$server2" $result fi done #########To delete all backup files older then BKP_DAYS ################# # find $BKP_DEST -type f -mtime +$BKP_DAYS -delete # # #### End of script ####