美文网首页
linux下通过脚本备份数据库笔记!

linux下通过脚本备份数据库笔记!

作者: DragonersLi | 来源:发表于2019-12-22 13:46 被阅读0次
命令行执行备份数据库
[root@launch-advisor-20190916 home]# mysqldump -h 127.0.0.1 -u root -pPWD --databases DBNAME> /home/backup_name.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@launch-advisor-20190916 home]# 

脚本执行备份数据库
vim mysql_backup.sh内容如下,给执行权限chmod -R 777 mysql_backup.sh,执行./mysql_backup.sh
#!/bin/bash
My_user='root'
MY_pwd='password'
MY_host='127.0.0.1'
MY_baktime=`date +%Y%m%d%H%M`
full_backup_dir=/home/
dbs=(seavisa)
if [ ! -d $full_backup_dir ];then
    mkdir -p $full_backup_dir
fi
backup_dir=$full_backup_dir/$(date +%Y%m%d)
for db in ${dbs[*]}
do
    if [ ! -d $backup_dir ]; then
        mkdir -p $backup_dir
    fi
    mysqldump -h ${MY_host} -u ${My_user} -p${MY_pwd} --databases $db > $backup_dir/$db-$MY_baktime.sql
done
###clean dump data
#oldData=`date --date='7 day ago' +%Y%m%d`
#rm -rf ${full_backup_dir}${oldData}
#find /mydata/backup -type f -mtime +30 -exec rm  {} \;
#echo "删除最近7天的备份


把脚本加入crontab定时执行
[root@launch-advisor-20190916 home]# vim /etc/crontab 

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
0 0 * * * root /bin/bash /home/mysql_backup.sh >> /home/mysql_backup.log 2>&1
 


相关文章

网友评论

      本文标题:linux下通过脚本备份数据库笔记!

      本文链接:https://www.haomeiwen.com/subject/nxidoqtx.html