Search code examples
chef-infrachef-recipechef-solo

How to do this in chef


Is there a directive in chef that will allow me to backup an existing file prior to it being overwritten?

Supposed I have this code.

template "myfile"
  source "myfile.erb"
  <more properties>
end

The myfile is an existing configuration file and I just need it to be overriden so I used a template.

However, I want the existing file to be backup before it is overwritten by my template.

I can do a backup command like this

execute do
    mv  "myfile"  "myfile.old"
end

template "myfile"
  source "myfile.erb"
  <more properties>
end

But i was thinking if there is an alternative.

----------------EDIT----------------------- This looks good but..I am not getting something.. sorry chef newbie here. According to the documentation, it says..I have to supply this..

backup FalseClass, Integer

template "myfile"
  source "myfile.erb"
  backup true, 1
end

But I am getting this error..

ERROR: wrong number of arguments (2 for 0..1)

Sorry, I am new to chef and ruby syntax. Just picked up chef and learning it on my own.


Solution

  • Maybe this can help you. I use a cookbook because it's a clear way to show you how it works + it avoids directory problems:

    first a directory in which I store my cookbooks:

    $ mkdir cookbooks
    

    Now I'm going to create my cookbook with the name create-backup

    $ chef generate cookbook cookbooks/create-backup
    

    I will also create a template inside my cookbook which will be the file I'll backup

    $ chef generate template cookbooks/create-backup myfile-tobackup.txt
    

    This is what's inside my cookbook:

    tree cookbooks/create-backup/
    $ cookbooks/create-backup/
    |-- Berksfile
    |-- chefignore
    |-- metadata.rb
    |-- README.md
    |-- recipes
    |   `-- default.rb
    |-- spec
    |   |-- spec_helper.rb
    |   `-- unit
    |       `-- recipes
    |           `-- default_spec.rb
    |-- templates
    |   |-- default
    |   `-- myfile-tobackup.txt.erb
    `-- test
        `-- smoke
            `-- default
                `-- default_test.rb
    

    Only the recipes and the templates are important for your case. I'll put some input in the myfile-tobackup.txt.erb

    $ vi cookbooks/create-backup/templates/myfile-tobackup.txt.erb
    Content:
    this is version 1
    

    Now I'm editing the default.rb in the recipes (this file will be executed if you run the cookbook).

    $ vi cookbooks/create-backup/recipes/default.rb
    

    And the content:

    template '/tmp/backup' do
       source 'myfile-tobackup.txt.erb'
       backup 3
    end
    

    This means I will create a file with the name backup in /tmp from the source myfile-tobackup.txt and I will create a max of 3 backups.

    Now I can execute my cookbook by running the recipe of my cookbook:

    $ chef-client --local-mode --runlist 'recipe[create-backup]'
    

    I'll check:

    $ cat /tmp/backup
    this is version 1
    

    Now I can edit the content of my template myfile-tobackup.txt.erb and set the version to 2. When I rerun the cookbook the /tmp/backup will contain "this is version 2" but your old /tmp/backup is backuped in /var/chef/backup (it will keep 3 files).