Can someone help me how to take backup for a specific folder and also zip/tar those folders. I am using this for taking a backup actually, but I would like a compressed output:

bash "backup #{dir_name}" do
    user 'user'
    cwd "#{path}"
    code <<-EOH
      cp -rp #{dir_name} #{dest}/#{dir_name}_bkp_#{timestmp}

Is there any chef resource to zip/tar a specific folder ?


1 Answer 1


Well that's not exactly about chef, you're basically asking how to use tar.

The command to create an archive of a folder would be
tar -zcvf <backup_thing>.tgz <path_to_backup>

In the tar options:

  • z pass the resulting tar archive to gzip for compression
  • c instruct tar to compress (create the archive)
  • v tells tar to be verbose, so you know at which point it is working, it has a performance impact
  • f <path> tells tar to output in a file or device

A correct chef resource for this would be as follow:

execute 'Backup #{dir_name}' do
  user 'user'
  command "tar -zcvf #{dest}/#{dir_name}_bkp_#{timestmp}.tgz #{dir_name}"
  live_stream :true # to see the progress during chef run, remove it and v in tar option if you don't care of it.

Using an execute resource avoid spawning a bash process to run tar, it is not needed, the shell_out class used by execute under the hood handles it already.

Be warned this is not idempotent, each run of chef will redo this backup command, that's maybe not what you're after.
For an update of jenkins (or something else) out of usual maintenance I would do a manual backup, check it is ok, and then run the upgrade (via chef eventually).

I'd recomend taking the tutorial at https://learn.chef.io and reading through the documentation at https://docs.chef.io

  • 1
    you are awesome @Tensibai
    – pandey
    Commented Apr 18, 2017 at 16:44
  • @pandey I hope you did learn about stackexchange sites in the process, and this one in particular :) Sorry to have pushed you a little, but I always found this way more effective. Welcome to Devops.SE :)
    – Tensibai
    Commented Apr 18, 2017 at 18:32
  • Thanks @Tensibai I got blocked to ask question over there in stackoverflow
    – pandey
    Commented Apr 18, 2017 at 19:04
  • @Pandey well the first instance of the question was not really high quality, SO has so much ´no effort and not willing to improve ' questions that we (I include myself on so) don't try much to push askers to improve. I hope this one did help you for this, keys are practical question, showing what you tried and explaining why it doesn't fit your goal, read twice to avoid typo, and your next question on SO should get a good reception, most people are willing to help someone showing he/she did tried by himself before asking :)
    – Tensibai
    Commented Apr 18, 2017 at 19:09
  • 1
    yes you are right. I will try to keep up from next time...
    – pandey
    Commented Apr 18, 2017 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.