easy_backup 0.0.6 → 0.0.7
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/README.md +60 -35
- data/easy_backup.gemspec +13 -15
- data/lib/easy_backup/configuration.rb +8 -14
- data/lib/easy_backup/{adapter → resources}/file_system.rb +30 -30
- data/lib/easy_backup/resources/postgres.rb +86 -0
- data/lib/easy_backup/{adapter → resources}/sftp.rb +14 -10
- data/lib/easy_backup/specification.rb +36 -0
- data/lib/easy_backup/version.rb +1 -1
- data/lib/easy_backup.rb +14 -43
- data/spec/file_system_spec.rb +136 -0
- data/spec/postgres_spec.rb +91 -0
- data/spec/scheduler_spec.rb +26 -0
- data/spec/sftp_spec.rb +47 -0
- data/spec/spec_helper.rb +17 -3
- data/spec/specification_spec.rb +40 -0
- data/spec/support/helpers/fake_storage.rb +11 -0
- data/spec/support/helpers/file_helper.rb +54 -0
- data/spec/support/helpers/{postgre_sql_helper.rb → postgres_helper.rb} +1 -1
- metadata +32 -68
- data/lib/easy_backup/adapter/db/postgre_sql.rb +0 -90
- data/lib/easy_backup/base.rb +0 -46
- data/lib/easy_backup/frequency.rb +0 -13
- data/lib/easy_backup/runner.rb +0 -9
- data/spec/execution/runner_spec.rb +0 -131
- data/spec/execution/scheduled_spec.rb +0 -29
- data/spec/files/config/backup_config.rb +0 -20
- data/spec/specification/configuration_spec.rb +0 -100
- data/spec/specification/easy_backup_spec.rb +0 -34
- data/spec/specification/file_system_adapter_spec.rb +0 -39
- data/spec/specification/postgre_sql_adapter_spec.rb +0 -36
- data/spec/specification/sftp_adapter_spec.rb +0 -18
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Postgres do
|
4
|
+
|
5
|
+
context 'Specification' do
|
6
|
+
|
7
|
+
it 'Database parameters' do
|
8
|
+
db = Postgres.new
|
9
|
+
db.host '192.168.0.1'
|
10
|
+
db.port 1234
|
11
|
+
db.database 'test_db'
|
12
|
+
db.username 'user'
|
13
|
+
db.password 'password'
|
14
|
+
db.dump_file 'db.sql'
|
15
|
+
db.zip
|
16
|
+
|
17
|
+
db.host.should eq '192.168.0.1'
|
18
|
+
db.port.should eq 1234
|
19
|
+
db.database.should eq 'test_db'
|
20
|
+
db.username.should eq 'user'
|
21
|
+
db.password.should eq 'password'
|
22
|
+
db.dump_file.should eq "#{EasyBackup.configuration.tmp_path}/pg_dump/db.sql"
|
23
|
+
db.zip_file.should eq "#{EasyBackup.configuration.tmp_path}/pg_dump/db.zip"
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'Database default parameters' do
|
27
|
+
db = Postgres.new
|
28
|
+
|
29
|
+
db.host.should eq 'localhost'
|
30
|
+
db.port.should eq 5432
|
31
|
+
db.database.should be_nil
|
32
|
+
db.username.should eq 'postgres'
|
33
|
+
db.password.should be_nil
|
34
|
+
db.dump_file.should eq "#{EasyBackup.configuration.tmp_path}/pg_dump/_#{Time.now.strftime('%Y%m%d%H%M%S')}.sql"
|
35
|
+
db.zip_file.should be_nil
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'Send' do
|
41
|
+
|
42
|
+
before :all do
|
43
|
+
PostgresHelper.create_db
|
44
|
+
end
|
45
|
+
|
46
|
+
after :all do
|
47
|
+
PostgresHelper.drop_db
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'Dump to sql file' do
|
51
|
+
db = PostgresHelper.configuration
|
52
|
+
|
53
|
+
pg = Postgres.new do
|
54
|
+
host db['host']
|
55
|
+
database db['database']
|
56
|
+
port db['port']
|
57
|
+
username db['username']
|
58
|
+
password db['password']
|
59
|
+
dump_file 'backup.sql'
|
60
|
+
end
|
61
|
+
|
62
|
+
storage = FakeStorage.new
|
63
|
+
|
64
|
+
pg.send_to storage
|
65
|
+
|
66
|
+
storage.received.should eq ["#{EasyBackup.configuration.tmp_path}/pg_dump/backup.sql"]
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'Zip after dump' do
|
70
|
+
db = PostgresHelper.configuration
|
71
|
+
|
72
|
+
pg = Postgres.new do
|
73
|
+
host db['host']
|
74
|
+
database db['database']
|
75
|
+
port db['port']
|
76
|
+
username db['username']
|
77
|
+
password db['password']
|
78
|
+
dump_file 'backup.sql'
|
79
|
+
zip
|
80
|
+
end
|
81
|
+
|
82
|
+
storage = FakeStorage.new
|
83
|
+
|
84
|
+
pg.send_to storage
|
85
|
+
|
86
|
+
storage.received.should eq ["#{EasyBackup.configuration.tmp_path}/pg_dump/backup.zip"]
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Scheduler' do
|
4
|
+
|
5
|
+
it 'Schedule specification' do
|
6
|
+
sample_file = "#{DATA_PATH}/sample.json"
|
7
|
+
target_folder = file_helper.create_temp_folder
|
8
|
+
|
9
|
+
Specification.new do
|
10
|
+
save FileSystem do
|
11
|
+
file sample_file
|
12
|
+
end
|
13
|
+
|
14
|
+
into FileSystem do
|
15
|
+
folder lambda { "#{target_folder}/#{Time.now.to_f}" }
|
16
|
+
end
|
17
|
+
|
18
|
+
schedule :every, 0.6
|
19
|
+
end
|
20
|
+
|
21
|
+
sleep(1)
|
22
|
+
|
23
|
+
Dir.glob("#{target_folder}/*/").count.should eq 1
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
data/spec/sftp_spec.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SFTP do
|
4
|
+
|
5
|
+
context 'Specification' do
|
6
|
+
|
7
|
+
it 'Sftp parameters' do
|
8
|
+
sftp = SFTP.new do
|
9
|
+
host '192.168.0.1'
|
10
|
+
username 'user'
|
11
|
+
password 'password'
|
12
|
+
folder '/backup'
|
13
|
+
end
|
14
|
+
|
15
|
+
sftp.host.should eq '192.168.0.1'
|
16
|
+
sftp.username.should eq 'user'
|
17
|
+
sftp.password.should eq 'password'
|
18
|
+
sftp.folder.should eq '/backup'
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'Save' do
|
24
|
+
|
25
|
+
it 'File to sftp' do
|
26
|
+
config = ConfigHelper.get 'sftp'
|
27
|
+
backup_path = "tmp/#{Time.now.strftime('%Y%m%d%H%M%S%L')}"
|
28
|
+
source_file = "#{DATA_PATH}/txt/1/text1.txt"
|
29
|
+
|
30
|
+
sftp = SFTP.new do
|
31
|
+
host config['host']
|
32
|
+
username config['username']
|
33
|
+
password config['password']
|
34
|
+
folder backup_path
|
35
|
+
end
|
36
|
+
|
37
|
+
sftp.save source_file
|
38
|
+
|
39
|
+
Net::SFTP.start(config['host'], config['username'], password: config['password']) do |sftp|
|
40
|
+
sftp.dir.glob(backup_path, 'text1.txt').should have(1).item
|
41
|
+
sftp.file.open("#{backup_path}/text1.txt", 'r') { |f| f.gets.should eq 'Text file 1' }
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,12 +1,26 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'bundler/setup'
|
3
1
|
require 'easy_backup'
|
2
|
+
require 'sequel'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
include EasyBackup
|
6
|
+
include EasyBackup::Resources
|
4
7
|
|
5
8
|
DATA_PATH = "#{File.dirname(__FILE__)}/files/data"
|
6
|
-
BACKUP_PATH = "#{ENV['tmp'].gsub('\\', '/')}/easy_backup/backups"
|
7
9
|
|
8
10
|
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
11
|
|
10
12
|
RSpec.configure do |config|
|
13
|
+
config.include FileHelper::Rspec
|
14
|
+
|
15
|
+
config.before :each do
|
16
|
+
initialize_file_helper
|
17
|
+
end
|
18
|
+
|
19
|
+
config.after :each do
|
20
|
+
file_helper.remove_temp_folders
|
21
|
+
end
|
11
22
|
|
23
|
+
config.after :all do
|
24
|
+
FileUtils.rm_rf EasyBackup.configuration.tmp_path
|
25
|
+
end
|
12
26
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Specification do
|
4
|
+
|
5
|
+
context 'Configuration' do
|
6
|
+
|
7
|
+
it 'Validate source resource' do
|
8
|
+
expect{Specification.new { save Object }}.should raise_exception
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'Validate target resource' do
|
12
|
+
expect{Specification.new { into Object }}.should raise_exception
|
13
|
+
end
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'Running' do
|
18
|
+
|
19
|
+
it 'Save file into folder' do
|
20
|
+
sample_file = "#{DATA_PATH}/sample.json"
|
21
|
+
target_folder = file_helper.create_temp_folder
|
22
|
+
|
23
|
+
spec = Specification.new do
|
24
|
+
save FileSystem do
|
25
|
+
file sample_file
|
26
|
+
end
|
27
|
+
|
28
|
+
into FileSystem do
|
29
|
+
folder target_folder
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
spec.run
|
34
|
+
|
35
|
+
File.exists?("#{target_folder}/#{File.basename(sample_file)}").should be_true
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
|
3
|
+
class FileHelper
|
4
|
+
|
5
|
+
module Rspec
|
6
|
+
def initialize_file_helper
|
7
|
+
@file_helper = FileHelper.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def file_helper
|
11
|
+
@file_helper
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def initialize
|
16
|
+
@temp_folders = []
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_temp_folder
|
20
|
+
folder_name = "#{ENV['TMP'].gsub('\\', '/')}/#{timestamp}"
|
21
|
+
@temp_folders << folder_name
|
22
|
+
Dir.mkdir folder_name
|
23
|
+
folder_name
|
24
|
+
end
|
25
|
+
|
26
|
+
def remove_temp_folders
|
27
|
+
@temp_folders.each do |folder_name|
|
28
|
+
FileUtils.rm_rf folder_name
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def create_temp_file(folder, content)
|
33
|
+
file_name = "#{folder}/file #{timestamp}.txt"
|
34
|
+
write_file file_name, content
|
35
|
+
file_name
|
36
|
+
end
|
37
|
+
|
38
|
+
def write_file(file_name, content)
|
39
|
+
Dir.mkdir File.dirname(file_name) unless Dir.exist? File.dirname(file_name)
|
40
|
+
File.open(file_name, 'w') { |f| f.puts content }
|
41
|
+
end
|
42
|
+
|
43
|
+
def read_file(file_name)
|
44
|
+
File.open(file_name, 'r') { |f| f.readlines.join("\n").strip }
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
|
49
|
+
def timestamp
|
50
|
+
sleep(0.01)
|
51
|
+
(Time.now.to_f * 1000).to_i
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_backup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rubyzip
|
16
|
-
requirement: &
|
16
|
+
requirement: &21699300 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *21699300
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: net-sftp
|
27
|
-
requirement: &
|
27
|
+
requirement: &21698880 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,32 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *21698880
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
|
-
name:
|
38
|
-
requirement: &
|
39
|
-
none: false
|
40
|
-
requirements:
|
41
|
-
- - ! '>='
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
version: 3.0.0
|
44
|
-
type: :runtime
|
45
|
-
prerelease: false
|
46
|
-
version_requirements: *21310524
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: i18n
|
49
|
-
requirement: &21310056 !ruby/object:Gem::Requirement
|
50
|
-
none: false
|
51
|
-
requirements:
|
52
|
-
- - ! '>='
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '0'
|
55
|
-
type: :runtime
|
56
|
-
prerelease: false
|
57
|
-
version_requirements: *21310056
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: chronic
|
60
|
-
requirement: &21309588 !ruby/object:Gem::Requirement
|
37
|
+
name: rufus-scheduler
|
38
|
+
requirement: &21698568 !ruby/object:Gem::Requirement
|
61
39
|
none: false
|
62
40
|
requirements:
|
63
41
|
- - ! '>='
|
@@ -65,32 +43,21 @@ dependencies:
|
|
65
43
|
version: '0'
|
66
44
|
type: :runtime
|
67
45
|
prerelease: false
|
68
|
-
version_requirements: *
|
46
|
+
version_requirements: *21698568
|
69
47
|
- !ruby/object:Gem::Dependency
|
70
48
|
name: json
|
71
|
-
requirement: &
|
49
|
+
requirement: &21891084 !ruby/object:Gem::Requirement
|
72
50
|
none: false
|
73
51
|
requirements:
|
74
52
|
- - ! '>='
|
75
53
|
- !ruby/object:Gem::Version
|
76
54
|
version: '0'
|
77
|
-
type: :
|
78
|
-
prerelease: false
|
79
|
-
version_requirements: *21309324
|
80
|
-
- !ruby/object:Gem::Dependency
|
81
|
-
name: rufus-scheduler
|
82
|
-
requirement: &21308640 !ruby/object:Gem::Requirement
|
83
|
-
none: false
|
84
|
-
requirements:
|
85
|
-
- - ! '>='
|
86
|
-
- !ruby/object:Gem::Version
|
87
|
-
version: '0'
|
88
|
-
type: :runtime
|
55
|
+
type: :development
|
89
56
|
prerelease: false
|
90
|
-
version_requirements: *
|
57
|
+
version_requirements: *21891084
|
91
58
|
- !ruby/object:Gem::Dependency
|
92
59
|
name: sequel
|
93
|
-
requirement: &
|
60
|
+
requirement: &21890436 !ruby/object:Gem::Requirement
|
94
61
|
none: false
|
95
62
|
requirements:
|
96
63
|
- - ! '>='
|
@@ -98,10 +65,10 @@ dependencies:
|
|
98
65
|
version: '0'
|
99
66
|
type: :development
|
100
67
|
prerelease: false
|
101
|
-
version_requirements: *
|
68
|
+
version_requirements: *21890436
|
102
69
|
- !ruby/object:Gem::Dependency
|
103
70
|
name: pg
|
104
|
-
requirement: &
|
71
|
+
requirement: &21889392 !ruby/object:Gem::Requirement
|
105
72
|
none: false
|
106
73
|
requirements:
|
107
74
|
- - ! '>='
|
@@ -109,10 +76,10 @@ dependencies:
|
|
109
76
|
version: '0'
|
110
77
|
type: :development
|
111
78
|
prerelease: false
|
112
|
-
version_requirements: *
|
79
|
+
version_requirements: *21889392
|
113
80
|
- !ruby/object:Gem::Dependency
|
114
81
|
name: rspec
|
115
|
-
requirement: &
|
82
|
+
requirement: &21888984 !ruby/object:Gem::Requirement
|
116
83
|
none: false
|
117
84
|
requirements:
|
118
85
|
- - ! '>='
|
@@ -120,8 +87,8 @@ dependencies:
|
|
120
87
|
version: '0'
|
121
88
|
type: :development
|
122
89
|
prerelease: false
|
123
|
-
version_requirements: *
|
124
|
-
description:
|
90
|
+
version_requirements: *21888984
|
91
|
+
description: Simple DSL to program backups
|
125
92
|
email:
|
126
93
|
- gabynaiman@gmail.com
|
127
94
|
executables: []
|
@@ -135,30 +102,27 @@ files:
|
|
135
102
|
- Rakefile
|
136
103
|
- easy_backup.gemspec
|
137
104
|
- lib/easy_backup.rb
|
138
|
-
- lib/easy_backup/adapter/db/postgre_sql.rb
|
139
|
-
- lib/easy_backup/adapter/file_system.rb
|
140
|
-
- lib/easy_backup/adapter/sftp.rb
|
141
|
-
- lib/easy_backup/base.rb
|
142
105
|
- lib/easy_backup/configuration.rb
|
143
106
|
- lib/easy_backup/extension/net_sftp_session.rb
|
144
|
-
- lib/easy_backup/
|
145
|
-
- lib/easy_backup/
|
107
|
+
- lib/easy_backup/resources/file_system.rb
|
108
|
+
- lib/easy_backup/resources/postgres.rb
|
109
|
+
- lib/easy_backup/resources/sftp.rb
|
110
|
+
- lib/easy_backup/specification.rb
|
146
111
|
- lib/easy_backup/version.rb
|
147
112
|
- spec/config.yaml
|
148
|
-
- spec/
|
149
|
-
- spec/execution/scheduled_spec.rb
|
150
|
-
- spec/files/config/backup_config.rb
|
113
|
+
- spec/file_system_spec.rb
|
151
114
|
- spec/files/data/sample.json
|
152
115
|
- spec/files/data/txt/1/text1.txt
|
153
116
|
- spec/files/data/txt/2/text2.txt
|
117
|
+
- spec/postgres_spec.rb
|
118
|
+
- spec/scheduler_spec.rb
|
119
|
+
- spec/sftp_spec.rb
|
154
120
|
- spec/spec_helper.rb
|
155
|
-
- spec/
|
156
|
-
- spec/specification/easy_backup_spec.rb
|
157
|
-
- spec/specification/file_system_adapter_spec.rb
|
158
|
-
- spec/specification/postgre_sql_adapter_spec.rb
|
159
|
-
- spec/specification/sftp_adapter_spec.rb
|
121
|
+
- spec/specification_spec.rb
|
160
122
|
- spec/support/helpers/config_helper.rb
|
161
|
-
- spec/support/helpers/
|
123
|
+
- spec/support/helpers/fake_storage.rb
|
124
|
+
- spec/support/helpers/file_helper.rb
|
125
|
+
- spec/support/helpers/postgres_helper.rb
|
162
126
|
homepage: https://github.com/gabynaiman/easy_backup
|
163
127
|
licenses: []
|
164
128
|
post_install_message:
|
@@ -182,5 +146,5 @@ rubyforge_project: easy_backup
|
|
182
146
|
rubygems_version: 1.8.16
|
183
147
|
signing_key:
|
184
148
|
specification_version: 3
|
185
|
-
summary:
|
149
|
+
summary: Simple DSL to program backups
|
186
150
|
test_files: []
|
@@ -1,90 +0,0 @@
|
|
1
|
-
require 'open3'
|
2
|
-
require 'zip/zip'
|
3
|
-
|
4
|
-
include Zip
|
5
|
-
|
6
|
-
module EasyBackup
|
7
|
-
module Adapter
|
8
|
-
module Db
|
9
|
-
class PostgreSQL
|
10
|
-
|
11
|
-
def host(host=nil)
|
12
|
-
host ? @host = host : (@host || 'localhost')
|
13
|
-
end
|
14
|
-
|
15
|
-
def database(database=nil)
|
16
|
-
database ? @database = database : @database
|
17
|
-
end
|
18
|
-
|
19
|
-
def username(username=nil)
|
20
|
-
username ? @username = username : (@username || 'postgres')
|
21
|
-
end
|
22
|
-
|
23
|
-
def password(password=nil)
|
24
|
-
password ? @password = password : @password
|
25
|
-
end
|
26
|
-
|
27
|
-
def port(port=nil)
|
28
|
-
port ? @port = port : (@port || 5432)
|
29
|
-
end
|
30
|
-
|
31
|
-
def dump_file(file_name=nil)
|
32
|
-
if file_name
|
33
|
-
@dump_file = file_name
|
34
|
-
else
|
35
|
-
if @dump_file
|
36
|
-
@dump_file.is_a?(Proc) ? @dump_file.call : @dump_file
|
37
|
-
else
|
38
|
-
"#{database}_#{Time.now.strftime('%Y%m%d%H%M%S')}.sql"
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def zip_file(file_name=nil)
|
44
|
-
if file_name
|
45
|
-
@zip_file = file_name
|
46
|
-
else
|
47
|
-
@zip_file.is_a?(Proc) ? @zip_file.call : @zip_file
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def zip
|
52
|
-
zip_file lambda { "#{File.basename(dump_file, '.*')}.zip" }
|
53
|
-
end
|
54
|
-
|
55
|
-
def send_to(storages)
|
56
|
-
dump_file_name = path_to(dump_file)
|
57
|
-
zip_file_name = path_to(zip_file)
|
58
|
-
|
59
|
-
FileUtils.mkpath File.dirname(dump_file_name) unless Dir.exist? File.dirname(dump_file_name)
|
60
|
-
|
61
|
-
EasyBackup.logger.info "[PostgreSQL] Dump postgres://#{username}:*****@#{host}:#{port}/#{database}\n#{' '*15}to #{dump_file_name}"
|
62
|
-
|
63
|
-
Open3.popen3 "pg_dump -h #{host} -p #{port} -U #{username} #{database} > #{dump_file_name}" do |i, o, e, t|
|
64
|
-
if t.value.success?
|
65
|
-
if zip_file
|
66
|
-
EasyBackup.logger.info "#{(' '*14)}zip #{zip_file_name}"
|
67
|
-
ZipFile.open(zip_file_name, ZipFile::CREATE) do |zip|
|
68
|
-
zip.add File.basename(dump_file_name), dump_file_name
|
69
|
-
end
|
70
|
-
end
|
71
|
-
storages.each { |s| s.save(zip_file ? zip_file_name : dump_file_name) }
|
72
|
-
else
|
73
|
-
EasyBackup.logger.error "[PostgreSQL] Error: #{e.readlines.join}"
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
FileUtils.rm dump_file_name if File.exist? dump_file_name
|
78
|
-
FileUtils.rm zip_file_name if zip_file && File.exist?(zip_file_name)
|
79
|
-
end
|
80
|
-
|
81
|
-
private
|
82
|
-
|
83
|
-
def path_to(file_name)
|
84
|
-
"#{EasyBackup.tmp_path}/pg_dump/#{file_name}"
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
data/lib/easy_backup/base.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'rufus-scheduler'
|
2
|
-
|
3
|
-
module EasyBackup
|
4
|
-
|
5
|
-
class Base
|
6
|
-
|
7
|
-
def initialize(interval=EasyBackup.interval, &block)
|
8
|
-
@configurations = {}
|
9
|
-
instance_eval &block
|
10
|
-
@scheduler = Rufus::Scheduler.start_new frequency: interval
|
11
|
-
schedule
|
12
|
-
end
|
13
|
-
|
14
|
-
def [](name)
|
15
|
-
@configurations[name]
|
16
|
-
end
|
17
|
-
|
18
|
-
def start
|
19
|
-
@scheduler.join
|
20
|
-
end
|
21
|
-
|
22
|
-
private
|
23
|
-
|
24
|
-
def []=(name, value)
|
25
|
-
@configurations[name] = value
|
26
|
-
end
|
27
|
-
|
28
|
-
def config(name=:default, &block)
|
29
|
-
self[name] = Configuration.new do
|
30
|
-
instance_eval &block
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def schedule
|
35
|
-
@configurations.each do |name, c|
|
36
|
-
c.frequencies.each do |f|
|
37
|
-
@scheduler.every f.interval, first_at: f.from do
|
38
|
-
EasyBackup.logger.info "[EasyBackup] Starting #{name} at #{Time.now}"
|
39
|
-
Runner.run c
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
end
|
46
|
-
end
|
@@ -1,13 +0,0 @@
|
|
1
|
-
require 'chronic'
|
2
|
-
|
3
|
-
module EasyBackup
|
4
|
-
class Frequency
|
5
|
-
attr_accessor :interval, :from
|
6
|
-
|
7
|
-
def initialize(interval, options={})
|
8
|
-
@interval = interval
|
9
|
-
@from = options[:from].is_a?(String) ? Chronic.parse(options[:from]) : options[:from]
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|