mysql_backups 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f72b9907288779ccc6967863b280003c7d601cab
4
+ data.tar.gz: 2a8e4aceb6e8306d4e22d6871a4ef2394ae20465
5
+ SHA512:
6
+ metadata.gz: f2281afa736ea165d99c944817df145f2356021db2cc6646ab0912fabd5efee38b86a2ccacd9eaa467a9cb70204c342efed0250ba369a87a35797a747d824bfb
7
+ data.tar.gz: 2028f5661337215a90488c697a2b6810ed6612fd63ed642b02e01a0661958b9a3bed436a9b3ca11b1112a8ab5b87ff93bc4b29b4f9eaf2b125f1dd0171939e7c
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mysql_backup.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mysql_backup (1.1.0)
5
+ colorize
6
+ trollop
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ colorize (0.8.1)
12
+ rake (10.5.0)
13
+ trollop (2.1.2)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bundler
20
+ mysql_backup!
21
+ rake (~> 10.0)
22
+
23
+ BUNDLED WITH
24
+ 1.13.6
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Nathan Reed
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # MySQL Backup Tools
2
+
3
+ ## Installation
4
+
5
+ $ gem install mysql_backup
6
+
7
+ ## Usage
8
+
9
+ Run the command `mysql-backup` to backup a database. You must provide a config file in order to tell it what to backup.
10
+
11
+ mysql-backup --config /path/to/config.yaml
12
+
13
+ ## Config files
14
+
15
+ The backup config is stored in a YAML file. The only required settings are `server`, `username` and `database`
16
+
17
+ For example:
18
+
19
+ # reddit-stream.backup.yaml
20
+ server: 127.0.0.1
21
+ username: backup_user
22
+ database: redditstream
23
+
24
+ There are a number of optional parameters as well:
25
+
26
+ # reddit-stream.backup.yaml
27
+ #
28
+ # These are the mandatory settings, same as before
29
+ server: 127.0.0.1
30
+ username: backup_user
31
+ database: redditstream
32
+
33
+ # you can specify the list of tables to be backed up
34
+ tables: [action_log_notes, payment_log, payment_log_notes]
35
+
36
+ # list multiple locations to save the backup to. SSH hosts can
37
+ # be listed here as well, and the file will be transferred using
38
+ # scp
39
+ save_to:
40
+ - ~/backups
41
+ - user@remote.com:backups/
42
+
43
+
44
+
45
+ ## License
46
+
47
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
48
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mysql_backup"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/mysql-backup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ require 'mysql_backup'
3
+
4
+ begin
5
+ MysqlBackup::App.new.main
6
+ rescue => e
7
+ puts e.to_s
8
+ end
@@ -0,0 +1,89 @@
1
+ require 'trollop'
2
+ require 'mysql_backup/version'
3
+ require 'mysql_backup/backup_config'
4
+
5
+ module MysqlBackup
6
+ class App
7
+ def main
8
+
9
+ opts = Trollop::options do
10
+ version "mysql-backup #{MysqlBackup::VERSION} (c) 2015 @reednj"
11
+ opt :config, "YAML config file", :type => :string
12
+ opt :filename, "Name of backup output file", :type => :string
13
+ end
14
+
15
+ Dir.mkdir tmp_dir if !File.exist? tmp_dir
16
+ @seed = (rand() * 10000).round.to_s
17
+
18
+ begin
19
+ config_path = opts[:config] || 'mysql-backup.conf'
20
+ @config = BackupConfig.load_from config_path
21
+ rescue => e
22
+ puts "Error: Could not load config file at '#{config_path}' - #{e.message}"
23
+ return
24
+ end
25
+
26
+ @output_file = opts[:filename] || "#{@config.database}.#{Date.today}.#{@seed}.sql.gz"
27
+ self.create_backup
28
+
29
+ @config.save_to.each do |path|
30
+ begin
31
+ self.save path
32
+ puts File.join(path, filename)
33
+ rescue => e
34
+ puts "Could not save backup to #{path} - #{e.message}"
35
+ end
36
+ end
37
+
38
+ self.clean
39
+ end
40
+
41
+ def create_backup
42
+ password = ''
43
+ password = "-p#{@config.password}" if @config.password?
44
+ options = '--skip-comments'
45
+ tables = @config.tables.join ' '
46
+ run! "mysqldump -u #{@config.username} #{password} --host=#{@config.server} #{options} #{@config.database} #{tables} | gzip > #{tmp_path}; exit ${PIPESTATUS[0]}"
47
+ end
48
+
49
+ def save(to_dir)
50
+ if _remote_path? to_dir
51
+ dest = File.join(to_dir, filename)
52
+ run! "scp #{tmp_path} #{dest}"
53
+ else
54
+ p = File.expand_path to_dir
55
+ dest = File.join(p, filename)
56
+ FileUtils.copy tmp_path, dest
57
+ end
58
+ end
59
+
60
+ def clean
61
+ File.delete tmp_path
62
+ end
63
+
64
+ def filename
65
+ @output_file
66
+ end
67
+
68
+ def tmp_dir
69
+ File.expand_path "~/.tmp/"
70
+ end
71
+
72
+ def tmp_path
73
+ File.join tmp_dir, "mysql.#{@seed}.bak"
74
+ end
75
+
76
+ # runs a shell command in such a way that if it fails (according to the exit status)
77
+ # and exception will be raised with the stderr output
78
+ def run!(cmd)
79
+ output = `(#{cmd}) 2>&1`
80
+ raise "#{output}" if $?.exitstatus != 0
81
+ end
82
+
83
+ # is this path on a remote server? do we need to use scp to copy the backup there?
84
+ def _remote_path?(path)
85
+ # not exactly foolproof, but it will do for now
86
+ path.include? ':'
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,66 @@
1
+ require 'ostruct'
2
+ require 'fileutils'
3
+ require 'json'
4
+ require 'yaml'
5
+ require 'time'
6
+
7
+ require 'mysql_backup/extensions'
8
+
9
+ module MysqlBackup
10
+ class BackupConfig
11
+ def initialize(config)
12
+ @config = config
13
+ _validate!
14
+ end
15
+
16
+ def self.load_from(path)
17
+ data = YAML.load_file(path)
18
+ BackupConfig.new data
19
+ end
20
+
21
+ def _validate!
22
+ raise 'config object required' if @config.nil?
23
+ raise 'server required' if server.nil?
24
+ raise 'username required' if username.nil?
25
+ raise 'database required' if database.nil?
26
+ raise 'backup destination required' if save_to.nil?
27
+ end
28
+
29
+ def server
30
+ @config['server']
31
+ end
32
+
33
+ def username
34
+ @config['username'] || (app_config? ? AppConfig.db[:username] : nil)
35
+ end
36
+
37
+ def password
38
+ pw = @config['password'] || (app_config? ? AppConfig.db[:password] : nil)
39
+ return nil if pw.nil? || pw.strip == ''
40
+ return pw
41
+ end
42
+
43
+ def database
44
+ @config['database']
45
+ end
46
+
47
+ def password?
48
+ !password.nil?
49
+ end
50
+
51
+ def tables
52
+ Array.from @config['tables']
53
+ end
54
+
55
+ def save_to
56
+ return ['.'] if @config['save_to'].nil?
57
+ Array.from @config['save_to']
58
+ end
59
+
60
+ def app_config?
61
+ !!defined?(AppConfig) && !AppConfig.nil? && !AppConfig.db.nil? && AppConfig.db[:database] == self.database
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,35 @@
1
+
2
+ def try_load_file(path)
3
+ begin
4
+ load_relative path
5
+ return path
6
+ rescue LoadError => e
7
+ return nil
8
+ end
9
+ end
10
+
11
+ def try_load(paths)
12
+ Array.from(paths).each do |path|
13
+ result = try_load_file path
14
+ return result if !result.nil?
15
+ end
16
+
17
+ return nil
18
+ end
19
+
20
+ def this_dir
21
+ file = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__
22
+ File.expand_path File.dirname(file)
23
+ end
24
+
25
+ def load_relative(path)
26
+ load File.expand_path path, this_dir()
27
+ end
28
+
29
+ class Array
30
+ def self.from(a)
31
+ return a if a.is_a? Array
32
+ return [] if a.nil?
33
+ return [a]
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module MysqlBackup
2
+ VERSION = "1.1.0"
3
+ end
data/mysql-backup-dev ADDED
@@ -0,0 +1,2 @@
1
+ #!/bin/bash
2
+ bundle exec exe/mysql-backup "$@"
data/mysql-test.conf ADDED
@@ -0,0 +1,7 @@
1
+ server: 127.0.0.1
2
+ username: linkuser
3
+ database: redditstream
4
+ tables: [action_log_notes, payment_log, payment_log_notes]
5
+ save_to:
6
+ - ~/Documents
7
+
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mysql_backup/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mysql_backups"
8
+ spec.version = MysqlBackup::VERSION
9
+ spec.authors = ["Nathan Reed"]
10
+ spec.email = ["reednj@gmail.com"]
11
+
12
+ spec.summary = %q{easily make mysql backups from the command line}
13
+ spec.homepage = "https://github.com/reedn/mysql_backup"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+
24
+ spec.required_ruby_version = '>=1.9.3'
25
+ spec.add_dependency 'colorize'
26
+ spec.add_dependency 'trollop'
27
+
28
+ end
metadata ADDED
@@ -0,0 +1,117 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mysql_backups
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nathan Reed
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: colorize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: trollop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - reednj@gmail.com
72
+ executables:
73
+ - mysql-backup
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - Gemfile
79
+ - Gemfile.lock
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - bin/console
84
+ - bin/setup
85
+ - exe/mysql-backup
86
+ - lib/mysql_backup.rb
87
+ - lib/mysql_backup/backup_config.rb
88
+ - lib/mysql_backup/extensions.rb
89
+ - lib/mysql_backup/version.rb
90
+ - mysql-backup-dev
91
+ - mysql-test.conf
92
+ - mysql_backup.gemspec
93
+ homepage: https://github.com/reedn/mysql_backup
94
+ licenses:
95
+ - MIT
96
+ metadata: {}
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: 1.9.3
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ requirements: []
112
+ rubyforge_project:
113
+ rubygems_version: 2.4.5.2
114
+ signing_key:
115
+ specification_version: 4
116
+ summary: easily make mysql backups from the command line
117
+ test_files: []