欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux mysql 备份脚本

程序员文章站 2022-07-12 22:02:47
...
#!/bin/bash
#################################################################
#Script for bak db for sql file every day
#Written by toptreegzh (toptreegzh@163.com)
#at 2010-09-03
#################################################################
db=数据库
dbUser=用户名
dbPassword=密码
basePath=备份路径

currDate=`date +%Y%m%d_%H`
currFile=${basePath}${db}_${currDate}.sql

# gets the size of last backuped file.
lastSize=`ls -ltr ${basePath}${db}*.sql.tz|tail -1|awk '{print $5}'`

currDate=`date +%Y%m%d_%H`
currFile=${basePath}/${db}_${currDate}.sql

# dump data from mysql
mysqldump -u${dbUser} -p${dbPassword} --default-character-set=utf8 --opt --extended-insert=false --triggers -R --hex-blob --single-transaction ${db} > ${currFile}

# backup by tar
tar cfz ${currFile}.tz ${currFile} 1>/dev/null 2>/dev/null
rm -rf ${currFile}

currSize=`ls -l ${currFile}.tz|awk '{print $5}'`

# dispatches backuped file to others
if [ -z ${lastSize} ] || [ ${lastSize} -ne ${currSize} ]
then
        echo ""
        #scp 可以备份到其他主机
else
    echo "" > ${currFile}.tz
    mv ${currFile}.tz ${currFile}.tz.same
fi