backupit 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -7,6 +7,8 @@
7
7
 
8
8
  storage :file do
9
9
  path '/opt/backup/'
10
+ mysql_check true
11
+ mysql_config({:host=>"localhost",:user=>"root",:password=>"test",:databases=>"checkdb"})
10
12
  end
11
13
 
12
14
  server 'delonghi' do
@@ -21,6 +23,7 @@
21
23
  options '-h 192.168.1.100'
22
24
  databases ['delonghi-staging', 'delonghi-production']
23
25
  tables ['users','products'] # this would overwrite databases! `man mysqldump` for more help.
26
+ check true
24
27
  end
25
28
  end
26
29
 
@@ -31,6 +34,7 @@
31
34
  user 'root'
32
35
  password 'mytopsecret'
33
36
  databases 'ot_staging'
37
+ check false
34
38
  end
35
39
  end
36
40
 
@@ -39,6 +43,8 @@
39
43
  only backup server 'delonghi' (pretend to run)
40
44
  2, backup -f /opt/backup/backup.rb
41
45
  backup all servers
46
+ 3, in storage , mysql_check(true | false) and mysql_config to check the backup can be restored
47
+ 4, in every mysql role, check mean to check the backup or not
42
48
 
43
49
  ## Note on Patches/Pull Requests
44
50
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.3.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backupit}
8
- s.version = "0.2.2"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Jinzhu"]
12
- s.date = %q{2011-07-11}
12
+ s.date = %q{2011-10-03}
13
13
  s.default_executable = %q{backup}
14
14
  s.description = %q{A tool to backup your servers}
15
15
  s.email = %q{wosmvp@gmail.com}
@@ -39,13 +39,14 @@ Gem::Specification.new do |s|
39
39
  ]
40
40
  s.homepage = %q{http://github.com/jinzhu/backupit}
41
41
  s.require_paths = ["lib"]
42
- s.rubygems_version = %q{1.6.2}
42
+ s.rubygems_version = %q{1.3.6}
43
43
  s.summary = %q{A tool to backup your servers}
44
44
 
45
45
  if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
47
  s.specification_version = 3
47
48
 
48
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
50
  s.add_runtime_dependency(%q<mail>, [">= 0"])
50
51
  else
51
52
  s.add_dependency(%q<mail>, [">= 0"])
@@ -28,6 +28,18 @@ module Backup
28
28
 
29
29
  @servers
30
30
  end
31
+
32
+ def check(name=nil,&block)
33
+ @check ||= {}
34
+
35
+ if block
36
+ name ||= "check_#{@check.keys.size}"
37
+ @check[name] = Backup::Configuration::Check.new
38
+ @check[name].instance_eval &block
39
+ end
40
+
41
+ @check
42
+ end
31
43
  end
32
44
  end
33
45
  end
@@ -2,7 +2,7 @@ module Backup
2
2
  module Configuration
3
3
  class Mysql
4
4
  extend Backup::Attribute
5
- generate_attributes :user, :password, :options, :databases, :tables
5
+ generate_attributes :user, :password, :options, :databases, :tables, :check
6
6
  end
7
7
  end
8
8
  end
@@ -2,7 +2,7 @@ module Backup
2
2
  module Configuration
3
3
  class Storage
4
4
  extend Backup::Attribute
5
- generate_attributes :path, :smtp
5
+ generate_attributes :path, :smtp, :mysql_config, :mysql_check
6
6
  end
7
7
  end
8
8
  end
@@ -3,7 +3,7 @@ require 'tempfile'
3
3
  module Backup
4
4
  class Storage
5
5
  extend Backup::Attribute
6
- attr_accessor :name, :config, :changes
6
+ attr_accessor :name, :config, :changes, :subject_prefix
7
7
 
8
8
  def initialize(name, config)
9
9
  self.name = name
@@ -24,7 +24,7 @@ module Backup
24
24
  backup_rsync
25
25
  backup_mysql
26
26
  commit_changes
27
- notice_changes
27
+ send_mail(changes.join("\n"))
28
28
  end
29
29
 
30
30
  def backup_rsync
@@ -35,7 +35,7 @@ module Backup
35
35
  @server_config.rsync.to_a.map do |path|
36
36
  remote_path = path.is_a?(Hash) ? path.first[0] : path
37
37
  target_name = File.basename(path.is_a?(Hash) ? path.first[1] : path)
38
- self.changes << Backup::Main.run("rsync -ravk #{@rsync_host}:#{remote_path.sub(/\/?$/,'/')} '#{File.join(target_path, target_name)}'")
38
+ run_with_changes("rsync -ravk #{@rsync_host}:#{remote_path.sub(/\/?$/,'/')} '#{File.join(target_path, target_name)}'")
39
39
  end
40
40
  end
41
41
 
@@ -52,32 +52,48 @@ module Backup
52
52
  mysql_config += " #{mysql.options}" if mysql.options
53
53
 
54
54
  tmpfile = Tempfile.new('mysql.sql')
55
- (self.changes << Backup::Main.run("ssh #{@ssh_host} 'mysqldump #{mysql_config} > #{tmpfile.path}'")) &&
56
- (self.changes << Backup::Main.run("scp #{@scp_host}:#{tmpfile.path} '#{target_path}/#{key}.sql'")) &&
57
- (self.changes << Backup::Main.run("ssh #{@ssh_host} 'rm #{tmpfile.path}'"))
55
+ run_with_changes("ssh #{@ssh_host} 'mysqldump #{mysql_config} > #{tmpfile.path}'") &&
56
+ run_with_changes("scp #{@scp_host}:#{tmpfile.path} '#{target_path}/#{key}.sql'") &&
57
+ run_with_changes("ssh #{@ssh_host} 'rm #{tmpfile.path}'"))
58
+
59
+ check_backuped_mysql(target_path, key) if config.mysql_check and (mysql.check || mysql.check.nil?)
58
60
  end
