s3_db_assets_backup 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,8 @@
|
|
1
1
|
config_file = "#{Rails.root.to_s}/config/s3_backup_config.yml"
|
2
2
|
if FileTest.exists?(config_file)
|
3
3
|
S3_CONFIG = YAML.load_file(config_file)[Rails.env].symbolize_keys
|
4
|
+
AWS.config(S3_CONFIG)
|
5
|
+
|
4
6
|
EMAIL_CONFIG = YAML.load_file(config_file)["email_config"].symbolize_keys
|
5
7
|
else
|
6
8
|
puts "WARNING: Can't find s3_backup_config.yml"
|
@@ -5,20 +5,29 @@ require 'aws/s3'
|
|
5
5
|
namespace :assets do
|
6
6
|
desc "Backup everything in the public folder."
|
7
7
|
task :backup => [:environment] do
|
8
|
-
AWS::S3::Base.establish_connection!(:access_key_id => S3_CONFIG[:access_key_id], :secret_access_key => S3_CONFIG[:secret_access_key])
|
9
|
-
BUCKET = S3_CONFIG[:bucket]
|
10
8
|
|
9
|
+
# establish a connection and create the s3 bucket
|
10
|
+
s3 = AWS::S3.new
|
11
|
+
bucket = s3.buckets.create(S3_CONFIG[:bucket])
|
12
|
+
|
13
|
+
# Build the backup directory and filename
|
11
14
|
datestamp = Time.now.strftime("%Y-%m-%d-%H-%M-%S")
|
12
15
|
base_path = ENV["RAILS_ROOT"] || "."
|
13
|
-
file_name = "
|
16
|
+
file_name = "#{Rails.env}_assets-#{datestamp}.sql.gz"
|
14
17
|
backup_file = File.join(base_path, "tmp", file_name)
|
18
|
+
|
15
19
|
sh "tar -cvzpf #{backup_file} public"
|
16
20
|
|
17
|
-
|
18
|
-
|
21
|
+
# Upload the backup file to Amazon and remove the file from the local filesystem
|
22
|
+
basename = File.basename(file_name)
|
23
|
+
object = bucket.objects[basename]
|
24
|
+
object.write(:file => backup_file)
|
25
|
+
|
26
|
+
puts "Uploaded #{file_name} to:"
|
27
|
+
puts object.public_url
|
28
|
+
|
19
29
|
FileUtils.rm_rf(backup_file)
|
20
30
|
|
21
|
-
bucket = AWS::S3::Bucket.find(BUCKET)
|
22
31
|
all_backups = bucket.objects.select { |f| f.key.match(/assets/) }.sort { |a,b| a.key <=> b.key }.reverse
|
23
32
|
max_backups = S3_CONFIG[:assets_backups_to_keep].to_i || 28
|
24
33
|
unwanted_backups = all_backups[max_backups..-1] || []
|
@@ -28,15 +37,16 @@ namespace :assets do
|
|
28
37
|
end
|
29
38
|
puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
|
30
39
|
end
|
31
|
-
end
|
32
40
|
|
33
|
-
namespace :assets do
|
34
41
|
desc "Restore the public folder from an available backup."
|
35
42
|
task :restore => [:environment] do
|
36
43
|
base_path = ENV["RAILS_ROOT"] || "."
|
37
|
-
|
38
|
-
|
39
|
-
|
44
|
+
|
45
|
+
# establish a connection and find the s3 bucket
|
46
|
+
s3 = AWS::S3.new
|
47
|
+
bucket = s3.buckets[S3_CONFIG[:bucket]]
|
48
|
+
|
49
|
+
backups = bucket.objects.select { |f| f.key.match(/assets/) }
|
40
50
|
if backups.size == 0
|
41
51
|
puts "no backups available, check your settings in config/s3_backup_config.yml"
|
42
52
|
else
|
@@ -56,12 +66,12 @@ namespace :assets do
|
|
56
66
|
if backups.at(selected).nil?
|
57
67
|
puts "Backup not found, aborting"
|
58
68
|
else
|
59
|
-
|
60
|
-
backup_file = File.join(base_path,
|
69
|
+
backup = backups.at(selected)
|
70
|
+
backup_file = File.join(base_path, "tmp", backup.key)
|
61
71
|
destination = File.join(base_path, "public")
|
62
72
|
puts "downloading backup..."
|
63
|
-
open(backup_file
|
64
|
-
|
73
|
+
open(backup_file, 'wb') do |file|
|
74
|
+
backup.read do |chunk|
|
65
75
|
file.write chunk
|
66
76
|
end
|
67
77
|
end
|
@@ -1,15 +1,16 @@
|
|
1
1
|
require 'find'
|
2
2
|
require 'fileutils'
|
3
|
-
require 'aws
|
3
|
+
require 'aws-sdk'
|
4
4
|
require 'pony'
|
5
5
|
require 'unit_converter'
|
6
6
|
|
7
7
|
namespace :db do
|
8
8
|
desc "Backup the database to a file. Options: RAILS_ENV=production"
|
9
9
|
task :backup => [:environment] do
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
|
11
|
+
# establish a connection and create the s3 bucket
|
12
|
+
s3 = AWS::S3.new
|
13
|
+
bucket = s3.buckets.create(S3_CONFIG[:bucket])
|
13
14
|
|
14
15
|
# Build the backup directory and filename
|
15
16
|
datestamp = Time.now.strftime("%Y-%m-%d-%H-%M-%S")
|
@@ -22,13 +23,17 @@ namespace :db do
|
|
22
23
|
sh "mysqldump -u #{db_config['username']} -p#{db_config['password']} --default-character-set=latin1 -N -Q --add-drop-table #{db_config['database']} | gzip -c > #{backup_file}"
|
23
24
|
|
24
25
|
# Upload the backup file to Amazon and remove the file from the local filesystem
|
25
|
-
|
26
|
-
|
26
|
+
basename = File.basename(file_name)
|
27
|
+
object = bucket.objects[basename]
|
28
|
+
object.write(:file => backup_file)
|
29
|
+
|
30
|
+
puts "Uploaded #{file_name} to:"
|
31
|
+
puts object.public_url
|
32
|
+
|
27
33
|
FileUtils.rm_rf(backup_file)
|
28
34
|
|
29
35
|
# Check the bucket for the number of backups and remove the oldest backups if
|
30
36
|
# it is over the number of backups sets configured
|
31
|
-
bucket = AWS::S3::Bucket.find(BUCKET)
|
32
37
|
all_backups = bucket.objects.select { |f| f.key.match(/dump/) }.sort { |a,b| a.key <=> b.key }.reverse
|
33
38
|
max_backups = S3_CONFIG[:database_backups_to_keep].to_i || 28
|
34
39
|
unwanted_backups = all_backups[max_backups..-1] || []
|
@@ -38,16 +43,17 @@ namespace :db do
|
|
38
43
|
end
|
39
44
|
puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
|
40
45
|
end
|
41
|
-
end
|
42
46
|
|
43
|
-
namespace :db do
|
44
47
|
desc "Restore the database from an available backup. Options: RAILS_ENV=production"
|
45
48
|
task :restore => [:environment] do
|
46
49
|
base_path = ENV["RAILS_ROOT"] || "."
|
47
50
|
db_config = ActiveRecord::Base.configurations[Rails.env]
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
+
|
52
|
+
# establish a connection and find the s3 bucket
|
53
|
+
s3 = AWS::S3.new
|
54
|
+
bucket = s3.buckets[S3_CONFIG[:bucket]]
|
55
|
+
|
56
|
+
backups = bucket.objects.select { |f| f.key.match(/dump/) }
|
51
57
|
if backups.size == 0
|
52
58
|
puts "no backups available, check your settings in config/s3_backup_config.yml"
|
53
59
|
else
|
@@ -67,11 +73,11 @@ namespace :db do
|
|
67
73
|
if backups.at(selected).nil?
|
68
74
|
puts "Backup not found, aborting"
|
69
75
|
else
|
70
|
-
|
71
|
-
backup_file = File.join(base_path, "tmp",
|
76
|
+
backup = backups.at(selected)
|
77
|
+
backup_file = File.join(base_path, "tmp", backup.key)
|
72
78
|
puts "downloading backup..."
|
73
79
|
open(backup_file, 'wb') do |file|
|
74
|
-
|
80
|
+
backup.read do |chunk|
|
75
81
|
file.write chunk
|
76
82
|
end
|
77
83
|
end
|
@@ -83,15 +89,15 @@ namespace :db do
|
|
83
89
|
end
|
84
90
|
end
|
85
91
|
end
|
86
|
-
end
|
87
92
|
|
88
|
-
namespace :db do
|
89
93
|
namespace :backup do
|
90
94
|
desc "Email a report of current backups."
|
91
95
|
task :status => [:environment] do
|
92
|
-
|
93
|
-
|
94
|
-
|
96
|
+
|
97
|
+
# establish a connection and find the s3 bucket
|
98
|
+
s3 = AWS::S3.new
|
99
|
+
bucket = s3.buckets[S3_CONFIG[:bucket]]
|
100
|
+
|
95
101
|
backups = bucket.objects.select { |f| f.key.match(/dump/) }.sort { |a,b| a.key <=> b.key }.reverse
|
96
102
|
email = EMAIL_CONFIG
|
97
103
|
message = "Archive contains the following backups:\n\n"
|
metadata
CHANGED
@@ -1,95 +1,86 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: s3_db_assets_backup
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.1
|
3
|
+
version: !ruby/object:Gem::Version
|
5
4
|
prerelease:
|
5
|
+
version: 0.2.2
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Chris Barnes
|
9
9
|
- Michael Burk
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
|
14
|
+
date: 2012-12-09 00:00:00 Z
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
16
17
|
name: pony
|
17
|
-
requirement: !ruby/object:Gem::Requirement
|
18
|
-
none: false
|
19
|
-
requirements:
|
20
|
-
- - ! '>='
|
21
|
-
- !ruby/object:Gem::Version
|
22
|
-
version: '1.4'
|
23
|
-
type: :runtime
|
24
18
|
prerelease: false
|
25
|
-
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
20
|
none: false
|
27
|
-
requirements:
|
28
|
-
- -
|
29
|
-
- !ruby/object:Gem::Version
|
30
|
-
version:
|
31
|
-
- !ruby/object:Gem::Dependency
|
32
|
-
name: aws-s3
|
33
|
-
requirement: !ruby/object:Gem::Requirement
|
34
|
-
none: false
|
35
|
-
requirements:
|
36
|
-
- - ! '>='
|
37
|
-
- !ruby/object:Gem::Version
|
38
|
-
version: 0.6.3
|
21
|
+
requirements:
|
22
|
+
- - ">="
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "1.4"
|
39
25
|
type: :runtime
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: aws-sdk
|
40
29
|
prerelease: false
|
41
|
-
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
42
31
|
none: false
|
43
|
-
requirements:
|
44
|
-
- -
|
45
|
-
- !ruby/object:Gem::Version
|
46
|
-
version:
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
with a time stamp and the config file allows you to set how many of each backup
|
51
|
-
to keep. Additionally, the plugin can be configured to generate and send a backup
|
52
|
-
status report via email.
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "1.7"
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
description: Generates rake tasks for backing up and restoring your database and public folder (which should also contain any user uploaded assets) to and from an existing bucket in your Amazon S3 account. The rake tasks compresses and uploads each backup with a time stamp and the config file allows you to set how many of each backup to keep. Additionally, the plugin can be configured to generate and send a backup status report via email.
|
53
39
|
email: chris@randomutterings.com
|
54
40
|
executables: []
|
41
|
+
|
55
42
|
extensions: []
|
56
|
-
|
43
|
+
|
44
|
+
extra_rdoc_files:
|
57
45
|
- README.rdoc
|
58
|
-
files:
|
59
|
-
- lib/generators/s3_backup/USAGE
|
46
|
+
files:
|
60
47
|
- lib/generators/s3_backup/s3_backup_generator.rb
|
61
48
|
- lib/generators/s3_backup/templates/s3_backup_config.yml
|
62
|
-
- lib/
|
49
|
+
- lib/generators/s3_backup/USAGE
|
63
50
|
- lib/s3_db_assets_backup/engine.rb
|
64
51
|
- lib/s3_db_assets_backup/railties/assets_backup.rake
|
65
52
|
- lib/s3_db_assets_backup/railties/db_backup.rake
|
53
|
+
- lib/s3_db_assets_backup.rb
|
66
54
|
- lib/unit_converter.rb
|
67
55
|
- config/initializers/load_s3_backup_config.rb
|
68
56
|
- README.rdoc
|
69
57
|
homepage: http://www.randomutterings.com/projects/s3_db_assets_backup
|
70
58
|
licenses: []
|
59
|
+
|
71
60
|
post_install_message:
|
72
61
|
rdoc_options: []
|
73
|
-
|
62
|
+
|
63
|
+
require_paths:
|
74
64
|
- lib
|
75
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
76
66
|
none: false
|
77
|
-
requirements:
|
78
|
-
- -
|
79
|
-
- !ruby/object:Gem::Version
|
80
|
-
version:
|
81
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
82
72
|
none: false
|
83
|
-
requirements:
|
84
|
-
- -
|
85
|
-
- !ruby/object:Gem::Version
|
86
|
-
version:
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "0"
|
87
77
|
requirements: []
|
78
|
+
|
88
79
|
rubyforge_project:
|
89
80
|
rubygems_version: 1.8.24
|
90
81
|
signing_key:
|
91
82
|
specification_version: 3
|
92
|
-
summary: Rake tasks for backing up and restoring db and public folder to and from
|
93
|
-
an existing S3 bucket with email notifications.
|
83
|
+
summary: Rake tasks for backing up and restoring db and public folder to and from an existing S3 bucket with email notifications.
|
94
84
|
test_files: []
|
85
|
+
|
95
86
|
has_rdoc:
|