You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

  1. #!/bin/sh
  2. SIZE=40M
  3. USE_RSYNC=false
  4. # don't touch anything below here.
  5. HDD_LOG=/var/log.hdd/
  6. RAM_LOG=/var/log/
  7. LOG2RAM_LOG="${HDD_LOG}log2ram.log"
  8. LOG_OUTPUT="tee -a $LOG2RAM_LOG"
  9. isSafe () {
  10. [ -d $HDD_LOG ] || echo "ERROR: $HDD_LOG doesn't exist! Can't sync."
  11. [ -d $HDD_LOG ] || exit 1
  12. }
  13. syncToDisk () {
  14. isSafe
  15. if [ "$USE_RSYNC" = true ]; then
  16. rsync -aXWv --delete --exclude log2ram.log --links $RAM_LOG $HDD_LOG 2>&1 | $LOG_OUTPUT
  17. else
  18. cp -rfup $RAM_LOG -T $HDD_LOG 2>&1 | $LOG_OUTPUT
  19. fi
  20. }
  21. syncFromDisk () {
  22. isSafe
  23. if [ ! -z `du -sh -t $SIZE $HDD_LOG | cut -f1` ]; then
  24. echo "ERROR: RAM disk too small. Can't sync."
  25. exit 1
  26. fi
  27. if [ "$USE_RSYNC" = true ]; then
  28. rsync -aXWv --delete --exclude log2ram.log --links $HDD_LOG $RAM_LOG 2>&1 | $LOG_OUTPUT
  29. else
  30. cp -rfup $HDD_LOG -T $RAM_LOG 2>&1 | $LOG_OUTPUT
  31. fi
  32. }
  33. case "$1" in
  34. start)
  35. [ -d $HDD_LOG ] || mkdir $HDD_LOG
  36. rm $LOG2RAM_LOG
  37. mount --bind $RAM_LOG $HDD_LOG
  38. mount --make-private $HDD_LOG
  39. mount -t tmpfs -o nosuid,noexec,nodev,mode=0755,size=$SIZE log2ram $RAM_LOG
  40. syncFromDisk
  41. ;;
  42. stop)
  43. syncToDisk
  44. umount -l $RAM_LOG
  45. umount -l $HDD_LOG
  46. ;;
  47. write)
  48. syncToDisk
  49. ;;
  50. *)
  51. echo "Usage: log2ram {start|stop|write}" >&2
  52. exit 1
  53. ;;
  54. esac