Created
June 5, 2011 02:50
-
-
Save Atalanta/1008594 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
Chef::Config[:solo] = false | |
class Opscode | |
class Backup | |
attr_accessor :backup_dir | |
def initialize | |
@backup_dir = Chef::Config[:knife][:chef_server_backup_dir] ? Chef::Config[:knife][:chef_server_backup_dir] : File.join(".chef", "chef_server_backup") | |
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 | |
b = Opscode::Backup.new | |
b.backup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment