mysql备份脚本(基础版)

#!/bin/bash
#authors misery 2019
#

BAK_DIR=/home/web_code1/backup/mysql_backup/`date +%Y-%m-%d`
MYSQL_CMD=/usr/bin/mysqldump
MYSQL_USER=root
MYSQL_PW=123456
MYSQL_DB=webapp

#判断用户是否正确
if [ $UID -ne 0 ];then
    echo -e "\033[1;31myou must be use root run '$0'\033[0m"
    exit
fi

#判断备份目录是否存在
if [ ! -d $BAK_DIR ];then
    mkdir -p $BAK_DIR
    echo -e "\033[32m$BAK_DIR Create new directory Successfully\033[0m"
else
    echo -e "\033[33mThis directory $BAK_DIR already exist\033[0m"
fi

# mysqldump -uroot -proot salt > $BAK_DIR/salt.sql
# 使用mysqldump工具执行备份命令
if [ -f $BAK_DIR/$MYSQL_DB.sql ];then
    echo -e "\033[33m$MYSQL_DB.sql Backup already exists,please check out.\033[0m"
    exit
else
    echo -e "\033[32mBackup is running...\033[0m"
    $MYSQL_CMD -u$MYSQL_USER -p$MYSQL_PW -d $MYSQL_DB > $BAK_DIR/$MYSQL_DB.sql
    if [ $? -eq 0 ];then
        echo -e "\033[32mMySQL backup successfully.\033[0m"
    else
        echo -e "\033[33mmysql backup failed,please check\033[0m"
    fi
fi

#加入定时任务:
# 0 0 * * * /bin/bash /root/auto_backup_mysql.sh >> /var/log/backup_mysql.log 2>&1

 

posted @ 2019-07-25 18:44 MiserySouL 阅读(...) 评论(...) 编辑 收藏