Skip to content

Instantly share code, notes, and snippets.

@fluffysquirrels
Created February 19, 2015 13:12
Show Gist options
  • Save fluffysquirrels/adbf48f9fd5ac67015a7 to your computer and use it in GitHub Desktop.
Save fluffysquirrels/adbf48f9fd5ac67015a7 to your computer and use it in GitHub Desktop.
Script to incrementally copy the end of a local file to a destination over SSH
usage() {
cat <<EOF
Usage:
copy-tail SOURCE_PATH REMOTE_HOST REMOTE_PATH
EOF
}
if [ "$#" -ne "3" ]; then
echo "Wrong number of arguments!"
usage
exit 1
fi
local_file="$1"
remote_host="$2"
remote_file="$3"
cat <<EOF
local_file = $local_file
remote_host = $remote_host
remote_file = $remote_file
EOF
ssh "$remote_host" touch "$remote_file"
curr_local_len=$(ls -lA "$local_file" | awk '{print $5}')
remote_len=$(ssh "$remote_host" ls -lA "$remote_file" | awk '{print $5}')
local_intro_hash=$(head -c$remote_len "$local_file" | md5sum | awk "{print \$1}")
remote_intro_hash=$(ssh "$remote_host" "head -c$remote_len \"$remote_file\" | md5sum" | awk "{print \$1}")
cat <<EOF
curr_local_len = $curr_local_len
local_intro_hash = $local_intro_hash
remote_len = $remote_len
remote_intro_hash = $remote_intro_hash
EOF
if [ "$remote_intro_hash" != "$local_intro_hash" ]; then
echo "Intro hashes don't match. Data is corrupted."
exit 1
fi
echo
echo Copying...
tail -c+$((remote_len + 1)) "$local_file" | pv -s $((curr_local_len - remote_len)) | ssh "$remote_host" "cat >> \"$remote_file\""
echo
new_remote_len=$(ssh "$remote_host" ls -lA "$remote_file" | awk '{print $5}')
cat <<EOF
new_remote_len = $new_remote_len
EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment