【shell脚本】自动备份mysql数据库auto_backup_mysql.sh

步骤:

1)定义变量

2)判断是否是root用户

3)判断目录是否存在

4)使用mysql命令备份数据库

5)制定定时任务,每天凌晨自动备份

[root@rhel8 shell]# cat auto_backup_mysql.sh 
#!/bin/bash
# auto backup mysql db
# bY authors tanbaobao 2020/06/08

# define backup path
BAK_DIR=/data/backup/`date +%Y%m%d`
MYSQLDB=phpshe
MYSQLUSER=root
MYSQLPW=000000
MYSQLCMD=/usr/bin/mysqldump

if [ $UID -ne 0 ];then
    echo "Must to be use root for exec shell."
    exit
fi

if [ ! -d $BAK_DIR ];then
    mkdir -p $BAK_DIR
    echo -e "\033[32mThe $BAK_DIR Create Successfully!\033[0m"
else
    echo "This $BAK_DIR is exists..."
fi

#Mysql BACKUP COMMAND
$MYSQLCMD -u$MYSQLUSER -p$MYSQLPW -d $MYSQLDB >$BAK_DIR/$MYSQLDB.sql

if [ $? -eq 0 ];then
    echo -e "\033[32mThe Mysql Backup $MYSQLDB Successfully!\033[0m"
else
    echo -e "\033[32mThe Mysql Backup $MYSQLDB Failed,Please check!\033[0m"
fi
[root@rhel8 shell]# crontab -e
0 0 * * * /bin/bash /tanbaobao/shell/auto_backup_mysql.sh >> /tmp/mysql_bak.log

 

【shell脚本】自动备份mysql数据库auto_backup_mysql.sh

上一篇:SQL SERVER 和 MY SQL 怎么选择


下一篇:Badboy安装和介绍