Created
January 25, 2014 20:14
-
-
Save MichalBryxi/8622834 to your computer and use it in GitHub Desktop.
Ever wanted to transfer multiple LVM volumes from one machine to other? Ever tried it? It's not hard, but it requires a lot of (manual) steps.
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 | |
# | |
# == History | |
# Ever wanted to transfer multiple LVM volumes from one machine to other? | |
# Ever tried it? It's not hard, but it requires a lot of (manual) steps. | |
# Things get especially annoying if you want to test it first and than | |
# transfer everything ASAP. If you like to automate things, you can try | |
# this script. It uses LVM2 snapshot mechanism, so in theory you don't | |
# need to stop any service using the volume. Of course this will ensure | |
# only LVM/filesystem-level consistency, not application-level sonsistency! | |
# | |
# == Requirements | |
# 1) This script executable | |
# 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 | |
# | |
# == Example run: | |
# sudo /usr/local/sbin/transfer_lvm.rb | |
# >>> get size of partition | |
# lvs /dev/vg00/db01 -o LV_SIZE --noheadings --units G --nosuffix | |
# Command took: 0.431847s | |
# >>> delete possible old snapshot | |
#lvremove -f /dev/vg00/db01_snap | |
# One or more specified logical volume(s) not found. | |
# Command took: 0.416606s | |
# >>> delete possible old remote partition | |
# ssh snap@newkulik 'sudo lvremove -f /dev/vg0/db01' | |
# One or more specified logical volume(s) not found. | |
# Command took: 0.270644s | |
# >>> create snapshot | |
# lvcreate -L2G -s -n db01_snap /dev/vg00/db01 | |
# Command took: 0.918021s | |
# >>> create partition on remote end | |
# ssh snap@newkulik 'sudo lvcreate -L10.49G -n db01 /dev/vg0' | |
# Command took: 0.33421s | |
# >>> transfer data | |
# dd if=/dev/vg00/db01_snap bs=4096 | pv | gzip | ssh snap@newkulik 'gzip -d | sudo dd of=/dev/vg0/db01' bs=4096 | |
# 9.77GB 0:05:51 [28.5MB/s] [ <=> ]2560000+0 records in | |
# Command took: 353.581616s | |
# | |
# == Configuration | |
# List all partitions you want to transfer | |
PARTITIONS = [ 'db01', 'db02', 'db03' ] | |
# Source LVM2 volume group | |
SRC_VG = '/dev/vg00' | |
# Destination LVM2 volume group | |
DST_VG = '/dev/vg0' | |
# How do you connect via SSH from source to destination machine? | |
REMOTE = 'snap@remotemachine' | |
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| | |
orig = "#{SRC_VG}/#{orig_name}" | |
src_name = "#{orig_name}_snap" | |
src = "#{SRC_VG}/#{src_name}" | |
dst = "#{DST_VG}/#{orig_name}" | |
puts ">>> get size of partition" | |
size = runme("lvs #{orig} -o LV_SIZE --noheadings --units G --nosuffix").strip.to_f | |
# get size of snapshot | |
snap_size = (size/10).ceil | |
puts ">>> delete possible old snapshot" | |
runme("lvremove -f #{src}") | |
puts ">>> delete possible old remote partition" | |
runme("ssh #{REMOTE} 'sudo lvremove -f #{dst}'") | |
puts ">>> create snapshot" | |
runme("lvcreate -L#{snap_size}G -s -n #{src_name} #{orig}") | |
puts ">>> create partition on remote end" | |
runme("ssh #{REMOTE} 'sudo 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