pg_export 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +1 -0
- data/.rubocop.yml +50 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/pg_export +45 -0
- data/bin/setup +8 -0
- data/lib/pg_export/actions/compress_dump.rb +39 -0
- data/lib/pg_export/actions/create_dump.rb +35 -0
- data/lib/pg_export/actions/remove_old_dumps.rb +25 -0
- data/lib/pg_export/actions/remove_old_dumps_from_ftp.rb +22 -0
- data/lib/pg_export/actions/send_dump_to_ftp.rb +19 -0
- data/lib/pg_export/actions.rb +5 -0
- data/lib/pg_export/configuration.rb +35 -0
- data/lib/pg_export/dump.rb +61 -0
- data/lib/pg_export/errors.rb +6 -0
- data/lib/pg_export/ftp_service/connection.rb +31 -0
- data/lib/pg_export/ftp_service.rb +25 -0
- data/lib/pg_export/logging.rb +11 -0
- data/lib/pg_export/version.rb +3 -0
- data/lib/pg_export.rb +56 -0
- data/pg_export.gemspec +30 -0
- metadata +158 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42e00c5fe412f42897cd36417b56c22e9e52d411
|
4
|
+
data.tar.gz: fcbe328c44560f4791ba7c12f51a9eeae47a7628
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4f6d936f9b5fe73d3b9fecc729762e6245778809db9eacada3a43a8f7d66da97f123c78bca24492a309ddd6e1c7a1e83d46c6626c9f95c15c111961a4ce0ad35
|
7
|
+
data.tar.gz: 4969b0a65ba230d1347c7ea8a4cd0d421395ef3800f9f57509d23ef2a3a37d0fdc32dc3ed9c1507e7cd353f63744291e069ada1fe8a3de3934c680dc6c5a0652
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.1
|
3
|
+
Exclude:
|
4
|
+
- 'spec/spec_helper.rb'
|
5
|
+
|
6
|
+
Style/ClassAndModuleChildren:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Metrics/LineLength:
|
10
|
+
Max: 200
|
11
|
+
|
12
|
+
Metrics/AbcSize:
|
13
|
+
Max: 20
|
14
|
+
Exclude:
|
15
|
+
- 'lib/pg_export.rb'
|
16
|
+
|
17
|
+
Lint/NestedMethodDefinition:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Lint/HandleExceptions:
|
21
|
+
Exclude:
|
22
|
+
- 'spec/actions/create_dump_spec.rb'
|
23
|
+
|
24
|
+
Lint/AssignmentInCondition:
|
25
|
+
Exclude:
|
26
|
+
- 'lib/pg_export/actions/compress_dump.rb'
|
27
|
+
|
28
|
+
Lint/AmbiguousOperator:
|
29
|
+
Exclude:
|
30
|
+
- 'lib/pg_export/configuration.rb'
|
31
|
+
|
32
|
+
Style/AlignArray:
|
33
|
+
Exclude:
|
34
|
+
- 'spec/configuration_spec.rb'
|
35
|
+
|
36
|
+
Style/GuardClause:
|
37
|
+
Exclude:
|
38
|
+
- '*.gemspec'
|
39
|
+
|
40
|
+
Style/AndOr:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Style/ParallelAssignment:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Documentation:
|
47
|
+
Enabled: false
|
48
|
+
|
49
|
+
CaseIndentation:
|
50
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
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 krzysztof.maicher@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/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Krzysztof Maicher
|
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,51 @@
|
|
1
|
+
# PgExport
|
2
|
+
|
3
|
+
CLI for exporting Rails Postgres database dump to FTP.
|
4
|
+
|
5
|
+
Can be used for backups or synchronizing databases between production and development environments.
|
6
|
+
|
7
|
+
## Dependencies
|
8
|
+
|
9
|
+
* Ruby >= 2.1
|
10
|
+
* PostgreSQL >= 9.1
|
11
|
+
* $ pg_dump
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'pg_export'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install pg_export
|
28
|
+
|
29
|
+
## CLI
|
30
|
+
|
31
|
+
$ pg_export -h
|
32
|
+
|
33
|
+
Usage: pg_export [options]
|
34
|
+
-d, --database DATABASE [Required] Name of the database to export
|
35
|
+
-k, --keep [KEEP] [Optional] Number of dump files to keep locally and on FTP (default: 10)
|
36
|
+
-t, --timestamped [Optional] Enables log messages with timestamps
|
37
|
+
-h, --help Show this message
|
38
|
+
|
39
|
+
## Development
|
40
|
+
|
41
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
42
|
+
|
43
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
44
|
+
|
45
|
+
## Contributing
|
46
|
+
|
47
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/maicher/pg_export. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'pg_export'
|
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/pg_export
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require 'ostruct'
|
5
|
+
require 'pg_export'
|
6
|
+
|
7
|
+
LOGS_TIMESTAMPED = ->(severity, datetime, progname, message) { "#{datetime} #{Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{progname} #{severity}: #{message}\n" }
|
8
|
+
LOGS_NORMAL = ->(_, _, _, message) { "#{message}\n" }
|
9
|
+
|
10
|
+
options = OpenStruct.new
|
11
|
+
option_parser = OptionParser.new do |opts|
|
12
|
+
opts.banner = 'Usage: pg_export [options]'
|
13
|
+
|
14
|
+
opts.on('-d', '--database DATABASE', '[Required] Name of the database to export') do |database|
|
15
|
+
options.database = database
|
16
|
+
end
|
17
|
+
|
18
|
+
opts.on('-k', '--keep [KEEP]', Integer, '[Optional] Number of dump files to keep locally and on FTP (default: 10)') do |keep|
|
19
|
+
options.keep = keep
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on('-t', '--timestamped', '[Optional] Enables log messages with timestamps') do
|
23
|
+
options.timestamped = true
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.on_tail('-h', '--help', 'Show this message') do
|
27
|
+
puts opts
|
28
|
+
exit
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
begin
|
33
|
+
option_parser.parse!
|
34
|
+
|
35
|
+
PgExport::Logging.logger.formatter = options.timestamped ? LOGS_TIMESTAMPED : LOGS_NORMAL
|
36
|
+
|
37
|
+
PgExport.new do |config|
|
38
|
+
config.database = options.database if options.database
|
39
|
+
config.keep_dumps = options.keep if options.keep
|
40
|
+
config.keep_ftp_dumps = options.keep if options.keep
|
41
|
+
end.call
|
42
|
+
rescue OptionParser::MissingArgument, PgExport::InvalidConfigurationError
|
43
|
+
puts 'Error: Invalid options.'
|
44
|
+
puts option_parser
|
45
|
+
end
|
data/bin/setup
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
class PgExport
|
2
|
+
class CompressDump
|
3
|
+
include Logging
|
4
|
+
|
5
|
+
def initialize(dump)
|
6
|
+
@dump = dump
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
validate_dumpfile_exists
|
11
|
+
compress_dumpfile
|
12
|
+
remove_dumpfile
|
13
|
+
self
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :dump
|
19
|
+
|
20
|
+
def validate_dumpfile_exists
|
21
|
+
File.exist?(dump.pathname) or raise DumpFileDoesNotExistError, "#{dump.pathname} does not exist"
|
22
|
+
end
|
23
|
+
|
24
|
+
def compress_dumpfile
|
25
|
+
Zlib::GzipWriter.open(dump.pathname_gz) do |gz|
|
26
|
+
File.open(dump.pathname) do |fp|
|
27
|
+
while chunk = fp.read(16 * 1024)
|
28
|
+
gz.write chunk
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
logger.info "Zip dump #{dump.basename_gz} (#{dump.size_gz})"
|
33
|
+
end
|
34
|
+
|
35
|
+
def remove_dumpfile
|
36
|
+
File.delete(dump.pathname)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class PgExport
|
2
|
+
class CreateDump
|
3
|
+
include Logging
|
4
|
+
|
5
|
+
def initialize(dump)
|
6
|
+
@dump = dump
|
7
|
+
end
|
8
|
+
|
9
|
+
def call
|
10
|
+
validate_pg_dump_exists
|
11
|
+
validate_db_exists(dump.database)
|
12
|
+
execute_dump_command
|
13
|
+
logger.info "Dump #{dump.database} to #{dump.pathname} (#{dump.size})"
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
attr_reader :dump
|
19
|
+
|
20
|
+
def validate_pg_dump_exists
|
21
|
+
out = `pg_dump -V`
|
22
|
+
/pg_dump \(PostgreSQL\)/ =~ out or raise DependencyRequiredError, 'pg_dump is required'
|
23
|
+
end
|
24
|
+
|
25
|
+
def validate_db_exists(database)
|
26
|
+
PG.connect(dbname: database)
|
27
|
+
rescue PG::ConnectionBad => e
|
28
|
+
raise DatabaseDoesNotExistError, e.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def execute_dump_command
|
32
|
+
`pg_dump -Fc --file #{dump.pathname} #{dump.database}`
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class PgExport
|
2
|
+
class RemoveOldDumps
|
3
|
+
include Logging
|
4
|
+
|
5
|
+
def initialize(dump, keep_dumps_count)
|
6
|
+
@dump = dump
|
7
|
+
@keep_dumps_count = keep_dumps_count
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
files.sort.reverse.drop(keep_dumps_count).each do |filename|
|
12
|
+
File.delete("#{dump.dirname}/#{filename}")
|
13
|
+
logger.info "Remove file #{filename}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_reader :dump, :keep_dumps_count
|
20
|
+
|
21
|
+
def files
|
22
|
+
Dir.entries(dump.dirname).grep(dump.regexp)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class PgExport
|
2
|
+
class RemoveOldDumpsFromFtp
|
3
|
+
include Logging
|
4
|
+
|
5
|
+
def initialize(dump, ftp_service, keep_dumps)
|
6
|
+
@dump = dump
|
7
|
+
@ftp_service = ftp_service
|
8
|
+
@keep_dumps = keep_dumps
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
ftp_service.list(dump.ftp_regexp).drop(keep_dumps).each do |filename|
|
13
|
+
ftp_service.delete(filename)
|
14
|
+
logger.info "Remove file #{filename} from FTP"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
attr_accessor :dump, :ftp_service, :keep_dumps
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class PgExport
|
2
|
+
class SendDumpToFtp
|
3
|
+
include Logging
|
4
|
+
|
5
|
+
def initialize(dump, ftp_service)
|
6
|
+
@dump = dump
|
7
|
+
@ftp_service = ftp_service
|
8
|
+
end
|
9
|
+
|
10
|
+
def call
|
11
|
+
ftp_service.upload_file(dump.pathname_gz)
|
12
|
+
logger.info "Export #{dump.basename_gz} to FTP"
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
attr_reader :dump, :ftp_service
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class PgExport
|
2
|
+
class Configuration
|
3
|
+
DEFAULTS = {
|
4
|
+
database: nil,
|
5
|
+
dumpfile_dir: ENV['DUMPFILE_DIR'] || 'tmp/dumps',
|
6
|
+
keep_dumps: ENV['KEEP_DUMPS'] || 10,
|
7
|
+
keep_ftp_dumps: ENV['KEEP_FTP_DUMPS'] || 10,
|
8
|
+
ftp_host: ENV['BACKUP_FTP_HOST'],
|
9
|
+
ftp_user: ENV['BACKUP_FTP_USER'],
|
10
|
+
ftp_password: ENV['BACKUP_FTP_PASSWORD']
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
attr_accessor *DEFAULTS.keys
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
DEFAULTS.each_pair do |key, value|
|
17
|
+
send("#{key}=", value)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def validate
|
22
|
+
DEFAULTS.keys.each do |field|
|
23
|
+
raise InvalidConfigurationError, "Field #{field} is required" if send(field).nil?
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def ftp_params
|
28
|
+
{
|
29
|
+
host: ftp_host,
|
30
|
+
user: ftp_user,
|
31
|
+
password: ftp_password
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
class PgExport
|
2
|
+
class Dump
|
3
|
+
TIMESTAMP_REGEX = '[0-9]{8}_[0-9]{6}'.freeze
|
4
|
+
|
5
|
+
attr_reader :database, :dir, :dirname, :basename, :basename_gz, :regexp, :ftp_regexp, :pathname, :pathname_gz
|
6
|
+
|
7
|
+
def initialize(a_database, a_dir)
|
8
|
+
@database = a_database
|
9
|
+
@dir = a_dir
|
10
|
+
@dirname = absolute_path(dir)
|
11
|
+
@basename = append_to_database(Time.now.strftime('%Y%m%d_%H%M%S'))
|
12
|
+
@basename_gz = basename + '.gz'
|
13
|
+
@regexp = Regexp.new(append_to_database(TIMESTAMP_REGEX))
|
14
|
+
@ftp_regexp = append_to_database('*')
|
15
|
+
@pathname = [dirname, basename].join('/')
|
16
|
+
@pathname_gz = pathname + '.gz'
|
17
|
+
create_dir_if_necessary
|
18
|
+
end
|
19
|
+
|
20
|
+
def size
|
21
|
+
file_size(pathname)
|
22
|
+
end
|
23
|
+
|
24
|
+
def size_gz
|
25
|
+
file_size(pathname_gz)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def file_size(path)
|
31
|
+
return unless File.exist?(path)
|
32
|
+
bytes2human(File.size(path))
|
33
|
+
end
|
34
|
+
|
35
|
+
def bytes2human(bytes)
|
36
|
+
{
|
37
|
+
'B' => 1024,
|
38
|
+
'kB' => 1024 * 1024,
|
39
|
+
'MB' => 1024 * 1024 * 1024,
|
40
|
+
'GB' => 1024 * 1024 * 1024 * 1024,
|
41
|
+
'TB' => 1024 * 1024 * 1024 * 1024 * 1024
|
42
|
+
}.each_pair { |e, s| return "#{(bytes.to_f / (s / 1024)).round(2)}#{e}" if bytes < s }
|
43
|
+
end
|
44
|
+
|
45
|
+
def create_dir_if_necessary
|
46
|
+
FileUtils.mkdir_p(dirname)
|
47
|
+
end
|
48
|
+
|
49
|
+
def append_to_database(segment)
|
50
|
+
[database, segment].join('_')
|
51
|
+
end
|
52
|
+
|
53
|
+
def absolute_path(dir)
|
54
|
+
if Pathname.new(dir).absolute?
|
55
|
+
dir
|
56
|
+
else
|
57
|
+
[Pathname.pwd, dir].join('/')
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class PgExport
|
2
|
+
class FtpService
|
3
|
+
class Connection
|
4
|
+
include Logging
|
5
|
+
|
6
|
+
attr_reader :ftp
|
7
|
+
|
8
|
+
def initialize(host:, user:, password:)
|
9
|
+
@host, @user, @password = host, user, password
|
10
|
+
open
|
11
|
+
end
|
12
|
+
|
13
|
+
def open
|
14
|
+
@ftp = Net::FTP.new(host, user, password)
|
15
|
+
@ftp.passive = true
|
16
|
+
logger.info "Connect to #{host}"
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
def close
|
21
|
+
@ftp.close
|
22
|
+
logger.info 'Close FTP'
|
23
|
+
self
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
attr_reader :host, :user, :password
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
class PgExport
|
2
|
+
class FtpService
|
3
|
+
def initialize(params)
|
4
|
+
connection = Connection.new(params)
|
5
|
+
@ftp = connection.ftp
|
6
|
+
ObjectSpace.define_finalizer(self, proc { connection.close })
|
7
|
+
end
|
8
|
+
|
9
|
+
def list(regexp)
|
10
|
+
ftp.list(regexp).map { |item| item.split(' ').last }.sort.reverse
|
11
|
+
end
|
12
|
+
|
13
|
+
def delete(filename)
|
14
|
+
ftp.delete(filename)
|
15
|
+
end
|
16
|
+
|
17
|
+
def upload_file(path)
|
18
|
+
ftp.putbinaryfile(path.to_s)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
attr_reader :ftp
|
24
|
+
end
|
25
|
+
end
|
data/lib/pg_export.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'logger'
|
2
|
+
require 'pathname'
|
3
|
+
require 'fileutils'
|
4
|
+
require 'zlib'
|
5
|
+
require 'net/ftp'
|
6
|
+
|
7
|
+
require 'pg'
|
8
|
+
|
9
|
+
require 'pg_export/version'
|
10
|
+
require 'pg_export/logging'
|
11
|
+
require 'pg_export/configuration'
|
12
|
+
require 'pg_export/errors'
|
13
|
+
require 'pg_export/dump'
|
14
|
+
require 'pg_export/actions'
|
15
|
+
require 'pg_export/ftp_service'
|
16
|
+
require 'pg_export/ftp_service/connection'
|
17
|
+
|
18
|
+
class PgExport
|
19
|
+
include Logging
|
20
|
+
|
21
|
+
def initialize
|
22
|
+
@config = Configuration.new
|
23
|
+
yield config if block_given?
|
24
|
+
config.validate
|
25
|
+
@dump = Dump.new(config.database, config.dumpfile_dir)
|
26
|
+
end
|
27
|
+
|
28
|
+
def initialize_ftp_service
|
29
|
+
self.ftp_service = FtpService.new(config.ftp_params)
|
30
|
+
end
|
31
|
+
|
32
|
+
def call
|
33
|
+
t = []
|
34
|
+
t << Thread.new { perform_local_job }
|
35
|
+
t << Thread.new { initialize_ftp_service }
|
36
|
+
t.each(&:join)
|
37
|
+
perform_ftp_job
|
38
|
+
self
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
|
43
|
+
attr_reader :config, :dump
|
44
|
+
attr_accessor :ftp_service
|
45
|
+
|
46
|
+
def perform_local_job
|
47
|
+
CreateDump.new(dump).call
|
48
|
+
CompressDump.new(dump).call
|
49
|
+
RemoveOldDumps.new(dump, config.keep_dumps).call
|
50
|
+
end
|
51
|
+
|
52
|
+
def perform_ftp_job
|
53
|
+
SendDumpToFtp.new(dump, ftp_service).call
|
54
|
+
RemoveOldDumpsFromFtp.new(dump, ftp_service, config.keep_ftp_dumps).call
|
55
|
+
end
|
56
|
+
end
|
data/pg_export.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'pg_export/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'pg_export'
|
8
|
+
spec.version = PgExport::VERSION
|
9
|
+
spec.authors = ['Krzysztof Maicher']
|
10
|
+
spec.email = ['krzysztof.maicher@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'CLI for exporting Rails Postgres database dump to FTP'
|
13
|
+
spec.description = "CLI for exporting Rails Postgres database dump to FTP.\
|
14
|
+
Can be used for backups or synchronizing databases between production and development environments"
|
15
|
+
spec.homepage = 'https://github.com/maicher/pg_export'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
19
|
+
spec.executables = ['pg_export']
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
spec.required_ruby_version = '>= 2.1.0'
|
22
|
+
|
23
|
+
spec.add_dependency 'pg', '>= 0.16'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
26
|
+
spec.add_development_dependency 'rubocop', '~> 0.44'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~>3.4'
|
29
|
+
spec.add_development_dependency 'pry'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,158 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pg_export
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Krzysztof Maicher
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.16'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubocop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.44'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.44'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: CLI for exporting Rails Postgres database dump to FTP. Can
|
98
|
+
be used for backups or synchronizing databases between production and development
|
99
|
+
environments
|
100
|
+
email:
|
101
|
+
- krzysztof.maicher@gmail.com
|
102
|
+
executables:
|
103
|
+
- pg_export
|
104
|
+
extensions: []
|
105
|
+
extra_rdoc_files: []
|
106
|
+
files:
|
107
|
+
- ".gitignore"
|
108
|
+
- ".rspec"
|
109
|
+
- ".rubocop.yml"
|
110
|
+
- ".travis.yml"
|
111
|
+
- CODE_OF_CONDUCT.md
|
112
|
+
- Gemfile
|
113
|
+
- LICENSE.txt
|
114
|
+
- README.md
|
115
|
+
- Rakefile
|
116
|
+
- bin/console
|
117
|
+
- bin/pg_export
|
118
|
+
- bin/setup
|
119
|
+
- lib/pg_export.rb
|
120
|
+
- lib/pg_export/actions.rb
|
121
|
+
- lib/pg_export/actions/compress_dump.rb
|
122
|
+
- lib/pg_export/actions/create_dump.rb
|
123
|
+
- lib/pg_export/actions/remove_old_dumps.rb
|
124
|
+
- lib/pg_export/actions/remove_old_dumps_from_ftp.rb
|
125
|
+
- lib/pg_export/actions/send_dump_to_ftp.rb
|
126
|
+
- lib/pg_export/configuration.rb
|
127
|
+
- lib/pg_export/dump.rb
|
128
|
+
- lib/pg_export/errors.rb
|
129
|
+
- lib/pg_export/ftp_service.rb
|
130
|
+
- lib/pg_export/ftp_service/connection.rb
|
131
|
+
- lib/pg_export/logging.rb
|
132
|
+
- lib/pg_export/version.rb
|
133
|
+
- pg_export.gemspec
|
134
|
+
homepage: https://github.com/maicher/pg_export
|
135
|
+
licenses:
|
136
|
+
- MIT
|
137
|
+
metadata: {}
|
138
|
+
post_install_message:
|
139
|
+
rdoc_options: []
|
140
|
+
require_paths:
|
141
|
+
- lib
|
142
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: 2.1.0
|
147
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
requirements: []
|
153
|
+
rubyforge_project:
|
154
|
+
rubygems_version: 2.4.8
|
155
|
+
signing_key:
|
156
|
+
specification_version: 4
|
157
|
+
summary: CLI for exporting Rails Postgres database dump to FTP
|
158
|
+
test_files: []
|