s3-mysql-backup 2.0.2 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +22 -13
  2. data/lib/s3_mysql_backup.rb +36 -16
  3. data/lib/s3utils.rb +3 -2
  4. metadata +8 -8
data/README.md CHANGED
@@ -1,13 +1,13 @@
1
1
  # S3 MySQL Backup
2
2
 
3
- Simple backup of a MySQL database to Amazon S3,
4
- with email notification via Gmail.
3
+ Simple backup of a MySQL database to Amazon S3,
4
+ with email notification via SMTP.
5
5
 
6
6
 
7
7
  ## What does it do?
8
8
 
9
- It makes a gzipped and timestamped local backup of the specified
10
- database using mysqldump. The local backup is then copied to
9
+ It makes a gzipped and timestamped local backup of the specified
10
+ database using mysqldump. The local backup is then copied to
11
11
  Amazon S3, and the results are emailed to the specified recipient.
12
12
 
13
13
  Local and S3 backups are retained at this schedule:
@@ -17,7 +17,7 @@ Local and S3 backups are retained at this schedule:
17
17
 
18
18
  The email summary is a short email like:
19
19
 
20
- From: my-user@gmail.com
20
+ From: my-user@example.com
21
21
  To: my-recipient@example.com
22
22
  Date: 2012-12-22
23
23
  Subject: sql backup: my_database_name: 42.0 MB
@@ -31,18 +31,20 @@ Configure with a YAML file:
31
31
 
32
32
  ```yaml
33
33
 
34
- # backup_dir where to store the local backups
34
+ # backup_dir where to store the local backups, default is ~/s3_mysql_backups
35
35
  backup_dir: ~/s3_mysql_backups
36
+ # remote_dir OPTIONAL, where to store the remote backups, default is the root of your s3_bucket
37
+ remote_dir: /path/to/remote/backups
36
38
 
37
39
  # s3_access_key_id your Amazon S3 access_key_id
38
40
  # s3_secret_access_key your Amazon S3 secret_access_key
39
41
  # s3_bucket your Amazon S3 bucket for the backups
40
- # s3_server OPTIONAL: your non-Amazon S3-compatible server
42
+ # s3_server OPTIONAL, your non-Amazon S3-compatible server
41
43
  s3_access_key_id: my-key
42
44
  s3_secret_access_key: my-secret
43
45
  s3_bucket: my-bucket
44
46
 
45
- # dump_host OPTIONAL: your mysql host name
47
+ # dump_host OPTIONAL, your mysql host name
46
48
  # dump_user the database user for mysqldump
47
49
  # dump_pass the password for the dump user
48
50
  dump_user: my-user
@@ -51,14 +53,18 @@ dump_pass: my-pass
51
53
  # mail_to where to send the backup summary email
52
54
  mail_to: recipient@example.com
53
55
 
54
- # Gmail credentials
55
- gmail_user: me@gmail.com
56
- gmail_pass: gmail-password
56
+ # Mail credentials
57
+ mail_user: me@example.com
58
+ mail_pass: example_password
59
+ mail_domain: smtp.example.com # OPTIONAL, defaults to: smtp.gmail.com
60
+ mail_port: 587 # OPTIONAL, defaults to: 587
61
+ mail_authentication: login # OPTIONAL, defaults to: :login
62
+ mail_start_tls: true # OPTIONAL, defaults to: true
57
63
 
58
64
  ```
59
65
 
60
66
 
61
- ## Installation
67
+ ## Installation
62
68
 
63
69
  gem install s3-mysql-backup
64
70
 
@@ -67,7 +73,9 @@ gmail_pass: gmail-password
67
73
 
68
74
  From Ruby:
69
75
 
70
- S3MysqlBackup.new('database_name', '/path/to/s3-mysql-backup-config.yml').run
76
+ S3MysqlBackup.new("database_name", "/path/to/s3-mysql-backup-config.yml").run
77
+ or
78
+ S3MysqlBackup.new("database_name", { hash: of_options }).run
71
79
 
72
80
  From command line:
73
81
 
@@ -84,6 +92,7 @@ Write tests
84
92
 
85
93
 
86
94
  ## Changelog
