s3archive 1.3.0 → 1.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -20,7 +20,7 @@ module S3Archive
20
20
  def initialize(path)
21
21
  @path = path
22
22
  end
23
-
23
+
24
24
  def run
25
25
  unless File.exists?(path)
26
26
  logger.error("COULD NOT FIND '#{path}'")
@@ -36,14 +36,15 @@ module S3Archive
36
36
 
37
37
  private
38
38
  def compress?
39
- @do_compress ||= begin
40
- if path.end_with?('.gz')
39
+ unless defined? @compression_needed
40
+ if path.end_with?('.gz') || path.end_with?('.tgz')
41
41
  logger.info("** #{path} already compressed, skipping compression")
42
- false
42
+ @compression_needed = false
43
43
  else
44
- true
44
+ @compression_needed = true
45
45
  end
46
46
  end
47
+ @compression_needed
47
48
  end
48
49
 
49
50
  def compress!
@@ -76,7 +77,11 @@ module S3Archive
76
77
 
77
78
  def key
78
79
  year, month, day = Time.now.strftime("%Y-%m-%d").split('-')
79
- [hostname, year, month, day, "#{filename}.gz"].join('/')
80
+ [hostname, year, month, day, basename_gz].join('/')
81
+ end
82
+
83
+ def basename_gz
84
+ compress? ? "#{filename}.gz" : filename
80
85
  end
81
86
 
82
87
  def filename
@@ -0,0 +1,40 @@
1
+ require 'thor'
2
+ $:.unshift File.join(File.dirname(__FILE__), '..')
3
+ require 's3archive/compress_and_upload'
4
+ require 's3archive/logging'
5
+
6
+ module S3Archive
7
+ class DbDump
8
+ include Logging
9
+
10
+ attr_reader :db_config, :dump_path, :name
11
+
12
+ def initialize(db_config, name = 'dbdump')
13
+ @db_config = db_config
14
+ @name = name.gsub(/\s+/, '_')
15
+ end
16
+
17
+ def run
18
+ backup_filename = "#{name}-#{Time.now.utc.strftime("%Y%m%dT%H%M%S")}.sql.gz"
19
+ backup_path = "/tmp/#{backup_filename}"
20
+ logger.info "* Dumping and compressing data to #{backup_path}."
21
+ case db_config['adapter']
22
+ when /^mysql/
23
+ db_host = db_config['host']
24
+ db_name = db_config['database']
25
+ password = "-p#{db_config['password']}" if db_config['password']
26
+ backup_options = "--opt --skip-lock-tables --single-transaction --skip-extended-insert"
27
+ dump_options = "-u #{db_config['username']} #{password} #{backup_options} -h #{db_host} #{db_name}"
28
+ logger.info "** Backing up mysql db #{db_name}@#{db_host}"
29
+ system "mysqldump #{dump_options} | gzip -c > #{backup_path}"
30
+ else
31
+ db_config.delete 'password'
32
+ msg = "Don't know how to dump #{db_config['adapter']} database: #{db_config.inspect}'"
33
+ logger.fatal msg
34
+ raise msg
35
+ end
36
+
37
+ @dump_path = backup_path
38
+ end
39
+ end
40
+ end
@@ -2,6 +2,7 @@ require 'thor'
2
2
  $:.unshift File.join(File.dirname(__FILE__), '..')
3
3
  require 's3archive/postrotate'
4
4
  require 's3archive/compress_and_upload'
5
+ require 's3archive/db_dump'
5
6
  require 's3archive/logging'
6
7
 
7
8
  module S3Archive
@@ -59,6 +60,36 @@ EOF
59
60
  puts ::S3Archive::Config.sample_yaml
60
61
  end
61
62
 
63
+ desc "dbdump [options] CONFIG_PATH", "Dump database for ENV (default production) and upload"
64
+ method_option :environment,
65
+ :aliases => '-e',
66
+ :type => :string,
67
+ :default => 'production',
68
+ :banner => 'ENV',
69
+ :desc => "Use db configuration from this environment."
70
+ method_option :name,
71
+ :aliases => '-n',
72
+ :type => :string,
73
+ :default => 'dbdump',
74
+ :desc => 'Basename for the dump file. Timestamp and sql.gz will be added.'
75
+ long_desc <<-EOT
76
+ Dump and archive a database specified in the rails-style YAML file at CONFIG_PATH.
77
+ By default the production database will be dumped.
78
+ EOT
79
+ def dbdump(yaml_path)
80
+ db_config = YAML.load_file(yaml_path)[options[:environment]]
81
+ dumper = ::S3Archive::DbDump.new(db_config, options[:name])
82
+ begin
83
+ dumper.run
84
+ set_config
85
+ ::S3Archive::CompressAndUpload.run(dumper.dump_path)
86
+ rescue Exception => e
87
+ logger.error(e)
88
+ ensure
89
+ File.unlink(dumper.dump_path) if dumper.dump_path && File.exists?(dumper.dump_path)
90
+ end
91
+ end
92
+
62
93
  no_tasks do
63
94
  def set_config
64
95
  unless File.exists?(options[:config])
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Petter Remen
9
+ - David Vrensk
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2012-05-31 00:00:00.000000000 Z
13
+ date: 2012-06-07 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: thor
@@ -70,6 +71,7 @@ files:
70
71
  - bin/s3archive
71
72
  - lib/s3archive/compress_and_upload.rb
72
73
  - lib/s3archive/config.rb
74
+ - lib/s3archive/db_dump.rb
73
75
  - lib/s3archive/logging.rb
74
76
  - lib/s3archive/postrotate.rb
75
77
  - lib/s3archive/s3_file_synchronizer.rb