Skip to content

Instantly share code, notes, and snippets.

@jtimberman
Forked from adamhjk/opscode-backup.rb
Created November 14, 2010 13:16
Show Gist options
  • Save jtimberman/676149 to your computer and use it in GitHub Desktop.
Save jtimberman/676149 to your computer and use it in GitHub Desktop.
require 'chef'
require 'chef/node'
class Opscode
class Backup
attr_accessor :backup_dir
def initialize(backup_dir, config_file)
@backup_dir = backup_dir
Chef::Config.from_file(config_file)
Chef::Log.level = Chef::Config[:log_level]
end
def backup
nodes
roles
data_bags
end
def nodes
backup_standard("nodes", Chef::Node)
end
def roles
backup_standard("roles", Chef::Role)
end
def data_bags
dir = File.join(@backup_dir, "data_bags")
system("mkdir -p #{dir}")
Chef::DataBag.list.each do |bag_name, url|
system("mkdir -p #{File.join(dir, bag_name)}")
Chef::DataBag.load(bag_name).each do |item_name, url|
Chef::Log.info("Backing up data bag #{bag_name} item #{item_name}")
item = Chef::DataBagItem.load(bag_name, item_name)
File.open(File.join(dir, bag_name, "#{item_name}.json"), "w") do |dbag_file|
dbag_file.print(item.to_json)
end
end
end
end
def backup_standard(component, klass)
dir = File.join(@backup_dir, component)
system("mkdir -p #{dir}")
klass.list.each do |component_name, url|
Chef::Log.info("Backing up #{component} #{component_name}")
component_obj = klass.load(component_name)
File.open(File.join(dir, "#{component_name}.json"), "w") do |component_file|
component_file.print(component_obj.to_json)
end
end
end
end
end
$:.unshift(File.join(File.dirname(__FILE__)))
require 'opscode-backup'
b = Opscode::Backup.new("/tmp/backup", File.expand_path("~/.chef/knife.rb"))
b.backup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment