dbmgr 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +0 -74
- data/README.md +0 -15
- data/bin/dbmgr +1 -1
- data/dbmgr.gemspec +3 -3
- data/lib/database/database.rb +33 -0
- data/lib/database/mysql.rb +44 -0
- data/lib/dbmgr/backup.rb +57 -0
- data/lib/dbmgr/databases.rb +6 -0
- data/lib/dbmgr/restore.rb +58 -0
- data/lib/dbmgr/version.rb +1 -1
- data/lib/dbmgr.rb +6 -2
- metadata +27 -23
- data/lib/dbmgr/cli/mysql.rb +0 -123
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6d50678d7d88d2e977fac194da5a297dfff2c611
|
4
|
+
data.tar.gz: 0f434fb01363d2bce751cda495b5122902b92575
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '088e02444623d19812306acde778136b6f8eb7c0ae40129c81cec93b9fa38d9a356435df2ddfc1cdb0900f083d30e0e76e1dfd88adc7d2be57aa93071eb7f5f5'
|
7
|
+
data.tar.gz: a6522f4e50ddc9043f2fdfff0637381d693a268228b1e38781ea7278f6c32a876f4d00fde74a8f22f17ae01c1c05a4fa63901c67d4abd615b23e67c79a78ad35
|
data/CODE_OF_CONDUCT.md
CHANGED
@@ -1,74 +0,0 @@
|
|
1
|
-
# Contributor Covenant Code of Conduct
|
2
|
-
|
3
|
-
## Our Pledge
|
4
|
-
|
5
|
-
In the interest of fostering an open and welcoming environment, we as
|
6
|
-
contributors and maintainers pledge to making participation in our project and
|
7
|
-
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
-
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
-
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
-
orientation.
|
11
|
-
|
12
|
-
## Our Standards
|
13
|
-
|
14
|
-
Examples of behavior that contributes to creating a positive environment
|
15
|
-
include:
|
16
|
-
|
17
|
-
* Using welcoming and inclusive language
|
18
|
-
* Being respectful of differing viewpoints and experiences
|
19
|
-
* Gracefully accepting constructive criticism
|
20
|
-
* Focusing on what is best for the community
|
21
|
-
* Showing empathy towards other community members
|
22
|
-
|
23
|
-
Examples of unacceptable behavior by participants include:
|
24
|
-
|
25
|
-
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
-
advances
|
27
|
-
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
-
* Public or private harassment
|
29
|
-
* Publishing others' private information, such as a physical or electronic
|
30
|
-
address, without explicit permission
|
31
|
-
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
-
professional setting
|
33
|
-
|
34
|
-
## Our Responsibilities
|
35
|
-
|
36
|
-
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
-
behavior and are expected to take appropriate and fair corrective action in
|
38
|
-
response to any instances of unacceptable behavior.
|
39
|
-
|
40
|
-
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
-
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
-
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
-
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
-
threatening, offensive, or harmful.
|
45
|
-
|
46
|
-
## Scope
|
47
|
-
|
48
|
-
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
-
when an individual is representing the project or its community. Examples of
|
50
|
-
representing a project or community include using an official project e-mail
|
51
|
-
address, posting via an official social media account, or acting as an appointed
|
52
|
-
representative at an online or offline event. Representation of a project may be
|
53
|
-
further defined and clarified by project maintainers.
|
54
|
-
|
55
|
-
## Enforcement
|
56
|
-
|
57
|
-
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
-
reported by contacting the project team at callahanrts@gmail.com. All
|
59
|
-
complaints will be reviewed and investigated and will result in a response that
|
60
|
-
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
-
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
-
Further details of specific enforcement policies may be posted separately.
|
63
|
-
|
64
|
-
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
-
faith may face temporary or permanent repercussions as determined by other
|
66
|
-
members of the project's leadership.
|
67
|
-
|
68
|
-
## Attribution
|
69
|
-
|
70
|
-
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
-
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
-
|
73
|
-
[homepage]: http://contributor-covenant.org
|
74
|
-
[version]: http://contributor-covenant.org/version/1/4/
|
data/README.md
CHANGED
@@ -33,21 +33,6 @@ $ dmgr mysql backup --options
|
|
33
33
|
$ dbmgr mysql restore --options
|
34
34
|
```
|
35
35
|
|
36
|
-
#### Config
|
37
|
-
Options can either be passed in each command or they can be set as defaults in the `~/.dbmgr`
|
38
|
-
config file.
|
39
|
-
```ruby
|
40
|
-
# ~/.dbmgr
|
41
|
-
|
42
|
-
:dbmgr:
|
43
|
-
:path: "~/.db_backups"
|
44
|
-
|
45
|
-
:mysql:
|
46
|
-
:port: 3306
|
47
|
-
:host: 'localhost'
|
48
|
-
:user: "root"
|
49
|
-
```
|
50
|
-
|
51
36
|
## Examples
|
52
37
|
#### Back up a MySQL database
|
53
38
|
|
data/bin/dbmgr
CHANGED
data/dbmgr.gemspec
CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Cody"]
|
10
10
|
spec.email = ["callahanrts@gmail.com"]
|
11
11
|
|
12
|
-
spec.required_ruby_version = '< 2.
|
12
|
+
spec.required_ruby_version = '< 2.5'
|
13
13
|
|
14
14
|
spec.summary = %q{Create database backups and restore from previously created backups}
|
15
15
|
spec.description = %q{Create database backups to share with others across your dev team. Other developers can restore from backups you've created.}
|
@@ -23,11 +23,11 @@ Gem::Specification.new do |spec|
|
|
23
23
|
spec.executables = spec.files.grep(%r{^bin/dbmgr$}) { |f| File.basename(f) }
|
24
24
|
spec.require_paths = ["lib"]
|
25
25
|
|
26
|
-
spec.add_dependency 'thor'
|
26
|
+
spec.add_dependency 'thor', "~> 0"
|
27
27
|
|
28
28
|
spec.add_development_dependency "bundler", "~> 1.13"
|
29
29
|
spec.add_development_dependency "rake", "~> 10.0"
|
30
30
|
spec.add_development_dependency "minitest", "~> 5.0"
|
31
31
|
spec.add_development_dependency "mysql2", "~> 0.4.5"
|
32
|
-
spec.add_development_dependency "mocha", "~> 1.2
|
32
|
+
spec.add_development_dependency "mocha", "~> 1.2"
|
33
33
|
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# Database Class
|
2
|
+
class Database
|
3
|
+
def initialize(options = {})
|
4
|
+
@user = options[:user]
|
5
|
+
@host = options[:host]
|
6
|
+
@port = options[:port]
|
7
|
+
@path = options[:path]
|
8
|
+
@file = options[:filename]
|
9
|
+
@backup = options[:backup]
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
# Construct a name for the backup. This is either a given name or a timestamped name
|
15
|
+
# for the given database name
|
16
|
+
def filename db_name
|
17
|
+
@file || "#{db_name}_#{Time.now.to_i}.sql"
|
18
|
+
end
|
19
|
+
|
20
|
+
# Back up from a given backup file--default to the most recent timestamped backup
|
21
|
+
def backup_file db_name
|
22
|
+
# Grab the backup file or the latest backup from the backups directory
|
23
|
+
backup = @backup || Dir.glob("#{@path}/#{db_name}_*.sql").last
|
24
|
+
raise "Restore failed: backup not found" unless File.file?(backup.to_s)
|
25
|
+
backup
|
26
|
+
end
|
27
|
+
|
28
|
+
# Construct the /file/path/backup.sql string
|
29
|
+
def filepath db_name
|
30
|
+
"#{@path}/#{filename db_name}"
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
# MySQL Module
|
4
|
+
module MySQL
|
5
|
+
def backup(db_name)
|
6
|
+
FileUtils::mkdir_p @path
|
7
|
+
|
8
|
+
# Create a mysql backup from the user supplied options
|
9
|
+
system backup_db_command db_name
|
10
|
+
rescue
|
11
|
+
raise 'Unable to back up database'
|
12
|
+
end
|
13
|
+
|
14
|
+
def restore(db_name)
|
15
|
+
# Create the database to restore if it doesn't exist already
|
16
|
+
system create_db_command db_name
|
17
|
+
|
18
|
+
# Restore the database from a backup
|
19
|
+
system restore_db_command db_name
|
20
|
+
rescue
|
21
|
+
raise 'Unable to restore database'
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def backup_db_command(db_name)
|
27
|
+
%( mysqldump -u#{@user} \
|
28
|
+
-h #{@host} \
|
29
|
+
-P #{@port} #{db_name} > #{filepath db_name} )
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_db_command(db_name)
|
33
|
+
%( mysql -u#{@user} \
|
34
|
+
-h #{@host} \
|
35
|
+
-P #{@port} \
|
36
|
+
-e \"CREATE DATABASE IF NOT EXISTS #{db_name}\" )
|
37
|
+
end
|
38
|
+
|
39
|
+
def restore_db_command(db_name)
|
40
|
+
%( mysql -u#{@user} #{db_name} \
|
41
|
+
-h #{@host} \
|
42
|
+
-P #{@port} < #{backup_file db_name})
|
43
|
+
end
|
44
|
+
end
|
data/lib/dbmgr/backup.rb
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
|
2
|
+
module Dbmgr
|
3
|
+
class CLI < Thor
|
4
|
+
|
5
|
+
#
|
6
|
+
# Backup
|
7
|
+
#
|
8
|
+
desc "backup [database_name]", "Create a backup"
|
9
|
+
method_option :filename,
|
10
|
+
aliases: ["f"],
|
11
|
+
type: :string,
|
12
|
+
banner: "my_backup.sql",
|
13
|
+
desc: "Name of the backup created"
|
14
|
+
|
15
|
+
method_option :path,
|
16
|
+
aliases: ["p"],
|
17
|
+
type: :string,
|
18
|
+
default: "#{ENV["HOME"]}/.db_backups",
|
19
|
+
banner: "#{ENV["HOME"]}/.db_backups",
|
20
|
+
desc: "Directory of database backups"
|
21
|
+
|
22
|
+
method_option :port,
|
23
|
+
aliases: ["P"],
|
24
|
+
type: :numeric,
|
25
|
+
default: 3306,
|
26
|
+
banner: "3306",
|
27
|
+
desc: "MySQL database port"
|
28
|
+
|
29
|
+
method_option :host,
|
30
|
+
aliases: ["h"],
|
31
|
+
type: :string,
|
32
|
+
default: "localhost",
|
33
|
+
banner: "localhost",
|
34
|
+
desc: "MySQL database host"
|
35
|
+
|
36
|
+
method_option :user,
|
37
|
+
aliases: ["u"],
|
38
|
+
type: :string,
|
39
|
+
default: "root",
|
40
|
+
banner: "root",
|
41
|
+
desc: "MySQL database user"
|
42
|
+
|
43
|
+
method_option :database,
|
44
|
+
aliases: "d",
|
45
|
+
type: :string,
|
46
|
+
default: "mysql",
|
47
|
+
banner: "mysql",
|
48
|
+
desc: "Database type"
|
49
|
+
|
50
|
+
def backup db_name
|
51
|
+
Database.new(options)
|
52
|
+
.extend(DATABASES[options[:database].to_sym])
|
53
|
+
.backup db_name
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
module Dbmgr
|
3
|
+
class CLI < Thor
|
4
|
+
|
5
|
+
#
|
6
|
+
# Restore
|
7
|
+
#
|
8
|
+
desc "restore", "Restore from a backup"
|
9
|
+
method_option :backup,
|
10
|
+
aliases: ["b"],
|
11
|
+
type: :string,
|
12
|
+
banner: "#{ENV["HOME"]}/.db_backups/backup.sql",
|
13
|
+
desc: "Path to backup to restore from"
|
14
|
+
|
15
|
+
method_option :path,
|
16
|
+
aliases: ["p"],
|
17
|
+
type: :string,
|
18
|
+
default: "#{ENV["HOME"]}/.db_backups",
|
19
|
+
banner: "#{ENV["HOME"]}/.db_backups",
|
20
|
+
desc: "Directory of database backups"
|
21
|
+
|
22
|
+
method_option :port,
|
23
|
+
aliases: ["P"],
|
24
|
+
type: :numeric,
|
25
|
+
default: 3306,
|
26
|
+
banner: "3306",
|
27
|
+
desc: "MySQL database port"
|
28
|
+
|
29
|
+
method_option :host,
|
30
|
+
aliases: ["h"],
|
31
|
+
type: :string,
|
32
|
+
default: "localhost",
|
33
|
+
banner: "localhost",
|
34
|
+
desc: "MySQL database host"
|
35
|
+
|
36
|
+
method_option :user,
|
37
|
+
aliases: ["u"],
|
38
|
+
type: :string,
|
39
|
+
default: "root",
|
40
|
+
banner: "root",
|
41
|
+
desc: "MySQL database user"
|
42
|
+
|
43
|
+
method_option :database,
|
44
|
+
aliases: "d",
|
45
|
+
type: :string,
|
46
|
+
default: "mysql",
|
47
|
+
banner: "mysql",
|
48
|
+
desc: "Database type"
|
49
|
+
|
50
|
+
def restore db_name
|
51
|
+
Database.new(options)
|
52
|
+
.extend(DATABASES[options[:database].to_sym])
|
53
|
+
.restore db_name
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
data/lib/dbmgr/version.rb
CHANGED
data/lib/dbmgr.rb
CHANGED
metadata
CHANGED
@@ -1,99 +1,99 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dbmgr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cody
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - ~>
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.13'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - ~>
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.13'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - ~>
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - ~>
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - ~>
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '5.0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - ~>
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '5.0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: mysql2
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - ~>
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: 0.4.5
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - ~>
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: 0.4.5
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: mocha
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - ~>
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: 1.2
|
89
|
+
version: '1.2'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - ~>
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: 1.2
|
96
|
+
version: '1.2'
|
97
97
|
description: Create database backups to share with others across your dev team. Other
|
98
98
|
developers can restore from backups you've created.
|
99
99
|
email:
|
@@ -103,8 +103,8 @@ executables:
|
|
103
103
|
extensions: []
|
104
104
|
extra_rdoc_files: []
|
105
105
|
files:
|
106
|
-
- .gitignore
|
107
|
-
- .travis.yml
|
106
|
+
- ".gitignore"
|
107
|
+
- ".travis.yml"
|
108
108
|
- CODE_OF_CONDUCT.md
|
109
109
|
- Gemfile
|
110
110
|
- LICENSE.txt
|
@@ -116,9 +116,13 @@ files:
|
|
116
116
|
- bin/setup
|
117
117
|
- dbmgr.gemspec
|
118
118
|
- examples/.dbmgr
|
119
|
+
- lib/database/database.rb
|
120
|
+
- lib/database/mysql.rb
|
119
121
|
- lib/dbmgr.rb
|
122
|
+
- lib/dbmgr/backup.rb
|
120
123
|
- lib/dbmgr/cli.rb
|
121
|
-
- lib/dbmgr/
|
124
|
+
- lib/dbmgr/databases.rb
|
125
|
+
- lib/dbmgr/restore.rb
|
122
126
|
- lib/dbmgr/version.rb
|
123
127
|
homepage: https://github.com/callahanrts/dbmgr
|
124
128
|
licenses:
|
@@ -130,17 +134,17 @@ require_paths:
|
|
130
134
|
- lib
|
131
135
|
required_ruby_version: !ruby/object:Gem::Requirement
|
132
136
|
requirements:
|
133
|
-
- - <
|
137
|
+
- - "<"
|
134
138
|
- !ruby/object:Gem::Version
|
135
|
-
version: '2.
|
139
|
+
version: '2.5'
|
136
140
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
137
141
|
requirements:
|
138
|
-
- -
|
142
|
+
- - ">="
|
139
143
|
- !ruby/object:Gem::Version
|
140
144
|
version: '0'
|
141
145
|
requirements: []
|
142
146
|
rubyforge_project:
|
143
|
-
rubygems_version: 2.
|
147
|
+
rubygems_version: 2.6.11
|
144
148
|
signing_key:
|
145
149
|
specification_version: 4
|
146
150
|
summary: Create database backups and restore from previously created backups
|
data/lib/dbmgr/cli/mysql.rb
DELETED
@@ -1,123 +0,0 @@
|
|
1
|
-
require 'yaml'
|
2
|
-
|
3
|
-
module Dbmgr::CLI
|
4
|
-
class Mysql < Thor
|
5
|
-
|
6
|
-
#
|
7
|
-
# Backup
|
8
|
-
#
|
9
|
-
desc "backup [database_name]", "Create a backup"
|
10
|
-
method_option :filename,
|
11
|
-
aliases: ["f"],
|
12
|
-
type: :string,
|
13
|
-
banner: "my_backup.sql",
|
14
|
-
desc: "Name of the backup created"
|
15
|
-
|
16
|
-
method_option :path,
|
17
|
-
aliases: ["p"],
|
18
|
-
type: :string,
|
19
|
-
default: "#{ENV["HOME"]}/.db_backups",
|
20
|
-
banner: "#{ENV["HOME"]}/.db_backups",
|
21
|
-
desc: "Directory of database backups"
|
22
|
-
|
23
|
-
method_option :port,
|
24
|
-
aliases: ["P"],
|
25
|
-
type: :numeric,
|
26
|
-
default: 3306,
|
27
|
-
banner: "3306",
|
28
|
-
desc: "MySQL database port"
|
29
|
-
|
30
|
-
method_option :host,
|
31
|
-
aliases: ["h"],
|
32
|
-
type: :string,
|
33
|
-
default: "localhost",
|
34
|
-
banner: "localhost",
|
35
|
-
desc: "MySQL database host"
|
36
|
-
|
37
|
-
method_option :user,
|
38
|
-
aliases: ["u"],
|
39
|
-
type: :string,
|
40
|
-
default: "root",
|
41
|
-
banner: "root",
|
42
|
-
desc: "MySQL database user"
|
43
|
-
|
44
|
-
def backup db_name
|
45
|
-
file = options[:filename] || "#{db_name}_#{Time.now.to_i}.sql"
|
46
|
-
puts "Backing up '#{db_name}' (#{options[:path]}/#{file})..."
|
47
|
-
|
48
|
-
# Create the backups directory if it doesn't exist already
|
49
|
-
system "mkdir -p #{options[:path]}"
|
50
|
-
|
51
|
-
# Create a mysql backup from the user supplied options
|
52
|
-
system "mysqldump -u#{options[:user]} -h #{options[:host]} -P #{options[:port]} #{db_name} > #{options[:path]}/#{file}"
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
#
|
57
|
-
# Restore
|
58
|
-
#
|
59
|
-
desc "restore", "Restore from a backup"
|
60
|
-
method_option :backup,
|
61
|
-
aliases: ["b"],
|
62
|
-
type: :string,
|
63
|
-
banner: "#{ENV["HOME"]}/.db_backups/backup.sql",
|
64
|
-
desc: "Path to backup to restore from"
|
65
|
-
|
66
|
-
method_option :path,
|
67
|
-
aliases: ["p"],
|
68
|
-
type: :string,
|
69
|
-
default: "#{ENV["HOME"]}/.db_backups",
|
70
|
-
banner: "#{ENV["HOME"]}/.db_backups",
|
71
|
-
desc: "Directory of database backups"
|
72
|
-
|
73
|
-
method_option :port,
|
74
|
-
aliases: ["P"],
|
75
|
-
type: :numeric,
|
76
|
-
default: 3306,
|
77
|
-
banner: "3306",
|
78
|
-
desc: "MySQL database port"
|
79
|
-
|
80
|
-
method_option :host,
|
81
|
-
aliases: ["h"],
|
82
|
-
type: :string,
|
83
|
-
default: "localhost",
|
84
|
-
banner: "localhost",
|
85
|
-
desc: "MySQL database host"
|
86
|
-
|
87
|
-
method_option :user,
|
88
|
-
aliases: ["u"],
|
89
|
-
type: :string,
|
90
|
-
default: "root",
|
91
|
-
banner: "root",
|
92
|
-
desc: "MySQL database user"
|
93
|
-
|
94
|
-
def restore db_name
|
95
|
-
puts "Create database if it doesn't exist..."
|
96
|
-
|
97
|
-
# Create the database to restore if it doesn't exist already
|
98
|
-
system "mysql -u#{options[:user]} -h #{options[:host]} -P #{options[:port]} -e \"CREATE DATABASE IF NOT EXISTS #{db_name}\""
|
99
|
-
|
100
|
-
# Grab the backup file or the latest backup from the backups directory
|
101
|
-
backup = options[:backup] || Dir.glob("#{options[:path]}/#{db_name}_*.sql").last
|
102
|
-
raise "Restore failed: backup not found" unless File.file?(backup.to_s)
|
103
|
-
|
104
|
-
# Restore the database from a backup
|
105
|
-
system("mysql -u#{options[:user]} #{db_name} -h #{options[:host]} -P #{options[:port]} < #{backup}")
|
106
|
-
end
|
107
|
-
|
108
|
-
private
|
109
|
-
|
110
|
-
def options
|
111
|
-
original = super
|
112
|
-
return original unless File.file?("#{home}/.dbmgr")
|
113
|
-
config = YAML::load_file("#{home}/.dbmgr") || {dbmgr: {}, mysql: {}}
|
114
|
-
config[:dbmgr][:path].gsub!("~/", "#{home}/") if !config[:dbmgr].nil? && config[:dbmgr].has_key?(:path) # Expand ~/ manually
|
115
|
-
Thor::CoreExt::HashWithIndifferentAccess.new([config[:dbmgr], config[:mysql], original].reduce &:merge)
|
116
|
-
end
|
117
|
-
|
118
|
-
def home
|
119
|
-
ENV["HOME"]
|
120
|
-
end
|
121
|
-
|
122
|
-
end
|
123
|
-
end
|