Created
January 16, 2016 21:25
-
-
Save XanClic/670b335b1258f24ce28e to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# coding: utf-8 | |
require 'shellwords' | |
PARALLEL_COPY = '~/bin/parallel-ssh-copy.rb' | |
def die(msg) | |
$stderr.puts(msg) | |
exit 1 | |
end | |
if ARGV[0] && ARGV[0][0] == '-' | |
connections = ARGV.shift[1..-1].to_i | |
end | |
connections = 24 if !connections || connections <= 0 | |
remote_path = ARGV[0] | |
local_path = ARGV[1] || '.' | |
if !remote_path || ['-h', '--help'].include?(remote_path) | |
die('Usage: parallel-ssh-copy ' + | |
'[-<connection count>] <remote path> [local path]') | |
end | |
rsplit = remote_path.split(':') | |
if rsplit.size < 2 | |
die("#{remote_path} is not a valid remote path (colon missing)") | |
end | |
remote_server = rsplit.shift | |
remote_path = rsplit * ':' | |
die("#{local_path} is not a directory") if !File.directory?(local_path) | |
dirs = `ssh #{remote_server.shellescape} #{('find ' + | |
remote_path.shellescape + ' ' + | |
'-type d -print0').shellescape}` \ | |
.split("\0") | |
die("No remote directory #{remote_path}") if dirs.empty? | |
rp_dir = File.dirname(remote_path) + '/' | |
dirs.each do |d| | |
dp = "#{local_path}/#{d.sub(rp_dir, '')}" | |
die("Failed to create #{d}") unless system("mkdir -p #{dp.shellescape}") | |
end | |
files = `ssh #{remote_server.shellescape} #{('find ' + | |
remote_path.shellescape + ' ' + | |
'-type f -print0').shellescape}` \ | |
.split("\0") | |
files.sort.each do |f| | |
puts | |
puts "=> #{f.sub(rp_dir, '')}" | |
puts | |
fp = "#{local_path}/#{f.sub(rp_dir, '')}" | |
system("#{PARALLEL_COPY} -#{connections} " + | |
(remote_server + ':' + f).shellescape + ' ' + | |
fp.shellescape) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment