Skip to content

Instantly share code, notes, and snippets.

@MichalBryxi
Last active August 29, 2015 13:57
Show Gist options
  • Save MichalBryxi/9731280 to your computer and use it in GitHub Desktop.
Save MichalBryxi/9731280 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
#
# == History
# Transfers multiple *.img files from one machine to corresponding
# LVM volumes on other machine.
#
# == Requirements
# 1) This script
# 2) Passwords-less sudo on destination machine
# 3) Sudo rights on source machine
# 4) Pubkey SSH authentication from source to destination machine
# 5) Installed "pv" - shows transfer progress
#
# == Configuration
# List all images you want to transfer
PARTITIONS = [ 'web03' ]
# Source directory vith images
SRC_DIR = '/var/lib/libvirt/images'
# Destination LVM2 volume group
DST_VG = '/dev/vg0'
# How do you connect via SSH from source to destination machine?
REMOTE = '[email protected]'
def runme(cmd)
puts cmd
start = Time.now
ret = `#{cmd}`
diff = Time.now - start
puts "Command took: #{diff}s\n"
return ret
end
PARTITIONS.each do |orig_name|
src = "#{SRC_DIR}/#{orig_name}.img"
dst = "#{DST_VG}/#{orig_name}"
puts ">>> get size of partition"
size = (runme("du #{src}").strip.to_f / 1000000).ceil
puts ">>> delete possible old remote partition"
runme("ssh #{REMOTE} 'sudo /sbin/lvremove -f #{dst}'")
puts ">>> create partition on remote end"
runme("ssh #{REMOTE} 'sudo /sbin/lvcreate -L#{size}G -n #{orig_name} #{DST_VG}'")
puts ">>> transfer data"
runme("dd if=#{src} bs=4096 | pv | gzip | ssh #{REMOTE} 'gzip -d | sudo dd of=#{dst}' bs=4096")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment