#!/usr/bin/env bash # # Ramdir creates a reverse-syncing ram drive of any directory # # Copyright (C) 2017 Aaron Ball # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # syncback() { local src=${1} local dst=${2} rsync -a "${src}/" "${dst}/" } shutdown() { local src=${1} local dst=${2} printf "\nShutting down\n" >&2 printf "Final sync back to disk\n" syncback "${src}" "${dst}" printf "Unmounting %s\n" "${src}" sudo umount "${src}" printf "Removing %s\n" "${src}" sudo rm -rf "${src}" exit 0 } setup() { local src=${1} local owner # Placeholder for current user name local group # Placeholder for current user group name # Get directory size # This is a tab -v- origsize=$(du -sm "${src}" | cut -d ' ' -f 1) newsize=$(( origsize + 500 )) owner=$(id -u) group=$(id -g) # Create a temp mount destination mount="$(sudo mktemp -d /mnt/superspeed.XXXXX)" if [ $? -gt 0 ]; then printf "Error creating temp mount point.\n" >&2 return 1 fi # Mount sudo mount -t tmpfs tmpfs -o size=${newsize}M "${mount}" >&2 sudo chown ${owner}:${group} ${mount} sudo chmod 750 ${mount} printf "${mount}" } run() { local base=${1} local bin=${2} chmod +x ${base}/${bin} ${base}/${bin} } main() { local path=${1} # Path of software to speed up to 11 local mount # Path for ram drive local syncstart # Start time of sync disk to ramdrive process local syncend # End time of sync disk to ramdrive process if [ -z ${path} ]; then printf "Path to turbo speed required\n" return 1 fi # Set up the mount mount=$(setup "${path}") [ $? -gt 0 ] && return 1 # Set up our shutdown trigger trap "shutdown ${mount} ${path}" SIGINT # Sync to temp mountpoint syncstart=$(date '+%s') syncback "${path}" "${mount}" syncend=$(date '+%s') # Print the transfer time printf "Finished sync to ramdrive in %d seconds\n" $(( syncend - syncstart )) # Notify when ready printf "Your files are available at super speed at ${mount}\n" # Sync to temp mountpoint while true; do printf "%s Syncing %s back to disk\n" "$(date '+%F %T')" "${mount}" syncback "${mount}" "${path}" sleep 30 done } main ${@}