简体   繁体   中英

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.

I have created a generic shell script to create tar files of log files :

#!/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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM