zfs-mysql-backup 0.0.8

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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Golick
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.
@@ -0,0 +1,18 @@
1
+ = zfs-mysql-backup
2
+
3
+ Description goes here.
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
13
+ bump version in a commit by itself I can ignore when I pull)
14
+ * Send me a pull request. Bonus points for topic branches.
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 James Golick. See LICENSE for details.
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "zfs-mysql-backup"
8
+ gem.summary = %Q{A script for backing up mysql databases using ZFS}
9
+ gem.description = %Q{A script for backing up mysql databases using ZFS}
10
+ gem.email = "james@giraffesoft.ca"
11
+ gem.homepage = "http://github.com/giraffesoft/zfs-mysql-backup"
12
+ gem.authors = ["James Golick"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_dependency "thor"
15
+ gem.add_dependency "activerecord"
16
+ gem.add_dependency "aws-s3"
17
+ gem.add_dependency "mysql"
18
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
19
+ end
20
+ rescue LoadError
21
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
22
+ end
23
+
24
+ require 'rake/testtask'
25
+ Rake::TestTask.new(:test) do |test|
26
+ test.libs << 'lib' << 'test'
27
+ test.pattern = 'test/**/*_test.rb'
28
+ test.verbose = true
29
+ end
30
+
31
+ begin
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/*_test.rb'
36
+ test.verbose = true
37
+ end
38
+ rescue LoadError
39
+ task :rcov do
40
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
41
+ end
42
+ end
43
+
44
+ task :test => :check_dependencies
45
+
46
+ task :default => :test
47
+
48
+ require 'rake/rdoctask'
49
+ Rake::RDocTask.new do |rdoc|
50
+ if File.exist?('VERSION')
51
+ version = File.read('VERSION')
52
+ else
53
+ version = ""
54
+ end
55
+
56
+ rdoc.rdoc_dir = 'rdoc'
57
+ rdoc.title = "zfs-mysql-backup #{version}"
58
+ rdoc.rdoc_files.include('README*')
59
+ rdoc.rdoc_files.include('lib/**/*.rb')
60
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.8
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path('../../lib/zfs-mysql-backup', __FILE__)
4
+
5
+ ZfsMysqlBackup.start
6
+
@@ -0,0 +1,94 @@
1
+ require 'thor'
2
+ require 'activerecord'
3
+ require 'aws/s3'
4
+
5
+ class ZfsMysqlBackup < Thor
6
+ no_tasks {
7
+ attr_reader :logger, :connection, :date, :status, :database_yml, :backup_location, :sms_number
8
+ }
9
+
10
+ desc "backup path/to/database.yml path/to/backup/dir phone_number_to_sms_if_theres_a_failure [name_of_s3_bucket_here to push to s3]", "Create a zfs backup."
11
+ def backup(database_yml, backup_location, sms_number, s3_bucket = false)
12
+ @database_yml = database_yml
13
+ @backup_location = backup_location
14
+ @s3_bucket = s3_bucket
15
+ @sms_number = sms_number
16
+
17
+ init
18
+ acquire_lock
19
+ take_snapshot
20
+ release_lock
21
+ handle_snapshot_result
22
+ push_to_s3 if success? && push_to_s3?
23
+ end
24
+
25
+ protected
26
+ def init
27
+ @logger = Logger.new(STDOUT)
28
+ ActiveRecord::Base.establish_connection(YAML.load(File.read(database_yml))[ENV['RAILS_ENV']])
29
+ @connection = ActiveRecord::Base.connection
30
+ end
31
+
32
+ def acquire_lock
33
+ logger.info("[MySQL Backup] Acquiring mysql lock.")
34
+ connection.execute("FLUSH TABLES WITH READ LOCK")
35
+ logger.info("[MySQL Backup] Lock acquired.")
36
+ end
37
+
38
+ def take_snapshot
39
+ @date = Time.now.strftime("%d%m%y%H%M")
40
+ logger.info("[MySQL Backup] Creating snapshot at #{date}.")
41
+ system("zfs snapshot data@#{date} 2>&1")
42
+ @status = $?.exitstatus
43
+ end
44
+
45
+ def handle_snapshot_result
46
+ success? ? success : failure
47
+ end
48
+
49
+ def success
50
+ logger.info("[MySQL Backup] Succeeded! Compressing and exporting to #{path_to_backup}")
51
+ system("zfs send data@#{date} | gzip > #{path_to_backup}")
52
+ end
53
+
54
+ def path_to_backup
55
+ "#{backup_location}/mysql-#{date}.gz"
56
+ end
57
+
58
+ def failure
59
+ logger.info("[MySQL Backup] FAILED. Notifying the authorities.")
60
+ system(%{echo "MySQL Backup at #{date} FAILED." | /usr/local/bin/send_sms #{sms_number}})
61
+ end
62
+
63
+ def release_lock
64
+ logger.info("[MySQL Backup] Unlocking tables")
65
+ connection.execute("UNLOCK TABLES;")
66
+ end
67
+
68
+ def push_to_s3?
69
+ @s3_bucket
70
+ end
71
+
72
+ def push_to_s3
73
+ logger.info("[MySQL Backup] Pushing to S3 at #{path_to_backup} in #{@s3_bucket}.")
74
+ begin
75
+ AWS::S3::Base.establish_connection! :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
76
+ :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
77
+ AWS::S3::S3Object.store(s3_object_name, open(path_to_backup), @s3_bucket)
78
+ rescue => e
79
+ logger.info("[MySQL Backup] Something went wrong. Notifying the authorities!")
80
+ logger.info("\tError Message #{e.message}")
81
+ system(%{echo "Pushing to S3 at #{date} FAILED." | /usr/local/bin/send_sms #{sms_number}})
82
+ end
83
+ logger.info("[MySQL Backup] Done!")
84
+ end
85
+
86
+ def s3_object_name
87
+ hostname = `hostname`
88
+ "mysqlbackups/#{hostname}-#{date}.gz"
89
+ end
90
+
91
+ def success?
92
+ status == 0
93
+ end
94
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'zfs-mysql-backup'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class ZfsMysqlBackupTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,68 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{zfs-mysql-backup}
8
+ s.version = "0.0.8"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Golick"]
12
+ s.date = %q{2009-10-28}
13
+ s.default_executable = %q{zfs-mysql-backup}
14
+ s.description = %q{A script for backing up mysql databases using ZFS}
15
+ s.email = %q{james@giraffesoft.ca}
16
+ s.executables = ["zfs-mysql-backup"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README.rdoc"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".gitignore",
24
+ "LICENSE",
25
+ "README.rdoc",
26
+ "Rakefile",
27
+ "VERSION",
28
+ "bin/zfs-mysql-backup",
29
+ "lib/zfs-mysql-backup.rb",
30
+ "test/test_helper.rb",
31
+ "test/zfs-mysql-backup_test.rb",
32
+ "zfs-mysql-backup.gemspec"
33
+ ]
34
+ s.homepage = %q{http://github.com/giraffesoft/zfs-mysql-backup}
35
+ s.rdoc_options = ["--charset=UTF-8"]
36
+ s.require_paths = ["lib"]
37
+ s.rubygems_version = %q{1.3.5}
38
+ s.summary = %q{A script for backing up mysql databases using ZFS}
39
+ s.test_files = [
40
+ "test/test_helper.rb",
41
+ "test/zfs-mysql-backup_test.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ s.add_runtime_dependency(%q<thor>, [">= 0"])
51
+ s.add_runtime_dependency(%q<activerecord>, [">= 0"])
52
+ s.add_runtime_dependency(%q<aws-s3>, [">= 0"])
53
+ s.add_runtime_dependency(%q<mysql>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ s.add_dependency(%q<thor>, [">= 0"])
57
+ s.add_dependency(%q<activerecord>, [">= 0"])
58
+ s.add_dependency(%q<aws-s3>, [">= 0"])
59
+ s.add_dependency(%q<mysql>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
63
+ s.add_dependency(%q<thor>, [">= 0"])
64
+ s.add_dependency(%q<activerecord>, [">= 0"])
65
+ s.add_dependency(%q<aws-s3>, [">= 0"])
66
+ s.add_dependency(%q<mysql>, [">= 0"])
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zfs-mysql-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - James Golick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-28 00:00:00 -07:00
13
+ default_executable: zfs-mysql-backup
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: thor
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activerecord
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: aws-s3
47
+ type: :runtime
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: mysql
57
+ type: :runtime
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ description: A script for backing up mysql databases using ZFS
66
+ email: james@giraffesoft.ca
67
+ executables:
68
+ - zfs-mysql-backup
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - LICENSE
73
+ - README.rdoc
74
+ files:
75
+ - .document
76
+ - .gitignore
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - bin/zfs-mysql-backup
82
+ - lib/zfs-mysql-backup.rb
83
+ - test/test_helper.rb
84
+ - test/zfs-mysql-backup_test.rb
85
+ - zfs-mysql-backup.gemspec
86
+ has_rdoc: true
87
+ homepage: http://github.com/giraffesoft/zfs-mysql-backup
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options:
92
+ - --charset=UTF-8
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: "0"
100
+ version:
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: "0"
106
+ version:
107
+ requirements: []
108
+
109
+ rubyforge_project:
110
+ rubygems_version: 1.3.5
111
+ signing_key:
112
+ specification_version: 3
113
+ summary: A script for backing up mysql databases using ZFS
114
+ test_files:
115
+ - test/test_helper.rb
116
+ - test/zfs-mysql-backup_test.rb