Skip to content

Instantly share code, notes, and snippets.

@anthroprose
Created June 5, 2013 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthroprose/9a1b3b7dbf3218578bcb to your computer and use it in GitHub Desktop.
Save anthroprose/9a1b3b7dbf3218578bcb to your computer and use it in GitHub Desktop.
cron "chef-snapshot-backup" do
user "root"
hour "*"
minute "0"
command "pgrep chef-client >/dev/null || chef-client -o 'recipe[zaas::snapshot-backup]' --no-color --force-logger --logfile /var/log/chef-client.log >> /var/log/chef-client.log 2>&1"
end
ruby_block "create_snapshot" do
block do
require "net/http"
require "uri"
uri = URI.parse('http://169.254.169.254/latest/meta-data/instance-id')
response = Net::HTTP.get_response(uri)
instance_id = response.body
time = Time.new
volume_id = ''
mountpoint = ''
if node['region'] then
region = node['region']
else
region = 'us-east-1'
end
volumes = %x[source /etc/profile;/opt/aws/bin/ec2-describe-instances --region #{region} #{instance_id} | awk '/vol-*/ {print $2 " " $3}'].strip
volumes.split("\n").each { |vol|
volar = vol.split(" ")
if volar[0] == '/dev/sdh1' then
mountpoint = volar[0]
volume_id = volar[1]
end
}
mountpoint = "/opt/zenoss"
if node['zaas'] and node['zaas']['backup'] and node['zaas']['backup']['snapshot-mount'] then
mountpoint = node['zaas']['backup']['snapshot-mount']
end
sync = %x[sync]
freeze = %x[xfs_freeze -f #{mountpoint}]
ss = %x[source /etc/profile;/opt/aws/bin/ec2-create-snapshot --region #{region} #{volume_id} -d #{instance_id}_#{volume_id}].strip
thaw = %x[xfs_freeze -u #{mountpoint}]
snapshot_id = ss.split(" ")[1]
snapdate = time.strftime("%Y-%m-%d")
snaptime = time.strftime("%H:%M:%S")
if node.has_key?('elasticsearch') and node["elasticsearch"]["cluster"]["name"] != ''
stackname=node["elasticsearch"]["cluster"]["name"]
else
stackname=node["cluster"]["name"]
end
tags = %x[source /etc/profile;/opt/aws/bin/ec2-create-tags --region #{region} #{snapshot_id} --tag Volume=#{volume_id} --tag Device=/dev/sdh1 --tag Date=#{snapdate} --tag=Instance=#{instance_id} --tag Name=#{stackname} --tag Time=#{snaptime}].strip
end
action :create
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment