-1

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.

1 Answer 1

4

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
Sign up to request clarification or add additional context in comments.

4 Comments

How can I implement this in my current script
can you tell me how to find files staring with logsbackup and ending with gz or bz2 extention in /var directory that are older than 10 days and delete them ?
For that try: find /var -name 'logsbackup*gz' -mtime +10 -delete
@anubhava please have a look at http://stackoverflow.com/questions/43687989/how-to-create-status-logs-for-shell-script-in-linux-bash

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.