View Single Post
  #3  
Old 13th December 2005, 11:13
themachine themachine is offline
Senior Member
 
Join Date: Oct 2005
Location: Texas, USA
Posts: 109
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok, so I played around a little... I don't know how well this will help, but here you go:


Code:
#!/bin/bash

# simple script... bla blah

BASE_DIR=/home/johnny/bin/test
DST_DIR=$BASE_DIR/dir
SRC_DIR=$BASE_DIR/src_dir
ROTATED_SUFFIX=`date +"%m%d%y-%H%M%S"`


# Move to the base dir
cd $BASE_DIR


# You probably want to test for a newer directory rather than a bigger one
# -nt = Newer Than
# You can modify the script to use this condition rather than the 
# Directory Size

if [ $SRC_DIR -nt $DST_DIR ]; then
        echo "$SRC_DIR is newer than $DST_DIR... It should be rotated!"
        echo
else
        echo "$SRC_DIR is not newer than $DST_DIR... Don't rotate!"
        echo
fi


# "-e" Tests that $DST_DIR Exists and then gets the 
# byte count for the directory
# du -cbs = "Disk Usage -c[total] -b[bytes] -s[supress output]
# awk gives you just the bytes count, nothing else

if [ -e $DST_DIR ]; then
        DST_BYTES=`du -cbs $DST_DIR | awk '/total/ {print $1}'`
        echo "The destination directory has $DST_BYTES bytes"
else
        echo "WTF => $DST_DIR doesn't exist!"
        exit 1
fi


# Test for $SRC_DIR and then get the byte count for the directory

if [ -e $SRC_DIR ]; then
        SRC_BYTES=`du -cbs $SRC_DIR | awk '/total/ {print $1}'`
        echo "The source directory has $SRC_BYTES bytes"
else
        echo "WTF => $SRC_DIR doesn't exist!"
        exit 1
fi


# Test to see if the Source Dir is "Greater Than" the Destination Dir
# -gt = Greater Than
# If the SRC_DIR is Greater Than the DST_DIR then it moves
# The DST_DIR to DST_DIR.[datestamp]-[timestamp]
# and then copies the SRC_DIR to DST_DIR

if [ $SRC_BYTES -gt $DST_BYTES ]; then
        echo "The source directory is bigger!"
        echo
        echo "Rotating ${DST_DIR} to ${DST_DIR}.${ROTATED_SUFFIX}!"
        mv ${DST_DIR} ${DST_DIR}.${ROTATED_SUFFIX}
        mv $SRC_DIR $DST_DIR
else
        echo "$SRC_DIR is not greater than $DST_DIR... Not rotating!"
fi

P.S. Bash isn't my strong suit, but you know... a couple minutes and a came up with that... hope it helps.
__________________
themachine
5dollarwhitebox.org

Last edited by themachine; 13th December 2005 at 11:15.
Reply With Quote