#!/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_BASE_DIR=$(dirname "$0") # BKP_DEST="$HOME/Documents/MySQL" # Enter the Backup directory,change this if you have someother location if [ ! -d $BKP_DEST ]; then mkdir $BKP_DEST; fi # ## Note: Scripts will delete all backup which are older then BKP_DAYS## # # D:3 W:22 M:93 BKP_DAYS="3" # Enter how many days backup you want, BKP_WEEKS="22" BKP_MONTHS="93" # ########### 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")"; #lundi=date +%u if [ $(date +%d) = "01" ]; then # 1er du mois => octobre_2019 BKP_DATE="$(date +"%B_%Y")"; elif [ $(date +%u) -eq 1 ]; then # lundi => S44_2019 BKP_DATE="$(date +"S%V_%Y")"; else BKP_DATE="$(date +"%A_%d-%m-%Y_%H-%M-%S")"; # => Mercredi_30-10-2019_11-33-17 fi # ########## 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 !" image="$BKP_BASE_DIR/success.png" else sound="Basso" message="Echec lors de l'envoi sur $2 : erreur $result" image="$BKP_BASE_DIR/error.png" fi if [[ "$OSTYPE" == "darwin"* ]] && [ -x "$(command -v terminal-notifier)" ]; then terminal-notifier -title "$1" -message "$message" -sound "$sound" -contentImage "$image" 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 -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 # xxx.S44_2019.sql.gz # find . -name "*.sql.gz" -mtime +$BKP_WEEKS | grep -E 'S\d{2}_\d{4}' # xxx.octobre_2019.sql.gz # find . -name "*.sql.gz" -mtime +$BKP_MONTHS | grep -E 'janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre' # xxx.Mercredi_30-10-2019_11-33-17.sql.gz #find $BKP_DEST -name "*.sql.gz" -mtime +$BKP_DAYS | grep -v -E '(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)|(S\d{2}_\d{4})' > day.txt find -E $BKP_DEST -mtime +$BKP_DAYS -regex '.*(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)|(S\d{2}_\d{4})' > day.txt echo $? #find $BKP_DEST -name "*.sql.gz" -mtime +$BKP_WEEKS | grep -E 'S\d{2}_\d{4}' > week.txt find -E $BKP_DEST -mtime +$BKP_WEEKS -regex 'S\d{2}_\d{4}' > week.txt echo $? #find $BKP_DEST -name "*.sql.gz" -mtime +$BKP_MONTHS | grep -E '(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)' > month.txt find -E $BKP_DEST -mtime +$BKP_MONTHS -regex '.*(janvier|fevrier|mars|avril|mai|juin|juillet|aout|septembre|octobre|novembre|decembre)' > month.txt echo $? # find . -name "*.sql.gz" | grep -E 'Monday|Tuesday' | xargs rm # cd /volume1/Backup/SilverBook/Bases_MySQL # find . -type f -mtime +3 -name '*.sql.gz' -delete # find /volume1/Backup/SilverBook/Bases_MySQL -type f -mtime +1 -name '*.sql.gz' # ssh dsm916e 'find /volume1/Backup/SilverBook/Bases_MySQL -type f -mtime +1 -name "*.sql.gz" -delete' # #### End of script ####