广告
返回顶部
首页 > 资讯 > 操作系统 >Linux centos下设置定时备份任务的方法步骤
  • 619
分享到

Linux centos下设置定时备份任务的方法步骤

centos定时备份centos定时备份任务 2022-06-04 22:06:43 619人浏览 薄情痞子
摘要

实现准备 # 需要备份文件路径:/opt/apollo/logs/access_log [root@localhost opt]# cd apollo/ [root@localhost apollo]# tree

实现准备


# 需要备份文件路径:/opt/apollo/logs/access_log
[root@localhost opt]# cd apollo/
[root@localhost apollo]# tree
.
├── logs
│  └── access_log
└── test.sh
# 文件备份存放路径:/tmp/logs
# 备份文件加上时间戳date + %Y%m%d%H%M%S

1.编写shell脚本


[root@localhost tmp]# 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@localhost apollo]# ./test.sh
-bash: ./test.sh: Permission denied

3.执行ls -la


[root@localhost apollo]# ls -la
total 8
drwxr-xr-x  2 root root  21 Jan 20 08:00 .
drwxr-xr-x. 14 root root 4096 Jan 20 07:07 ..
-rw-r--r--  1 root root 489 Jan 20 08:00 test.sh

4.给文件test.sh赋与执行权限


[root@localhost apollo]# chmod +x ./test.sh
[root@localhost apollo]# ls -la
total 8
drwxr-xr-x  2 root root  21 Jan 20 08:00 .
drwxr-xr-x. 14 root root 4096 Jan 20 07:07 ..
-rwxr-xr-x  1 root root 489 Jan 20 08:00 test.sh

5.再次执行,脚本没有报错


[root@localhost apollo]# ./test.sh
/tmp/logs
/opt/apollo/logs/access_log
20190120080932
/tmp/logs /tmp/logs/20190120080932_access.log

6.编辑定时任务


[root@localhost logs]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab

7.查看定时任务


# 每分钟执行一次test.sh
* * * * * sh /opt/apollo/test.sh

8.重启crond


[root@localhost logs]# service crond reload
Redirecting to /bin/systemctl reload cron

d.service You have new mail in /var/spool/mail/root

9.编写文件access_log


# 需要备份文件路径:
/opt/apollo/logs/access_log
# 编辑文件
[root@localhost logs]# vi /opt/apollo/logs/access_log
# 追加内容如下:
mmmmmmmmmmmmmmmmmmmmm

10.过1分钟,再去查备份存放目录


[root@localhost logs]# cat 20190120083101_access.log
djddjsjsjsjjsjsjsj
mmmmmmmmmmmmmmmmmmmmm

11.到此为止,定时备份任务完成.

恭喜你,学会备份了!

12.删除定时任务


[root@localhost logs]# crontab -r
You have new mail in /var/spool/mail/root

13.查看定时任务


[root@localhost logs]# crontab -l
no crontab for root

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

--结束END--

本文标题: Linux centos下设置定时备份任务的方法步骤

本文链接: https://www.lsjlt.com/news/20101.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作