学透shell 带你写常用的100个 shell 脚本(一)定时删除过期文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学透shell 带你写常用的100个 shell 脚本(一)定时删除过期文件相关的知识,希望对你有一定的参考价值。

参考技术A 本文已参与「新人创作礼」活动,一起开启掘金创作之路。

需要清理服务器中过期的文件、日志、临时压缩未清理的垃圾文件、过久的备份文件,手动删除过于繁琐,可以通过此脚本进行删除,如有不同场景,适量修改即可

目标:删除 /home/logs/下超过30天的日志文件,文件名称为xxxx-service-2022-06-18-20.out

如果只需单次清理,只需要执行删除命令即可

添加定时任务,设置每个月1号晚上4点10分执行脚本.当然你也可以根据你自己的需求去整

OK ,此次结束 🔚。

shell脚本加密笔记!

你写的shell在生产环境是否加密?反正我是没有,线上环境看就看呗,觉得没啥。其实想想,好像也有几个好处。

1、简单加密,防止别人看里面具体内容。

2、可以隐蔽脚本中的密码等信息。(比如你的备份脚本,涉及到密码等问题)

好像还有点用处,大概常用的两种方式。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第一种方法(gzexe):

这种加密方式不是非常保险的方法,但是能够满足一般的加密用途。它是使用系统自带的gzexe程序,它不但加密,同时压缩文件。

使用方法: gzexe file.sh 它会把原来没有加密的文件备份为 xx.sh~ ,同时 xx.sh 即被变成加密文件;

[[email protected] ~]# ./1.sh 

2016年 08月 31日 星期三 15:38:45 CST

[[email protected] ~]# gzexe 1.sh 

1.sh: 120.0%


[[email protected] ~]# ll 1.sh*

-rwxr-xr-x 1 root root 831 8月  31 15:38 1.sh

-rwxr-xr-x 1 root root   5 8月  31 15:30 1.sh~

[[email protected] ~]# cat 1.sh~

date

[[email protected] ~]#

建议使用,一方面系统自带命令,达到目的即可,没人闲的破解你的脚本,就算要破解,先进入你系统再说。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

第二种方式 shc

shc是一个专业的加密shell脚本的工具.它的作用是把shell脚本转换为一个可执行的二进制文件,这个办法也很好的解决了脚本中含有IP、密码等不希望公开的问题。

包下载链接:

http://www.datsi.fi.upm.es/~frosal/sources/

wget http://www.datsi.fi.upm.es/~frosal/sources/shc-3.8.6.tgz

[[email protected] local]# tar xf shc-3.8.6.tgz 

[[email protected] local]# cd shc-3.8.6

[[email protected] shc-3.8.6]# mkdir -p /usr/local/man/man1

这步是必须的,不然安装过程中会报错,shc将安装命令到/usr/local/bin/目录下;将帮助文档存放在/usr/local/man/man1/目录下,如果系统中无此目录,安装时会报错,可创建此目录后再执行安装。

[[email protected] shc-3.8.6]# make install

*** Installing shc and shc.1 on /usr/local

*** Do you want to continue? yes

install -c -s shc /usr/local/bin/

install -c -m 644 shc.1 /usr/local/man/man1/

[[email protected] shc-3.8.6]#

yum install glibc  glibc.i686 -y

用法:

-f 指定要加密的文件

[[email protected] ~]# shc -r -f 1.sh

1.sh.x.c: In function ‘chkenv‘:

1.sh.x.c:277: warning: cast from pointer to integer of different size

运行后会生成两个文件,xx.x 和 xx.x.c.   其中xx.x是加密后的可执行的二进制文件;用./xx.x即可运行,xx.x.c是生成xx.x的原文件(c语言).

[[email protected] ~]# ll 1.sh*

-rwxr-xr-x 1 root root   831 8月  31 15:38 1.sh

-rwxr-xr-x 1 root root     5 8月  31 15:30 1.sh~

-rwx--x--x 1 root root 12656 8月  31 16:04 1.sh.x

-rw-r--r-- 1 root root 14001 8月  31 16:04 1.sh.x.c

[[email protected] ~]#

不建议使用,需要单独安装管理,比较麻烦。如果安全性要求极高,倒是可以参数。

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


本文出自 “[email protected]” 博客,请务必保留此出处http://renzhiyuan.blog.51cto.com/10433137/1844814

以上是关于学透shell 带你写常用的100个 shell 脚本(一)定时删除过期文件的主要内容,如果未能解决你的问题,请参考以下文章

手把手带你写一个minishell

shell 脚本传参

shell脚本加密笔记!

三分钟带你学shellshell编程基础

Sqoop 脚本开发规范(实例手把手带你写sqoop export和sqoop import)

今天,带你写个操作系统!