Class: EngineYard::Backup

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

Constant Summary collapse

VERSION =
"0.0.1"
RELEASES =
5
TIMESTAMP =
"%Y%m%d%H%M%S"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Backup

Returns a new instance of Backup.



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

def initialize(file)
  raise "No such file found" unless File.file?(file)
  @filename, @backups = file, []
end

Instance Attribute Details

#backupsObject (readonly)

Returns the value of attribute backups.



5
6
7
# File 'lib/backup/backup.rb', line 5

def backups
  @backups
end

#filenameObject (readonly)

Returns the value of attribute filename.



5
6
7
# File 'lib/backup/backup.rb', line 5

def filename
  @filename
end

Instance Method Details

#backup_currentObject



22
23
24
# File 'lib/backup/backup.rb', line 22

def backup_current
  FileUtils.mv(@filename, "#{@filename}.#{Time.now.strftime(TIMESTAMP)}")
end

#delete_old_backupsObject

Look for releases and delete the oldest ones outside of our RELEASES threshold



27
28
29
30
31
# File 'lib/backup/backup.rb', line 27

def delete_old_backups
  find_all_releases
  delete_list = @backups - keep_list
  delete_list.each {|f| File.delete(f) }
end

#find_all_releasesObject

Find all versions of our backup filename, which match file.TIMESTAMP



38
39
40
41
42
43
44
45
46
# File 'lib/backup/backup.rb', line 38

def find_all_releases
  Dir.chdir(File.dirname(@filename))
  backups = Dir.glob("#{File.basename(@filename)}.*")
  remove_faults(backups)
  backups.sort! do |x,y| 
    Date.strptime(x.split(".").last, TIMESTAMP) <=> Date.strptime(y.split(".").last, TIMESTAMP)
  end
  @backups = backups
end

#keep_listObject



33
34
35
# File 'lib/backup/backup.rb', line 33

def keep_list
  @backups[-RELEASES..-1]
end

#runObject



17
18
19
20
# File 'lib/backup/backup.rb', line 17

def run
  backup_current
  delete_old_backups
end