Linux centos下设置定时备份任务的方法步骤
实现准备
#需要备份文件路径:/opt/apollo/logs/access_log [root@localhostopt]#cdapollo/ [root@localhostapollo]#tree . ├──logs │└──access_log └──test.sh #文件备份存放路径:/tmp/logs #备份文件加上时间戳date+%Y%m%d%H%M%S
1.编写shell脚本
[root@localhosttmp]#vi/opt/apollo/test.sh
#编译器
#!/bin/bash
#日志备份到该目录下,定义变量使用单引号
mypath='/tmp/logs'
#回应/tmp/logs
echo${mypath}
#要备份的日志
mylog='/opt/apollo/logs/access_log'
#回应/opt/apollo/logs/access_log
echo${mylog}
#时间戳,执行命令使用``,esc下面的
time=`date+%Y%m%d%H%M%S`
#回应时间戳
echo${time}
#备份日志access_log到/tmp/logs路径下
cp${mylog}${mypath}/${time}_access.log
#回应
echo${mypath}${mypath}/${time}_access.log
2.执行test.sh
[root@localhostapollo]#./test.sh -bash:./test.sh:Permissiondenied
3.执行ls-la
[root@localhostapollo]#ls-la total8 drwxr-xr-x2rootroot21Jan2008:00. drwxr-xr-x.14rootroot4096Jan2007:07.. -rw-r--r--1rootroot489Jan2008:00test.sh
4.给文件test.sh赋与执行权限
[root@localhostapollo]#chmod+x./test.sh [root@localhostapollo]#ls-la total8 drwxr-xr-x2rootroot21Jan2008:00. drwxr-xr-x.14rootroot4096Jan2007:07.. -rwxr-xr-x1rootroot489Jan2008:00test.sh
5.再次执行,脚本没有报错
[root@localhostapollo]#./test.sh /tmp/logs /opt/apollo/logs/access_log 20190120080932 /tmp/logs/tmp/logs/20190120080932_access.log
6.编辑定时任务
[root@localhostlogs]#crontab-e nocrontabforroot-usinganemptyone crontab:installingnewcrontab
7.查看定时任务
#每分钟执行一次test.sh *****sh/opt/apollo/test.sh
8.重启crond
[root@localhostlogs]#servicecrondreload Redirectingto/bin/systemctlreloadcrond.service Youhavenewmailin/var/spool/mail/root
9.编写文件access_log
#需要备份文件路径: /opt/apollo/logs/access_log #编辑文件 [root@localhostlogs]#vi/opt/apollo/logs/access_log #追加内容如下: mmmmmmmmmmmmmmmmmmmmm
10.过1分钟,再去查备份存放目录
[root@localhostlogs]#cat20190120083101_access.log djddjsjsjsjjsjsjsj mmmmmmmmmmmmmmmmmmmmm
11.到此为止,定时备份任务完成.
恭喜你,学会备份了!
12.删除定时任务
[root@localhostlogs]#crontab-r Youhavenewmailin/var/spool/mail/root
13.查看定时任务
[root@localhostlogs]#crontab-l nocrontabforroot
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
10 宝宝新生入学祝福语简短
11 新年祝福语给婆婆简短
12 亲戚红包生日祝福语简短
13 回复同事新年祝福语简短
14 炸鸡拼盘生日祝福语简短
15 爸爸爱你祝福语大全简短
16 涂老师毕业祝福语简短
17 茶人生日祝福语简短
18 2026年春节祝福语简短