Ask Your Question

How to take backup of file before changing

asked 2017-03-20 07:26:24 -0500

bentech4u gravatar image

updated 2017-03-20 15:42:48 -0500

DarylW gravatar image

HI Community,

i am new to puppet and just started with coding,

i wrote and class to do some changes on hosts file, the code below

class company-standard::hostname{
  File {
    backup => 'true'
   ensure => file,
   path => '/tmp/hello.txt',
   content => "Test Contect",

  file { "/etc/hosts":
   ensure => present,
  file_line { "Append a line to /etc/hosts":
   path => "/etc/hosts",
   line => template("company-standard/hosts.erb"),

  file_line{ "Commenting IPV6":
   path => "/etc/hosts",
   line => "#::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 test",
   match => "localhost6.*$",

how can i take backup of the file before doing the changes, even though i put file => backup, it's not working

thanks & Regards, Ben

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2017-03-20 16:09:09 -0500

DarylW gravatar image

Have you read the documentation on the backup parameter?


Whether (and how) file content should be backed up before being replaced. This attribute works best as a resource default in the site manifest (File { backup => main }), so it can affect all file resources.

•If set to false, file content won’t be backed up.

•If set to a string beginning with . (e.g., .puppet-bak), Puppet will use copy the file in the same directory with that value as the extension of the backup. (A value of true is a synonym for .puppet-bak.)

•If set to any other string, Puppet will try to back up to a filebucket with that title. See the filebucket resource type for more details. (This is the preferred method for backup, since it can be centralized and queried.)

Default value: puppet, which backs up to a filebucket of the same name. (Puppet automatically creates a local filebucket named puppet if one doesn’t already exist.)

Backing up to a local filebucket isn’t particularly useful. If you want to make organized use of backups, you will generally want to use the puppet master server’s filebucket service. This requires declaring a filebucket resource and a resource default for the backup attribute in site.pp:

There are two things going on here. One is that the file resource is not the resource making the changes to the file, the file_line resource is. I think that for the backup parameter to work successfully, you would need to make the modifications via the file resource.

The second thing is that you have a string 'true' set, so if it is backing up, it would be trying to backup to a filebucket named 'true'.

If I try a really simple test

$ echo 'foo' > /tmp/hello.txt
$ ls /tmp/hello.txt*
$ cat ~/testbackup.pp 
  ensure => file,
  path => '/tmp/hello.txt',
  content => "Test Content",
  backup => 'true'

$ puppet apply ~/testbackup.pp
Notice: Complied catalog for hostname in environment production in 0.09 seconds
Notice: /Stage[main]/Main/File[/tmp/hello.txt]/content: content changed '{md5}d3b07384d113edec49eaa6238ad5ff00' to '{md5}d65cdbadce08158e7de64a5a44b4617'
Notice: Finished catalog run in 0.03 seconds
$ ls /tmp/hello.txt*
/tmp/hello.txt              /tmp/hello.txt.puppet-bak
$ cat /tmp/hello.txt.puppet-bak
$ cat /tmp/hello.txt
Test Content

In that case, I can see that puppet created a backup file with the extension of '.puppet-bak' with the file's prior content.

What specifically are you trying to achieve?

edit flag offensive delete link more


Thanks, yes if i am adding content <content ==""> "Test Content", > , i can able to see puppet is taking backup of the file. But i am changing file contents with file_line , how can i take backup of that file in that case.

bentech4u gravatar imagebentech4u ( 2017-03-20 23:23:10 -0500 )edit

I don't believe file_line has that capability

DarylW gravatar imageDarylW ( 2017-03-22 07:14:49 -0500 )edit

answered 2017-03-21 03:40:38 -0500

bentech4u gravatar image


Is it possible to trigger backup without changing content?

Regards, Ben

edit flag offensive delete link more


I THINK you can get similar behavior by using the audit parameter.. I haven't tried it out though.

DarylW gravatar imageDarylW ( 2017-03-21 08:11:31 -0500 )edit

i checked audit and it's collecting the file parameters but not taking backup of the file

bentech4u gravatar imagebentech4u ( 2017-03-25 23:25:04 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-03-20 07:26:24 -0500

Seen: 38 times

Last updated: Mar 21