步骤:
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 "