Created
September 7, 2011 18:18
-
-
Save cwebberOps/1201300 to your computer and use it in GitHub Desktop.
vdr snapshotting
This file contains hidden or 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/ruby -w | |
### | |
# This script stops the specified vdr VM and snapshots the zfs pool | |
# that backends the vmdk(s) used to backup systems so it can be synced | |
# elsewhere. | |
# | |
# Prerequisites: | |
# - .keytab file in the homedir of the user executing the script that | |
# access to the zfs account on the remote host | |
# - ./vmware/credstore that is apropriate for these activites | |
# - vSphere perl SDK installed | |
# - infrawiki tools in place on this host | |
# | |
### | |
require File.join(File.dirname(__FILE__), '../lib/email.rb') | |
require "/inst/pkg/infrawiki/lib/infrawiki.rb" | |
require "optparse" | |
vi_server = 'virtualcenter.domain' | |
confluence = InfraWiki.load_config('confluence.yaml') | |
remote_user = "zfs" | |
keytab = File.expand_path('~') + '/.keytab' | |
guest = "#{File.dirname(__FILE__)}/../apps/vm/guestinfo.pl --server #{vi_server} --operation display --vmname " | |
shutdown = "#{File.dirname(__FILE__)}/../apps/vm/vmcontrol.pl --server #{vi_server} --operation shutdown --vmname " | |
poweron = "#{File.dirname(__FILE__)}/../apps/vm/vmcontrol.pl --server #{vi_server} --operation poweron --vmname " | |
status = "on" | |
errors = "" | |
options = {} | |
optparse = OptionParser.new do |opts| | |
opts.banner = "Usage: #{File.basename(__FILE__)} [options]" | |
opts.on("-h", | |
"--vdr-host HOST", | |
"VMware Data Recovery Host") do |h| | |
options[:vdr] = h | |
end | |
opts.on("-f", | |
"--filesystem FILESYSTEM", | |
"ZFS filesystem to be snapshoted") do |f| | |
options[:filesystem] = f | |
end | |
opts.on("-z", | |
"--zfs-host HOST", | |
"Host of the zfs filesystem") do |z| | |
options[:zfs] = z | |
end | |
options[:verbose] = false | |
opts.on("-v", | |
"--verbose", | |
"Verbose") do |verbose| | |
options[:verbose] = verbose | |
end | |
opts.on("-H", | |
"--help", | |
"Display this screen") do | |
puts opts | |
exit | |
end | |
end | |
begin | |
optparse.parse! | |
mandatory = [:zfs, :filesystem, :vdr] | |
missing = mandatory.select{ |param| options[param].nil? } | |
if not missing.empty? | |
puts "Missing options: #{missing.join(', ')}" | |
puts optparse | |
exit | |
end | |
v = options[:verbose] | |
rescue OptionParser::InvalidOption, OptionParser::MissingArgument | |
puts $!.to_s | |
puts optparse | |
exit | |
end | |
# Check to see if snapdisable is set. If set, update | |
snapdisable = "#{File.expand_path('~')}/snapdisable-#{options[:vdr]}" | |
if File.exist?(snapdisable) | |
if v then puts "Snapshots for #{options[:vdr]} have been disabled since #{File.ctime(snapdisable)}" end | |
Email.send( | |
"address@domain", | |
"VDR Snapshot Manager", | |
"address@domain", | |
"Infrastructure Ops", | |
"[vdr] DISABLED Snapshots: #{options[:vdr]}", | |
"Snapshots for #{options[:vdr]} have been disabled since #{File.ctime(snapdisable)}" | |
) | |
exit 1 | |
end | |
# Shutdown the vdr vm | |
if v then puts "Shutdown #{options[:vdr]}" end | |
if not system("#{shutdown} #{options[:vdr]} &> /dev/null") | |
if v then puts "#{options[:vdr]} could not be shutdown" end | |
Email.send( | |
"address@domain", | |
"VDR Snapshot Manager", | |
"address@domain", | |
"Infrastructure Ops", | |
"[vdr] ERROR: #{options[:vdr]}", | |
"#{options[:vdr]} could not be shutdown" | |
) | |
exit 1 | |
end | |
until status == 'off' | |
sleep(60) | |
if not `#{guest} #{options[:vdr]}` =~ /guestState: running/ | |
status = 'off' | |
end | |
end | |
if v then puts "#{options[:vdr]} has been shutdown" end | |
# Get Ticket | |
if v then puts "Requesting kerberos ticket" end | |
system("/usr/kerberos/bin/kinit -f -k -t #{keytab} kerberos-principal &> /dev/null") | |
# SSH to the zfs host and run a snapshot | |
if v then puts "Initiate snapshot of #{options[:filesystem]} on #{options[:zfs]}" end | |
ssh_command = "ssh #{remote_user}@#{options[:zfs]} '/inst/pkg/utilities/bin/zsnap -b #{options[:filesystem]} -f -p day' &> /dev/null" | |
if not system(ssh_command) | |
if v then puts "\"#{ssh_command}\" did not execute successfully for an unknown reason" end | |
Email.send( | |
"address@domain", | |
"VDR Snapshot Manager", | |
"address@domain", | |
"Infrastructure Ops", | |
"[vdr] ERROR: #{options[:vdr]}", | |
"\"#{ssh_command}\" did not execute successfully for an unknown reason" | |
) | |
errors = errors + "\"#{ssh_command}\" did not execute successfully for an unknown reason\n" | |
end | |
# Restart the vm | |
if v then puts "Starting #{options[:vdr]}" end | |
if not system("#{poweron} #{options[:vdr]} &> /dev/null") | |
if v then puts "#{options[:vdr]} could not be powered on" end | |
Email.send( | |
"address@domain", | |
"VDR Snapshot Manager", | |
"address@domain", | |
"Infrastructure Ops", | |
"[vdr] ERROR: #{options[:vdr]}", | |
"#{options[:vdr]} could not be powered on. Please manually bring it back up." | |
) | |
errors = errors + "#{options[:vdr]} could not be powered on.\n" | |
end | |
# Destroy the kerberos ticket | |
if v then puts "Destroy the kerberos ticket" end | |
system("/usr/kerberos/bin/kdestroy &> /dev/null") | |
status = "Snapshots for #{options[:vdr]} ran at #{Time.new}.\n\nErrors:\n#{errors}" | |
output = `#{confluence['app']} --server #{confluence['server']} --user #{confluence['user']} --password "#{confluence['pass']}" --action storePage --title "VDR Snapshots - #{options[:vdr]}" --parent "Script Status" --space datacenter --content '#{status}' 2>&1` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment