cd /data/shell/ vim auto_backup_mysql.sh #!/bin/bash #auto backup mysql db #by authors kanghui 2016-6-12 #define backup path BAK_DIR = / data / backup / `date +% Y% m% d` MYSQLDB=duscyz #Database MYSQLUSR=root #username MYSQLPW=123456 #password MYSQLCMD=/usr/bin/mysqldump #Backup command if [ $UID -ne 0 ]; then #if not equal to 0 echo "Must to be use root for exec shell...." exit be if [ ! -d $BAK_DIR ];then #if this directory does not exist mkdir -p $BAK_DIR #Then create this directory echo -e "\033[32mThe $BAK_DIR Create Successfully!\033[0m" else echo "This $BAK_DIR is exists....." be #Mysql Bachkup command mysqldump -u$MYSQLUSR -pMYSQLPW -d $MYSQLDB >$BAK_DIR/MYSQLDB.sql if [ $? -eq 0 ];then #equal to 0, the following information will be output if the backup is successful ech0 -e "\033[32mThe Mysql Backup $MYSQLDB successfully!\033[0m" else #Otherwise print failure and output the following information echo -e "\033[32mThe Mysql Backup $MYSQLDB Failed,please check...033[0m" be
add crontab -e
0 0 * * * /bin/bash /data/shell/auto_backup_mysql.sh >>/tmp/mysql_bak.log