s3_db_assets_backup 0.1.5 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,13 +1,12 @@
1
1
  = S3DbAssetsBackup
2
2
 
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).
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. Additionally, the plugin can be configured to generate and send a backup status report via email.
4
4
 
5
5
  == Install
6
6
 
7
- Edit your Gemfile to include the following
7
+ Edit your Gemfile to include
8
8
 
9
9
  gem "s3_db_assets_backup"
10
- gem "aws-s3"
11
10
 
12
11
  Then run
13
12
 
@@ -17,7 +16,7 @@ Create the config file.
17
16
 
18
17
  rails g s3_backup
19
18
 
20
- Edit config/s3_backup_config.yml with your aws credentials.
19
+ Edit config/s3_backup_config.yml with your aws credentials and email settings
21
20
 
22
21
  Rails 2.x (switch to the rails_2.x branch)
23
22
 
@@ -39,21 +38,37 @@ Restore the public folder (you will be prompted to select an existing backup)
39
38
 
40
39
  rake assets:restore
41
40
 
42
- Optionally setup cron to do your backups with the following crontab
43
-
44
- # db backup every day at 11:01
45
- 1 23 * * * cd /path/to/your/project && /var/lib/gems/1.8/bin/rake db:backup RAILS_ENV=production
46
-
47
- # asset backup every wednesday at 11:30
48
- 30 23 * * 3 cd /path/to/your/project && /var/lib/gems/1.8/bin/rake assets:backup
41
+ Send a backup status report to the configured email address
42
+
43
+ rake db:backup:status
44
+
45
+ == Scheduling with whenever
46
+
47
+ Optionally, you can use "whenever" to automate backups and status reports.
48
+
49
+ Add whenever to your Gemfile
50
+
51
+ gem 'whenever', :require => false
52
+
53
+ To create the whenever config file, run the following command from the app root directory.
54
+
55
+ wheneverize .
56
+
57
+ Edit config/schedule.rb to schedule the rake tasks.
58
+
59
+ every 12.hours do
60
+ rake "db:backup"
61
+ end
49
62
 
50
- Running RVM and Ruby 1.9.2, I had to setup cron like this (rvm is loaded in .profile).
63
+ every 4.days do
64
+ rake "db:backup:status"
65
+ end
51
66
 
52
- SHELL=/bin/bash
53
- HOME=/home/ubuntu
67
+ Then run the following command to write the config to your cron file
54
68
 
55
- # db backup every day at 11:01
56
- 01 23 * * * source ~/.profile && cd /home/ubuntu/cpos && rake db:backup RAILS_ENV=production
69
+ whenever -w
70
+
71
+ For more information on the whenever gem, visit https://github.com/javan/whenever
57
72
 
58
73
  == Troubleshooting and FAQs
59
74
 
@@ -67,6 +82,10 @@ You need to create a tmp directory in the project root.
67
82
 
68
83
  You need to create the S3 bucket you're trying to backup to.
69
84
 
85
+ <b>My crontab doesn't run in development.</b>
86
+
87
+ Deving on a mac? Cron is deprecated in Mountain Lion.
88
+
70
89
  == Found a bug?
71
90
 
72
91
  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.
@@ -1,6 +1,7 @@
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
+ EMAIL_CONFIG = YAML.load_file(config_file)["email_config"].symbolize_keys
4
5
  else
5
6
  puts "WARNING: Can't find s3_backup_config.yml"
6
- end
7
+ end
@@ -22,4 +22,19 @@ production:
22
22
  # how many backups would you like to keep for
23
23
  # the database and public folder
24
24
  database_backups_to_keep: 8
25
- assets_backups_to_keep: 3
25
+ assets_backups_to_keep: 3
26
+
27
+ email_config:
28
+ # email address that will receive email reports
29
+ to: report_receiver@domain.com
30
+
31
+ # server settings that Pony will use to send emails
32
+ via: :smtp
33
+ subject: <MYSITE> Backup Report
34
+ address: smtp_server_address
35
+ port: '587'
36
+ enable_starttls_auto: true
37
+ user_name: email_user_name
38
+ password: email_password
39
+ authentication: :plain
40
+ domain: HELO
@@ -1,23 +1,32 @@
1
1
  require 'find'
2
2
  require 'fileutils'