59
61
  end
60
62
 
61
- def notice_changes
63
+ def check_backuped_mysql(target_path, key)
64
+ self.changes << "DBCheck running -- checking #{target_path}/#{key}.sql #{Time.now}"
65
+ status = run_with_changes("mysql -h#{config.mysql_config[:host]} -u#{config.mysql_config[:user]} #{config.mysql_config[:databases]} < #{target_path}/#{key}.sql") ? "SUCCESSFUL" : "FAILURE"
66
+ self.changes << "DBCheck finished #{status} -- #{Time.now}"
67
+ end
68
+
69
+ def commit_changes
70
+ Dir.chdir(@backup_path) do
71
+ run_with_changes("git init") unless system("git status")
72
+ run_with_changes("git add .")
73
+ run_with_changes("git commit -am '#{Time.now.strftime("%Y-%m-%d %H:%M")}'")
74
+ end
75
+ end
76
+
77
+ def run_with_changes(shell)
78
+ self.changes << "== #{shell}"
79
+ result = Backup::Main.run(shell)
80
+ self.subject_prefix = "[ERROR]" unless result
81
+ self.changes << result
82
+ result
83
+ end
84
+
85
+ def send_mail(message)
62
86
  Dir.chdir(@backup_path) do
63
87
  smtp_config = config.smtp
64
88
  Mail.defaults { delivery_method :smtp, smtp_config } if smtp_config
65
89
 
66
90
  Backup::Main.email(:from => @server_config.email,
67
91
  :to => @server_config.email,
68
- :subject => "#{@server.name} backed up at #{Time.now}",
69
- :body => changes.join("\n"),
92
+ :subject => "#{self.subject_prefix} #{@server.name} backed up at #{Time.now}",
93
+ :body => message,
70
94
  :charset => 'utf-8', :content_type => 'text/plain; charset=utf-8'
71
95
  ) if @server_config.email
72
96
  end
73
97
  end
74
-
75
- def commit_changes
76
- Dir.chdir(@backup_path) do
77
- (self.changes << Backup::Main.run("git init")) unless system("git status")
78
- self.changes << Backup::Main.run("git add .")
79
- self.changes << Backup::Main.run("git commit -am '#{Time.now.strftime("%Y-%m-%d %H:%M")}'")
80
- end
81
- end
82
98
  end
83
99
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backupit
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
8
- - 2
9
- - 2
10
- version: 0.2.2
7
+ - 3
8
+ - 0
9
+ version: 0.3.0
11
10
  platform: ruby
12
11
  authors:
13
12
  - Jinzhu
@@ -15,18 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2011-07-11 00:00:00 +08:00
17
+ date: 2011-10-03 00:00:00 +08:00
19
18
  default_executable: backup
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: mail
23
22
  prerelease: false
24
23
  requirement: &id001 !ruby/object:Gem::Requirement
25
- none: false
26
24
  requirements:
27
25
  - - ">="
28
26
  - !ruby/object:Gem::Version
29
- hash: 3
30
27
  segments:
31
28
  - 0
32
29
  version: "0"
@@ -69,27 +66,23 @@ rdoc_options: []
69
66
  require_paths:
70
67
  - lib
71
68
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
69
  requirements:
74
70
  - - ">="
75
71
  - !ruby/object:Gem::Version
76
- hash: 3
77
72
  segments:
78
73
  - 0
79
74
  version: "0"
80
75
  required_rubygems_version: !ruby/object:Gem::Requirement
81
- none: false
82
76
  requirements:
83
77
  - - ">="
84
78
  - !ruby/object:Gem::Version
85
- hash: 3
86
79
  segments:
87
80
  - 0
88
81
  version: "0"
89
82
  requirements: []
90
83
 
91
84
  rubyforge_project:
92
- rubygems_version: 1.6.2
85
+ rubygems_version: 1.3.6
93
86
  signing_key:
94
87
  specification_version: 3
95
88
  summary: A tool to backup your servers