How Puppet Recovering Overwritten Files ? [#Tutorial]


Puppet Recovering Overwritten Files/ We all know the benefit of using Puppet in managing huge IT infrastructures. Puppet Master will be used to send configuration or desired state of puppet agents or clients or servers we wish to manage using manifests.

The alarming thing is that puppet default behavior is to overwrite the files, if the file already exists on the puppet agent machine. Which is sometimes good, sometimes bad (if we had deleted or overwritten a right file, wrongly).

This can be real disastrous, in any production environment.

There must be a way in puppet recovering overwritten files.

YES there is !!

Puppet creates a backup copy of any file it changes, and stores it on the machine in a place called the clientbucket – /var/lib/puppet/clientbucket – to be precise. This can be handy if you ever accidentally overwrite an important file, and want to retrieve its original contents.

Now check these steps to see puppet recovering overwritten files:

1. 
I created a puppet manifest. A file resource which will be creating a file /tmp/bucket.txt with some sample text. Consider this text as very important for your production server. So here I am applying the manifest for the first time. Which will create the original and right file at the right place. The original contents here are – “This is backed up under clientbucket, if overwritten”.


Original File created by puppet

2.
Now, I am intentionally, editing (consider it by accident) the original file /tmp/bucket.txt using puppet manifest. Creating a scenario that the original file is overwritten, by mistake.


Editing the original file, using puppet

3.
Now after applying the new puppet manifest using puppet apply testbucket.pp, the original file /tmp/bucket.txt is overwritten by our fake or wrong contents, leading to non desired results on our Linux servers. You can see the new line – “Overwriting now” appended in the file.
Overwritten file by puppet
4.
Now comes the challenge. We want our original file back. We already learned that puppet, by default, make a backup of all overwritten files under a location – clientbucket – /var/lib/puppet/clientbucket. So if I go under – /var/lib/puppet/clientbucket and do a grep for the word “bucket” (which was there in the original file). I should be able to get the original contents.

Recovering overwritten file from /var/lib/puppet/clientbucket

5.
Now GEEKS !! Once you know the exact location, you can go in the path displayed and recover the original file and simply use the cp command to copy it in the original location.



Copy the contents from client bucket to original location

6.
This is indeed a good Puppet hack to know. But in case you don’t want, think twice that you really don’t want. You don’t want puppet to create a backup copy of the file under – /var/lib/puppet/clientbucket. That simply means you cannot recover it in case of accidental modification.

You can still tell puppet not to create backup of any overwritten file by using – backup => false – argument within the file resource.


Configuring puppet not to create backup

About The Author:
This tutorial has written by Alok Srivastava. He is the founder of Network Nuts, India's most loved Linux training company.