touch mysql_backup.sh

chmod +x mysql_backup.sh

vim mysql_backup.sh

#!/bin/bash  

id="root"

pwd="123456"

dbs="test  rockoa"          test rockoa为备份数据库

backuppath="/root/mysqlbackup"  备份路径自行设置,可自定义目录

day=15   

[ ! -d $backpath ] &&mkdir -p $backuppath    

cd $backuppath     

backupname=mysql_$(date +%Y-%m-%d)  

for db in $dbs;        

do  

   mysqldump -u$id -p$pwd -S /var/lib/mysql/mysql.sock $db >$backupname_$db.sql

   if [ "$?" == "0" ]    

   then 

      echo $(date +%Y-%m-%d)" $db  mysqldump sucess">>mysql.log

   else 

      echo $(date +%Y-%m-%d)"  $db mysql dump failed">>mysql.log 

      exit 0  

   fi   

done  

tar -czf $backupname.tar.gz *.sql   

if [ "$?" == "0" ]  

then 

     echo $(date +%Y-%m-%d)" tar sucess">>mysql.log

else     

   echo $(date +%Y-%m-%d)" tar failed">>mysql.log

   exit 0

fi

rm -f *.sql

delname=mysql_$(date -d "$day day ago" +%Y-%m-%d).tar.gz

rm -f $delname


sh mysql_backup.sh 

ll /root/mysqlbackup/

total 56

-rw-r--r-- 1 root root 52346 Sep 24 16:44 mysql_2016-09-24.tar.gz

-rw-r--r-- 1 root root   184 Sep 24 16:44 mysql.log


cd /root/mysqlbackup/

tar zxvf mysql_2016-09-24.tar.gz 

rockoa.sql

test.sql


vim /etc/crontab 

5  9-6  *  *  1-7 root sh /root/mysql_backup.sh

*/5  *  *  *  *  root ntpdate 服务器IP

ntpdate  服务器IP

clock -w

/etc/rc.d/init.d/ntpd restart    生产环境尽量使用reload

/etc/rc.d/init.d/crond restart

chkconfig  --level 35 ntpd on

chkconfig  --level 35 crond on