Skip to content

Instantly share code, notes, and snippets.

@ouyi
Last active July 18, 2018 15:52
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 ouyi/a1843a3963bc01589c36246acb6f4520 to your computer and use it in GitHub Desktop.
Save ouyi/a1843a3963bc01589c36246acb6f4520 to your computer and use it in GitHub Desktop.
Server-to-server file copying
#/usr/bin/env bash
set -euo pipefail
src_host="$1"
src_path="$2"
dst_host="$3"
dst_path="$4"
pattern="${5:-*}"
tmp_file="/var/tmp/test.data.tar.gz"
ssh -t $dst_host "sudo -s bash -c \"rm -f $tmp_file\""
ssh -t $src_host "sudo -s bash -c \"rm -f $tmp_file\""
ssh -t $src_host "sudo -s bash -c \"find '$src_path' -name '$pattern' -printf '%f\n' | tar czf $tmp_file -C $src_path --files-from=-\""
scp -3 $src_host:$tmp_file $dst_host:$tmp_file
ssh -t $dst_host "sudo -s bash -c \"mkdir -p $dst_path && tar xzf $tmp_file -C $dst_path\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment