#!/bin/bash#file: mysql_backup.sh#date 2016-1-9backdes=/data/msyql_back/' date +%Y-%m-%d ' //Create the directory that holds the backup data backname= ' date +%y-%m-%d ' //backup name, You can omit the database=test //backed up database mysqluser=root // The user name at the time of backup #mysqlsec= //the password at the time of backup, consider security, General manual input if [ $UID -ne 0 ] ; then //this if statement to indicate that root user backup must be used echo " you must use root to backup!" sleep 2 exitfiif [ ! -d $BACKDES ] ; then // This segment if statement, which indicates that the directory where the data is stored does not exist, creates mkdir -p $BACKDESelse echo "This dir exists." ficd $BACKDES && /usr/bin/mysqldump -u $MYSQLUSER -p -d $ database > "$BACKNAME". sql //into the directory where the data is stored, the command that is currently executing the backup database if [ $? -le 0 ] ; then //This segment if statement indicates that the previous instruction executed successfully, Package. sql and prompt for export database success tar -czvf $BACKNAME. tar.gz "$BACKNAME" .sql echo "$DATABASE check is successful! " else echo "checkout $DATABASE is not successful! " Ficd /data/msyql_back/ && find . -type d -mtime +10 -exec rm -rf {} \; && ls //Delete the directory 10 days ago and view the current directory if [ $? -le 0 ] ; then //if the previous execution succeeds, prompt data backup completes, otherwise prompts to have the error, needs to check. echo "Congratulations, the data package backup successful! " else echo "Sorry, something error, must check it! " Fi
This article from "Experience from pain" blog, declined reprint!
Shell---mysql backup