linux mysql 备份脚本

#!/bin/bash
#################################################################
#Script for bak db for sql file every day
#Written by toptreegzh ([email protected])
#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

 

 

你可能感兴趣的:(sql,mysql,linux,脚本,bash)