lvm-mysql-backup 0.0.0

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
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -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.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = lvm-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.
data/Rakefile ADDED
@@ -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 = "lvm-mysql-backup"
8
+ gem.summary = %Q{script for making lvm backups of mysql}
9
+ gem.description = %Q{script for making lvm backups of mysql}
10
+ gem.email = "james@giraffesoft.ca"
11
+ gem.homepage = "http://github.com/giraffesoft/lvm-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 = "lvm-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.0
@@ -0,0 +1,99 @@
1
+ require 'thor'
2
+ require 'activerecord'
3
+ require 'aws/s3'
4
+
5
+ class LvmMysqlBackup < 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 an lvm 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("lvcreate -L 5G -s -n backup-#{date} /dev/data/data")
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("mkdir -p /current-backup")
52
+ system("mount /dev/data/backup-#{date} /current-backup -onouuid,ro")
53
+ system("cd /current-backup tar --file=#{path_to_backup} -czv mysql")
54
+ system("umount /current-backup")
55
+ system("lvremove -f /dev/data/backup-#{date}")
56
+ end
57
+
58
+ def path_to_backup
59
+ "#{backup_location}/mysql-#{date}.gz"
60
+ end
61
+
62
+ def failure
63
+ logger.info("[MySQL Backup] FAILED. Notifying the authorities.")
64
+ system(%{echo "MySQL Backup at #{date} FAILED." | /usr/local/bin/send_sms #{sms_number}})
65
+ end
66
+
67
+ def release_lock
68
+ logger.info("[MySQL Backup] Unlocking tables")
69
+ connection.execute("UNLOCK TABLES;")
70
+ end
71
+
72
+ def push_to_s3?
73
+ @s3_bucket
74
+ end
75
+
76
+ def push_to_s3
77
+ logger.info("[MySQL Backup] Pushing to S3 at #{s3_object_name} in #{@s3_bucket}.")
78
+ begin
79
+ AWS::S3::Base.establish_connection! :access_key_id => ENV['AWS_ACCESS_KEY_ID'],
80
+ :secret_access_key => ENV['AWS_SECRET_ACCESS_KEY']
81
+ AWS::S3::S3Object.store(s3_object_name, open(path_to_backup), @s3_bucket)
82
+ rescue => e
83
+ logger.info("[MySQL Backup] Something went wrong. Notifying the authorities!")
84
+ logger.info("\tError Message #{e.message}")
85
+ system(%{echo "Pushing to S3 at #{date} FAILED." | /usr/local/bin/send_sms #{sms_number}})
86
+ end
87
+ logger.info("[MySQL Backup] Done!")
88
+ end
89
+
90
+ def s3_object_name
91
+ hostname = `hostname`.strip
92
+ "mysqlbackups/#{hostname}.#{date}.gz"
93
+ end
94
+
95
+ def success?
96
+ status == 0
97
+ end
98
+ end
99
+
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class LvmMysqlBackupTest < 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,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 'lvm-mysql-backup'
8
+
9
+ class Test::Unit::TestCase
10
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lvm-mysql-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - James Golick
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-31 00:00:00 -07:00
13
+ default_executable:
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: script for making lvm backups of mysql
66
+ email: james@giraffesoft.ca
67
+ executables: []
68
+
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
+ - lib/lvm-mysql-backup.rb
82
+ - test/lvm-mysql-backup_test.rb
83
+ - test/test_helper.rb
84
+ has_rdoc: true
85
+ homepage: http://github.com/giraffesoft/lvm-mysql-backup
86
+ licenses: []
87
+
88
+ post_install_message:
89
+ rdoc_options:
90
+ - --charset=UTF-8
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: "0"
98
+ version:
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: "0"
104
+ version:
105
+ requirements: []
106
+
107
+ rubyforge_project:
108
+ rubygems_version: 1.3.5
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: script for making lvm backups of mysql
112
+ test_files:
113
+ - test/lvm-mysql-backup_test.rb
114
+ - test/test_helper.rb