CentOS 7下mysql数据库定时备份创建定时任务
1.创建定时任务脚本
vi /usr/bin/bakeup_mysql.sh
内容:
#!/bin/bash
#delete old bakeup
rm -rf /data/mysql_bak/*
backup_dir='/data/mysql_bak/'
current_time=$(date +'%Y-%m-%d_%H%M%S')
filepath=$backup_dir$current_time'.sql.gz'
s_ip='127.0.0.1'
username='root'
password='Sy@admin123'
source_database='senyuan'
echo "-----------------------------"$(date +%F%r)"bakeup starting-------------------------"
echo $(date +%F%r)"start data bakeuping......."
# 2>/dev/null
mysqldump -h${s_ip} -u${username} -p"${password}" ${source_database} | gzip > $filepath
echo $(date + %F%r)"finish sync......."
2.创建定时任务
1)修改定时任务
crontab -e
2)修改定时任务内容
00 00 * * * /usr/bin/bakeup_mysql.sh
3)查看定时任务
crontab -l