3
3
  require 'aws/s3'
4
+ require 'pony'
4
5
 
5
6
  namespace :db do
6
7
  desc "Backup the database to a file. Options: RAILS_ENV=production"
7
8
  task :backup => [:environment] do
9
+ # Establish connection to Amazon S3 and fetch the bucket name from the config
8
10
  AWS::S3::Base.establish_connection!(:access_key_id => S3_CONFIG[:access_key_id], :secret_access_key => S3_CONFIG[:secret_access_key])
9
11
  BUCKET = S3_CONFIG[:bucket]
10
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
16
  file_name = "#{Rails.env}_dump-#{datestamp}.sql.gz"
14
17
  backup_file = File.join(base_path, "tmp", file_name)
18
+
19
+ # Load database configuration and dump the sql database to the backup file
15
20
  db_config = ActiveRecord::Base.configurations[Rails.env]
16
21
  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}"
22
+
23
+ # Upload the backup file to Amazon and remove the file from the local filesystem
17
24
  AWS::S3::S3Object.store(file_name, open(backup_file), BUCKET)
18
25
  puts "Created backup: #{file_name}"
19
26
  FileUtils.rm_rf(backup_file)
20
27
 
28
+ # Check the bucket for the number of backups and remove the oldest backups if
29
+ # it is over the number of backups sets configured
21
30
  bucket = AWS::S3::Bucket.find(BUCKET)
22
31
  all_backups = bucket.objects.select { |f| f.key.match(/dump/) }.sort { |a,b| a.key <=> b.key }.reverse
23
32
  max_backups = S3_CONFIG[:database_backups_to_keep].to_i || 28
@@ -73,4 +82,37 @@ namespace :db do
73
82
  end
74
83
  end
75
84
  end
76
- end
85
+ end
86
+
87
+ namespace :db do
88
+ namespace :backup do
89
+ desc "Email a report of current backups."
90
+ task :status => [:environment] do
91
+ AWS::S3::Base.establish_connection!(:access_key_id => S3_CONFIG[:access_key_id], :secret_access_key => S3_CONFIG[:secret_access_key])
92
+ BUCKET = S3_CONFIG[:bucket]
93
+ bucket = AWS::S3::Bucket.find(BUCKET)
94
+ all_backups = bucket.objects.select { |f| f.key.match(/dump/) }.sort { |a,b| a.key <=> b.key }.reverse
95
+
96
+ message = "Archive contains the following backups:\n\n"
97
+ all_backups.each { |f| message << f.key + "\n" }
98
+ email = EMAIL_CONFIG
99
+
100
+ Pony.mail(
101
+ :to => email[:to],
102
+ :via => email[:via],
103
+ :subject => email[:subject],
104
+ :body => message,
105
+
106
+ :via_options => {
107
+ :address => email[:address],
108
+ :port => email[:port],
109
+ :enable_starttls_auto => email[:enable_starttls_auto],
110
+ :user_name => email[:user_name],
111
+ :password => email[:password],
112
+ :authentication => email[:authentication],
113
+ :domain => email[:domain]
114
+ }
115
+ )
116
+ end
117
+ end
118
+ end
metadata CHANGED
@@ -2,18 +2,40 @@
2
2
  name: s3_db_assets_backup
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.5
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Chris Barnes
9
+ - Michael Burk
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
13
 
13
- date: 2012-10-09 00:00:00 Z
14
- dependencies: []
15
-
16
- 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. This gem is based on (http://www.magnionlabs.com/2009/7/7/db-backups).
14
+ date: 2012-11-11 00:00:00 Z
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: pony
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "1.4"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: aws-s3
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: 0.6.3
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.
17
39
  email: chris@randomutterings.com
18
40
  executables: []
19
41
 
@@ -54,10 +76,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
54
76
  requirements: []
55
77
 
56
78
  rubyforge_project:
57
- rubygems_version: 1.7.2
79
+ rubygems_version: 1.8.24
58
80
  signing_key:
59
81
  specification_version: 3
60
- summary: Generates rake tasks for backing up and restoring db and public folder to and from an existing S3 bucket
82
+ summary: Rake tasks for backing up and restoring db and public folder to and from an existing S3 bucket with email notifications.
61
83
  test_files: []
62
84
 
63
85
  has_rdoc: