Skip to content

Instantly share code, notes, and snippets.

@adamhjk
Created November 10, 2010 19:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save adamhjk/671369 to your computer and use it in GitHub Desktop.
Save adamhjk/671369 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
require 'ospcode-backup'
b = Opscode::Backup.new("/tmp/backup", "~/.chef/knife.rb")
b.backup
@paulschreiber
Copy link

In use_it.rb, line 1 should be require 'opscode-backup'. And ~ expansion doesn't seem to work; I had to specify the full path to knife.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment