summaryrefslogtreecommitdiff
path: root/ramdir.sh
blob: 323c415b9b58fe14769e9addfa6d03d9983bcd72 (plain)
    1 #!/usr/bin/env bash
    2 #
    3 # Ramdir creates a reverse-syncing ram drive of any directory
    4 #
    5 # Copyright (C) 2017  Aaron Ball <nullspoon@oper.io>
    6 # 
    7 # This program is free software: you can redistribute it and/or modify
    8 # it under the terms of the GNU General Public License as published by
    9 # the Free Software Foundation, either version 3 of the License, or
   10 # (at your option) any later version.
   11 # 
   12 # This program is distributed in the hope that it will be useful,
   13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
   14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15 # GNU General Public License for more details.
   16 # 
   17 # You should have received a copy of the GNU General Public License
   18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
   19 #
   20 
   21 
   22 syncback() {
   23   local src=${1}
   24   local dst=${2}
   25   rsync -a "${src}/" "${dst}/"
   26 }
   27 
   28 shutdown() {
   29   local src=${1}
   30   local dst=${2}
   31 
   32   printf "\nShutting down\n" >&2
   33 
   34   printf "Final sync back to disk\n"
   35   syncback "${src}" "${dst}"
   36   printf "Unmounting %s\n" "${src}"
   37   sudo umount "${src}"
   38   printf "Removing %s\n" "${src}"
   39   sudo rm -rf "${src}"
   40   exit 0
   41 }
   42 
   43 setup() {
   44   local src=${1}
   45 
   46   local owner # Placeholder for current user name
   47   local group # Placeholder for current user group name
   48 
   49   # Get directory size
   50   #                     This is a tab -v-
   51   origsize=$(du -sm "${src}" | cut -d '	' -f 1)
   52   newsize=$(( origsize + 500 ))
   53   
   54   owner=$(id -u)
   55   group=$(id -g)
   56   
   57   # Create a temp mount destination
   58   mount="$(sudo mktemp -d /mnt/superspeed.XXXXX)"
   59   if [ $? -gt 0 ]; then
   60     printf "Error creating temp mount point.\n" >&2
   61     return 1
   62   fi
   63   
   64   # Mount
   65   sudo mount -t tmpfs tmpfs -o size=${newsize}M "${mount}" >&2
   66   sudo chown ${owner}:${group} ${mount}
   67   sudo chmod 750 ${mount}
   68 
   69   printf "${mount}"
   70 }
   71 
   72 run() {
   73   local base=${1}
   74   local bin=${2}
   75 
   76   chmod +x ${base}/${bin}
   77   ${base}/${bin}
   78 }
   79 
   80 main() {
   81   local path=${1} # Path of software to speed up to 11
   82   local mount     # Path for ram drive
   83   local syncstart # Start time of sync disk to ramdrive process
   84   local syncend   # End time of sync disk to ramdrive process
   85   
   86   if [ -z ${path} ]; then
   87     printf "Path to turbo speed required\n"
   88     return 1
   89   fi
   90   
   91   # Set up the mount
   92   mount=$(setup "${path}")
   93   [ $? -gt 0 ] && return 1
   94 
   95   # Set up our shutdown trigger
   96   trap "shutdown ${mount} ${path}" SIGINT
   97   
   98   # Sync to temp mountpoint
   99   syncstart=$(date '+%s')
  100   syncback "${path}" "${mount}"
  101   syncend=$(date '+%s')
  102 
  103   # Print the transfer time
  104   printf "Finished sync to ramdrive in %d seconds\n" $(( syncend - syncstart ))
  105   # Notify when ready
  106   printf "Your files are available at super speed at ${mount}\n"
  107   
  108   # Sync to temp mountpoint
  109   while true; do
  110     printf "%s   Syncing %s back to disk\n" "$(date '+%F %T')" "${mount}"
  111     syncback "${mount}" "${path}"
  112     sleep 30
  113   done
  114 }
  115 
  116 main ${@}

Generated by cgit