Class: Backup::Rotator

Inherits:
Object
  • Object
show all
Includes:
FileUtils
Defined in:
lib/backup/rotator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(actor) ⇒ Rotator

Returns a new instance of Rotator.



11
12
13
14
# File 'lib/backup/rotator.rb', line 11

def initialize(actor)
  @actor = actor
  @configuration = actor.configuration
end

Instance Attribute Details

#actorObject (readonly)

Returns the value of attribute actor.



7
8
9
# File 'lib/backup/rotator.rb', line 7

def actor
  @actor
end

#configurationObject (readonly) Also known as: c

Returns the value of attribute configuration.



8
9
10
# File 'lib/backup/rotator.rb', line 8

def configuration
  @configuration
end

Class Method Details

.timestamped_prefix(name, time = "%Y-%m-%d-%H-%M-%S") ⇒ Object



146
147
148
149
150
# File 'lib/backup/rotator.rb', line 146

def self.timestamped_prefix(name,time="%Y-%m-%d-%H-%M-%S")
  time ||= "%Y-%m-%d-%H-%M-%S"  # there has to be a better way to do this
                                # but it works.
  newname = Time.now.strftime(time) + "_" + File.basename(name)
end

Instance Method Details

#create_sons_today?Boolean

Returns:

  • (Boolean)


95
# File 'lib/backup/rotator.rb', line 95

def create_sons_today?;     is_today_a? :son_created_on;     end

#promote_fathers_today?Boolean

Returns:

  • (Boolean)


97
# File 'lib/backup/rotator.rb', line 97

def promote_fathers_today?; is_today_a? :father_promoted_on; end

#promote_sons_today?Boolean

Returns:

  • (Boolean)


96
# File 'lib/backup/rotator.rb', line 96

def promote_sons_today?;    is_today_a? :son_promoted_on;    end

#rotate_via_ftp(last_result) ⇒ Object

TODO



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/backup/rotator.rb', line 70

def rotate_via_ftp(last_result)
#      ftp = Backup::FtpActor.new(c) 
#      ftp.connect 
#
#      hierarchy.each do |m| 
#        dir = c[:backup_path] + "/" + m
#        ftp.verify_directory_exists(dir) 
#      end  
#
#      newname = timestamped_prefix(last_result)
#      ftp.run "mv #{last_result} #{place_in}/#{newname}"
#
#      ftp.cleanup_directory(place_in, how_many_to_keep_today)
#      ftp.close
end

#rotate_via_mv(last_result) ⇒ Object

Take the last result and rotate it via mv on the local machine



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/backup/rotator.rb', line 17

def rotate_via_mv(last_result)
  # verify that each of the directories exist, grandfathers, fathers, sons
  hierarchy.each { |m| verify_local_backup_directory_exists(m) }

  where = place_in

  # place todays backup into the specified directory with a timestamp. 
  newname = timestamped_prefix(last_result)
  mv last_result, "#{where}/#{newname}"

  cleanup_via_mv(where, how_many_to_keep_today)
  update_state
end

#rotate_via_s3(last_result) ⇒ Object



86
87
88
89
90
91
92
93
# File 'lib/backup/rotator.rb', line 86

def rotate_via_s3(last_result)
  s3 = Backup::ChunkingS3Actor.new(c)
  s3.verify_rotation_hierarchy_exists(hierarchy)
  index = s3.rotation
  index[todays_generation] << last_result
  s3.rotation = index
  s3.cleanup(todays_generation, how_many_to_keep_today)
end

#rotate_via_ssh(last_result) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/backup/rotator.rb', line 50

def rotate_via_ssh(last_result)
  ssh = Backup::SshActor.new(c) 
  ssh.connect 
  ssh.run "echo \"#{last_result}\""

  hierarchy.each do |m| 
    dir = c[:backup_path] + "/" + m
    ssh.verify_directory_exists(dir) 
  end  

  where = place_in

  newname = timestamped_prefix(last_result)
  ssh.run "mv #{last_result} #{where}/#{newname}"

  ssh.cleanup_directory(where, how_many_to_keep_today)
  ssh.close
end

#timestamped_prefix(name) ⇒ Object

Given name returns a timestamped version of name.



153
154
155
# File 'lib/backup/rotator.rb', line 153

def timestamped_prefix(name)
  Backup::Rotator.timestamped_prefix(name,c[:timestamp])
end

#todays_generationObject



141
142
143
144
# File 'lib/backup/rotator.rb', line 141

def todays_generation
  goes_in = promote_fathers_today? ? "grandfathers" :         \
            promote_sons_today?    ? "fathers"      : "sons"
end

#update_stateObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/backup/rotator.rb', line 31

def update_state
  return unless :numeric == c[:rotation_mode] 
  where = find_goes_in_by_state

  previous_sons     = $state.system.sons_since_last_promotion    || 0
  previous_fathers  = $state.system.fathers_since_last_promotion || 0

  case where
    when "sons"
     $state.system.sons_since_last_promotion = previous_sons + 1
    when "fathers"
     $state.system.sons_since_last_promotion = 0
     $state.system.fathers_since_last_promotion = previous_fathers + 1
    when "grandfathers"
     $state.system.sons_since_last_promotion = 0
     $state.system.fathers_since_last_promotion = 0
  end
end