engineyard-backup 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.txt +1 -1
- data/lib/backup.rb +33 -1
- data/lib/backup/backup.rb +20 -10
- data/spec/backup/backup_spec.rb +16 -7
- metadata +4 -12
data/README.txt
CHANGED
data/lib/backup.rb
CHANGED
@@ -5,5 +5,37 @@ APP_ROOT = File.expand_path('../') unless defined?(APP_ROOT)
|
|
5
5
|
|
6
6
|
require 'date'
|
7
7
|
require 'fileutils'
|
8
|
+
require 'highline/import'
|
8
9
|
|
9
|
-
require 'backup/backup.rb'
|
10
|
+
require 'backup/backup.rb'
|
11
|
+
|
12
|
+
# Set debug options
|
13
|
+
debug = {
|
14
|
+
"1" => "DEBUG",
|
15
|
+
"2" => "STANDARD"
|
16
|
+
}
|
17
|
+
puts "DEBUG IS TURNED TO #{debug[ENV['DEBUG']]}" if ENV['DEBUG']
|
18
|
+
$debug = ENV['DEBUG']
|
19
|
+
|
20
|
+
# Debug command defined
|
21
|
+
def debug(text)
|
22
|
+
say text if $debug
|
23
|
+
end
|
24
|
+
|
25
|
+
# Highline options
|
26
|
+
SEP = '='*72 unless defined? SEP
|
27
|
+
CLEAR = "\e[H\e[2J" unless defined? CLEAR
|
28
|
+
HighLine.track_eof = false # highline has an issue with threading
|
29
|
+
ft = HighLine::ColorScheme.new do |cs|
|
30
|
+
cs[:headline] = [ :bold, :yellow, :on_black ]
|
31
|
+
cs[:horizontal_line] = [ :bold, :white ]
|
32
|
+
cs[:even_row] = [ :red ]
|
33
|
+
cs[:odd_row] = [ :yellow ]
|
34
|
+
cs[:debug] = [ :yellow ]
|
35
|
+
cs[:warn] = [ :cyan ]
|
36
|
+
cs[:info] = [ :white ]
|
37
|
+
cs[:critical] = [ :red ]
|
38
|
+
cs[:pass] = [ :green ]
|
39
|
+
cs[:fail] = [ :red ]
|
40
|
+
end
|
41
|
+
HighLine.color_scheme = ft
|
data/lib/backup/backup.rb
CHANGED
@@ -4,7 +4,7 @@ module EngineYard
|
|
4
4
|
attr_reader :filename, :backups
|
5
5
|
attr_accessor :releases
|
6
6
|
|
7
|
-
VERSION = "0.0.
|
7
|
+
VERSION = "0.0.4"
|
8
8
|
TIMESTAMP = "%Y%m%d%H%M%S"
|
9
9
|
|
10
10
|
# Pass in a filename, Backup will set the directory it works in from this file
|
@@ -12,7 +12,8 @@ module EngineYard
|
|
12
12
|
# Backup.new("/my/file")
|
13
13
|
# # adjust the class to keep 3 releases
|
14
14
|
# Backup.new("/my/file", 3)
|
15
|
-
def initialize(file, releases = 5)
|
15
|
+
def initialize( file, releases = 5 )
|
16
|
+
debug "initialized with #{file} and handling #{releases} releases"
|
16
17
|
raise Errno::ENOENT, "#{file}", caller unless File.file?(file)
|
17
18
|
@filename, @backups = file, []
|
18
19
|
@releases = releases
|
@@ -22,20 +23,22 @@ module EngineYard
|
|
22
23
|
# Options:
|
23
24
|
# + operation = :move or :copy
|
24
25
|
# + skip_cleanup = :yes or :no
|
25
|
-
def run(operation = :copy, skip_cleanup = :no)
|
26
|
-
|
26
|
+
def run( operation = :copy, skip_cleanup = :no )
|
27
|
+
debug "operation is #{operation.to_s}"
|
28
|
+
debug "skip_cleanup is #{skip_cleanup.to_s}"
|
29
|
+
move_current( operation )
|
27
30
|
cleanup unless skip_cleanup == :yes
|
28
31
|
end
|
29
32
|
|
30
33
|
# Look for releases and delete the oldest ones outside of the X releases threshold
|
31
34
|
def cleanup
|
32
35
|
find_all_releases
|
33
|
-
delete_list.each {|f| File.delete(f) }
|
36
|
+
delete_list.each {|f| File.delete( f ) }
|
34
37
|
end
|
35
38
|
|
36
39
|
# Returns an array of files that will be kept
|
37
40
|
def keep_list
|
38
|
-
@backups.first(@releases)
|
41
|
+
@backups.first( @releases )
|
39
42
|
end
|
40
43
|
|
41
44
|
# Returns an array of files that will be deleted
|
@@ -53,7 +56,7 @@ module EngineYard
|
|
53
56
|
@backups = backups
|
54
57
|
case format
|
55
58
|
when :datetime
|
56
|
-
@backups.collect { |b| d = date_from(b.split(".").last); d.strftime(
|
59
|
+
@backups.collect { |b| d = date_from(b.split(".").last); d.strftime(TIMESTAMP) }
|
57
60
|
when :filename
|
58
61
|
backups
|
59
62
|
end
|
@@ -62,13 +65,15 @@ module EngineYard
|
|
62
65
|
private
|
63
66
|
|
64
67
|
def remove_faults(backups)
|
68
|
+
remove_list = []
|
65
69
|
backups.each do |backup|
|
66
70
|
begin
|
67
71
|
Date.strptime(backup.split(".").last, TIMESTAMP)
|
68
72
|
rescue ArgumentError
|
69
|
-
|
73
|
+
remove_list << backup
|
70
74
|
end
|
71
75
|
end
|
76
|
+
backups -= remove_list
|
72
77
|
backups
|
73
78
|
end
|
74
79
|
|
@@ -76,8 +81,13 @@ module EngineYard
|
|
76
81
|
DateTime.strptime(string, TIMESTAMP)
|
77
82
|
end
|
78
83
|
|
79
|
-
def move_current
|
80
|
-
|
84
|
+
def move_current( operation = :move )
|
85
|
+
case operation
|
86
|
+
when :move
|
87
|
+
FileUtils.mv(@filename, "#{@filename}.#{Time.now.strftime(TIMESTAMP)}")
|
88
|
+
when :copy
|
89
|
+
FileUtils.cp(@filename, "#{@filename}.#{Time.now.strftime(TIMESTAMP)}")
|
90
|
+
end
|
81
91
|
end
|
82
92
|
|
83
93
|
end
|
data/spec/backup/backup_spec.rb
CHANGED
@@ -25,7 +25,8 @@ describe Backup do
|
|
25
25
|
end
|
26
26
|
|
27
27
|
it "should save a new file and delete all backups out of the threshold" do
|
28
|
-
FileUtils.expects( :mv ).times(
|
28
|
+
FileUtils.expects( :mv ).times( 0 )
|
29
|
+
FileUtils.expects( :cp ).times( 1 )
|
29
30
|
valid_backups( 9, :chronological ).last( 4 ).each do |backup|
|
30
31
|
File.expects( :delete ).with( backup ).returns( 1 )
|
31
32
|
end
|
@@ -33,8 +34,9 @@ describe Backup do
|
|
33
34
|
end
|
34
35
|
|
35
36
|
it "should not raise errors with zero current backups" do
|
37
|
+
FileUtils.expects( :mv ).times( 0 )
|
38
|
+
FileUtils.expects( :cp ).times( 1 )
|
36
39
|
Dir.stubs( :glob ).returns( [] )
|
37
|
-
FileUtils.expects( :mv )
|
38
40
|
File.expects( :delete ).times( 0 ).returns( 1 )
|
39
41
|
@backup.run
|
40
42
|
end
|
@@ -49,7 +51,7 @@ describe Backup do
|
|
49
51
|
it "should sort them into chronological order" do
|
50
52
|
@backup.backups.should == valid_backups( 9, :chronological )
|
51
53
|
end
|
52
|
-
|
54
|
+
|
53
55
|
it "should keep the 5 newest releases when creating a new backup" do
|
54
56
|
@backup.keep_list.should == valid_backups( 5, :chronological )
|
55
57
|
end
|
@@ -65,11 +67,18 @@ describe Backup do
|
|
65
67
|
backup.keep_list.should == valid_backups( 4, :chronological )
|
66
68
|
end
|
67
69
|
|
68
|
-
it "should
|
69
|
-
backup = Backup.new( "ey_my.cnf", 4 )
|
70
|
+
it "should by default, copy the file not move" do
|
70
71
|
FileUtils.expects( :mv ).times( 0 )
|
71
|
-
|
72
|
-
|
72
|
+
FileUtils.expects( :cp ).times( 1 )
|
73
|
+
File.expects( :delete ).times( 4 )
|
74
|
+
@backup.run
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should delete the original file if told to move instead of copy" do
|
78
|
+
FileUtils.expects( :mv ).times( 1 )
|
79
|
+
FileUtils.expects( :cp ).times( 0 )
|
80
|
+
File.expects( :delete ).times( 4 )
|
81
|
+
@backup.run( :move )
|
73
82
|
end
|
74
83
|
end
|
75
84
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: engineyard-backup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jamie van Dyke
|
@@ -10,18 +10,10 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2008-
|
13
|
+
date: 2008-06-11 00:00:00 -07:00
|
14
14
|
default_executable:
|
15
|
-
dependencies:
|
16
|
-
|
17
|
-
name: mocha
|
18
|
-
version_requirement:
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
20
|
-
requirements:
|
21
|
-
- - ">="
|
22
|
-
- !ruby/object:Gem::Version
|
23
|
-
version: 0.5.6
|
24
|
-
version:
|
15
|
+
dependencies: []
|
16
|
+
|
25
17
|
description: Backup is a Ruby library to ease the backup of files, keeping the latest X releases. Just require it and go.
|
26
18
|
email: jvandyke@engineyard.com
|
27
19
|
executables: []
|