pg_export 0.1.3 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -1
- data/bin/pg_export +16 -1
- data/lib/pg_export/actions/create_dump.rb +2 -2
- data/lib/pg_export/configuration.rb +18 -0
- data/lib/pg_export/version.rb +1 -1
- data/lib/pg_export.rb +21 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 69622bfd641c2024a474d24d95c53e2baf552a2a
|
4
|
+
data.tar.gz: 7123b34af0f29d2b36b9690efb4b64afcdb60130
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 44241da6e0c062993be0990996cefeb784fe1bc9d2df71aa56743f262f123e8f6f8a068ee320e0a1d43922201cab3639e7f3aae46b748fceca38efa2ae1709bf
|
7
|
+
data.tar.gz: f69d004ff3a44aa4e014bf230f78ba9bd91445149736f412ec3ed13b5b1647a81f13f7f6d532c484d67cdce7fc2ee0982fcf482992c8d11a05822afd90edac6b
|
data/README.md
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# PgExport
|
2
2
|
|
3
|
-
CLI for exporting
|
3
|
+
CLI for exporting PostgreSQL database dump to FTP.
|
4
4
|
|
5
5
|
Can be used for backups or synchronizing databases between production and development environments.
|
6
6
|
|
7
|
+
Configurable by ENV variables and (partially) by command line options.
|
8
|
+
|
7
9
|
## Dependencies
|
8
10
|
|
9
11
|
* Ruby >= 2.1
|
@@ -31,11 +33,57 @@ Or install it yourself as:
|
|
31
33
|
$ pg_export -h
|
32
34
|
|
33
35
|
Usage: pg_export [options]
|
36
|
+
-c, --configuration Prints the configuration
|
37
|
+
-f, --ftp Tries connecting to FTP to verify the connection
|
34
38
|
-d, --database DATABASE [Required] Name of the database to export
|
35
39
|
-k, --keep [KEEP] [Optional] Number of dump files to keep locally and on FTP (default: 10)
|
36
40
|
-t, --timestamped [Optional] Enables log messages with timestamps
|
37
41
|
-h, --help Show this message
|
38
42
|
|
43
|
+
## How to start
|
44
|
+
|
45
|
+
__Step 1.__ Prepare FTP account and put configuration into env variables.
|
46
|
+
|
47
|
+
# /etc/enviranment
|
48
|
+
BACKUP_FTP_HOST="yourftp.example.com"
|
49
|
+
BACKUP_FTP_USER="user"
|
50
|
+
BACKUP_FTP_PASSWORD="password"
|
51
|
+
|
52
|
+
Password cannot include `#` sign, [more info](http://serverfault.com/questions/539730/environment-variable-in-etc-environment-with-pound-hash-sign-in-the-value).
|
53
|
+
|
54
|
+
__Step 2.__ Configure other variables (optional).
|
55
|
+
|
56
|
+
# /etc/environment
|
57
|
+
DUMPFILE_DIR="/var/dumps" # default: "tmp/dumps"
|
58
|
+
KEEP_DUMPS=3 # default: 10
|
59
|
+
KEEP_FTP_DUMPS=5 # default: 10
|
60
|
+
|
61
|
+
__Step 3.__ Print the configuration to verify if env variables has been loaded.
|
62
|
+
|
63
|
+
$ pg_export --configuration
|
64
|
+
=> database:
|
65
|
+
dumpfile_dir: /var/dumps
|
66
|
+
keep_dumps: 3
|
67
|
+
keep_ftp_dumps: 5
|
68
|
+
ftp_host: yourftp.example.com
|
69
|
+
ftp_user: user
|
70
|
+
ftp_password: pass***
|
71
|
+
|
72
|
+
__Step 4.__ Try connecting to FTP to verify the connection.
|
73
|
+
|
74
|
+
$ pg_export --ftp
|
75
|
+
=> Connect to yourftp.example.com
|
76
|
+
Close FTP
|
77
|
+
|
78
|
+
__Step 5.__ Perform database export.
|
79
|
+
|
80
|
+
$ pg_export -d your_database
|
81
|
+
=> Dump your_database to /var/dumps/your_database_20161020_125747 (892.38kB)
|
82
|
+
Zip dump your_database_20161020_125747.gz (874.61kB)
|
83
|
+
Connect to yourftp.example.com
|
84
|
+
Export your_database_20161020_125747.gz to FTP
|
85
|
+
Close FTP
|
86
|
+
|
39
87
|
## Development
|
40
88
|
|
41
89
|
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.
|
data/bin/pg_export
CHANGED
@@ -23,10 +23,23 @@ option_parser = OptionParser.new do |opts|
|
|
23
23
|
options.timestamped = true
|
24
24
|
end
|
25
25
|
|
26
|
-
opts.
|
26
|
+
opts.on('-h', '--help', 'Show this message') do
|
27
27
|
puts opts
|
28
28
|
exit
|
29
29
|
end
|
30
|
+
|
31
|
+
opts.separator "\nSetting can be verified by running following commands:"
|
32
|
+
|
33
|
+
opts.on('-c', '--configuration', 'Prints the configuration') do
|
34
|
+
puts PgExport::Configuration.new.to_s
|
35
|
+
exit
|
36
|
+
end
|
37
|
+
|
38
|
+
opts.on('-f', '--ftp', 'Tries connecting to FTP to verify the connection') do
|
39
|
+
PgExport::Logging.logger.formatter = LOGS_NORMAL
|
40
|
+
PgExport::FtpService.new(PgExport::Configuration.new.ftp_params)
|
41
|
+
exit
|
42
|
+
end
|
30
43
|
end
|
31
44
|
|
32
45
|
begin
|
@@ -42,4 +55,6 @@ begin
|
|
42
55
|
rescue OptionParser::MissingArgument, PgExport::InvalidConfigurationError => e
|
43
56
|
puts "Error: #{e}"
|
44
57
|
puts option_parser
|
58
|
+
rescue PgExport::DatabaseDoesNotExistError, PgExport::DependencyRequiredError => e
|
59
|
+
puts e
|
45
60
|
end
|
@@ -19,13 +19,13 @@ class PgExport
|
|
19
19
|
|
20
20
|
def validate_pg_dump_exists
|
21
21
|
out = `pg_dump -V`
|
22
|
-
/pg_dump \(PostgreSQL\)/ =~ out or raise DependencyRequiredError, 'pg_dump is required'
|
22
|
+
/pg_dump \(PostgreSQL\)/ =~ out or raise DependencyRequiredError, 'Shell command "pg_dump" is required. Pleas install "pg_dump" and try again.'
|
23
23
|
end
|
24
24
|
|
25
25
|
def validate_db_exists(database)
|
26
26
|
PG.connect(dbname: database)
|
27
27
|
rescue PG::ConnectionBad => e
|
28
|
-
raise DatabaseDoesNotExistError, e
|
28
|
+
raise DatabaseDoesNotExistError, e
|
29
29
|
end
|
30
30
|
|
31
31
|
def execute_dump_command
|
@@ -31,5 +31,23 @@ class PgExport
|
|
31
31
|
password: ftp_password
|
32
32
|
}
|
33
33
|
end
|
34
|
+
|
35
|
+
def to_s
|
36
|
+
DEFAULTS.keys.map(&method(:print_attr))
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def print_attr(key)
|
42
|
+
if key == :ftp_password
|
43
|
+
if send(key)
|
44
|
+
"#{key}: #{send(key)[0..2]}***\n"
|
45
|
+
else
|
46
|
+
"#{key}:\n"
|
47
|
+
end
|
48
|
+
else
|
49
|
+
"#{key}: #{send(key)}\n"
|
50
|
+
end
|
51
|
+
end
|
34
52
|
end
|
35
53
|
end
|
data/lib/pg_export/version.rb
CHANGED
data/lib/pg_export.rb
CHANGED
@@ -22,26 +22,36 @@ class PgExport
|
|
22
22
|
@config = Configuration.new
|
23
23
|
yield config if block_given?
|
24
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
25
|
end
|
31
26
|
|
32
27
|
def call
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
28
|
+
initialize_dump
|
29
|
+
concurrently do |job|
|
30
|
+
job << Thread.new { perform_local_job }
|
31
|
+
job << Thread.new { initialize_ftp_service }
|
32
|
+
end
|
37
33
|
perform_ftp_job
|
38
34
|
self
|
39
35
|
end
|
40
36
|
|
41
37
|
private
|
42
38
|
|
43
|
-
attr_reader :config
|
44
|
-
attr_accessor :ftp_service
|
39
|
+
attr_reader :config
|
40
|
+
attr_accessor :ftp_service, :dump
|
41
|
+
|
42
|
+
def initialize_dump
|
43
|
+
self.dump = Dump.new(config.database, config.dumpfile_dir)
|
44
|
+
end
|
45
|
+
|
46
|
+
def initialize_ftp_service
|
47
|
+
self.ftp_service = FtpService.new(config.ftp_params)
|
48
|
+
end
|
49
|
+
|
50
|
+
def concurrently
|
51
|
+
t = []
|
52
|
+
yield t
|
53
|
+
t.each(&:join)
|
54
|
+
end
|
45
55
|
|
46
56
|
def perform_local_job
|
47
57
|
CreateDump.new(dump).call
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_export
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Krzysztof Maicher
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-10-
|
11
|
+
date: 2016-10-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|