95
+ - 2013-10-13 2.1.0 Allow other mail hosts, allow Hash config at runtime (https://github.com/7compass/s3-mysql-backup/pull/7)
87
96
  - 2013-06-01 1.2.2 Bugfix for passwords with spaces
88
97
  - 2013-06-01 1.2.1 Added mysql host option (github.com/sagrimson)
89
98
  - 2013-05-30 1.1.0 Added support for S3-compatible services, e.g. DreamObjects (thanks to John N. Milner - github.com/jnm)
@@ -7,30 +7,43 @@ require File.dirname(__FILE__) + '/s3utils'
7
7
 
8
8
  #
9
9
  class S3MysqlBackup
10
-
10
+
11
11
  def initialize(db_name, path_to_config)
12
12
  @db_name = db_name
13
13
  @path_to_config = path_to_config
14
14
 
15
15
  self
16
16
  end
17
-
17
+
18
18
  def run
19
19
  ensure_backup_dir_exists
20
-
21
20
  connect_to_s3
22
-
23
21
  remove_old_backups
24
-
25
22
  mail_notification(dump_db)
26
23
  end
27
-
28
-
24
+
25
+
29
26
  protected
30
-
27
+
31
28
  def config
32
- defaults = { 'dump_host' => 'localhost' }
33
- @s3config ||= defaults.merge(YAML::load_file(@path_to_config))
29
+ defaults = {
30
+ "dump_host" => "localhost",
31
+ "mail_domain" => "smtp.gmail.com",
32
+ "mail_port" => "587",
33
+ "mail_authentication" => :login,
34
+ "backup_dir" => "~/s3_mysql_backups",
35
+ }
36
+
37
+ if @s3config.nil?
38
+ @s3config = @path_to_config.is_a?(Hash) ? defaults.merge(stringify_keys(@path_to_config)) : defaults.merge(YAML::load_file(@path_to_config))
39
+
40
+ # Backcompat for gmail_* keys
41
+ @s3config.keys.each do |key|
42
+ @s3config[key.sub(/^gmail/, "mail")] = @s3config.delete(key)
43
+ end
44
+ end
45
+
46
+ @s3config
34
47
  end
35
48
 
36
49
  def connect_to_s3
@@ -42,7 +55,7 @@ class S3MysqlBackup
42
55
  filename = Time.now.strftime("#{@backup_dir}/#{@db_name}.%Y%m%d.%H%M%S.sql.gz")
43
56
  mysqldump = `which mysqldump`.to_s.strip
44
57
  `#{mysqldump} --host='#{config['dump_host']}' --user='#{config['dump_user']}' --password='#{config['dump_pass']}' '#{@db_name}' | gzip > #{filename}`
45
- @s3utils.store(filename)
58
+ @s3utils.store(filename, config['remote_dir'])
46
59
  filename
47
60
  end
48
61
 
@@ -66,17 +79,17 @@ class S3MysqlBackup
66
79
  subject = "sql backup: #{@db_name}: #{human_size(stats.size)}"
67
80
 
68
81
  content = []
69
- content << "From: #{config['gmail_user']}"
82
+ content << "From: #{config['mail_user']}"
70
83
  content << "To: #{config['mail_to']}"
71
84
  content << "Subject: #{subject}"
72
85
  content << "Date: #{Time.now.rfc2822}"
73
86
  content << "\n#{File.basename(filename)}\n" # body
74
87
  content = content.join("\n")
75
88
 
76
- smtp = Net::SMTP.new("smtp.gmail.com", 587)
77
- smtp.enable_starttls
78
- smtp.start("smtp.gmail.com", config['gmail_user'], config['gmail_pass'], :login) do
79
- smtp.send_message(content, config['gmail_user'], config['mail_to'])
89
+ smtp = Net::SMTP.new(config["mail_domain"], config["mail_port"])
90
+ smtp.enable_starttls unless config["mail_start_tls"] == false
91
+ smtp.start(config["mail_domain"], config['mail_user'], config['mail_pass'], config['mail_authentication']) do
92
+ smtp.send_message(content, config['mail_user'], config['mail_to'])
80
93
  end
81
94
  end
82
95
 
@@ -111,4 +124,11 @@ class S3MysqlBackup
111
124
  end # Dir.each
112
125
  end # remove_old_backups
113
126
 
127
+ def stringify_keys(hash)
128
+ hash.keys.each do |key|
129
+ hash[key.to_s] = hash.delete(key)
130
+ end
131
+ hash
132
+ end
133
+
114
134
  end
data/lib/s3utils.rb CHANGED
@@ -11,8 +11,9 @@ class S3Utils
11
11
  self
12
12
  end
13
13
 
14
- def store(file_path)
15
- @s3_bucket.objects.create(File.basename(file_path), open(file_path))
14
+ def store(file_path, remote_path=nil)
15
+ upload_location = (!remote_path.nil? && !remote_path.empty?) ? "#{remote_path}/#{File.basename(file_path)}" : File.basename(file_path)
16
+ @s3_bucket.objects.create(upload_location, open(file_path))
16
17
  end
17
18
 
18
19
  def delete(file_path)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3-mysql-backup
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.2
4
+ version: 2.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-09-29 00:00:00.000000000 Z
12
+ date: 2013-10-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: aws-sdk
16
- requirement: &70338179173760 !ruby/object:Gem::Requirement
16
+ requirement: &70118190119740 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70338179173760
24
+ version_requirements: *70118190119740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70338179173280 !ruby/object:Gem::Requirement
27
+ requirement: &70118190119260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '2.12'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70338179173280
35
+ version_requirements: *70118190119260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rr
38
- requirement: &70338179172820 !ruby/object:Gem::Requirement
38
+ requirement: &70118190118800 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,7 +43,7 @@ dependencies:
43
43
  version: 1.0.5
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70338179172820
46
+ version_requirements: *70118190118800
47
47
  description: A simple mysql backup to Amazon S3
48
48
  email: jeff@7compass.com
49
49
  executables: