Bash script to check network volume storage

546 views Asked by At

I am trying to script checking a network volume and comparing it against with a local copy to see if there is enough space to copy the files.

The files will copied using rsync and I use the following to get the available space

VAR1=`du -sh /Volumes/networkshare/user`
VAR2=`du -sh /Users/user1/`

If I was to do an

if [ $VAR1 -lt $VAR2 ]
then echo "Enough Space"
fi

Then that might work for the initial backup but this will run once a day using rsync and therefore wont be a good check for available space as only new files and changed files will be copied up. Is there a better way to do this sort of check?

Edit: The share has a size limit of 50gb but I am unsure how I could use this if rsync isnt copying everything

Thanks

2

There are 2 answers

4
David C. Rankin On

There is a way to get a feel for the total size of the new or changed files that will be sent. However, this is before rsync computes the incremental part of each file that will be transferred if it already exists on the destination host. There is no way to obtain the incremental size that rsync will actually transfer to update the remote location. This code can be used as a one-liner albeit a long one liner. Take a look and see if knowing the size of the new/changed files helps you in your calculation.

Note: the files in the rsync command are just for example:

rsync -uavn ~/cnf/data nirvana:~/cnf > /tmp/rfn                       # list of files
lns=$(cat /tmp/rfn | wc -l)                                           # line count
xsize=$(tail -n$((lns - 2)) /tmp/rfn | head -n$((lns - 5)) | du -hs)  # isolate files & du
printf "\n You will transfer files totaling : %s\n\n" "${xsize%[^A-Za-z0-9]*}"
rm /tmp/rfn                                                           # remove tmp file

example:

$ bash chksz.sh

 You will transfer files totaling : 8.0M

This could be used as an outside approximation of the maximum space the new/changed files would add to the remote storage.

4
Gilles Quénot On

Try doing this:

#!/bin/bash

var1=$(du -s /Volumes/networkshare/user | cut -f1)
var2=$(du -s /Users/user1/ | cut -f1)

if ((var1 < var2)); then
    ...
fi

If you want to sum the needed size for the case 2 (incremental), consider the Etan Reisner's proposition bellow your post.