Class: Backsum::Project

Inherits:
Object
  • Object
show all
Defined in:
lib/backsum/project.rb,
lib/backsum/project_dsl.rb

Defined Under Namespace

Classes: Dsl

Constant Summary collapse

"Latest"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Project

Returns a new instance of Project.



20
21
22
23
24
25
26
27
# File 'lib/backsum/project.rb', line 20

def initialize(*args)
  self.keep_days = 3
  self.keep_weeks = 4
  self.name = "default"
  self.backup_to = Proc.new { "./backups/#{name}" }
  self.servers = []
  super
end

Class Method Details

.dsl(*args, &block) ⇒ Object



7
8
9
# File 'lib/backsum/project_dsl.rb', line 7

def self.dsl(*args, &block)
  Dsl.new(*args, &block).instance
end

Instance Method Details

#backup_namesObject



73
74
75
76
77
78
# File 'lib/backsum/project.rb', line 73

def backup_names
  Dir[File.join(self.backup_to, "*")].map do |backup_path|
    next if File.basename(backup_path) == LATEST_LINK_NAME
    File.basename(backup_path)
  end.compact
end

#backup_toObject



29
30
31
# File 'lib/backsum/project.rb', line 29

def backup_to
  @backup_to.respond_to?(:call) ? instance_eval(&@backup_to) :  @backup_to.to_s
end

#backupsObject



69
70
71
# File 'lib/backsum/project.rb', line 69

def backups
  self.backup_names.map { |backup_name| Backup.new(name: backup_name, base_dir: self.backup_to) }
end

#build_target_backup_folderObject



39
40
41
42
# File 'lib/backsum/project.rb', line 39

def build_target_backup_folder
  FileUtils.mkdir_p(self.backup_to)
  FileUtils.mkdir_p(self.current_backup.path) if !self.latest_backup
end

#cleanup_outdate_backupsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/backsum/project.rb', line 84

def cleanup_outdate_backups
  backups = self.backups
  
  backups.each { |b| b.outdated = true }
  
  # keep weekly backups
  cweeks_mapping = backups.group_by(&:cweek)
  cweeks_mapping.keys.sort.reverse.slice(0, self.keep_weeks).each do |cweek|
    cweeks_mapping[cweek].sort.last.outdated = false
  end
  
  # keep daily backups
  days_mapping = backups.group_by(&:day)
  days_mapping.keys.sort.reverse.slice(0, self.keep_days).each do |day|
    days_mapping[day].sort.last.outdated = false
  end
  
  # keep latest backup
  # skip, already keep by daily
  
  backups.each do |backup|
    FileUtils.rm_rf(backup.path) if backup.outdated?
  end
end

#current_backupObject



57
58
59
# File 'lib/backsum/project.rb', line 57

def current_backup
  @current_backup ||= Backup.new(backup_at: DateTime.now, base_dir: self.backup_to)
end

#latest_backupObject



61
62
63
64
65
66
67
# File 'lib/backsum/project.rb', line 61

def latest_backup
  path = File.join(self.backup_to, LATEST_LINK_NAME)
  if File.exists?(path)
    real_path = File.readlink(path)
    Backup.new(name: File.basename(real_path), base_dir: self.backup_to)
  end
end


80
81
82
# File 'lib/backsum/project.rb', line 80

def latest_link_name
  LATEST_LINK_NAME
end

#performObject



33
34
35
36
37
# File 'lib/backsum/project.rb', line 33

def perform
  build_target_backup_folder # prepare
  sync_servers_data # execute
  cleanup_outdate_backups # cleanup
end

#sync_servers_dataObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/backsum/project.rb', line 44

def sync_servers_data
  lastest_backup_path = self.latest_backup.path  if self.latest_backup
  
  self.servers.each do |server|
    server.sync self.current_backup.path, lastest_backup_path
  end
  
  Dir.chdir self.backup_to do
    FileUtils.rm_rf LATEST_LINK_NAME
    FileUtils.ln_sf self.current_backup.name, LATEST_LINK_NAME
  end
end