时间:2022-10-26 09:57:47 | 栏目:Linux | 点击:次
一、创建backup.sh脚本文件
#!/bin/sh SOURCE_FOLDER=/data DB_FOLDER=/data/db BACKUP_FOLDER=/data/backup TM=`date +%Y%m%d%H%M%S` echo start to dump mysql database and backup files at $TM # delete old sql backup files cd $DB_FOLDER rm -rf $DB_FOLDER/*.sql #dump mysql data /usr/bin/mysqldump -h127.0.0.1 -uroot -pcz_pims@2018 --single-transaction --master-data=2 --databases ms > ms-$TM.sql echo dump db pims_hf as pims_hf-$TM.sql /usr/bin/mysqldump -h127.0.0.1 -uroot -ppass --single-transaction --master-data=2 --databases ms_workflow > pims_hf_workflow-$TM.sql echo dump db ms_workflow as pims_hf_workflow-$TM.sql /usr/bin/mysqldump -h127.0.0.1 -uroot -ppass --single-transaction --master-data=2 --databases files > files-$TM.sql echo dump db filesrv as files-$TM.sql # delete backup files which 30 days before than today find $BACKUP_FOLDER/* -type f -mtime +30 -name "*.zip" -exec rm {} \; # compress cd $SOURCE_FOLDER zip -q -r $BACKUP_FOLDER/db-$TM.zip db zip -q -r $BACKUP_FOLDER/files-new.zip files echo zip $SOURCE_FOLDER files and backup to $BACKUP_FOLDER end # 调用系统方式 send notice msg to 钉钉通知 curl -X POST -d "templateCode=003&content=文件备份成功,备份文件保存到此目录: $BACKUP_FOLDER" http://127.0.0.1:8080/admin/do/notify/dingding echo -e end backup files at $TM '\n\n\n'
二、创建定时任务
1)、在命令行输入: crontab -e 然后添加相应的任务,wq存盘退出。
2)、直接编辑/etc/crontab 文件,即vi /etc/crontab,添加相应的任务。
crontab文件格式:
* * * * * command
minute hour day month week command
分 时 天 月 星期 命令
例如:每周每月每天1点0分执行备份脚本,日志放在backup.log文件中
0 1 * * * /bin/sh /home/script/backup.sh >> /home/script/backup.log 2>&1
若测试脚本是否正常运行,可以手动执行测试
3)、查询当前用户定时任务。
总结