Need borne shell to compare backup space required to available space

Christopher Schene
Christopher Schene used Ask the Experts™
on
I need a borne shell script to do the following

1)Determine how much disk space is used by two directories on my Linux box
2)Determine the amount of free space on the backup volume

if free space in backup volume is less than 120% of the space used by the directories then
      log an error “insufficient disk space on backup m volume: Backup aborting”
else
      proceed with backup
end if
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
You can get the disk space usage of the directories in kilobytes with:

du -sk /path/to/dir1 /path/to/second/dir | awk '{s=s+$1}END{print s}' > dir.siz

You can get the free space in kilobytes on the destination directory:

df -f /path/to/target | awk 'NR==2{print $4}' > bkup.spc

There are several ways to compare then values.  One is using expr:

req_space=$(expr $(cat dir.siz) \* 120)
avail_space=$(expr $(cat bkup.spc) \* 100)
if [ $req_space -lt $avail_space ]; then
  echo Doing backup - there is enough space
  # run backup command here
else
  echo "Not enough space - need $(cat dir.siz) + 20%, have $(cat bkup.spc)"
fi
Commented:
#!/bin/bash

dir1="/path/to/dir1"
dir2="/path/to/dir2"

spaceused1=$(du -B1 -s $dir1 | awk '{print $1}')
spaceused2=$(du -B1 -s $dir2 | awk '{print $1}')

spaceavailable=$(df $dir1 | grep -v ^Filesystem | awk '{print $4}')
spaceneeded=$(( (($spaceused1 + $spaceused2) * 6) / 5 )) # 6/5 = 120%

if [ $spaceneeded -gt $spaceavailable ] ; then
	echo log an error \“insufficient disk space on backup m volume: Backup aborting\”
else
	echo proceed with backup
fi

Open in new window

Christopher ScheneSystem Engineer/Software Engineer

Author

Commented:
See below---the block size in use is 123 times greater than the blocks available on the disk----I know this is probably a block size specification, but don't know how to fix it.

[root@localhost Carefx]# du -B1 -s /opt/IBM
4756897792      /opt/IBM
[root@localhost Carefx]# df /opt
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/VolGroup00-LogVol00
                      38502208  11879332  24635500  33% /
[root@localhost Carefx]# expr  4756897702 / 38502208
123
[root@localhost Carefx]#
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Christopher ScheneSystem Engineer/Software Engineer

Author

Commented:
I just changed this line:

from this

spaceavailable=$(df $dir1 | grep -v ^Filesystem | awk '{print $4}')

to this

spaceavailable=$(df --block-size=1024 /opt/backup | grep -v ^Filesystem | awk '{print $3}')

and it worked fine
df -B1 gives the size in bytes, my df defaulted to 1k blocks.
Did you mean to change the df line to have "awk '{print $3}')" a the end?  $3 in df output is the used space, $4 is the available space.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial