0

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.

Community
  • 1
  • 1
david
  • 741
  • 3
  • 10
  • 29
  • please check this http://serverfault.com/questions/428970/copy-a-whole-directory-structure-in-chef handling recursive copying of directory structure with chef – hexerei software Apr 08 '15 at 20:17
  • sorry maybe I wasn't clear. I would like to copy a local directory and all it's contents to a different location – david Apr 08 '15 at 21:02

2 Answers2

1

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

0

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
david
  • 741
  • 3
  • 10
  • 29