首页 > 代码库 > mysql备份脚本

mysql备份脚本

touch mysql_backup.sh

chmod +x mysql_backup.sh

vim mysql_backup.sh

#!/bin/bash  

id="root"

pwd="123456"

dbs="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 /root/mysql_backup.sh

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

本文出自 “mailfile” 博客,谢绝转载!

mysql备份脚本