<p>Well that's not exactly about chef, you're basically asking how to use <code>tar</code>.</p>

<p>The command to create an archive of a folder would be<br>
<code>tar -zcvf &lt;backup_thing&gt;.tgz &lt;path_to_backup&gt;</code></p>

<p>In the tar options:</p>

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

<p>A correct chef resource for this would be as follow:</p>

<pre><code>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.
end
</code></pre>

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

<p>Be warned this is not idempotent, each run of chef will redo this backup command, that's maybe not what you're after.<br>
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).</p>

<p>I'd recomend taking the tutorial at <a href="https://learn.chef.io" rel="noreferrer">https://learn.chef.io</a> and reading through the documentation at <a href="https://docs.chef.io" rel="noreferrer">https://docs.chef.io</a> </p>

CC BY-SA 3.0
2017-04-18T16:44:28.783Z
you are awesome @Tensibai
pandey
CC BY-SA 3.0
2017-04-18T18:32:10.807Z
@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
CC BY-SA 3.0
2017-04-18T19:04:23.453Z
Thanks @Tensibai I got blocked to ask question over there in stackoverflow
pandey
CC BY-SA 3.0
2017-04-18T19:09:48.917Z
@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
CC BY-SA 3.0
2017-04-18T19:12:32.360Z
yes you are right. I will try to keep up from next time...
pandey
CC BY-SA 3.0
2017-04-18T15:52:51.677Z
2017-04-18T15:52:51.677Z
Tensibai
<p>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:</p>

<pre><code>bash "backup #{dir_name}" do
    user 'user'
    cwd "#{path}"
    code &lt;&lt;-EOH
      cp -rp #{dir_name} #{dest}/#{dir_name}_bkp_#{timestmp}
    EOH
end
</code></pre>

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

<chef><backup>
Zip/tar a folder using chef resource
pandey
# Zip/tar a folder using chef resource

(Asked by: username_0 on 2017-04-18)

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}
    EOH
end

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

**Tags:** chef, backup


---

## 1 answers

### Answer by username_1 on 2017-04-18. Score: 7 | Accepted answer

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 .tgz 
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  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.
end

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

> **Comments:**
> **username_0**: you are awesome @username_1  
> **username_1**: @username_0 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 :)  
> **username_0**: Thanks @username_1 I got blocked to ask question over there in stackoverflow  
> **username_1**: @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 :)  
> **username_0**: yes you are right. I will try to keep up from next time...  

---

