module Puppet::Util::Backups

Public Instance Methods

perform_backup(file = nil) click to toggle source

Deal with backups.

   # File lib/puppet/util/backups.rb
 6 def perform_backup(file = nil)
 7   # if they specifically don't want a backup, then just say
 8   # we're good
 9   return true unless self[:backup]
10 
11   # let the path be specified
12   file ||= self[:path]
13   return true unless Puppet::FileSystem.exist?(file)
14 
15   return(self.bucket ? perform_backup_with_bucket(file) : perform_backup_with_backuplocal(file, self[:backup]))
16 end

Private Instance Methods

backup_file_with_filebucket(f) click to toggle source
   # File lib/puppet/util/backups.rb
81 def backup_file_with_filebucket(f)
82   sum = self.bucket.backup(f)
83   self.info _("Filebucketed %{f} to %{filebucket} with sum %{sum}") % { f: f, filebucket: self.bucket.name, sum: sum }
84   return sum
85 end
perform_backup_with_backuplocal(fileobj, backup) click to toggle source
   # File lib/puppet/util/backups.rb
34 def perform_backup_with_backuplocal(fileobj, backup)
35   file = (fileobj.class == String) ? fileobj : fileobj.name
36   newfile = file + backup
37 
38   remove_backup(newfile)
39 
40   begin
41     bfile = file + backup
42 
43     # N.B. cp_r works on both files and directories
44     FileUtils.cp_r(file, bfile, :preserve => true)
45     return true
46   rescue => detail
47     # since they said they want a backup, let's error out
48     # if we couldn't make one
49     self.fail Puppet::Error, _("Could not back %{file} up: %{message}") % { file: file, message: detail.message }, detail
50   end
51 end
perform_backup_with_bucket(fileobj) click to toggle source
   # File lib/puppet/util/backups.rb
20 def perform_backup_with_bucket(fileobj)
21   file = (fileobj.class == String) ? fileobj : fileobj.name
22   case Puppet::FileSystem.lstat(file).ftype
23   when "directory"
24     # we don't need to backup directories when recurse is on
25     return true if self[:recurse]
26     info _("Recursively backing up to filebucket")
27     Find.find(self[:path]) { |f| backup_file_with_filebucket(f) if File.file?(f) }
28   when "file"; backup_file_with_filebucket(file)
29   when "link";
30   end
31   true
32 end
remove_backup(newfile) click to toggle source
   # File lib/puppet/util/backups.rb
53 def remove_backup(newfile)
54   if self.class.name == :file and self[:links] != :follow
55     method = :lstat
56   else
57     method = :stat
58   end
59 
60   begin
61     stat = Puppet::FileSystem.send(method, newfile)
62   rescue Errno::ENOENT
63     return
64   end
65 
66   if stat.ftype == "directory"
67     raise Puppet::Error, _("Will not remove directory backup %{newfile}; use a filebucket") % { newfile: newfile }
68   end
69 
70   info _("Removing old backup of type %{file_type}") % { file_type: stat.ftype }
71 
72   begin
73     Puppet::FileSystem.unlink(newfile)
74   rescue => detail
75     message = _("Could not remove old backup: %{detail}") % { detail: detail }
76     self.log_exception(detail, message)
77     self.fail Puppet::Error, message, detail
78   end
79 end