s3archive 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ # The main entry point db:backup_to_s3 is run by /etc/logrotate.d/rails3app
2
+ # daily. changed
3
+
4
+ require 'thor'
5
+ require 'socket' # For hostname
6
+ require 'tempfile'
7
+ require_relative 'logging'
8
+ require_relative 'config'
9
+ require_relative 's3_file_synchronizer'
10
+
11
+ module S3Archive
12
+ class Postrotate
13
+ include Logging
14
+
15
+ def self.run(logfile_path)
16
+ new(logfile_path).run
17
+ end
18
+
19
+ attr_reader :logfile_path
20
+ def initialize(logfile_path)
21
+ @logfile_path = logfile_path
22
+ end
23
+
24
+ def run
25
+ raise "Could not find #{prev_logfile}" unless File.exists?(prev_logfile)
26
+ bucket = S3Archive.config.bucket
27
+ logger.info("* Uploading #{prev_logfile} to s3://#{bucket}/#{key}")
28
+ S3FileSynchronizer.run(prev_logfile, bucket, key)
29
+ end
30
+
31
+ private
32
+
33
+ def key
34
+ year, month, day = prev_date.strftime("%Y-%m-%d").split('-') # Quick & dirty way to obtain leading zeroes in months and days
35
+ [hostname, year, month, day, File.basename(prev_logfile)].join('/')
36
+ end
37
+
38
+ def hostname
39
+ Socket.gethostname
40
+ end
41
+
42
+ def prev_logfile
43
+ "#{logfile_basename}-#{prev_date_str}.gz"
44
+ end
45
+
46
+ def prev_date_str
47
+ prev_date.strftime("%Y%m%d")
48
+ end
49
+
50
+ def prev_date
51
+ logfile_date.prev_day
52
+ end
53
+
54
+ def logfile_date
55
+ parse_logfile_path unless @logfile_date
56
+ @logfile_date
57
+ end
58
+
59
+ def logfile_basename
60
+ parse_logfile_path unless @logfile_basename
61
+ @logfile_basename
62
+ end
63
+
64
+ def parse_logfile_path
65
+ logfile_path =~ /\A(.*)-(\d{8})\z/ || raise("Could not parse logfile path")
66
+ @logfile_basename = $1
67
+ @logfile_date = Date.strptime($2, "%Y%m%d")
68
+ end
69
+ end
70
+ end
@@ -1,11 +1,12 @@
1
1
  require 'thor'
2
2
  $:.unshift File.join(File.dirname(__FILE__), '..')
3
+ require 's3archive/postrotate'
3
4
  require 's3archive/compress_and_upload'
4
5
  require 's3archive/logging'
5
6
 
6
7
  module S3Archive
7
8
  class Cli < Thor
8
- include Logging
9
+ include ::S3Archive::Logging
9
10
  namespace :s3archive
10
11
 
11
12
  class_option :config,
@@ -15,31 +16,57 @@ module S3Archive
15
16
  :aliases => "-c",
16
17
  :default => "/etc/s3archive.yml"
17
18
 
18
- desc "upload PATH [PATH...]", "Sleeps, compresses and uploads to s3://<bucket>/<year>/<month>/<day>/<filename>.gz"
19
- method_option :sleep,
20
- :aliases => "-s",
21
- :default => 5,
22
- :banner => "SECONDS",
23
- :desc => "The number of seconds to sleep before uploading"
19
+ desc "postrotate LOGFILE [LOGFILE...]", "A postrotate hook for logrotate."
20
+ long_desc <<-EOF
21
+ Uploads the logfile that was rotated yesterday! I.e., after
22
+ logrotate has run, you will typically three files: application.log-20100805.gz
23
+ (this is the file that is uploaded), application.log-20100806 and
24
+ application.log.
24
25
 
25
- def upload (*paths)
26
- unless File.exists?(options[:config])
27
- logger.error("Could not find config file #{options[:config]}")
28
- exit(1)
26
+ Note: For this to work, you *must* have the following stanzas in
27
+ the logrotate configuration: compress, delayedcompress, dateext & ifempty.
28
+ EOF
29
+
30
+ def postrotate (*paths)
31
+ set_config
32
+ help(:postrotate) && exit if paths.empty?
33
+
34
+ paths.each do |path|
35
+ begin
36
+ ::S3Archive::Postrotate.run(path)
37
+ rescue Exception => e
38
+ logger.error(e)
39
+ end
29
40
  end
41
+ end
30
42
 
31
- S3Archive.config_path = options[:config]
43
+ desc "upload FILE [FILE...]", "Uploads the file(s) (compressed) to s3://bucket/<hostname>/<year>/<month>/<day>/<filename>.gz"
44
+ def upload (*paths)
45
+ set_config
32
46
  help(:upload) && exit if paths.empty?
33
47
 
34
- logger.info("Sleeping for #{options[:sleep]} seconds") && sleep(options[:sleep])
35
48
  paths.each do |path|
36
- CompressAndUpload.run(path)
49
+ begin
50
+ ::S3Archive::CompressAndUpload.run(path)
51
+ rescue Exception => e
52
+ logger.error(e)
53
+ end
37
54
  end
38
55
  end
39
56
 
40
57
  desc "genconfig", "Prints a sample config file to stdout"
41
58
  def genconfig
42
- puts S3Archive::Config.sample_yaml
59
+ puts ::S3Archive::Config.sample_yaml
60
+ end
61
+
62
+ no_tasks do
63
+ def set_config
64
+ unless File.exists?(options[:config])
65
+ logger.error("Could not find config file #{options[:config]}")
66
+ exit(1)
67
+ end
68
+ ::S3Archive.config_path = options[:config]
69
+ end
43
70
  end
44
71
  end
45
72
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: s3archive
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -71,6 +71,7 @@ files:
71
71
  - lib/s3archive/compress_and_upload.rb
72
72
  - lib/s3archive/config.rb
73
73
  - lib/s3archive/logging.rb
74
+ - lib/s3archive/postrotate.rb
74
75
  - lib/s3archive/s3_file_synchronizer.rb
75
76
  - lib/tasks/s3archive.thor
76
77
  homepage: http://github.com/spab/s3archive