filbert 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -0
- data/lib/filbert/db_config.rb +24 -0
- data/lib/filbert/task.rb +52 -7
- data/lib/filbert/version.rb +1 -1
- metadata +3 -2
data/README.md
CHANGED
@@ -8,11 +8,22 @@ Filbert downloads a backup of a follower database for a given heroku application
|
|
8
8
|
|
9
9
|
And reload your path (e.g. `rbenv rehash`). Then run
|
10
10
|
|
11
|
+
heroku login
|
12
|
+
|
13
|
+
to get access to the app you are planning to backup.
|
11
14
|
|
12
15
|
## Usage
|
13
16
|
|
14
17
|
filbert backup --app your-heroku-appname
|
15
18
|
|
19
|
+
You can add it as a cron task if you want
|
20
|
+
|
21
|
+
crontab -e
|
22
|
+
|
23
|
+
# Then put this in there to run every 15 minutes.
|
24
|
+
# 'man 5 crontab' for more examples
|
25
|
+
*/15 * * * * bash -lc "filbert backup --app heroku-app-name"
|
26
|
+
|
16
27
|
## Contributing
|
17
28
|
|
18
29
|
1. Fork it
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Filbert
|
2
|
+
class DbConfig
|
3
|
+
def initialize(config_path, env)
|
4
|
+
@config_path = config_path
|
5
|
+
@env = env || ENV['RAILS_ENV'] || 'development'
|
6
|
+
end
|
7
|
+
|
8
|
+
def username
|
9
|
+
config['username']
|
10
|
+
end
|
11
|
+
|
12
|
+
def database
|
13
|
+
config['database']
|
14
|
+
end
|
15
|
+
|
16
|
+
def password
|
17
|
+
config['password']
|
18
|
+
end
|
19
|
+
|
20
|
+
def config
|
21
|
+
@config ||= YAML.load_file(@config_path)[@env]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/filbert/task.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
|
+
require 'filbert/db_config'
|
2
|
+
|
1
3
|
module Filbert
|
2
4
|
class Task < Thor
|
3
5
|
include Thor::Actions
|
4
|
-
class_option :app, type: :string, required: true
|
5
6
|
|
6
|
-
|
7
|
+
method_option :app, type: :string, required: true
|
7
8
|
desc "backup", "capture and pull latest production snapshot and migrate local database"
|
8
9
|
def backup
|
9
10
|
say "Looking for the follower DB..."
|
@@ -19,7 +20,7 @@ module Filbert
|
|
19
20
|
invoke :cleanup
|
20
21
|
end
|
21
22
|
|
22
|
-
desc "cleanup", "remove backup files older than
|
23
|
+
desc "cleanup", "remove backup files older than 12 hours"
|
23
24
|
def cleanup
|
24
25
|
old_files.each do |file|
|
25
26
|
say "Deleting old #{File.basename(file.path)}"
|
@@ -27,6 +28,27 @@ module Filbert
|
|
27
28
|
end
|
28
29
|
end
|
29
30
|
|
31
|
+
method_option :config, type: :string, default: "config/database.yml"
|
32
|
+
method_option :env, type: :string, default: "production"
|
33
|
+
desc "restore", "restore the latest db dump"
|
34
|
+
def restore
|
35
|
+
most_recent_file = ordered_dumps.last
|
36
|
+
db_config = DbConfig.new(options[:config], options[:env])
|
37
|
+
|
38
|
+
check_dump_ready(most_recent_file)
|
39
|
+
check_config_ready(db_config)
|
40
|
+
|
41
|
+
say "Restoring: #{db_config.database} <--- #{most_recent_file.path}"
|
42
|
+
kill_connections(db_config.database, db_config.username)
|
43
|
+
ENV['PGPASSWORD'] = db_config.password
|
44
|
+
run! "pg_restore -U #{db_config.username} -d #{db_config.database} -w #{most_recent_file.path}"
|
45
|
+
|
46
|
+
rescue Errno::ENOENT
|
47
|
+
say "Could not find config file #{options[:config]}. Please pass in --config with a path to database.yml"
|
48
|
+
ensure
|
49
|
+
ENV['PGPASSWORD'] = nil
|
50
|
+
end
|
51
|
+
|
30
52
|
private
|
31
53
|
|
32
54
|
def run!(cmd)
|
@@ -40,13 +62,17 @@ module Filbert
|
|
40
62
|
|
41
63
|
def old_files
|
42
64
|
hurdle = Time.now - 60*60*12
|
65
|
+
ordered_dumps.select{ |file|
|
66
|
+
file.mtime < hurdle
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
def ordered_dumps
|
43
71
|
Dir.new(backups_dir).select{ |x|
|
44
72
|
x.end_with? '.dump'
|
45
73
|
}.map { |filename|
|
46
|
-
|
47
|
-
}.
|
48
|
-
file.mtime < hurdle
|
49
|
-
}
|
74
|
+
File.new(File.join(backups_dir, filename))
|
75
|
+
}.sort_by(&:mtime)
|
50
76
|
end
|
51
77
|
|
52
78
|
def file_path
|
@@ -56,5 +82,24 @@ module Filbert
|
|
56
82
|
def backups_dir
|
57
83
|
File.join(Dir.home, '.heroku_backups')
|
58
84
|
end
|
85
|
+
|
86
|
+
def check_dump_ready(most_recent_file)
|
87
|
+
if most_recent_file.nil?
|
88
|
+
say "Didn't find any backup files in #{backups_dir}"
|
89
|
+
exit 0
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def check_config_ready(db_config)
|
94
|
+
if db_config.config.nil?
|
95
|
+
say "Could not find config for \"#{options[:env]}\" in #{options[:config]}"
|
96
|
+
exit 0
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def kill_connections(database, user)
|
101
|
+
sql = "SELECT pg_terminate_backend(procpid) FROM pg_stat_activity WHERE procpid <> pg_backend_pid();"
|
102
|
+
run "echo #{sql} | psql -d #{database} -U #{user}"
|
103
|
+
end
|
59
104
|
end
|
60
105
|
end
|
data/lib/filbert/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: filbert
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-17 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: heroku
|
@@ -60,6 +60,7 @@ files:
|
|
60
60
|
- bin/filbert
|
61
61
|
- filbert.gemspec
|
62
62
|
- lib/filbert.rb
|
63
|
+
- lib/filbert/db_config.rb
|
63
64
|
- lib/filbert/task.rb
|
64
65
|
- lib/filbert/version.rb
|
65
66
|
homepage: https://github.com/alphasights/filbert
|