liquid_backup 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.md +17 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/lib/backup.rb +15 -0
- data/lib/backup/handler/s3.rb +25 -0
- data/lib/backup/job.rb +111 -0
- data/lib/backup/manager.rb +30 -0
- data/spec/backup_spec.rb +41 -0
- data/spec/fs_root/home/user1/app1/production/current/config/backup.rb +6 -0
- data/spec/fs_root/home/user1/app1/production/current/public/system/important_files/file1.txt +1 -0
- data/spec/fs_root/home/user1/app1/production/current/public/system/important_files/file2.txt +1 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +104 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Dan Williams
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# liquid_backup
|
2
|
+
|
3
|
+
Backup Library used on our servers
|
4
|
+
|
5
|
+
## Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
13
|
+
* Send me a pull request. Bonus points for topic branches.
|
14
|
+
|
15
|
+
## Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2010 Liquid Media. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "liquid_backup"
|
8
|
+
gem.summary = %Q{Backup your application server}
|
9
|
+
gem.description = %Q{Backup your application server}
|
10
|
+
gem.email = "os@liquidmedia.ca"
|
11
|
+
gem.homepage = "http://github.com/liquidmedia/liquid_backup"
|
12
|
+
gem.authors = ["Dan Williams"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_development_dependency "yard", ">= 0"
|
15
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
begin
|
39
|
+
require 'yard'
|
40
|
+
YARD::Rake::YardocTask.new
|
41
|
+
rescue LoadError
|
42
|
+
task :yardoc do
|
43
|
+
abort "YARD is not available. In order to run yardoc, you must: sudo gem install yard"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
task :backup do
|
48
|
+
require 'lib/backup'
|
49
|
+
|
50
|
+
p Backup::Manager
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/backup.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'singleton'
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
module Backup
|
6
|
+
KEEP = 7
|
7
|
+
end
|
8
|
+
|
9
|
+
$: << File.join(File.dirname(__FILE__),'backup')
|
10
|
+
|
11
|
+
require 'job'
|
12
|
+
require 'manager'
|
13
|
+
|
14
|
+
|
15
|
+
Dir.glob(File.join(File.dirname(__FILE__),'backup','handler','*.rb')).each{|h| require h}
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'aws/s3'
|
2
|
+
|
3
|
+
module Backup
|
4
|
+
module Handler
|
5
|
+
class S3
|
6
|
+
def initialize(options={})
|
7
|
+
AWS::S3::Base.establish_connection!(:access_key_id => options[:access_key_id], :secret_access_key => options[:secret_access_key])
|
8
|
+
end
|
9
|
+
|
10
|
+
def store(location, files = [])
|
11
|
+
return false unless files.any?
|
12
|
+
|
13
|
+
AWS::S3::Bucket.create(location)
|
14
|
+
|
15
|
+
files.each do |file|
|
16
|
+
AWS::S3::S3Object.store("#{File.mtime(file).strftime("%Y%m%d%H%M%S")}_#{File.split(file)[-1]}", File.open(file), location)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def cleanup
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/backup/job.rb
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
class Backup::Job
|
2
|
+
attr_accessor :script_path
|
3
|
+
attr_accessor :application_directory
|
4
|
+
attr_accessor :backups_path
|
5
|
+
attr_accessor :current_backup_path
|
6
|
+
attr_accessor :previous_backups_path
|
7
|
+
|
8
|
+
attr_accessor :destination_handler
|
9
|
+
attr_accessor :destination_location
|
10
|
+
|
11
|
+
def initialize(script_path)
|
12
|
+
@script_path = script_path
|
13
|
+
@application_directory = script_path.gsub('/current/config/backup.rb','')
|
14
|
+
@backups_path = File.join(application_directory,'backups')
|
15
|
+
@current_backup_path = File.join(backups_path,'current')
|
16
|
+
@previous_backups_path = File.join(backups_path,'previous')
|
17
|
+
|
18
|
+
create_backup_folders
|
19
|
+
end
|
20
|
+
|
21
|
+
def perform
|
22
|
+
archive
|
23
|
+
|
24
|
+
eval(File.read(script_path), binding)
|
25
|
+
|
26
|
+
finalize
|
27
|
+
upload
|
28
|
+
end
|
29
|
+
|
30
|
+
def backup
|
31
|
+
yield
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def archive
|
37
|
+
archive_files = Dir.glob(File.join(current_backup_path,'*.tar.gz'))
|
38
|
+
last_run = File.mtime(current_backup_path).strftime("%Y%m%d%H%M%S")
|
39
|
+
last_run_destination = File.join(previous_backups_path,last_run)
|
40
|
+
|
41
|
+
FileUtils.mkdir_p last_run_destination
|
42
|
+
FileUtils.mv archive_files, last_run_destination
|
43
|
+
end
|
44
|
+
|
45
|
+
def finalize
|
46
|
+
finalize_folders
|
47
|
+
finalize_databases
|
48
|
+
|
49
|
+
cleanup
|
50
|
+
end
|
51
|
+
|
52
|
+
def cleanup
|
53
|
+
FileUtils.rm_rf File.join(current_backup_path,'folders')
|
54
|
+
FileUtils.rm_rf File.join(current_backup_path,'databases')
|
55
|
+
|
56
|
+
previous_backups = Dir.glob(File.join(previous_backups_path,'*')).sort
|
57
|
+
|
58
|
+
backups_to_prune = previous_backups - previous_backups[-Backup::KEEP,Backup::KEEP]
|
59
|
+
|
60
|
+
FileUtils.rm_rf backups_to_prune
|
61
|
+
end
|
62
|
+
|
63
|
+
def finalize_folders
|
64
|
+
source = File.join(current_backup_path,'folders')
|
65
|
+
destination = File.join(current_backup_path,'folders.tar.gz')
|
66
|
+
|
67
|
+
compress(source, destination)
|
68
|
+
end
|
69
|
+
|
70
|
+
def finalize_databases
|
71
|
+
source = File.join(current_backup_path,'databases')
|
72
|
+
destination = File.join(current_backup_path,'databases.tar.gz')
|
73
|
+
|
74
|
+
compress(source, destination)
|
75
|
+
end
|
76
|
+
|
77
|
+
def upload
|
78
|
+
files_to_backup = Dir.glob(File.join(current_backup_path,'*.tar.gz'))
|
79
|
+
Backup::Manager.handler(destination_handler).store(destination_location, files_to_backup)
|
80
|
+
end
|
81
|
+
|
82
|
+
def compress(source,destination)
|
83
|
+
`/usr/bin/env tar czf #{destination} #{source}`
|
84
|
+
end
|
85
|
+
|
86
|
+
def create_backup_folders
|
87
|
+
FileUtils.mkdir_p backups_path
|
88
|
+
FileUtils.mkdir_p current_backup_path
|
89
|
+
FileUtils.mkdir_p File.join(current_backup_path,'folders')
|
90
|
+
FileUtils.mkdir_p File.join(current_backup_path,'databases')
|
91
|
+
FileUtils.mkdir_p previous_backups_path
|
92
|
+
end
|
93
|
+
|
94
|
+
def destination(name, location)
|
95
|
+
@destination_handler = name
|
96
|
+
@destination_location = location
|
97
|
+
end
|
98
|
+
|
99
|
+
def folder(path, options = {})
|
100
|
+
source = File.join(application_directory,'current',path)
|
101
|
+
destination = File.join(current_backup_path,'folders',path.gsub('/','_') + '.tar.gz')
|
102
|
+
|
103
|
+
compress(source, destination)
|
104
|
+
end
|
105
|
+
|
106
|
+
def database(name, options = {})
|
107
|
+
destination = File.join(current_backup_path,'databases', name + '_' + Date.today.strftime("%Y%m%d%H%M%S") + '.sql')
|
108
|
+
|
109
|
+
`/usr/local/pgsql/bin/pg_dump -U postgres #{name} > #{destination}`
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
class Backup::Manager
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
attr_accessor :backup_handler
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@backup_handler = {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def collect_jobs(pattern)
|
12
|
+
@backup_jobs = []
|
13
|
+
Dir.glob(pattern).each do |backupjob|
|
14
|
+
@backup_jobs << Backup::Job.new(backupjob)
|
15
|
+
end
|
16
|
+
@backup_jobs
|
17
|
+
end
|
18
|
+
|
19
|
+
def use(name, handler)
|
20
|
+
@backup_handler[name] = handler
|
21
|
+
end
|
22
|
+
|
23
|
+
def handler(name)
|
24
|
+
@backup_handler[name]
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.method_missing(method, *args)
|
28
|
+
self.instance.send(method, *args)
|
29
|
+
end
|
30
|
+
end
|
data/spec/backup_spec.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe Backup do
|
4
|
+
|
5
|
+
describe Backup::Manager do
|
6
|
+
it "should return a list of backup jobs" do
|
7
|
+
jobs = Backup::Manager.collect_jobs(File.join(File.dirname(__FILE__),'fs_root/home/**/current/config/backup.rb'))
|
8
|
+
jobs.should be_an(Array)
|
9
|
+
|
10
|
+
jobs.first.should be_an(Backup::Job)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe Backup::Job do
|
15
|
+
before(:each) do
|
16
|
+
@job = Backup::Job.new(File.join(File.dirname(__FILE__), "/fs_root/home/user1/app1/production/current/config/backup.rb"))
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should report the correct application directory" do
|
20
|
+
@job.application_directory.should eql(File.join(File.dirname(__FILE__), "/fs_root/home/user1/app1/production"))
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should create a backup directory" do
|
24
|
+
File.directory?(@job.backups_path).should be true
|
25
|
+
end
|
26
|
+
|
27
|
+
context "S3" do
|
28
|
+
it "should attempt to save the files to S3" do
|
29
|
+
credidentials = {:access_key_id =>'acces_key', :secret_access_key =>"secret"}
|
30
|
+
AWS::S3::Base.should_receive(:establish_connection!).with(credidentials).and_return(true)
|
31
|
+
|
32
|
+
s3 = Backup::Handler::S3.new(credidentials)
|
33
|
+
Backup::Manager.use(:s3, s3)
|
34
|
+
|
35
|
+
@job = Backup::Job.new(File.join(File.dirname(__FILE__), "/fs_root/home/user1/app1/production/current/config/backup.rb"))
|
36
|
+
Backup::Manager.handler(:s3).should_receive(:store).with('liquidmedia_fairness',Dir.glob(File.join(@job.current_backup_path,'*.tar.gz'))).and_return(true)
|
37
|
+
@job.perform
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
Important!
|
@@ -0,0 +1 @@
|
|
1
|
+
Also Important!
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: liquid_backup
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Dan Williams
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-03-04 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: yard
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
version: "0"
|
44
|
+
type: :development
|
45
|
+
version_requirements: *id002
|
46
|
+
description: Backup your application server
|
47
|
+
email: os@liquidmedia.ca
|
48
|
+
executables: []
|
49
|
+
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- LICENSE
|
54
|
+
- README.md
|
55
|
+
files:
|
56
|
+
- .document
|
57
|
+
- .gitignore
|
58
|
+
- LICENSE
|
59
|
+
- README.md
|
60
|
+
- Rakefile
|
61
|
+
- VERSION
|
62
|
+
- lib/backup.rb
|
63
|
+
- lib/backup/handler/s3.rb
|
64
|
+
- lib/backup/job.rb
|
65
|
+
- lib/backup/manager.rb
|
66
|
+
- spec/backup_spec.rb
|
67
|
+
- spec/fs_root/home/user1/app1/production/current/public/system/important_files/file1.txt
|
68
|
+
- spec/fs_root/home/user1/app1/production/current/public/system/important_files/file2.txt
|
69
|
+
- spec/spec.opts
|
70
|
+
- spec/spec_helper.rb
|
71
|
+
has_rdoc: true
|
72
|
+
homepage: http://github.com/liquidmedia/liquid_backup
|
73
|
+
licenses: []
|
74
|
+
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options:
|
77
|
+
- --charset=UTF-8
|
78
|
+
require_paths:
|
79
|
+
- lib
|
80
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
segments:
|
85
|
+
- 0
|
86
|
+
version: "0"
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.3.6
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Backup your application server
|
101
|
+
test_files:
|
102
|
+
- spec/backup_spec.rb
|
103
|
+
- spec/fs_root/home/user1/app1/production/current/config/backup.rb
|
104
|
+
- spec/spec_helper.rb
|