Mysql 自动备份脚本

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  #数据库
MYSQLUSR=root   #用户名
MYSQLPW=123456  #密码
MYSQLCMD=/usr/bin/mysqldump  #备份命令


if [ $UID -ne 0 ]; then #如果不等于0    
     echo "Must to be use root for exec shell...."
     exit
fi

if [ ! -d $BAK_DIR ];then    #如果这个目录不存在 
         mkdir -p $BAK_DIR   #那就创建这个目录
         echo -e "\033[32mThe $BAK_DIR Create Successfully!\033[0m"
else  
     echo "This $BAK_DIR is exists....."
fi
#Mysql Bachkup command
mysqldump -u$MYSQLUSR -pMYSQLPW -d $MYSQLDB >$BAK_DIR/MYSQLDB.sql

if [ $? -eq 0 ];then #等于0备份成功就输出以下信息
     ech0 -e "\033[32mThe Mysql Backup $MYSQLDB successfully!\033[0m"  
else   #否则打印失败输出以下信息
     echo -e "\033[32mThe Mysql Backup $MYSQLDB Failed,please check...033[0m"
fi  

 加入crontab -e

0 0 * * * /bin/bash /data/shell/auto_backup_mysql.sh >>/tmp/mysql_bak.log

 

你可能感兴趣的:(Mysql 自动备份脚本)