上周学习了Linux,其中有使用crontab定时备份日志的内容,现把主要步骤记录以下:shell
首先须要备份的日志的源目录位于/opt/lampp/logs/access_logbash
备份到/tmp/logs下学习
备份文件加上时间戳date +%Y%m%d%H%M%Sspa
以上是备份说明,下面分三步操做日志
1、shell脚本blog
[root@CentOs6 lampp]# vi test.sh #建立shell脚本crontab
#!/bin/bash #编译器
mypath='/tmp/logs' #日志备份到该目录下,定义变量使用单引号
echo ${mypath} #回应/tmp/logs编译器
mylog='/opt/lampp/logs/access_log' #咱们要备份的日志
echo ${mylog} #回应/opt/lampp/logs/access_log编译
time=`date +%Y%m%d%H%M%S`#时间戳,执行命令使用``,esc下面的
echo ${time} #回应时间戳
cp ${mylog} ${mypath}/${time}_access.log #备份日志access_log到/tmp/logs路径下
echo ${mypath} ${mypath}/${time}_access.log#回应test
[root@CentOs6 lampp]# ./test.sh #执行test.sh
-bash: ./test.sh: 权限不够 #会提示权限不够
[root@CentOs6 lampp]# chmod +x ./test.sh #须要给这它赋与权限
[root@CentOs6 lampp]# ./test.sh #再次执行,脚本没有报错
/tmp/logs
/opt/lampp/logs/access_log
20161227172323
/tmp/logs /tmp/logs/20161227172323_access.log
2、crontab 定时任务
[root@CentOs6 lampp]# crontab -e #编辑定时任务
* * * * * sh /opt/lampp/test.sh #每分钟执行一次test.sh,crontab使用在上篇Linux命令中详细介绍
[root@CentOs6 lampp]# crontab -l #查看定时任务
* * * * * sh /opt/lampp/test.sh
3、重启crond
[root@CentOs6 lampp]# service crond reload
从新载入 crond: [肯定]
打开xftp查看
总结:
1- 先单独执行 .sh ,肯定 .sh 能够执行不报错
2- 从根目录查找 *.access.log 看看备份的存放路径是否正确
3- 检查 crontab 里的空格是否是半角英文