heroku_cloud_backup 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +26 -15
- data/lib/heroku_cloud_backup/tasks.rb +1 -1
- data/lib/heroku_cloud_backup/version.rb +1 -1
- data/lib/heroku_cloud_backup.rb +46 -35
- metadata +26 -3
data/README.md
CHANGED
@@ -14,7 +14,7 @@ If you want this to run daily, you'll need to enable the Heroku cron addon:
|
|
14
14
|
|
15
15
|
heroku addons:add cron:daily
|
16
16
|
|
17
|
-
For Rails 3
|
17
|
+
For Rails 3 and later, add this to your Gemfile:
|
18
18
|
|
19
19
|
gem 'heroku_backup_task'
|
20
20
|
gem 'heroku_cloud_backup'
|
@@ -27,42 +27,53 @@ For Rails 2.1 and later, add this to your in your environment.rb:
|
|
27
27
|
In your Rakefile:
|
28
28
|
|
29
29
|
require "heroku_backup_task"
|
30
|
+
require "heroku_cloud_backup"
|
30
31
|
task :cron do
|
31
32
|
HerokuBackupTask.execute
|
32
|
-
|
33
|
+
HerokuCloudBackup.execute
|
33
34
|
end
|
34
35
|
|
35
36
|
## Usage
|
36
37
|
|
37
38
|
The first thing you'll want to do is configure the addon.
|
38
39
|
|
39
|
-
|
40
|
+
HCB_PROVIDER (aws, rackspace, google) - Add which provider you're using. **Required**
|
40
41
|
|
41
|
-
heroku config:add
|
42
|
+
heroku config:add HCB_PROVIDER='aws' # or 'google' or 'rackspace'
|
42
43
|
|
43
|
-
HCB_BUCKET (
|
44
|
+
HCB_BUCKET (alphanumberic characters, dashes, period, underscore are allowed, between 3 and 255 characters long) - Select a bucket name to upload to. This the bucket or root directory that your files will be stored in. If the bucket doesn't exist, it will be created. **Required**
|
44
45
|
|
45
46
|
heroku config:add HCB_BUCKET='mywebsite'
|
46
47
|
|
47
|
-
HCB_PREFIX (Defaults to "db") - The direction prefix for where the backups are stored. This is so you can store your backups within a specific sub directory within the bucket. **Optional**
|
48
|
+
HCB_PREFIX (Defaults to "db") - The direction prefix for where the backups are stored. This is so you can store your backups within a specific sub directory within the bucket. heroku_cloud_backup will also append the ENV var of the database to the path, so you can backup multiple databases, by their ENV names. **Optional**
|
48
49
|
|
49
50
|
heroku config:add HCB_PREFIX='backups/pg'
|
50
51
|
|
51
|
-
|
52
|
+
HCB_MAX (Defaults to no limit) - The number of backups to store before the script will prune out older backups. A value of 10 will allow you to store 10 of the most recent backups. Newer backups will replace older ones. **Optional**
|
52
53
|
|
53
|
-
heroku config:add
|
54
|
+
heroku config:add HCB_MAX=10
|
54
55
|
|
55
|
-
Depending on which
|
56
|
+
Depending on which provider you specify, you'll need to provide different login credentials.
|
56
57
|
|
57
|
-
|
58
|
+
For Amazon:
|
58
59
|
|
59
|
-
heroku config:add
|
60
|
-
heroku config:add
|
60
|
+
heroku config:add HCB_KEY1="aws_access_key_id"
|
61
|
+
heroku config:add HCB_KEY2="aws_secret_access_key"
|
62
|
+
|
63
|
+
For Rackspace:
|
64
|
+
|
65
|
+
heroku config:add HCB_KEY1="rackspace_username"
|
66
|
+
heroku config:add HCB_KEY2="rackspace_api_key"
|
67
|
+
|
68
|
+
For Google Storage:
|
69
|
+
|
70
|
+
heroku config:add HCB_KEY1="google_storage_secret_access_key"
|
71
|
+
heroku config:add HCB_KEY2="google_storage_access_key_id"
|
61
72
|
|
62
73
|
You can run this manually like this:
|
63
74
|
|
64
|
-
rake heroku_backup
|
65
|
-
rake heroku:cloud_backup
|
75
|
+
heroku rake heroku_backup
|
76
|
+
heroku rake heroku:cloud_backup
|
66
77
|
|
67
78
|
## Note on Patches/Pull Requests
|
68
79
|
|
@@ -76,4 +87,4 @@ You can run this manually like this:
|
|
76
87
|
|
77
88
|
### Copyright
|
78
89
|
|
79
|
-
Copyright (c) 2011 Jack Chu. See LICENSE for details.
|
90
|
+
Copyright (c) 2011 Jack Chu. See LICENSE for details.
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'heroku_cloud_backup'
|
2
2
|
|
3
3
|
namespace :heroku do
|
4
|
-
desc "
|
4
|
+
desc "Transfer PostgreSQL database backups from Heroku to the cloud"
|
5
5
|
task :cloud_backup => :environment do
|
6
6
|
HerokuCloudBackup.execute
|
7
7
|
end
|
data/lib/heroku_cloud_backup.rb
CHANGED
@@ -12,15 +12,15 @@ module HerokuCloudBackup
|
|
12
12
|
def log(message)
|
13
13
|
puts "[#{Time.now}] #{message}"
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
def backups_url
|
17
17
|
ENV["PGBACKUPS_URL"]
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
def client
|
21
21
|
@client ||= PGBackups::Client.new(ENV["PGBACKUPS_URL"])
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
def databases
|
25
25
|
if db = ENV["HEROKU_BACKUP_DATABASES"]
|
26
26
|
db.split(",").map(&:strip)
|
@@ -28,7 +28,7 @@ module HerokuCloudBackup
|
|
28
28
|
["DATABASE_URL"]
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
31
|
+
|
32
32
|
def backup_name(to_url)
|
33
33
|
# translate s3://bucket/email/foo/bar.dump => foo/bar
|
34
34
|
parts = to_url.split('/')
|
@@ -38,45 +38,56 @@ module HerokuCloudBackup
|
|
38
38
|
def execute
|
39
39
|
log "heroku:backup started"
|
40
40
|
|
41
|
-
@bucket_name = ENV['HCB_BUCKET'] || "
|
41
|
+
@bucket_name = ENV['HCB_BUCKET'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_BUCKET' config variable."))
|
42
42
|
@backup_path = ENV['HCB_PREFIX'] || "db"
|
43
|
-
@
|
43
|
+
@provider = ENV['HCB_PROVIDER'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_PROVIDER' config variable."))
|
44
|
+
@key1 = ENV['HCB_KEY1'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_KEY1' config variable."))
|
45
|
+
@key2 = ENV['HCB_KEY2'] || raise(HerokuCloudBackup::Errors::NotFound.new("Please provide a 'HCB_KEY2' config variable."))
|
46
|
+
|
44
47
|
b = client.get_latest_backup
|
45
48
|
raise HerokuCloudBackup::Errors::NoBackups.new("You don't have any pgbackups. Please run heroku pgbackups:capture first.") if b.empty?
|
46
49
|
|
47
|
-
|
48
|
-
case provider
|
50
|
+
begin
|
51
|
+
case @provider
|
49
52
|
when 'aws'
|
50
|
-
@
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
53
|
+
@connection = Fog::Storage.new(
|
54
|
+
:provider => 'AWS',
|
55
|
+
:aws_access_key_id => @key1,
|
56
|
+
:aws_secret_access_key => @key2
|
57
|
+
)
|
58
|
+
when 'rackspace'
|
59
|
+
connection = Fog::Storage.new(
|
60
|
+
:provider => 'Rackspace',
|
61
|
+
:rackspace_username => @key1,
|
62
|
+
:rackspace_api_key => @key2
|
63
|
+
)
|
64
|
+
when 'google'
|
65
|
+
connection = Fog::Storage.new(
|
66
|
+
:provider => 'Google',
|
67
|
+
:google_storage_secret_access_key => @key1,
|
68
|
+
:google_storage_access_key_id => @key2
|
69
|
+
)
|
61
70
|
else
|
62
|
-
raise HerokuCloudBackup::Errors::InvalidProvider.new("
|
71
|
+
raise HerokuCloudBackup::Errors::InvalidProvider.new("Your provider was invalid. Valid values are 'aws', 'rackspace', or 'google'")
|
63
72
|
end
|
73
|
+
rescue
|
74
|
+
raise HerokuCloudBackup::Errors::ConnectionError.new("There was an error connecting to your provider.")
|
75
|
+
end
|
64
76
|
|
65
|
-
|
77
|
+
directory = @connection.directories.get(@bucket_name) rescue Excon::Errors::Forbidden nil
|
66
78
|
|
67
|
-
|
68
|
-
|
69
|
-
|
79
|
+
if !directory
|
80
|
+
directory = @connection.directories.create(:key => @bucket_name)
|
81
|
+
end
|
70
82
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
end
|
83
|
+
public_url = b["public_url"]
|
84
|
+
created_at = DateTime.parse b["created_at"]
|
85
|
+
db_name = b["from_name"]
|
86
|
+
name = "#{created_at.strftime('%Y-%m-%d-%H%M%S')}.dump"
|
87
|
+
begin
|
88
|
+
directory.files.create(:key => "#{@backup_path}/#{b["from_name"]}/#{name}", :body => open(public_url))
|
89
|
+
rescue Exception => e
|
90
|
+
raise HerokuCloudBackup::Errors::UploadError.new(e.message)
|
80
91
|
end
|
81
92
|
|
82
93
|
prune
|
@@ -87,13 +98,13 @@ module HerokuCloudBackup
|
|
87
98
|
private
|
88
99
|
|
89
100
|
def prune
|
90
|
-
number_of_files = ENV['
|
101
|
+
number_of_files = ENV['HCB_MAX']
|
91
102
|
if number_of_files && number_of_files.to_i > 0
|
92
103
|
directory = @connection.directories.get(@bucket_name)
|
93
104
|
files = directory.files.all(:prefix => @backup_path)
|
94
105
|
file_count = 0
|
95
106
|
files.reverse.each do |file|
|
96
|
-
if file.key =~ Regexp.new("/#{@backup_path}
|
107
|
+
if file.key =~ Regexp.new("/#{@backup_path}\/\d{4}-\d{2}-\d{2}-\d{6}\.sql\.gz$/i")
|
97
108
|
file_count += 1
|
98
109
|
else
|
99
110
|
next
|
metadata
CHANGED
@@ -1,8 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: heroku_cloud_backup
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
4
5
|
prerelease:
|
5
|
-
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
6
11
|
platform: ruby
|
7
12
|
authors:
|
8
13
|
- Jack Chu
|
@@ -10,7 +15,8 @@ autorequire:
|
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
17
|
|
13
|
-
date: 2011-
|
18
|
+
date: 2011-06-08 00:00:00 -04:00
|
19
|
+
default_executable:
|
14
20
|
dependencies:
|
15
21
|
- !ruby/object:Gem::Dependency
|
16
22
|
name: fog
|
@@ -20,6 +26,11 @@ dependencies:
|
|
20
26
|
requirements:
|
21
27
|
- - ">="
|
22
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 7
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 7
|
33
|
+
- 2
|
23
34
|
version: 0.7.2
|
24
35
|
type: :runtime
|
25
36
|
version_requirements: *id001
|
@@ -31,6 +42,11 @@ dependencies:
|
|
31
42
|
requirements:
|
32
43
|
- - ">="
|
33
44
|
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 1
|
49
|
+
- 4
|
34
50
|
version: 2.1.4
|
35
51
|
type: :runtime
|
36
52
|
version_requirements: *id002
|
@@ -55,6 +71,7 @@ files:
|
|
55
71
|
- lib/heroku_cloud_backup/railtie.rb
|
56
72
|
- lib/heroku_cloud_backup/tasks.rb
|
57
73
|
- lib/heroku_cloud_backup/version.rb
|
74
|
+
has_rdoc: true
|
58
75
|
homepage: ""
|
59
76
|
licenses: []
|
60
77
|
|
@@ -68,17 +85,23 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
68
85
|
requirements:
|
69
86
|
- - ">="
|
70
87
|
- !ruby/object:Gem::Version
|
88
|
+
hash: 3
|
89
|
+
segments:
|
90
|
+
- 0
|
71
91
|
version: "0"
|
72
92
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
93
|
none: false
|
74
94
|
requirements:
|
75
95
|
- - ">="
|
76
96
|
- !ruby/object:Gem::Version
|
97
|
+
hash: 3
|
98
|
+
segments:
|
99
|
+
- 0
|
77
100
|
version: "0"
|
78
101
|
requirements: []
|
79
102
|
|
80
103
|
rubyforge_project: heroku_cloud_backup
|
81
|
-
rubygems_version: 1.
|
104
|
+
rubygems_version: 1.4.2
|
82
105
|
signing_key:
|
83
106
|
specification_version: 3
|
84
107
|
summary: Backup pg dumps to the cloud
|