Class: EngineYard::Backup

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

Constant Summary collapse

VERSION =
"0.0.4"
TIMESTAMP =
"%Y%m%d%H%M%S"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, releases = 5) ⇒ Backup

Pass in a filename, Backup will set the directory it works in from this file

# default to keeping 5 releases
Backup.new("/my/file")
# adjust the class to keep 3 releases
Backup.new("/my/file", 3)

Raises:

  • (Errno::ENOENT)


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

def initialize( file, releases = 5 )
  debug "initialized with #{file} and handling #{releases} releases"
  raise Errno::ENOENT, "#{file}", caller unless File.file?(file)
  @filename, @backups = file, []
  @releases = releases
end

Instance Attribute Details

#backupsObject (readonly)

Returns the value of attribute backups.



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

def backups
  @backups
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

#releasesObject

Returns the value of attribute releases.



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

def releases
  @releases
end

Instance Method Details

#cleanupObject

Look for releases and delete the oldest ones outside of the X releases threshold



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

def cleanup
  find_all_releases
  delete_list.each {|f| File.delete( f ) }
end

#delete_listObject

Returns an array of files that will be deleted



45
46
47
# File 'lib/backup/backup.rb', line 45

def delete_list
  @backups - keep_list
end

#find_all_releases(format = :filename) ⇒ Object

Returns all versions of our backup filename that match file.TIMESTAMP Optional return format (:datetime)



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/backup/backup.rb', line 51

def find_all_releases(format = :filename)
  Dir.chdir(File.dirname(@filename))
  backups = Dir.glob("#{File.basename(@filename)}.*")
  remove_faults(backups)
  backups.sort! { |x,y| date_from(y.split(".").last) <=> date_from(x.split(".").last) }
  @backups = backups
  case format
  when :datetime
    @backups.collect { |b| d = date_from(b.split(".").last); d.strftime(TIMESTAMP) }
  when :filename
    backups
  end
end

#keep_listObject

Returns an array of files that will be kept



40
41
42
# File 'lib/backup/backup.rb', line 40

def keep_list
  @backups.first( @releases )
end

#run(operation = :copy, skip_cleanup = :no) ⇒ Object

Backup the current file, and keep X number of older versions Options: + operation = :move or :copy + skip_cleanup = :yes or :no



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

def run( operation = :copy, skip_cleanup = :no )
  debug "operation is #{operation.to_s}"
  debug "skip_cleanup is #{skip_cleanup.to_s}"
  move_current( operation )
  cleanup unless skip_cleanup == :yes
end