一、安装crontabs
crond是linux系统中用来定期执行命令或者指定程序任务的一种服务或者软件。
(用rpm -qa | grep crontab查看是否安装,如果没有安装就执行 yum install -y crontabs )
[root@izbp1938t1plpjxn10fcqwz ~]# yum install crontab -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
No package crontab available.
Error: Nothing to do
[root@izbp1938t1plpjxn10fcqwz ~]# yum install crontabs -y
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Package crontabs-1.11-6.20121102git.el7.noarch already installed and latest version
Nothing to do
二、编写MySQL备份脚本
#!/bin/bash
#获取当前时间
date_now=$(date "+%Y%m%d%H%M%S")
backUpFolder=/data/backup/mysql
username="root"
password="brillilab_MYSQL_01"
db_name="brill_lab"
#定义备份文件名
fileName="${db_name}_${date_now}.sql"
#定义备份文件目录
backUpFileName="${backUpFolder}/${fileName}"
echo "starting backup mysql ${db_name} at ${date_now}."
/usr/bin/mysqldump -u${username} -p${password} --lock-all-tables --flush-logs --master-data=1 ${db_name} > ${backUpFileName}
#进入到备份文件目录
cd ${backUpFolder}
#压缩备份文件
tar zcvf ${fileName}.tar.gz ${fileName}
rm -rf ${fileName}
# use nodejs to upload backup file other place
#NODE_ENV=$backUpFolder@$backUpFileName node /home/tasks/upload.js
date_end=$(date "+%Y%m%d-%H%M%S")
echo "finish backup mysql database ${db_name} at ${date_end}."
三、添加任务到crontabs
[root@iZbp1gwwsckn9afrdy0itkZ ~]# crontab -e
加入以下行
0 0 * * 0 sh /root/mysqlBackup.sh
检查结果
[root@iZbp1gwwsckn9afrdy0itkZ ~]# crontab -l
0 0 * * 0 sh /root/mysqlBackup.sh
完成!