Created
November 10, 2010 19:33
-
-
Save adamhjk/671369 to your computer and use it in GitHub Desktop.
This file contains 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
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 |
This file contains 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
require 'ospcode-backup' | |
b = Opscode::Backup.new("/tmp/backup", "~/.chef/knife.rb") | |
b.backup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
use_it.rb
, line 1 should berequire 'opscode-backup'
. And~
expansion doesn't seem to work; I had to specify the full path toknife.rb
.