备份脚本为
[root@xxx scripts]# cat back_log.sh
#!/bin/bash
#majihui
#backup prd3 block log to ip
#2019/2/25
/usr/bin/rsync -az /ivargo/log/* root@ip:/ivargo/prd3/$HOSTNAME/
然后做定时任务,每天凌晨1点传到备份服务器上
删除日志脚本,每天晚上当时2点开始删除
[root@xxx scripts]# cat rm_7date_log.sh
#!/bin/bash
#majihui
#backup prd3 block log to ip
#2019/2/25
find /ivargo/log/ -type f -name "*log.2*" -mtime +6 |xargs rm -f
[root@xxx scripts]# crontab -l
#backup /ivargo/log to ip:/ivargo/prd3/
0 1 * * * /bin/sh /opt/scripts/back_log.sh >/dev/null 2>&1
#rm 7date log /ivargo/log
0 2 * * * /bin/sh /opt/scripts/rm_7date_log.sh >/dev/null 2>&1
提示:需要在备份服务器上提前创建好相应的分组目录,备份日志的时候,需要与备份主机建立好免密登陆。
扩展:
上面的find命令还可以写成
find /ivargo/log/ -type f -name "log.2" -mtime +6 -exec rm -f {} ;
例子
[root@NewCDH-0–144 log]# find /ivargo/log/ -type f -name "*log*24" -exec ls -l {} ;
-rw-r–r– 1 root root 0 Feb 25 13:05 /ivargo/log/NewCDH-0–144.log.2019-02-24
-rw-r–r– 1 root root 0 Feb 25 13:23 /ivargo/log/vpush/NewCDH-0–144.log.vpush.2019-02-24
[root@NewCDH-0–144 log]# find /ivargo/log/ -type f -name "*log*24" -exec rm -f {} ;
[root@NewCDH-0–144 log]# find /ivargo/log/ -type f -name "*log*24" -exec ls -l {} ;