新建一个备份的脚本文件
/data/shell 路径
auto_backup_mysql.sh 文件名,备份脚本内容放入auto_backup_mysql.sh文件中
#!/bin/bash #auto backup mysql db #by authors wugk 2020 #define backup path BAK_DIR=/data/backup/`date +%Y%m%d` MYSQLDB=test MYSQLUSR=root MYSQLPW=admin 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 " 33[32mThe $BAK_DIR Create Successfully! 33[0m" else echo -e "This $BAK_DIR is exists..." fi #Mysql BACKUP COMMAND $MYSQLCMD -u$MYSQLUSR -p$MYSQLPW -d $MYSQLDB >$BAK_DIR/$MYSQLDB.sql if [ $? -eq 0 ];then echo -e " 33[32mThe Mysql Backup $MYSQLDB Successfully! 33[0m" else echo -e " 33[32mThe Mysql Backup $MYSQLDB Failed,Please check. 33[0m" fi
crontab -e 设置定时任务
0 0 * * * /bin/bash /data/shell/auto_backup_mysql.sh >>/tmp/mysql_bak.log 备份计划
待优化内容:没有压缩,没有设置保存多少天自动清除