Created
February 27, 2013 22:45
-
-
Save ewr/5052557 to your computer and use it in GitHub Desktop.
Backup and upload scripts for migrating from one Chef server to another.
This file contains hidden or 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
# disable JSON inflation | |
JSON.create_id = "no_thanks" | |
out_dir = "BACKUP" | |
Dir.mkdir(out_dir) | |
["nodes","clients","roles"].each do |t| | |
Dir.mkdir( File.join( out_dir, t) ) | |
names = api.get(t).keys | |
names.each do |name| | |
the_node = api.get("#{t}/#{name}") | |
open(File.join(out_dir,t,name),"w") do |f| | |
f.write(the_node.to_json) | |
end | |
end | |
end | |
# Data bags are a little trickier | |
#Dir.mkdir( File.join( out_dir, "data_bags" ) ) | |
data_bags = {} | |
api.get("data").keys.each do |bag_name| | |
data_bags[ bag_name ] = {} | |
# get on the bag name to get items | |
api.get("data/#{bag_name}").keys.each do |item_name| | |
item = api.get("data/#{bag_name}/#{item_name}") | |
data_bags[ bag_name ][ item_name ] = item | |
end | |
end | |
open(File.join(out_dir,"data_bags.json"),"w") do |f| | |
f.write(data_bags.to_json) | |
end |
This file contains hidden or 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
# disable JSON inflation | |
JSON.create_id = "no_thanks" | |
out_dir = "BACKUP" | |
# -- load clients -- # | |
Dir["#{out_dir}/clients/*"].each do |path| | |
client = JSON.parse(IO.read(path)) | |
puts "Posting client #{client['name']}" | |
api.post("clients", { | |
:name => client['name'], | |
:public_key => client['public_key'] | |
}) | |
end | |
# -- load nodes -- # | |
Dir["#{out_dir}/nodes/*"].each do |path| | |
the_node = JSON.parse(IO.read(path)) | |
api.post("nodes", the_node) | |
end | |
# -- load roles -- # | |
Dir["#{out_dir}/roles/*"].each do |path| | |
the_node = JSON.parse(IO.read(path)) | |
api.post("roles", the_node) | |
end | |
# -- load data bags -- # | |
data_bags = {} | |
File.open("#{out_dir}/data_bags.json") do |f| | |
data_bags = JSON.parse(f.read()) | |
end | |
data_bags.each do |bag_name,bag| | |
# create data bag | |
puts "Creating data bag #{bag_name}" | |
api.post("data", {:name => bag_name}) | |
# create the items | |
bag.each do |item_name,item| | |
puts "Creating data bag item #{bag_name}/#{item_name}" | |
api.post("data/#{bag_name}",item) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment