david
david

Reputation: 761

backup a directory with a chef resource?

I'm wondering if there's a resource out there to make a copy of a whole directory. I know I could always use a bash resource and just do a 'cp', but I was wondering if there's an analog to the 'file' resource for directories.

bash resource solution

bash "backup org folder" do
  code <<-EOL
  mkdir /opt/tmp/copy
  mv /opt/tmp/org/* /opt/tmp/copy

  EOL
end

I found this: How to move/copy files locally with Chef But is 2 years old so I'm wondering if there's any new resource for this case.

Thanks.

Upvotes: 0

Views: 679

Answers (2)

david
david

Reputation: 761

The only way I found to do this is using the bash resource but there are a few things you need to be careful with.

 bash "backup testDir directory" do
  user "jhon"
  cwd "/opt/lnp"
  environment ({'HOME' => '/home/jhon', 'USER' => 'jhon'})
  code <<-EOL
  {
    mv /opt/lnp/testDir/* /opt/lnp/testDir.#{TODAY}
  }
  EOL
  not_if { Dir['/opt/lnp/testDir/*'].empty? }
end
  1. If you need to do the copy as a specific user you need to initialize the environment
  2. in order to avoid an error if the folder is empty you should check if the directory is empty via a not_if block

Upvotes: 0

Pubudu Perera
Pubudu Perera

Reputation: 84

No other way as per my knowledge. Either you have to use a bash resource as you have done or through a Ruby block

Upvotes: 1

Related Questions