Created
October 25, 2011 12:48
-
-
Save spohnan/1312630 to your computer and use it in GitHub Desktop.
Uses rsync and ssh for transfer, compression and encryption. Notification of file changes provided by inotify-tools.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Safer Move | |
# | |
# Uses rsync and ssh for transfer, compression and encryption | |
# Notification of file changes provided by inotify-tools which | |
# is available as source or .deb (Ubuntu) or rpm through EPEL repo. | |
# https://github.com/rvoicilas/inotify-tools/wiki/ | |
# | |
# Assumes you've set up your ssh key for password-less login | |
# on the remote machine prior to running. | |
FROM=/home/spohna/test_from/ | |
REMOTE_HOST=spohna@access | |
REMOTE_DIR=~/test_to | |
LOG=/home/spohna/safer_move.log | |
RSYNC_OPTS="--partial -avz -e ssh --log-file=$LOG" | |
# Floating point number comparison to compare rsync version numbers | |
float_test() { | |
echo | awk 'END { exit (!('"$1"')); }' | |
} | |
# A test of both the remote ssh key setup (or not) and the remote rsync version | |
REMOTE_RSYNC_VERSION=$(ssh $REMOTE_HOST -C 'rsync --version | grep version') | |
if [ -z "$REMOTE_RSYNC_VERSION" ]; then | |
echo "Error: Check to ensure the ssh key is configured on the remote host." | |
exit -3 | |
fi | |
# Check both ends to see if we can use the rsync --remove-source-files option to | |
# remove transferred files. Available from v2.6.9 onwards. (RHEL 6.0+) | |
USE_RSYNC_REMOVE_REMOTE=false | |
float_test "$(echo $REMOTE_RSYNC_VERSION | awk '{print $3}') >= 2.6.9" && USE_RSYNC_REMOVE_REMOTE=true | |
USE_RSYNC_REMOVE=false | |
float_test "$(rsync --version | grep version | awk '{print $3}') >= 2.6.9" && USE_RSYNC_REMOVE=true | |
# In a parallel subshell we'll wait a couple of seconds and then refresh the timestamp | |
# on any existing files at script startup so they'll be picked up | |
(sleep 5; find $FROM -exec touch {} \;) & | |
# Sync thereafter in response to inotify events | |
inotifywait -m --timefmt '%d/%m/%y %H:%M' --format '%T %w %f' -e close_write $FROM | \ | |
while read date time dir file; do | |
FILECHANGE=${dir}${file} # File to be transferred and removed | |
# Use rsync remove command if possible. If not available, then | |
# check for the succesful exit value and just remove the file | |
if $USE_RSYNC_REMOVE && $USE_RSYNC_REMOVE_REMOTE; then | |
rsync $RSYNC_OPTS --remove-source-files $FILECHANGE $REMOTE_HOST:$REMOTE_DIR | |
else | |
rsync $RSYNC_OPTS $FILECHANGE $REMOTE_HOST:$REMOTE_DIR && rm $FILECHANGE | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment