keepitsafe 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "activesupport"
4
+
5
+ group :development do
6
+ gem "rspec", "~> 2.3.0"
7
+ gem "cucumber"
8
+ gem "bundler", "~> 1.0.0"
9
+ gem "jeweler", "~> 1.6.4"
10
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,42 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ activesupport (3.1.3)
5
+ multi_json (~> 1.0)
6
+ builder (3.0.0)
7
+ cucumber (1.1.4)
8
+ builder (>= 2.1.2)
9
+ diff-lcs (>= 1.1.2)
10
+ gherkin (~> 2.7.1)
11
+ json (>= 1.4.6)
12
+ term-ansicolor (>= 1.0.6)
13
+ diff-lcs (1.1.3)
14
+ gherkin (2.7.3)
15
+ json (>= 1.4.6)
16
+ git (1.2.5)
17
+ jeweler (1.6.4)
18
+ bundler (~> 1.0)
19
+ git (>= 1.2.5)
20
+ rake
21
+ json (1.6.5)
22
+ multi_json (1.0.4)
23
+ rake (0.9.2.2)
24
+ rspec (2.3.0)
25
+ rspec-core (~> 2.3.0)
26
+ rspec-expectations (~> 2.3.0)
27
+ rspec-mocks (~> 2.3.0)
28
+ rspec-core (2.3.1)
29
+ rspec-expectations (2.3.0)
30
+ diff-lcs (~> 1.1.2)
31
+ rspec-mocks (2.3.0)
32
+ term-ansicolor (1.0.7)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ activesupport
39
+ bundler (~> 1.0.0)
40
+ cucumber
41
+ jeweler (~> 1.6.4)
42
+ rspec (~> 2.3.0)
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Darwin
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.rdoc ADDED
@@ -0,0 +1,32 @@
1
+ = keepitsafe
2
+
3
+ A Gem for simple creation of backup scripts.
4
+
5
+ == Goals.
6
+
7
+ * Backups can be handled and configured from a central place
8
+ * Backups are put in simple to understand location (~/backups/#{server_domain_name}/#{timestamp}/)
9
+ * Files keep there path as where they where on the server (/var/uploads/dummy.jpg -> ~/backups/#{server_domain_name}/#{timestamp}/var/uploads/dummy.jpg)
10
+ * When coping to another server the backups are put in the same place (~/backups/#{server_domain_name}/#{timestamp}/)
11
+ * Easy to understand, easy to extend
12
+ * Good enough defaults
13
+
14
+ == How to use
15
+
16
+ Look in the example dir
17
+
18
+ == Contributing to keepitsafe
19
+
20
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
21
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
22
+ * Fork the project
23
+ * Start a feature/bugfix branch
24
+ * Commit and push until you are happy with your contribution
25
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
26
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
27
+
28
+ == Copyright
29
+
30
+ Copyright (c) 2012 Darwin. See LICENSE.txt for
31
+ further details.
32
+
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "keepitsafe"
18
+ gem.homepage = "http://github.com/bjornblomqvist/keepitsafe"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{A Gem for simple creation of backup scripts}
21
+ gem.description = %Q{A Gem for simple creation of backup scripts}
22
+ gem.email = "darwin.git@marianna.se"
23
+ gem.authors = ["Darwin"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rspec/core'
29
+ require 'rspec/core/rake_task'
30
+ RSpec::Core::RakeTask.new(:spec) do |spec|
31
+ spec.pattern = FileList['spec/**/*_spec.rb']
32
+ end
33
+
34
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
35
+ spec.pattern = 'spec/**/*_spec.rb'
36
+ spec.rcov = true
37
+ end
38
+
39
+ require 'cucumber/rake/task'
40
+ Cucumber::Rake::Task.new(:features)
41
+
42
+ task :default => :spec
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "keepitsafe #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.3
@@ -0,0 +1,20 @@
1
+ gem 'keepitsafe'
2
+ require 'keepitsafe'
3
+
4
+ mail_options = eval(File.read(File.expand_path("~/.config/smtp.rb"))) # Read email options, same key names as for mail gem.
5
+
6
+ ErrorMail.setup("backup_script@yourdomain.com","you@yourdomain.com",mail_options)
7
+ BackupReport.new("backup_script@yourdomain.com","you@yourdomain.com",mail_options) do
8
+
9
+ Keepitsafe.new("yourdomain.com") do |backup|
10
+
11
+ backup.all_mongo 750 # Throws an error if the db is biggar than 750 meg
12
+ backup.all_postgres 100 # Throws an error if the db is biggar than 100 meg
13
+
14
+ backup.files "/var/rails/yourapp/shared/uploads/"
15
+ backup.tar_gz # Lets save some space
16
+ backup.scp "backup.yourdomain.com" # Copy the backup of site
17
+ backup.remove_old_backups
18
+
19
+ end
20
+ end
@@ -0,0 +1,9 @@
1
+ Feature: something something
2
+ In order to something something
3
+ A user something something
4
+ something something something
5
+
6
+ Scenario: something something
7
+ Given inspiration
8
+ When I create a sweet new gem
9
+ Then everyone should see how awesome I am
File without changes
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ begin
3
+ Bundler.setup(:default, :development)
4
+ rescue Bundler::BundlerError => e
5
+ $stderr.puts e.message
6
+ $stderr.puts "Run `bundle install` to install missing gems"
7
+ exit e.status_code
8
+ end
9
+
10
+ $LOAD_PATH.unshift(File.dirname(__FILE__) + '/../../lib')
11
+ require 'keepitsafe'
12
+
13
+ require 'rspec/expectations'
data/lib/backup.rb ADDED
File without changes
@@ -0,0 +1,31 @@
1
+ require 'mail'
2
+ require "socket"
3
+
4
+ class BackupReport
5
+
6
+ def initialize from = ENV['USER'],to = "root",mail_options = {}
7
+
8
+ capture = STDCapture.capture do
9
+ yield
10
+ end
11
+
12
+ # Send email report
13
+ mail = Mail.new
14
+ mail.to to
15
+ mail.from from
16
+ mail.subject "Backup report"
17
+ mail.body %@
18
+
19
+ We are runnig on: #{Socket.gethostname}
20
+ Below is the output log.
21
+
22
+ ----
23
+ #{capture}
24
+ ----@
25
+
26
+ mail.delivery_method.settings = mail.delivery_method.settings.merge(mail_options)
27
+ mail.deliver!
28
+
29
+ end
30
+
31
+ end
data/lib/error_mail.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'mail'
2
+ require "socket"
3
+
4
+ class ErrorMail
5
+
6
+ def self.setup from = ENV['USER'],to = "root",mail_options = {}
7
+
8
+ Keepitsafe.on_error do |backup,options|
9
+
10
+ mail = Mail.new
11
+ mail.from from
12
+ mail.to to
13
+ mail.subject 'Backup error!'
14
+ mail.body %@
15
+
16
+ Backup problems when backing up: #{backup.domain}
17
+ We are runnig on: #{Socket.gethostname}
18
+
19
+ #{options[:error].inspect}
20
+ #{options[:error].backtrace}@
21
+
22
+ mail.delivery_method.settings = mail.delivery_method.settings.merge(mail_options)
23
+ mail.deliver!
24
+
25
+ puts "Error has been mailed to #{mail.to}!"
26
+ end
27
+
28
+ end
29
+
30
+ end
data/lib/events.rb ADDED
@@ -0,0 +1,68 @@
1
+ module Events
2
+
3
+ def self.included(base)
4
+ base.extend(Events::EventClassMethods)
5
+ end
6
+
7
+ def events
8
+ @events || {}
9
+ end
10
+
11
+ def trigger name,options = {}
12
+ @events ||= {}
13
+ ((@events[name.to_sym] || []) + (self.class.events[name.to_sym] || [])).each do |v|
14
+ v.call self,options
15
+ end
16
+ end
17
+
18
+ def register_handler name,lambda = nil, &block
19
+ @events ||= {}
20
+ @events[name.to_sym] ||= []
21
+ @events[name.to_sym] << (lambda || block)
22
+ end
23
+
24
+
25
+ # Add code for handeling the dsl on_error,after_error,etc...
26
+ def method_missing(sym,*args, &block)
27
+ if sym.to_s.match(/^(on|before|after)_/)
28
+ register_handler(sym.to_s,args.first,&block)
29
+ else
30
+ super
31
+ end
32
+ end
33
+
34
+ def respond_to?(sym)
35
+ sym.to_s.match(/^(on|before|after)_/).nil? == false || super
36
+ end
37
+
38
+ module EventClassMethods
39
+
40
+ def events
41
+ @@events
42
+ end
43
+
44
+ def self.extended(base)
45
+ @@events ||= {}
46
+ end
47
+
48
+ def register_handler name,lambda = nil, &block
49
+ @@events[name.to_sym] ||= []
50
+ @@events[name.to_sym] << (lambda || block)
51
+ end
52
+
53
+ # Add code for handeling the dsl on_error,after_error,etc...
54
+ def method_missing(sym,*args, &block)
55
+ if sym.to_s.match(/^(on|before|after)_/)
56
+ register_handler(sym.to_s,args.first,&block)
57
+ else
58
+ super
59
+ end
60
+ end
61
+
62
+ def respond_to?(sym)
63
+ sym.to_s.match(/^(on|before|after)_/).nil? == false || super
64
+ end
65
+
66
+ end
67
+
68
+ end
data/lib/keepitsafe.rb ADDED
@@ -0,0 +1,177 @@
1
+ require 'rubygems'
2
+ require 'tempfile'
3
+ require 'net/ssh'
4
+ require 'net/scp'
5
+ require 'active_support/all'
6
+ require 'etc'
7
+
8
+ require "#{File.dirname(__FILE__)}/events.rb"
9
+ require "#{File.dirname(__FILE__)}/stdcapture.rb"
10
+ require "#{File.dirname(__FILE__)}/backup_report.rb"
11
+ require "#{File.dirname(__FILE__)}/error_mail.rb"
12
+ require "#{File.dirname(__FILE__)}/plugins/postgres.rb"
13
+ require "#{File.dirname(__FILE__)}/plugins/mongo.rb"
14
+ require "#{File.dirname(__FILE__)}/plugins/tar.rb"
15
+ require "#{File.dirname(__FILE__)}/plugins/rsync.rb"
16
+ require "#{File.dirname(__FILE__)}/plugins/files.rb"
17
+ require "#{File.dirname(__FILE__)}/plugins/scp.rb"
18
+ require "#{File.dirname(__FILE__)}/plugins/remove.rb"
19
+
20
+ class Keepitsafe
21
+
22
+ include Events
23
+ include BackupPlugins::Mongo
24
+ include BackupPlugins::Postgres
25
+ include BackupPlugins::Tar
26
+ include BackupPlugins::Rsync
27
+ include BackupPlugins::Files
28
+ include BackupPlugins::Scp
29
+ include BackupPlugins::Remove
30
+
31
+ def initialize server_domain = "localhost", options = {}, &block
32
+
33
+ @options = options
34
+ @options[:username] ||= Etc.getlogin
35
+ @options[:forward_agent] ||= true
36
+
37
+ @server_domain = server_domain
38
+
39
+ run &block if block_given?
40
+ end
41
+
42
+ def domain
43
+ @server_domain
44
+ end
45
+
46
+ def run &block
47
+
48
+ puts "\n\n\t -- #{@server_domain} --\n\n"
49
+ @backup_timastamp = Time.now.utc.strftime("%Y%m%d_%H%M.%S-UTC")
50
+
51
+
52
+ if on_localhost?
53
+ do_the_stuff &block if block_given?
54
+ else
55
+ Net::SSH.start( @server_domain , @options[:username] , :forward_agent => @options[:forward_agent]) do |ssh|
56
+ @ssh = ssh
57
+
58
+ do_the_stuff &block if block_given?
59
+
60
+ end
61
+ end
62
+ end
63
+
64
+ def exist_on_server? path
65
+ (run_cmd("ls #{path}") || '').match(/No such file or directory/).nil?
66
+ end
67
+
68
+ def upload src,target
69
+ src = run_cmd("echo #{src}").strip # expand "~/"
70
+ target = `echo #{target}`.strip # expand "~/"
71
+ puts "local:#{src} => remote:#{target}"
72
+ if on_localhost?
73
+ `cp -a #{src} #{target}`
74
+ else
75
+ @ssh.scp.upload!(src,target)
76
+ end
77
+ end
78
+
79
+ def download src,target
80
+ src = run_cmd("echo #{src}").strip # expand "~/"
81
+ target = `echo #{target}`.strip # expand "~/"
82
+ puts "remote:#{src} => local:#{target}"
83
+ if on_localhost?
84
+ `cp -a #{src} #{target}`
85
+ else
86
+ @ssh.scp.download!(src,target)
87
+ end
88
+ end
89
+
90
+ def upload_log
91
+ home_dir = run_cmd('echo ~')
92
+ target_dir = "#{home_dir.strip}#{backup_target_dir[1..-1]}"
93
+ if exist_on_server?(target_dir)
94
+ puts "Uploading backup log"
95
+ t = Tempfile.new('foo')
96
+ t.write(log)
97
+ t.close
98
+
99
+ upload(t.path,"#{target_dir}"+"backup_log.txt")
100
+ t.unlink
101
+ else
102
+ puts "Backup dir does not exist (any longer ?)"
103
+ end
104
+ end
105
+
106
+ def log
107
+ @log_buffer.string
108
+ end
109
+
110
+ def run_cmd cmd
111
+ puts "run_cmd(#{@server_domain}): #{cmd}"
112
+ if on_localhost?
113
+ `#{cmd} 2>&1`
114
+ else
115
+ @ssh.exec! cmd
116
+ end
117
+ end
118
+
119
+ def backup_target_dir
120
+ "~/backups/#{@server_domain}/#{@backup_timastamp}/"
121
+ end
122
+
123
+ def create_backup_target_dir
124
+ puts run_cmd "mkdir -p #{backup_target_dir}"
125
+ end
126
+
127
+ def create_pending_file
128
+ puts run_cmd "touch #{backup_target_dir}/pending"
129
+ end
130
+
131
+ def remove_pending_file
132
+ puts run_cmd "rm #{backup_target_dir}/pending"
133
+ end
134
+
135
+ def check_disk_left limit = 1000
136
+ disk_free_on_root = run_cmd('df -ha').match(/^(.*)\/$/)[0].scan(/([0-9MG\.%]{2,10})/)[2].join("")
137
+ if disk_free_on_root.match(/g$/i)
138
+ disk_free_on_root = disk_free_on_root.match(/(\d*)/)[1].to_i * 1000
139
+ elsif disk_free_on_root.match(/t$/i)
140
+ disk_free_on_root = disk_free_on_root.match(/(\d*)/)[1].to_i * 1000 * 1000
141
+ end
142
+
143
+ raise "root disk limit reached limit:#{limit} disk_free_on_root:#{disk_free_on_root}" if disk_free_on_root < limit
144
+
145
+ end
146
+
147
+ def on_localhost?
148
+ @server_domain == "localhost"
149
+ end
150
+
151
+ private
152
+
153
+ def do_the_stuff
154
+ @log_buffer = StringIO.new
155
+
156
+ STDCapture.capture(@log_buffer) do
157
+ begin
158
+
159
+ check_disk_left
160
+ create_backup_target_dir
161
+ create_pending_file
162
+
163
+ yield self
164
+
165
+ remove_pending_file
166
+ rescue StandardError => e
167
+ puts e.inspect
168
+ puts e.backtrace
169
+
170
+ trigger('on_error',{:error => e})
171
+ end
172
+ end
173
+
174
+ upload_log
175
+ end
176
+
177
+ end
@@ -0,0 +1,23 @@
1
+ module BackupPlugins
2
+ module Files
3
+ def files path
4
+
5
+ puts "\n\tbackup.files(#{path})\n"
6
+
7
+ path = run_cmd("echo #{path}").strip # expand "~/"
8
+
9
+ file_target_dir = "#{backup_target_dir}#{path}"
10
+
11
+ run_cmd("mkdir -p #{file_target_dir}")
12
+
13
+ cmd = "cp -a #{path} #{file_target_dir}"
14
+
15
+ unless(run_cmd('which ionice').blank? || run_cmd('ionice -c3 ls').match(/ioprio_set: Operation not permitted/))
16
+ cmd = "ionice -c3 #{cmd}"
17
+ end
18
+
19
+ puts run_cmd(cmd)
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,20 @@
1
+ module BackupPlugins
2
+ module Mongo
3
+
4
+
5
+ def all_mongo limit = 30
6
+
7
+ # Check limit
8
+ disk_usage_kb = (run_cmd "du /var/lib/mongodb/").match(/^([0-9]+)/).to_s.to_i
9
+
10
+ raise "mongo limit reached limit:#{limit} disk_usage:#{disk_usage_kb/1000}" if disk_usage_kb > limit * 1000
11
+
12
+ puts run_cmd "mkdir -p #{backup_target_dir}/mongo"
13
+
14
+ puts run_cmd "cd #{backup_target_dir}/mongo/; mongodump --directoryperdb"
15
+
16
+ puts "backup of mongo db on #{@server_domain}"
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,18 @@
1
+ module BackupPlugins
2
+ module Postgres
3
+
4
+ def all_postgres limit = 500
5
+
6
+ # Check limit
7
+ cluster_size_in_bytes = run_cmd('env psql postgres -c "SELECT sum(pg_database_size(pg_database.datname)) AS cluster_size FROM pg_database;"').match(/([0-9]{5,16})/).to_s.to_i
8
+ raise "postgres limit reached limit:#{limit} cluster_size:#{cluster_size_in_bytes/1000/1000}" if cluster_size_in_bytes > limit * 1000 * 1000
9
+
10
+ puts run_cmd "mkdir -p #{backup_target_dir}/postgres"
11
+
12
+ puts run_cmd "cd #{backup_target_dir}/postgres/; pg_dumpall > dumpall.pg.sql"
13
+
14
+ puts "backup of postgres db on #{@server_domain}"
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,110 @@
1
+
2
+ require 'date'
3
+
4
+ module BackupPlugins
5
+ module Remove
6
+
7
+
8
+ def remove paths,test_run = false
9
+
10
+ puts "\n\tbackup.remove(#{paths},#{test_run})\n\n"
11
+
12
+ paths = [paths] unless paths.is_a? Array
13
+
14
+ raise "We are missing a block here!" unless block_given?
15
+
16
+ paths.each do |path|
17
+
18
+ backups = run_cmd("ls #{path}").strip.split("\n").map do |file_name|
19
+ time_data = DateTime.strptime(file_name,'%Y%m%d_%H%M.%S').to_time.utc
20
+ Time.utc(time_data.year,time_data.month,time_data.day,time_data.hour,time_data.min,time_data.sec,time_data.usec)
21
+ end
22
+
23
+ to_keep = (yield(backups) || []).uniq
24
+
25
+ to_remove = backups.delete_if {|v| to_keep.include?(v) }
26
+
27
+ to_remove.each do |backup_to_remove|
28
+
29
+ cmd = "rm -rf #{path}/#{backup_to_remove.strftime("%Y%m%d_%H%M.%S")}*"
30
+
31
+ if test_run
32
+ puts cmd
33
+ else
34
+ run_cmd(cmd)
35
+ end
36
+
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ def remove_old_backups
43
+ remove(backup.run_cmd('ls -m ~/backups/').strip.split(/,\s/).map{|path| "~/backups/#{path}" }) do |backups|
44
+
45
+ # First backup of each month 1 years back
46
+ months = keep_first_each_month(backups,1.years.ago)
47
+
48
+ # First backup of each week 2 months back
49
+ weeks = keep_first_each_week(backups,1.months.ago)
50
+
51
+ # First backup of each day 1 week back
52
+ days = keep_first_each_day(backups,7.days.ago)
53
+
54
+ # All backups during the last 24 hours
55
+ all = keep_all(backups,1.day.ago)
56
+
57
+ all + days + weeks + months
58
+ end
59
+ end
60
+
61
+ def keep_first_each_month backups,time_back = 1.year.ago
62
+ months = {}
63
+ backups.sort.each do |time|
64
+ if time > time_back
65
+ months[time.strftime('%Y%m')] = time unless months[time.strftime('%Y%m')]
66
+ end
67
+ end
68
+
69
+ months.values
70
+ end
71
+
72
+ def keep_first_each_week backups,time_back = 2.months.ago
73
+ weeks = {}
74
+ backups.sort.each do |time|
75
+ if time > time_back
76
+ string = time.strftime('%Y%m') + time.to_date.cweek.to_s
77
+ weeks[string] = time unless weeks[string]
78
+ end
79
+ end
80
+
81
+ weeks.values
82
+ end
83
+
84
+ def keep_first_each_day backups,time_back = 7.days.ago
85
+
86
+ days = {}
87
+ backups.sort.each do |time|
88
+ if time > time_back
89
+ string = time.strftime('%Y%m%d')
90
+ days[string] = time unless days[string]
91
+ end
92
+ end
93
+
94
+ days.values
95
+ end
96
+
97
+ def keep_all backups,time_back = 1.day.ago
98
+ hours = []
99
+ backups.sort.each do |time|
100
+ if time > 1.days.ago
101
+ hours << time
102
+ end
103
+ end
104
+
105
+ hours
106
+ end
107
+
108
+
109
+ end
110
+ end
@@ -0,0 +1,27 @@
1
+ module BackupPlugins
2
+ module Rsync
3
+
4
+
5
+
6
+ def rsync target_domain
7
+
8
+ rsync_options = " --recursive "
9
+
10
+ path = if !run_cmd("ls #{tar_path}").match(/No such file or directory/)
11
+ tar_path
12
+ elsif !run_cmd("ls #{tar_gz_path}").match(/No such file or directory/)
13
+ tar_gz_path
14
+ elsif !run_cmd("ls #{backup_target_dir}").match(/No such file or directory/)
15
+ backup_target_dir
16
+ else
17
+ raise "This code sucks as we cant find a file to transfer =(, target_domain: #{target_domain}"
18
+ end
19
+
20
+ puts "\n\t backup.rsync(\"#{target_domain}\")\n\n"
21
+ puts run_cmd("ssh #{target_domain} \"mkdir -p #{File.dirname(path)}\"")
22
+ puts run_cmd("rsync #{rsync_options} #{path} \"#{target_domain}:#{path}\"")
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ module BackupPlugins
2
+ module Scp
3
+
4
+
5
+
6
+ def scp target_domain
7
+
8
+ options = " -r "
9
+
10
+ path = if !run_cmd("ls #{tar_path}").match(/No such file or directory/)
11
+ tar_path
12
+ elsif !run_cmd("ls #{tar_gz_path}").match(/No such file or directory/)
13
+ tar_gz_path
14
+ elsif !run_cmd("ls #{backup_target_dir}").match(/No such file or directory/)
15
+ backup_target_dir
16
+ else
17
+ raise "This code sucks as we cant find a file to transfer =(, target_domain: #{target_domain}"
18
+ end
19
+
20
+ puts "\n\t backup.scp(\"#{target_domain})\"\n\n"
21
+ puts run_cmd("ssh #{target_domain} \"mkdir -p #{File.dirname(path)}\"")
22
+ puts run_cmd("scp #{options} #{path} \"#{target_domain}:#{path}\"")
23
+
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ module BackupPlugins
2
+ module Tar
3
+
4
+ def tar_path
5
+ "#{backup_target_dir[0..-2]}.tar"
6
+ end
7
+
8
+ def tar_gz_path
9
+ "#{backup_target_dir[0..-2]}.tar.gz"
10
+ end
11
+
12
+ def tar options = {:keep_files => false}
13
+ puts "\n\tTaring files"
14
+ puts run_cmd "cd #{backup_dir_path}; tar -cf #{tar_path} #{backup_dir_name}"
15
+ unless options[:keep_files]
16
+ puts "\n\tRemoving backup dir"
17
+ puts run_cmd "rm -rf #{backup_target_dir}"
18
+ end
19
+ end
20
+
21
+ def tar_gz options = {:keep_files => false}
22
+ puts "\n\tTaring and gziping files\n\n"
23
+ puts run_cmd "cd #{backup_dir_path}; tar -czf #{tar_gz_path} #{backup_dir_name}"
24
+
25
+ unless options[:keep_files]
26
+ puts "\n\tRemoving backup dir"
27
+ puts run_cmd "rm -rf #{backup_target_dir}"
28
+ end
29
+ end
30
+
31
+ private
32
+
33
+ def backup_dir_path
34
+ File.dirname(backup_target_dir)
35
+ end
36
+
37
+ def backup_dir_name
38
+ File.basename(backup_target_dir)
39
+ end
40
+ end
41
+ end
data/lib/stdcapture.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'stringio'
2
+
3
+ class STDCapture < StringIO
4
+
5
+ def initialize io1,io2
6
+ @io1 = io1
7
+ @io2 = io2
8
+ super ""
9
+ end
10
+
11
+ def write text
12
+ @io1.write text
13
+ @io2.write text
14
+ end
15
+
16
+ def self.capture buffer = nil
17
+
18
+ buffer ||= StringIO.new
19
+
20
+ stdout_old = $stdout
21
+ stderr_old = $stderr
22
+
23
+ $stdout = STDCapture.new(stdout_old,buffer)
24
+ $stderr = STDCapture.new(stderr_old,buffer)
25
+
26
+ yield
27
+
28
+ buffer.string
29
+ ensure
30
+ $stdout = stdout_old
31
+ $stderr = stderr_old
32
+ end
33
+ end
34
+
@@ -0,0 +1,7 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "Keepitsafe" do
4
+ it "fails" do
5
+ fail "hey buddy, you should probably rename this file and start specing for real"
6
+ end
7
+ end
@@ -0,0 +1,12 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'keepitsafe'
5
+
6
+ # Requires supporting files with custom matchers and macros, etc,
7
+ # in ./support/ and its subdirectories.
8
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
+
10
+ RSpec.configure do |config|
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: keepitsafe
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.3
6
+ platform: ruby
7
+ authors:
8
+ - Darwin
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-01-16 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: rspec
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 2.3.0
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: cucumber
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: "0"
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: bundler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.0.0
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: jeweler
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ~>
65
+ - !ruby/object:Gem::Version
66
+ version: 1.6.4
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ description: A Gem for simple creation of backup scripts
71
+ email: darwin.git@marianna.se
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files:
77
+ - LICENSE.txt
78
+ - README.rdoc
79
+ files:
80
+ - .document
81
+ - .rspec
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.rdoc
86
+ - Rakefile
87
+ - VERSION
88
+ - example/server.com.rb
89
+ - features/keepitsafe.feature
90
+ - features/step_definitions/keepitsafe_steps.rb
91
+ - features/support/env.rb
92
+ - lib/backup.rb
93
+ - lib/backup_report.rb
94
+ - lib/error_mail.rb
95
+ - lib/events.rb
96
+ - lib/keepitsafe.rb
97
+ - lib/plugins/files.rb
98
+ - lib/plugins/mongo.rb
99
+ - lib/plugins/postgres.rb
100
+ - lib/plugins/remove.rb
101
+ - lib/plugins/rsync.rb
102
+ - lib/plugins/scp.rb
103
+ - lib/plugins/tar.rb
104
+ - lib/stdcapture.rb
105
+ - spec/keepitsafe_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: http://github.com/bjornblomqvist/keepitsafe
108
+ licenses:
109
+ - MIT
110
+ post_install_message:
111
+ rdoc_options: []
112
+
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ none: false
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ hash: 3051253900240229054
121
+ segments:
122
+ - 0
123
+ version: "0"
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: "0"
130
+ requirements: []
131
+
132
+ rubyforge_project:
133
+ rubygems_version: 1.8.10
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: A Gem for simple creation of backup scripts
137
+ test_files: []
138
+