Initial commit
This commit is contained in:
97
backup_mysql.sh
Executable file
97
backup_mysql.sh
Executable file
@@ -0,0 +1,97 @@
|
||||
#!/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)"
|
||||
echo "$MYSQL"
|
||||
#
|
||||
###################### Get database list ################################
|
||||
#
|
||||
DB_LIST="$($MYSQL -u $BKP_USER -h $MYSQL_HOST -p$BKP_PASS -Bse 'SHOW DATABASES')"
|
||||
echo "$DB_LIST"
|
||||
#
|
||||
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
|
||||
|
||||
scp "$BKP_GZ_FILENAME" funnymac@ftp.cluster011.ovh.net:www/backup/
|
||||
fi
|
||||
done
|
||||
#########To delete all backup files older then BKP_DAYS #################
|
||||
#
|
||||
find $BKP_DEST -type f -mtime +$BKP_DAYS -delete
|
||||
#
|
||||
#
|
||||
#### End of script ####
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user