Find the answer to your Linux question:
Results 1 to 4 of 4
In a nutshell: I am in need to create a script that queries how large a partition is and when it hits a certain percentage (say 90%) it will execute ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6

    script to tar up files when a partition hits a certain use percentage

    In a nutshell:

    I am in need to create a script that queries how large a partition is and when it hits a certain percentage (say 90%) it will execute another script that tars up certain files (or they could just be part of the same script). I would create a cronjob that runs this script once a day.

    I have the script that tars up the files I need, sets permissions, etc. (btw, the files in question are audit logs). I just need the part that runs something like a df -h and takes the use percentage of the /var partition in that query and if that percentage is greater than/ equal to 90%, it kicks off the tar script.

    Here is a sniplet of the df -h with just the /var partition shown:

    Filesystem Size Used Avail Use% Mounted on
    /dev/sda5 7.5G 331M 6.8G 5% /var
    So, when the cronjob sees that the Use% is >= 90%, it would kick off the tar script...if not above 90%, it closes.

    Thanks.

  2. #2
    Just Joined! hunter_thom's Avatar
    Join Date
    Apr 2010
    Posts
    89
    Code:
    #!/bin/bash
    SPACE=`df -h | grep var | awk '{print $5}' | sed 's/%//g'`
    if [ $SPACE -ge 90 ]; then
      # tar files
    else
      # No tarring
    fi
    
    exit 0

  3. #3
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    Hmm, I donīt think hunter_thomīs script will work as a cronjob (sorry, no offence and yes: I see that the script is meant as a skeleton ), because there is no guarding against concurrent runs. Depends solely on the cron schedule.

    A better approach might be to use the built-in logrotate and/or disk free options of auditd
    auditd.conf: audit daemon config file - Linux man page
    You must always face the curtain with a bow.

  4. #4
    Just Joined!
    Join Date
    Jan 2010
    Posts
    6
    Thank you both for the responses. (and sorry this isn't in the correct forum, but I have sent a message to get the thread moved to the appropriate forum).

    Anyhow, this is what I got from another site that I was browsing and it works for what I need it to do.

    #!/bin/bash
    a=$( df -h | grep -i "var" | awk '{print $5}' | awk 'sub(".$","")')
    if [ $a -ge 90 ]
    then
    sh /var/log/blah_script.sh
    else
    exit
    fi
    It looks very similar to the script above. But again, thanks for the responses!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...