MySQL Backup: - If you are using MySQL to store information that means you can't lose anything from that database. For every Administrator, It is required to take backup of MySQL Database.
Here I am going to demonstrate, How to schedule backup of MySQL databases using bash script and send Email notification.
Prerequisites: -
1- On Linux Machine installed MySQL Server
2- Root access for MySQL Server
3- One user with sudoers privileges for SSH
4- SMTP Server details
Step 1: - Create bash script and configure Backup destination
Copy above script and paste into a file and save as Mysql_backup.sh.
Step 2: - Make Script executable
MySQL Backup script has been configured successfully with Email notification.
If you want to install SendEmail utility then you can have a look at blog link below.
https://linuxhowtoguide.blogspot.ca/2017/08/install-sendemail-simple-smtp-client.html
Thank you so much for reading this blog!
#--> User and host MyUSER="root" # USERNAME MyPASS="*****" # PASSWORD MyHOST="localhost" # Hostname HOST="$hostname" # Locations of binaries MYSQL="$(which mysql)" MYSQLDUMP="$(which mysqldump)" GZIP="$(which gzip)" #SMAIL="$(which sendEmail)" SMAIL="$(which sendEmail)" # Backup Dest directory, change this if you have someother location DEST="/var/backup" REPORT="/var/backup" # Get data in dd-mm-yyyy format NOW="$(date +"%m-%d-%Y")" # Main directory where backup will be stored MBD="$DEST/$NOW" # Get hostname HOST="$(hostname)" # File to store current backup file FILE="" # Store list of databases DBS="" # DO NOT BACKUP these databases IGNOREDBS=" mysql information_schema performance_schema " [ ! -d $MBD ] && mkdir -p $MBD || : # Get all database list first DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')" #DBS="$($MYSQL -u $MyUSER -h $MyHOST -Bse 'show databases')" for db in $DBS do skipdb=-1 if [ "$IGNOREDBS" != "" ]; then for i in $IGNOREDBS do [ "$db" == "$i" ] && skipdb=1 || : done fi if [ "$skipdb" == "-1" ] ; then FILE="$MBD/$db.$HOST.$NOW.sql.gz" # do all inone job in pipe, # connect to mysql using mysqldump for select mysql database # and pipe it out to gz file in backup dir :) $MYSQLDUMP -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > $FILE #$MYSQLDUMP -u $MyUSER -h $MyHOST $db | $GZIP -9 > $FILE fi done #--> Email Function du -sh $MBD/* | cat > $REPORT/$NOW"Backup-report".txt sendEmail -f "Backup Admin <user@domain.com>" -u "MySQL Backup" -m "Please review attached file" -t recipient@domain.com -s SMTP-HOST:PORT -a $REPORT/$NOW"Backup-report".txt
Copy above script and paste into a file and save as Mysql_backup.sh.
Step 2: - Make Script executable
chmod a+x Mysql_backup.sh
Step 3: - Schedule backup script in Cron Job list to run on specific time.crontab -e
# m h dom mon dow command
0 2 * * * /Path_to_Script/Mysql_backup.sh
The above Cron has been scheduled to run in Night at 2:00 AM.MySQL Backup script has been configured successfully with Email notification.
If you want to install SendEmail utility then you can have a look at blog link below.
https://linuxhowtoguide.blogspot.ca/2017/08/install-sendemail-simple-smtp-client.html
Thank you so much for reading this blog!
No comments:
Post a Comment