Wednesday, February 26, 2014

Ramdisk automation

I have replaced the HDD of my laptop with SSD. The speed increase was amazing. The drawback is that the frequent compilation could use up the storage quickly.
Ramdisk was proposed to be used for cache area of browsers and I have also found some interesting posts about persistent ram disk usage.
The point was to get rid of the frequent update of the files in the target directory of my java projects (that was typically caused by mvn clean install/package).

I have put together a nice little script that replaces the current working directory with an in-memory version. It also takes care of synchronizing back the data every once in a while.

#! /bin/sh 
#
if [ -z "$2" ]
then
  echo "Usage: bin/ramdisk.sh {start|stop|sync} {dir}"
  exit 1
fi
 
case "$1" in
  start)
    mkdir -p /tmp/$2
    rsync -aF $2/ /tmp/$2
    mv $2 $2.save
    ln -s /tmp/$2 $2
    crontab -l | cat - $2/.crontab | crontab -
    ;;
  sync)
    echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched to HD >> log/ramdisk_sync.log
    rsync -aF --delete --recursive --force /tmp/$2/ $2.save
    ;;
  stop)
    echo [`date +"%Y-%m-%d %H:%M"`] Ramdisk Synched to HD and STOP >> log/ramdisk_sync.log
    crontab -l | sed -e "\,$2,d" | crontab -
    rm $2
    rsync -aF --delete --recursive --force /tmp/$2/ $2.save
    mv $2.save $2
    rm -rf /tmp/$2
    ;;
  *)
    echo "Usage: bin/ramdisk.sh {start|stop|sync} {dir}"
    exit 1
    ;;
esac

exit 0

It seems just good enough.
The point is that I put a .crontab file in the working directory, so each project can define its own synchronization policy.
The typical crontab file looks like this:
*/10 * * * * bin/ramdisk.sh sync proj/xpbydoing
Happy ramdisk usage!

No comments:

Post a Comment