[英]How to create shell script for creating logs on linux server based on hostname
All, 所有,
I have a requirement that if the hostname of the server starts with tm1 or dm1 then it should create gz1 format of log files , if the hostname starts with pc1 then it should create bz1 format of logs. 我有一个要求,如果服务器的主机名以tm1或dm1开头,则它应创建gz1格式的日志文件,如果主机名以pc1开头,则应创建bz1格式的日志。
I have created a generic shell script to create tar files of log files : 我创建了一个通用的shell脚本来创建日志文件的tar文件:
#!/bin/bash
#START
TIME=$(date +%Y%-m%-d)
FILENAME=logsbackup-$TIME.tar.gz
SRCDIR=/var/log/
DESDIR=/var/
find $SRCDIR -mtime +1 | xargs tar -cpzf $DESDIR/$FILENAME
#END
How can I implement the above mentioned changes in my script. 如何在脚本中实现上述更改。
You can use a condition like this: 您可以使用以下条件:
#!/bin/bash
#START
TIME=$(date +%Y%-m%-d)
FILENAME=logsbackup-$TIME.tar
SRCDIR=/var/log/
DESDIR=/var/
host=$(hostname)
if [[ $host == @(tm1|dm1)* ]]; then
echo "creating gz format"
find $SRCDIR -mtime +1 -print0 | xargs -0 tar -cpzf $DESDIR/$FILENAME.gz
elif [[ $host == pc1* ]]; then
echo "creating bz2 format"
find $SRCDIR -mtime +1 | xargs -0 tar -cjf $DESDIR/$FILENAME.bz2
fi
# END
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.