Skip to content

Instantly share code, notes, and snippets.

@ArgonQQ
Last active July 12, 2016 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ArgonQQ/045798e1f7c5c23349ccb866a3c1d419 to your computer and use it in GitHub Desktop.
Save ArgonQQ/045798e1f7c5c23349ccb866a3c1d419 to your computer and use it in GitHub Desktop.

bash-hacker

I wrote this script to keep a file or a folder in sync local or remotely. This worked out very well to sync changes in the code to a remote server in seconds without touching any button on my keyboard. The method to check if anything has changed is even simpler. It checks the file or folder last change date 'ls --full-time'.

Set the variable path to a directory or file. This could be a remote path aswell.

FP_rsync_from='/folder/from'
FP_rsync_to='/folder/to'

#REMOTE
[user]@[remote server]:[remote path]
FP_rsync_from='argonqq@10.10.158.10:/folder/from'
FP_rsync_to='root@10.10.158.11:/folder/to'

After you've started the script it will sync a first time. Now it will only sync if anything changes.

asciicast

#!/bin/bash
#rsync_filepath
FP_rsync_from='/folder/from'
FP_rsync_to='/folder/to'
platform='unknown'
unamestr=$(uname)
# Linux
if [ "$unamestr" == 'Linux' ]; then
printf "\nYour system is $unamestr based.\n"
while true
do
lastchanged_before=$(ls --full-time $FP_rsync_from | awk '{print $6, $7}')
if [ "$lastchanged_before" != "$lastchanged_after" ]; then
echo; date; echo; rsync -avh $FP_rsync_from $FP_rsync_to
lastchanged_after=$lastchanged_before
fi
sleep 3
done
fi
#OS X
if [ "$unamestr" == 'Darwin' ]; then
echo; echo "Your system is $unamestr based."; echo;
while true
do
lastchanged_before=$(ls -alT $FP_rsync_from | awk '{print $8}')
if [ "$lastchanged_before" != "$lastchanged_after" ]; then
echo; date; echo; rsync -avh $FP_rsync_from $FP_rsync_to
lastchanged_after=$lastchanged_before
fi
sleep 3
done
fi
unset unamestr
unset lastchanged_before
unset lastchanged_after
unset platform
unset FP_rsync_from
unset FP_rsync_to
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment