Class: KnifeSharp::SharpBackup

Inherits:
Chef::Knife
  • Object
show all
Includes:
Common
Defined in:
lib/chef/knife/sharp-backup.rb

Instance Method Summary collapse

Methods included from Common

included

Instance Method Details

#backupObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/chef/knife/sharp-backup.rb', line 21

def backup
  timestamp = Time.now.to_i

  unless  sharp_config["global"]["backupdir"]
    ui.error "You need to add global/backupdir to your config file"
    exit 1
  end

  backup_path = sharp_config["global"]["backupdir"] + "/backup-#{timestamp}"

  if File.exist?(backup_path)
    ui.error "Backup path (#{backup_path}) exists. Will not overwrite."
    exit 1
  end

  ui.msg("Backing up roles")
  FileUtils.mkdir_p(backup_path + "/roles")
  Chef::Role.list.keys.each do |role|
    begin
      remote_role = Chef::Role.load(role)
      File.open(backup_path + "/roles/#{role}.json", "w") do |file|
        file.puts JSON.pretty_generate(remote_role)
      end
    rescue Exception => e
      ui.error "Unable to dump #{role} role (#{e.message})"
    end
  end

  ui.msg("Backing up environments")
  FileUtils.mkdir_p(backup_path + "/environments")
  Chef::Environment.list.keys.each do |environment|
    begin
      env = Chef::Environment.load(environment)
      File.open(backup_path + "/environments/#{environment}.json", "w") do |file|
        file.puts JSON.pretty_generate(env)
      end
    rescue Exception => e
      ui.error "Unable to dump #{environment} environment (#{e.message})"
    end
  end

  ui.msg("Backing up databags")
  FileUtils.mkdir_p(backup_path + "/databags")
  Chef::DataBag.list.keys.each do |bag|
    Dir.mkdir(backup_path + "/databags/" + bag)
    Chef::DataBag.load(bag).keys.each do |item|
      begin
        data = Chef::DataBagItem.load(bag,item)
        File.open(backup_path + "/databags/#{bag}/#{item}.json", "w") do |file|
          file.puts JSON.pretty_generate(data)
        end
      rescue Exception => e
        ui.error "Unable to dump item #{item} from databag #{bag} (#{e.message})"
      end
    end
  end

end

#runObject



17
18
19
# File 'lib/chef/knife/sharp-backup.rb', line 17

def run
  backup()
end