s3_db_assets_backup 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,12 +1,14 @@
1
1
  = S3DbAssetsBackup
2
2
 
3
- Generates rake tasks for backing up your database and public folder (which should also contain any uploaded assets) to 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. This script is "very largly based on" the following (http://www.magnionlabs.com/2009/7/7/db-backups).
3
+ Generates rake tasks for backing up and restoring your database and public folder (which should also contain any 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. This plugin is based on (http://www.magnionlabs.com/2009/7/7/db-backups).
4
4
 
5
5
  == Install
6
6
 
7
7
  script/plugin install git://github.com/randomutterings/s3_db_assets_backup.git
8
-
9
- Edit config/assets_backup_config.yml with your aws credentials.
8
+
9
+ or
10
+
11
+ gem install s3_db_assets_backup
10
12
 
11
13
  Install aws-s3 library
12
14
 
@@ -14,46 +16,50 @@ Install aws-s3 library
14
16
 
15
17
  Create the backup rake tasks, config file, and initializer.
16
18
 
17
- script/generate assets_backup
19
+ script/generate s3_backup
20
+
21
+ Edit config/assets_backup_config.yml with your aws credentials.
22
+
23
+ == Usage
18
24
 
19
25
  Backup the production database.
20
26
 
21
27
  rake db:backup RAILS_ENV=production
22
28
 
29
+ Restore the production database (you will be prompted to select an existing backup)
30
+
31
+ rake db:restore RAILS_ENV=production
32
+
23
33
  Backup the public folder
24
34
 
25
35
  rake assets:backup
26
36
 
27
- Optionally setup cron to do your backups with the following crontab
28
-
29
- # Full backup every Wednesday at 11:01
30
- 1 23 * * 3 cd /path/to/your/project && /var/lib/gems/1.8/bin/rake db:backup RAILS_ENV=production
31
- 1 23 * * 3 cd /path/to/your/project && /var/lib/gems/1.8/bin/rake assets:backup
37
+ Restore the public folder (you will be prompted to select an existing backup)
32
38
 
33
- If you ever need to restore the production database, use the tool of your choice to download the backup from S3 and run.
39
+ rake assets:restore
34
40
 
35
- gzip -dc production_dump-YYYY-MM-DD_HH-MM-SS.sql.gz | mysql -u root -p <production database>
41
+ Optionally setup cron to do your backups with the following crontab
42
+
43
+ # db backup every day at 11:01
44
+ 1 23 * * * cd /path/to/your/project && /var/lib/gems/1.8/bin/rake db:backup RAILS_ENV=production
45
+
46
+ # asset backup every wednesday at 11:30
47
+ 30 23 * * 3 cd /path/to/your/project && /var/lib/gems/1.8/bin/rake assets:backup
36
48
 
37
49
  == Troubleshooting and FAQs
38
50
 
39
- <b>I get "cannot create ./tmp/production_dump-YYYY-MM-DD_HH-MM-SS.sql.gz: Directory nonexistent" when running the database backup to S3</b>
51
+ <b>I get "cannot create ./tmp/production_dump-YYYY-MM-DD-HH-MM-SS.sql.gz: Directory nonexistent" when running the database backup to S3</b>
40
52
 
41
- You need to create a tmp directory in the root.
53
+ You need to create a tmp directory in the project root.
42
54
 
43
55
  mkdir tmp
44
56
 
45
- <b>Another problem?</b>
46
-
47
- Make sure you're using Rails 2.3.5.
48
-
49
-
50
57
  == Found a bug?
51
58
 
52
- If you are having a problem with the site, first look at the FAQs above. If you still cannot resolve it, please submit an issue here.
59
+ If you are having a problem with the plugin, first look at the FAQs above. If you still cannot resolve it, please submit an issue here.
53
60
 
54
61
  http://github.com/randomutterings/s3_db_assets_backup/issues
55
62
 
56
-
57
63
  == Rails 3
58
64
 
59
65
  This project does not yet officially work with Rails 3.
@@ -2,7 +2,7 @@ Description:
2
2
  generates backup rake tasks, config yaml file, and initializer
3
3
 
4
4
  Example:
5
- ./script/generate assets_backup
5
+ ./script/generate s3_backup
6
6
 
7
7
  This will create:
8
8
  lib/tasks/assets_backup.rake
@@ -1,4 +1,4 @@
1
- class AssetsBackupGenerator < Rails::Generator::Base
1
+ class S3BackupGenerator < Rails::Generator::Base
2
2
  def manifest
3
3
  record do |m|
4
4
  m.file "assets_backup_config.yml", "config/assets_backup_config.yml"
@@ -0,0 +1,75 @@
1
+ require 'find'
2
+ require 'ftools'
3
+ require 'aws/s3'
4
+
5
+ namespace :assets do
6
+ desc "Backup everything in the public folder."
7
+ task :backup => [:environment] do
8
+ AWS::S3::Base.establish_connection!(:access_key_id => APP_CONFIG['access_key_id'], :secret_access_key => APP_CONFIG['secret_access_key'])
9
+ BUCKET = APP_CONFIG['bucket']
10
+
11
+ datestamp = Time.now.strftime("%Y-%m-%d-%H-%M-%S")
12
+ base_path = ENV["RAILS_ROOT"] || "."
13
+ file_name = "assets-#{datestamp}.tgz"
14
+ backup_file = File.join(base_path, "tmp", file_name)
15
+ sh "tar -cvzpf #{backup_file} public"
16
+ AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)
17
+ puts "Created backup: #{file_name}"
18
+ FileUtils.rm_rf(backup_file)
19
+
20
+ bucket = AWS::S3::Bucket.find(BUCKET)
21
+ all_backups = bucket.objects.select { |f| f.key.match(/assets/) }.sort { |a,b| a.key <=> b.key }.reverse
22
+ max_backups = APP_CONFIG['assets_backups_to_keep'].to_i || 28
23
+ unwanted_backups = all_backups[max_backups..-1] || []
24
+ for unwanted_backup in unwanted_backups
25
+ unwanted_backup.delete
26
+ puts "deleted #{unwanted_backup.key}"
27
+ end
28
+ puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
29
+ end
30
+ end
31
+
32
+ namespace :assets do
33
+ desc "Restore the public folder from an available backup."
34
+ task :restore => [:environment] do
35
+ base_path = ENV["RAILS_ROOT"] || "."
36
+ AWS::S3::Base.establish_connection!(:access_key_id => APP_CONFIG['access_key_id'], :secret_access_key => APP_CONFIG['secret_access_key'])
37
+ bucket_name = APP_CONFIG['bucket']
38
+ backups = AWS::S3::Bucket.objects(bucket_name)
39
+ if backups.size == 0
40
+ puts "no backups available, check your settings in config/assets_backup_config.yml"
41
+ else
42
+ puts "#{backups.size} backups are available..."
43
+ counter = 0
44
+ backups.each do |backup|
45
+ puts "[#{counter}] #{backup.key}"
46
+ counter += 1
47
+ end
48
+ if backups.size == 1
49
+ puts "Which backup should we restore? [0]"
50
+ else
51
+ puts "Which backup should we restore? [0-#{backups.size - 1}]"
52
+ end
53
+ STDOUT.flush()
54
+ selected = STDIN.gets.chomp.to_i
55
+ if backups.at(selected).nil?
56
+ puts "Backup not found, aborting"
57
+ else
58
+ file_name = backups.at(selected).key
59
+ backup_file = File.join(base_path, file_name)
60
+ destination = File.join(base_path, "public")
61
+ puts "downloading backup..."
62
+ open(backup_file, 'w') do |file|
63
+ AWS::S3::S3Object.stream(file_name, bucket_name) do |chunk|
64
+ file.write chunk
65
+ end
66
+ end
67
+ FileUtils.remove_dir(destination)
68
+ sh "tar zxfv #{backup_file}"
69
+ puts "cleaning up..."
70
+ FileUtils.rm_rf(backup_file)
71
+ puts "Finished"
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,76 @@
1
+ require 'find'
2
+ require 'ftools'
3
+ require 'aws/s3'
4
+
5
+ namespace :db do
6
+ desc "Backup the database to a file. Options: RAILS_ENV=production"
7
+ task :backup => [:environment] do
8
+ AWS::S3::Base.establish_connection!(:access_key_id => APP_CONFIG['access_key_id'], :secret_access_key => APP_CONFIG['secret_access_key'])
9
+ BUCKET = APP_CONFIG['bucket']
10
+
11
+ datestamp = Time.now.strftime("%Y-%m-%d-%H-%M-%S")
12
+ base_path = ENV["RAILS_ROOT"] || "."
13
+ file_name = "#{RAILS_ENV}_dump-#{datestamp}.sql.gz"
14
+ backup_file = File.join(base_path, "tmp", file_name)
15
+ db_config = ActiveRecord::Base.configurations[RAILS_ENV]
16
+ sh "mysqldump -u #{db_config['username']} -p#{db_config['password']} -Q --add-drop-table -O add-locks=FALSE -O lock-tables=FALSE #{db_config['database']} | gzip -c > #{backup_file}"
17
+ AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)
18
+ puts "Created backup: #{file_name}"
19
+ FileUtils.rm_rf(backup_file)
20
+
21
+ bucket = AWS::S3::Bucket.find(BUCKET)
22
+ all_backups = bucket.objects.select { |f| f.key.match(/dump/) }.sort { |a,b| a.key <=> b.key }.reverse
23
+ max_backups = APP_CONFIG['database_backups_to_keep'].to_i || 28
24
+ unwanted_backups = all_backups[max_backups..-1] || []
25
+ for unwanted_backup in unwanted_backups
26
+ unwanted_backup.delete
27
+ puts "deleted #{unwanted_backup.key}"
28
+ end
29
+ puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
30
+ end
31
+ end
32
+
33
+ namespace :db do
34
+ desc "Restore the database from an available backup. Options: RAILS_ENV=production"
35
+ task :restore => [:environment] do
36
+ base_path = ENV["RAILS_ROOT"] || "."
37
+ db_config = ActiveRecord::Base.configurations[RAILS_ENV]
38
+ AWS::S3::Base.establish_connection!(:access_key_id => APP_CONFIG['access_key_id'], :secret_access_key => APP_CONFIG['secret_access_key'])
39
+ bucket_name = APP_CONFIG['bucket']
40
+ backups = AWS::S3::Bucket.objects(bucket_name)
41
+ if backups.size == 0
42
+ puts "no backups available, check your settings in config/assets_backup_config.yml"
43
+ else
44
+ puts "#{backups.size} backups are available..."
45
+ counter = 0
46
+ backups.each do |backup|
47
+ puts "[#{counter}] #{backup.key}"
48
+ counter += 1
49
+ end
50
+ if backups.size == 1
51
+ puts "Which backup should we restore? [0]"
52
+ else
53
+ puts "Which backup should we restore? [0-#{backups.size - 1}]"
54
+ end
55
+ STDOUT.flush()
56
+ selected = STDIN.gets.chomp.to_i
57
+ if backups.at(selected).nil?
58
+ puts "Backup not found, aborting"
59
+ else
60
+ file_name = backups.at(selected).key
61
+ backup_file = File.join(base_path, "tmp", file_name)
62
+ puts "downloading backup..."
63
+ open(backup_file, 'w') do |file|
64
+ AWS::S3::S3Object.stream(file_name, bucket_name) do |chunk|
65
+ file.write chunk
66
+ end
67
+ end
68
+ puts "download complete, restoring to #{RAILS_ENV} database"
69
+ sh "gzip -dc #{backup_file} | mysql -u #{db_config['username']} -p#{db_config['password']} #{db_config['database']}"
70
+ puts "cleaning up..."
71
+ FileUtils.rm_rf(backup_file)
72
+ puts "Finished"
73
+ end
74
+ end
75
+ end
76
+ end
@@ -9,13 +9,13 @@ PKG_FILES = FileList[
9
9
 
10
10
  spec = Gem::Specification.new do |s|
11
11
  s.name = "s3_db_assets_backup"
12
- s.version = "0.0.4"
12
+ s.version = "0.0.5"
13
13
  s.author = "Chris Barnes"
14
14
  s.email = "randomutterings@gmail.com"
15
15
  s.homepage = "http://www.randomutterings.com/projects/s3_db_assets_backup"
16
16
  s.platform = Gem::Platform::RUBY
17
- s.summary = "Generates rake tasks for backing up db and public folder to an existing S3 bucket"
18
- s.description = "Generates rake tasks for backing up your database and public folder (which should also contain any uploaded assets) to 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. This script is 'very largly based on' the following (http://www.magnionlabs.com/2009/7/7/db-backups)."
17
+ s.summary = "Generates rake tasks for backing up and restoring db and public folder to and from an existing S3 bucket"
18
+ s.description = "Generates rake tasks for backing up and restoring your database and public folder (which should also contain any 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. This gem is based on (http://www.magnionlabs.com/2009/7/7/db-backups)."
19
19
  s.files = PKG_FILES.to_a
20
20
  s.extra_rdoc_files = ["README.rdoc"]
21
21
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 4
9
- version: 0.0.4
8
+ - 5
9
+ version: 0.0.5
10
10
  platform: ruby
11
11
  authors:
12
12
  - Chris Barnes
@@ -14,11 +14,11 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-23 00:00:00 -04:00
17
+ date: 2010-04-18 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies: []
20
20
 
21
- description: Generates rake tasks for backing up your database and public folder (which should also contain any uploaded assets) to 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. This script is 'very largly based on' the following (http://www.magnionlabs.com/2009/7/7/db-backups).
21
+ description: Generates rake tasks for backing up and restoring your database and public folder (which should also contain any 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. This gem is based on (http://www.magnionlabs.com/2009/7/7/db-backups).
22
22
  email: randomutterings@gmail.com
23
23
  executables: []
24
24
 
@@ -30,14 +30,13 @@ files:
30
30
  - MIT-LICENSE
31
31
  - Rakefile
32
32
  - README.rdoc
33
- - s3_db_assets_backup-0.0.3.gem
34
33
  - s3_db_assets_backup.gemspec
35
- - generators/assets_backup/assets_backup_generator.rb
36
- - generators/assets_backup/templates/assets_backup.rake
37
- - generators/assets_backup/templates/assets_backup_config.yml
38
- - generators/assets_backup/templates/db_backup.rake
39
- - generators/assets_backup/templates/load_assets_backup_config.rb
40
- - generators/assets_backup/USAGE
34
+ - generators/s3_backup/s3_backup_generator.rb
35
+ - generators/s3_backup/templates/assets_backup.rake
36
+ - generators/s3_backup/templates/assets_backup_config.yml
37
+ - generators/s3_backup/templates/db_backup.rake
38
+ - generators/s3_backup/templates/load_assets_backup_config.rb
39
+ - generators/s3_backup/USAGE
41
40
  - lib/s3_db_assets_backup.rb
42
41
  - test/s3_db_assets_backup_test.rb
43
42
  - test/test_helper.rb
@@ -70,6 +69,6 @@ rubyforge_project:
70
69
  rubygems_version: 1.3.6
71
70
  signing_key:
72
71
  specification_version: 3
73
- summary: Generates rake tasks for backing up db and public folder to an existing S3 bucket
72
+ summary: Generates rake tasks for backing up and restoring db and public folder to and from an existing S3 bucket
74
73
  test_files: []
75
74
 
@@ -1,30 +0,0 @@
1
- require 'find'
2
- require 'ftools'
3
- require 'aws/s3'
4
-
5
- namespace :assets do
6
- desc "Backup everything in the public folder."
7
- task :backup => [:environment] do
8
- AWS::S3::Base.establish_connection!(:access_key_id => APP_CONFIG['access_key_id'], :secret_access_key => APP_CONFIG['secret_access_key'])
9
- BUCKET = APP_CONFIG['bucket']
10
-
11
- datestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
12
- base_path = ENV["RAILS_ROOT"] || "."
13
- file_name = "#{RAILS_ENV}_assets-#{datestamp}.tgz"
14
- backup_file = File.join(base_path, "tmp", file_name)
15
- sh "tar -cvzpf #{backup_file} public"
16
- AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)
17
- puts "Created backup: #{file_name}"
18
- FileUtils.rm_rf(backup_file)
19
-
20
- bucket = AWS::S3::Bucket.find(BUCKET)
21
- all_backups = bucket.objects.select { |f| f.key.match(/assets/) }.sort { |a,b| a.key <=> b.key }.reverse
22
- max_backups = APP_CONFIG['assets_backups_to_keep'].to_i || 28
23
- unwanted_backups = all_backups[max_backups..-1] || []
24
- for unwanted_backup in unwanted_backups
25
- unwanted_backup.delete
26
- puts "deleted #{unwanted_backup.key}"
27
- end
28
- puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
29
- end
30
- end
@@ -1,31 +0,0 @@
1
- require 'find'
2
- require 'ftools'
3
- require 'aws/s3'
4
-
5
- namespace :db do
6
- desc "Backup the database to a file. Options: RAILS_ENV=production"
7
- task :backup => [:environment] do
8
- AWS::S3::Base.establish_connection!(:access_key_id => APP_CONFIG['access_key_id'], :secret_access_key => APP_CONFIG['secret_access_key'])
9
- BUCKET = APP_CONFIG['bucket']
10
-
11
- datestamp = Time.now.strftime("%Y-%m-%d_%H-%M-%S")
12
- base_path = ENV["RAILS_ROOT"] || "."
13
- file_name = "#{RAILS_ENV}_dump-#{datestamp}.sql.gz"
14
- backup_file = File.join(base_path, "tmp", file_name)
15
- db_config = ActiveRecord::Base.configurations[RAILS_ENV]
16
- sh "mysqldump -u #{db_config['username']} -p#{db_config['password']} -Q --add-drop-table -O add-locks=FALSE -O lock-tables=FALSE #{db_config['database']} | gzip -c > #{backup_file}"
17
- AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)
18
- puts "Created backup: #{file_name}"
19
- FileUtils.rm_rf(backup_file)
20
-
21
- bucket = AWS::S3::Bucket.find(BUCKET)
22
- all_backups = bucket.objects.select { |f| f.key.match(/dump/) }.sort { |a,b| a.key <=> b.key }.reverse
23
- max_backups = APP_CONFIG['database_backups_to_keep'].to_i || 28
24
- unwanted_backups = all_backups[max_backups..-1] || []
25
- for unwanted_backup in unwanted_backups
26
- unwanted_backup.delete
27
- puts "deleted #{unwanted_backup.key}"
28
- end
29
- puts "Deleted #{unwanted_backups.length} backups, #{all_backups.length - unwanted_backups.length} backups available"
30
- end
31
- end
Binary file