christianhellsten-mysql-backup 1.0.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/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2009-05-02
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ bin/mysql_backup
6
+ bin/mysql_backup_install
7
+ lib/mysql_backup.rb
8
+ lib/mysql_backup/install.rb
9
+ lib/mysql_backup/pty.rb
10
+ lib/mysql_backup/backup.rb
11
+ lib/mysql_backup/options.rb
12
+ lib/mysql_backup/mysql_backup.yml.example
13
+ test/test_mysql_backup.rb
data/README.txt ADDED
@@ -0,0 +1,48 @@
1
+ = blah
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2009 FIX
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/mysql_backup.rb'
6
+
7
+ Hoe.new('MysqlBackup', MysqlBackup::VERSION) do |p|
8
+ # p.rubyforge_name = 'MysqlBackupx' # if different than lowercase project name
9
+ p.developer('Christian Hellsten', 'christian@aktagon.com')
10
+ end
11
+
12
+ task :cultivate do
13
+ system "touch Manifest.txt; rake check_manifest | grep -v \"(in \" | patch"
14
+ system "rake debug_gem | grep -v \"(in \" > `basename \\`pwd\\``.gemspec"
15
+ end
16
+
17
+ # vim: syntax=Ruby
data/bin/mysql_backup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'mysql_backup'
5
+
6
+ MysqlBackup.run
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'rubygems'
4
+ require 'mysql_backup'
5
+
6
+ MysqlBackup.install
7
+
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.dirname(__FILE__)) unless
2
+ $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
3
+
4
+ require 'rubygems'
5
+ require 'sequel'
6
+ require 'expect'
7
+ require 'pty'
8
+ require 'pp'
9
+
10
+ class MysqlBackup
11
+ VERSION = '1.1.0'
12
+ end
13
+
14
+ ['install', 'pty', 'options', 'backup'].each do |file|
15
+ require File.dirname(__FILE__) + "/mysql_backup/#{file}"
16
+ end
17
+
18
+ if __FILE__ == $0
19
+ MysqlBackup.run
20
+ end
@@ -0,0 +1,61 @@
1
+ #require 'ruby-debug'
2
+
3
+ class MysqlBackup
4
+ class << self
5
+
6
+ # TODO refactor, too long
7
+ def run
8
+ user = options['user']
9
+ password = options['password']
10
+ host = options['host']
11
+ encoding = options['encoding']
12
+ dir = options['dir']
13
+ format = options['format']
14
+ keep = options['keep'] || 10
15
+ skip = options['skip']
16
+ mysqldump_options = options['mysqldump']['options']
17
+ path = options['mysqldump']['path']
18
+
19
+ timestamp = Time.now.strftime(format)
20
+
21
+ connection = Sequel.mysql nil, :user => user, :password => password, :host => host, :encoding => encoding
22
+
23
+ databases = []
24
+ connection['show databases'].each do |db|
25
+ databases << db[:Database]
26
+ end
27
+ databases = databases - skip #['mysql', 'test', 'information_schema']
28
+
29
+ databases.each do |db|
30
+ raise "The backup directory '#{dir}' doesn't exist" if !File.exist?(dir)
31
+
32
+ file = File.join(dir, "#{db}_#{timestamp}.sql")
33
+ p "Backing up #{db.ljust(40)} > #{file}"
34
+
35
+ cmd = "#{path}mysqldump -u#{user} -p#{password} -h#{host} #{mysqldump_options} #{db} --result-file=#{file}"
36
+
37
+ result = exec_pty(cmd, password)
38
+
39
+ if options['gzip'] != false
40
+ `gzip -fq #{file}`
41
+ end
42
+
43
+ # Find all backups and sort
44
+ all_backups = Dir.entries("#{dir}").select{|f| f =~ /^#{db}/}.select{|f| File.file? "#{File.join(dir, f)}" }.sort_by { |f| File.mtime("#{File.join(dir,f)}") }.reverse
45
+
46
+ #pp all_backups
47
+
48
+ keep = all_backups[0..keep] # eg. 10 latest
49
+
50
+ remove = all_backups - keep
51
+
52
+ remove.each do |file|
53
+ puts "Removing backup '#{file}' keeping #{keep}"
54
+ FileUtils.rm_rf(file)
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,71 @@
1
+ require 'fileutils'
2
+
3
+ class MysqlBackup
4
+ CONF_FILE = '/etc/mysql_backup'
5
+
6
+ class << self
7
+ def ask_for_options
8
+ require "highline/import"
9
+
10
+ puts <<-PROMPT
11
+
12
+ Please enter your MySQL username and password.
13
+
14
+ PROMPT
15
+
16
+ user = ask("MySQL username : ")
17
+ password = ask("MySQL password : ") { |q| q.echo = false }
18
+ host = ask("MySQL host : ") { |q| q.default = 'localhost' }
19
+ dir = ask("Backup directory : ") { |q| q.default = '/tmp' }
20
+
21
+ [user, password, host, dir]
22
+ end
23
+
24
+ def install
25
+ begin
26
+ if File.exist?(CONF_FILE)
27
+ puts <<-WARN
28
+
29
+ #{CONF_FILE} already exists. Remove it and try again.
30
+
31
+ WARN
32
+
33
+ exit
34
+ end
35
+
36
+ user, password, host, dir = ask_for_options
37
+
38
+ File.open(CONF_FILE, 'w') do |file|
39
+ template = File.read(File.join(File.dirname(__FILE__), 'mysql_backup.yml.example'))
40
+ template.gsub!('USER', user)
41
+ template.gsub!('PASS', password)
42
+ template.gsub!('HOST', host)
43
+ template.gsub!('DIR', dir)
44
+
45
+ file << template
46
+ end
47
+
48
+ FileUtils.chmod(750, CONF_FILE)
49
+
50
+ puts <<-ERR
51
+
52
+
53
+ Configuration file #{CONF_FILE} written.
54
+
55
+ You can now backup all databases by running the following command:
56
+
57
+ $ mysql_backup
58
+
59
+ ERR
60
+
61
+ rescue Errno::EACCES => e
62
+ puts <<-ERR
63
+
64
+ You need to run this command with sudo or as root.
65
+
66
+ ERR
67
+ exit
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,11 @@
1
+ user: USER
2
+ password: PASS
3
+ host: HOST
4
+ dir: DIR
5
+ format: %d-%m-%y
6
+ keep: 10
7
+
8
+ mysqldump:
9
+ options: -Q -c -C --add-drop-table --add-locks --quick --lock-tables
10
+ path:
11
+ skip: [mysql, test, information_schema]
@@ -0,0 +1,30 @@
1
+ #
2
+ # This errors means the directory doesn't exist or there's a problem with permissions:
3
+ # /usr/local/lib/ruby/1.8/expect.rb:17:in `expect': undefined method `chr' for nil:NilClass (NoMethodError)
4
+ #
5
+ class MysqlBackup
6
+ class << self
7
+ def exec_pty(cmd, password)
8
+ #$expect_verbose = true
9
+ password = '' if password.nil?
10
+
11
+ PTY.spawn(cmd) do |reader, writer, pid|
12
+ begin
13
+ reader.expect(/Enter password/, 1) do |line|
14
+ writer.puts password
15
+ end
16
+ rescue
17
+ print reader.gets
18
+ end
19
+
20
+ begin
21
+ while line = reader.gets
22
+ # TODO detect errors we should act upon
23
+ # raise line if line.strip.length > 1
24
+ end
25
+ rescue Errno::EIO # EOF
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
File without changes
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: christianhellsten-mysql-backup
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian Hellsten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-05-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.8.2
24
+ version:
25
+ description: mysql-backup is a command line tool that backups all MySQL database instances it can find on a server.
26
+ email:
27
+ - christian@aktagon.com
28
+ executables:
29
+ - mysql_backup
30
+ - mysql_backup_install
31
+ extensions: []
32
+
33
+ extra_rdoc_files:
34
+ - History.txt
35
+ - Manifest.txt
36
+ - README.txt
37
+ files:
38
+ - History.txt
39
+ - Manifest.txt
40
+ - README.txt
41
+ - Rakefile
42
+ - bin/mysql_backup
43
+ - bin/mysql_backup_install
44
+ - lib/mysql_backup.rb
45
+ - lib/mysql_backup/install.rb
46
+ - lib/mysql_backup/pty.rb
47
+ - lib/mysql_backup/backup.rb
48
+ - lib/mysql_backup/mysql_backup.yml.example
49
+ - test/test_mysql_backup.rb
50
+ has_rdoc: true
51
+ homepage: http://aktagon.com/projects/ruby/mysql-backup
52
+ post_install_message:
53
+ rdoc_options:
54
+ - --main
55
+ - README.txt
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project: mysqlbackup
73
+ rubygems_version: 1.2.0
74
+ signing_key:
75
+ specification_version: 2
76
+ summary: mysql-backup is a command line tool that backups all MySQL database instances it can find on a server.
77
+ test_files:
78
+ - test/test_mysql_backup.rb