LINUX环境MySQL定时备份脚本
作者:互联网
(0)备份目标
备份:tel数据库
目录:/mysqlbackup/
策略:每天3点
(1)创建一个备份用户,用户名未backup 密码backup
mysql>grant all on tel. to 'backup'@'localhost' identified by "backup";
提供更小的权限:
mysql>grant select,show view,lock tables,trigger on tel. to 'backup'@'localhost' identified by "backup";
(2)编写备份脚本
shell> vim mysql_backup.sh
#!/bin/sh
###定义变量####
date=date +%Y%m%d
ago10date=date -d"10 day ago" +"%Y%m%d"
backuser=backup
db_host=localhost
backupdir=/mysqlbackup
###判断存放目录是否存在####
if [ ! -d $backupdir ]
then
mkdir -p $backupdir
fi
###判断第二级存放目录是否存在####
if [ ! -d $backupdir/$date ]
then
mkdir -p $backupdir/$date
fi
##########执行备份命令#############
/usr/bin/mysqldump -u$backuser -h $db_host -p'backup' --single-transaction tel > "$backupdir/$date/tel.sql"
##########删除10天前的备份#########
if [ -d $backupdir/$ago10date ]
then
rm -rf $backupdir/$ago10date
fi
(3)添加执行权限
shell>chmod +x mysql_backup.sh
(4)添加定时任务
shell> crontab -l
- 3 * /usr/bin/sh /mysqlbackup/mysql_backup.sh
标签:tel,备份,LINUX,sh,MySQL,date,backup,backupdir 来源: https://blog.51cto.com/7603402/2438175