Skip to content

Instantly share code, notes, and snippets.

@troyready
Last active August 29, 2015 14:18
Show Gist options
  • Save troyready/45ea89f8762bbae568fb to your computer and use it in GitHub Desktop.
Save troyready/45ea89f8762bbae568fb to your computer and use it in GitHub Desktop.
Script to snapshot (or purge) snapshots for all Nimble storage volumes
#!/bin/env ruby
require 'net/http'
if ARGV[0].nil?
puts 'Nimble hostname required, e.g.:'
puts 'snap_all_nimble_vols mysan.local'
puts ''
fail
else
@nimble_host = URI.parse(ARGV[0]).path
end
@nimble_user = ENV['USER']
@nimble_pass = nil
def ssh(cmd)
require 'net/ssh'
Net::SSH.start(
@nimble_host,
@nimble_user,
keys: "#{ENV['HOME']}/.ssh/id_rsa"
) do |ssh|
ssh.exec!(
cmd
)
end
end
t = Time.now.strftime('%F::%T.%L')
protected_vols = []
vol_colls = ssh('volcoll --list | tail -n +5').chomp.split("\n")
vol_colls.map! do |v|
info = ssh("volcoll --info #{v.split("\s").first}")
next if info.match(/Associated volumes: none/)
protected_vols = protected_vols.concat(
info.split('Associated volumes: ')[1].split("\n")[0].split(', ')
)
v.split("\s").first
end
vol_colls.compact!
standalone_vols = ssh('vol --list | tail -n +5').chomp.split("\n")
# Only operate on online volumes
standalone_vols.map! do |v|
v.split("\s").first if v.match(/^.*\s\d*\sYes/) &&
!protected_vols.include?(v.split("\s").first)
end
standalone_vols.compact!
if !(ARGV[1] == '--delete') && !(ARGV[1] == '-d')
vol_colls.each do |v|
puts "Snapshotting volume collection #{v} with snapshot 'MassSnap-#{t}'"
ssh(
"volcoll --snap #{v} --snapcoll_name MassSnap-#{t}"
)
sleep 1
end
standalone_vols.each do |v|
puts "Snapshotting volume #{v} with snapshot 'MassSnap-#{t}'"
ssh(
"vol --snap #{v} --snapname MassSnap-#{t}"
)
sleep 1
end
else
puts 'Purging snapshots...'
sleep 4
snaps = ssh('snap --list | grep MassSnap-')
if !snaps.nil?
snaps.chomp.split("\n").each do |s|
vol = s.split(' ')[0]
snap = s.split(' ')[1]
puts "Deleting #{vol}'s snapshot #{snap}"
ssh("snap --delete #{snap} --vol #{vol}")
end
else
puts 'No snapshots found'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment