一、这个是在哔哩哔哩看到的脚本,下面把网址贴出来(建议对着视频看一遍作者的解释)
https://www.bilibili.com/video/av17383342/?p=4
二、这个是脚本内容
#!/bin/bash
#Author By Cheery 2018/10/17
#Auto Backup Mysql DB
#Define backup path
BAK_DIR=/home/data/backup/`date +%Y%m%d`
MYSQLDB=shelltest
MYSQLUSR=root
MYSQLPW=root
MYSQLCMD=/usr/bin/mysqldump
# 1.判断是否为系统用户登录,如果不是就结束
if [ $UID -ne 0 ];then
echo "Must use root for exec shell"
exit
fi
# 2.判断改路径是否存在
if [ ! -d $BAK_DIR ];then
mkdir -p $BAK_DIR
echo "\033[32mThe $BAK_DIR Create Successfully!"
else
echo -e "This $BAK_DIR is exists..."
fi
# 3.执行Mysql备份命令
$MYSQLCMD -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB > $BAK_DIR/$MYSQLDB.sql
# 4.判断是否执行成功
if [ $? -eq 0 ];then
echo -e "\033[32mThe MysqlDB Backup Successful!\033[0m"
else
echo -e "\033[32mThe MysqlDb Backup Failed!\033[0m"
fi