您的当前位置:首页mysql的常规定时备份

mysql的常规定时备份

2024-12-14 来源:哗拓教育
#!/bin/bash
############################################################
# $Name:         sc_backup.sh
# $Version:      v1
# $Function:     backup dabase sc 
# $Author:       Scott.wang
# $organization: ****
# $Create Date:  2018年9月20日 23:22:59
# $Description:  full backup database sc on everyday
############################################################

set -o errexit
set -o nounset

dat=`/bin/date  '+%F %H:%M:%S'`
USER="sc"
PASSWORD="xxx"
DATABASE="sc"
PATH="/data/backup-mysql"
DISK_USAGE="90"
LOG="/data/backup-mysql/backup_sc.log"


# 备份sc数据库
backup_sc(){

    /usr/local/mysqlbase/bin/mysqldump -R -u"${USER}" -p"${PASSWORD}"   --databases "${DATABASE}"  --socket=/data/mysql/3306/base/mysql.sock  > "${PATH}"/sc_$(/bin/date +%F_%H_%M_%S).bak 
    if [[ $? -eq 0 ]]; then
            echo "$dat Backup database sc success."  >> "${LOG}"
        else
            echo "$dat Backup database sc failed."   >> "${LOG}"
    fi

}

# 在备份目录下,删除距离当天5天前的文件,请勿随意修改下方内容
del_backup(){

    /bin/find "${PATH}" -type f -mtime +7 -name "sc_*.bak" | /usr/bin/xargs /bin/rm -rf  
    if [[ $? -eq 0 ]]; then
            echo "$dat Del backup file before 5 days success." >> "${LOG}"
        else
            echo "$dat Del backup file before 5 days failed."  >> "${LOG}"
    fi
}

# 服务器磁盘空间自检
inspect_disk_usage_on_local(){

    disk_usage=`/bin/df -P | /bin/grep "/dev" | /bin/grep -w "/" | /bin/awk '{print $5}' | /bin/cut -d"%" -f1`
    if [[ ${disk_usage} -gt ${DISK_USAGE} ]];then
        echo "$dat Mounted point [/data] disk usage have exceed ${DISK_USAGE}% on localhost, please adjust it."  >> "${LOG}"
        exit 1
    else
        echo "$dat Inspect mounted point [/data] disk usage on localhost success."  >> "${LOG}"
    fi
}

main(){
    inspect_disk_usage_on_local
    backup_sc
    del_backup
}
main

设置定时任务:
0 */2 * * * /bin/bash /data/.sc_backup.sh >/dev/null 2>&1

显示全文