astrails-safe 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/astrails/safe.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'extensions/mktmpdir'
1
2
  require 'astrails/safe/tmp_file'
2
3
  require 'astrails/safe/config/node'
3
4
  require 'astrails/safe/config/builder'
@@ -20,6 +21,7 @@ module Astrails
20
21
 
21
22
  Astrails::Safe::Mysqldump.run(config[:mysqldump, :databases], timestamp)
22
23
  Astrails::Safe::Archive.run(config[:tar, :archives], timestamp)
24
+ Astrails::Safe::TmpFile.cleanup
23
25
  end
24
26
  end
25
27
  end
@@ -6,6 +6,8 @@ module Astrails
6
6
  "tar -cf - #{@config[:options]} #{tar_exclude_files} #{tar_files}"
7
7
  end
8
8
 
9
+ def extension; 'tar'; end
10
+
9
11
  protected
10
12
 
11
13
  def tar_exclude_files
@@ -3,7 +3,7 @@ module Astrails
3
3
  module Config
4
4
  class Builder
5
5
  COLLECTIONS = %w/database archive/
6
- ITEMS = %w/s3 key secret bucket path gpg password keep local mysqldump options
6
+ ITEMS = %w/s3 key secret bucket path prefix gpg password keep local mysqldump options
7
7
  user socket tar files exclude/
8
8
  NAMES = COLLECTIONS + ITEMS
9
9
  def initialize(node)
@@ -21,7 +21,7 @@ module Astrails
21
21
  def run
22
22
  puts "#{kind}: #{@id}" if $_VERBOSE
23
23
 
24
- stream = Stream.new(@config, command, File.join(path, backup_filename))
24
+ stream = Stream.new(@config, command, backup_filepath)
25
25
  stream.run # execute backup comand. result is file stream.filename
26
26
 
27
27
  # UPLOAD
@@ -34,24 +34,26 @@ module Astrails
34
34
 
35
35
  protected
36
36
 
37
- def kind
38
- self.class.name.split('::').last.downcase
37
+ def self.kind
38
+ name.split('::').last.downcase
39
39
  end
40
-
41
- def backup_filename
42
- @backup_filename ||= "#{kind}-#{id}.#{timestamp}.tar"
40
+ def kind
41
+ self.class.kind
43
42
  end
44
43
 
45
44
  def expand(path)
46
- path.gsub(/:kind\b/, kind).gsub(/:id\b/, id)
45
+ path .
46
+ gsub(/:kind\b/, kind) .
47
+ gsub(/:id\b/, id) .
48
+ gsub(/:timestamp\b/, timestamp)
47
49
  end
48
50
 
49
- def s3_path
50
- @s3_path ||= expand(@config[:s3, :path] || ":kind/:id")
51
+ def s3_prefix
52
+ @s3_prefix ||= expand(@config[:s3, :prefix] || ":kind/:id")
51
53
  end
52
54
 
53
- def path
54
- @path ||= File.expand_path(expand(@config[:path] || raise(RuntimeError, "missing :path in configuration")))
55
+ def backup_filepath
56
+ @backup_filepath ||= File.expand_path(expand(@config[:path] || raise(RuntimeError, "missing :path in configuration"))) << "." << extension
55
57
  end
56
58
 
57
59
 
@@ -63,7 +65,7 @@ module Astrails
63
65
 
64
66
  return unless bucket && key && secret
65
67
 
66
- upload_path = File.join(s3_path, File.basename(filename))
68
+ upload_path = File.join(s3_prefix, File.basename(filename))
67
69
 
68
70
  puts "Uploading file #{filename} to #{bucket}/#{upload_path}" if $_VERBOSE || $DRY_RUN
69
71
  if $LOCAL
@@ -74,7 +76,7 @@ module Astrails
74
76
 
75
77
  unless $DRY_RUN
76
78
  AWS::S3::Bucket.create(bucket)
77
- AWS::S3::S3Object.store(s3_path, open(filename), bucket)
79
+ AWS::S3::S3Object.store(upload_path, open(filename), bucket)
78
80
  end
79
81
  end
80
82
  puts "...done" if $_VERBOSE
@@ -112,12 +114,12 @@ module Astrails
112
114
 
113
115
  base = File.basename(filename).split(".").first
114
116
 
115
- puts "listing files in #{bucket}:#{s3_path}"
116
- files = AWS::S3::Bucket.objects(bucket, :prefix => s3_path, :max_keys => keep * 2)
117
- puts files.collect(&:key) if $_VERBOSE
117
+ puts "listing files in #{bucket}:#{s3_prefix}"
118
+ files = AWS::S3::Bucket.objects(bucket, :prefix => s3_prefix, :max_keys => keep * 2)
119
+ puts files.collect {|x| x.key} if $_VERBOSE
118
120
 
119
121
  files = files.
120
- collect(&:key).
122
+ collect {|x| x.key}.
121
123
  select{|f| File.basename(f)[0..(base.length - 1)] == base}.
122
124
  sort
123
125
 
@@ -6,6 +6,8 @@ module Astrails
6
6
  "mysqldump --defaults-extra-file=#{password_file} #{@config[:options]} #{mysql_skip_tables} #{@id}"
7
7
  end
8
8
 
9
+ def extension; 'sql'; end
10
+
9
11
  protected
10
12
 
11
13
  def password_file
@@ -5,6 +5,10 @@ module Astrails
5
5
  @KEEP_FILES = []
6
6
  TMPDIR = Dir.mktmpdir
7
7
 
8
+ def self.cleanup
9
+ FileUtils.remove_entry_secure TMPDIR
10
+ end
11
+
8
12
  def self.create(name)
9
13
  # create temp directory
10
14
 
@@ -0,0 +1,45 @@
1
+ require 'tmpdir'
2
+
3
+ unless Dir.respond_to?(:mktmpdir)
4
+ # backward compat for 1.8.6
5
+ class Dir
6
+ def Dir.mktmpdir(prefix_suffix=nil, tmpdir=nil)
7
+ case prefix_suffix
8
+ when nil
9
+ prefix = "d"
10
+ suffix = ""
11
+ when String
12
+ prefix = prefix_suffix
13
+ suffix = ""
14
+ when Array
15
+ prefix = prefix_suffix[0]
16
+ suffix = prefix_suffix[1]
17
+ else
18
+ raise ArgumentError, "unexpected prefix_suffix: #{prefix_suffix.inspect}"
19
+ end
20
+ tmpdir ||= Dir.tmpdir
21
+ t = Time.now.strftime("%Y%m%d")
22
+ n = nil
23
+ begin
24
+ path = "#{tmpdir}/#{prefix}#{t}-#{$$}-#{rand(0x100000000).to_s(36)}"
25
+ path << "-#{n}" if n
26
+ path << suffix
27
+ Dir.mkdir(path, 0700)
28
+ rescue Errno::EEXIST
29
+ n ||= 0
30
+ n += 1
31
+ retry
32
+ end
33
+
34
+ if block_given?
35
+ begin
36
+ yield path
37
+ ensure
38
+ FileUtils.remove_entry_secure path
39
+ end
40
+ else
41
+ path
42
+ end
43
+ end
44
+ end
45
+ end
data/safe.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "safe"
3
- s.version = "0.0.8"
3
+ s.version = "0.0.9"
4
4
  s.date = "2009-03-15"
5
5
  s.summary = "Astrails Safe"
6
6
  s.email = "we@astrails.com"
@@ -18,6 +18,7 @@ Gem::Specification.new do |s|
18
18
  lib/astrails/safe/engine.rb
19
19
  lib/astrails/safe/archive.rb
20
20
  lib/astrails/safe/tmp_file.rb
21
+ lib/extensions/mktmpdir.rb
21
22
  templates/script.rb
22
23
  safe.gemspec
23
24
  )
data/templates/script.rb CHANGED
@@ -1,10 +1,12 @@
1
1
  safe do
2
- # global path
2
+ # backup file path (full, including filename)
3
+ # Note: do not include .tar, .sql, .gz or .pgp, it will be added automatically
3
4
  # supported substitutions:
4
5
  # :kind -> backup 'engine' kind, e.g. "mysqldump" or "archive"
5
6
  # :id -> backup 'id', e.g. "blog", "production", etc.
7
+ # :timestamp -> current run timestamp (same for all the backups in the same 'run')
6
8
  # you can set separate :path for all backups (or once globally here)
7
- path "/backup/:kind/:id"
9
+ path "/backup/:kind/:id-:timestamp"
8
10
 
9
11
  ## uncomment to enable uploads to Amazon S3
10
12
  ## Amazon S3 auth (optional)
@@ -12,6 +14,8 @@ safe do
12
14
  # key YOUR_S3_KEY
13
15
  # secret YOUR_S3_SECRET
14
16
  # bucket S3_BUCKET
17
+ # # prefix for uploads to S3. supports same substitution like :path
18
+ # prefix ":kind/:id" # this is default
15
19
  # end
16
20
 
17
21
  ## alternative style:
@@ -34,8 +38,6 @@ safe do
34
38
  # backup mysql databases with mysqldump
35
39
  mysqldump do
36
40
  # you can override any setting from parent in a child:
37
- path "/backup/mysql/:id"
38
-
39
41
  options "-ceKq --single-transaction --create-options"
40
42
 
41
43
  user "astrails"
@@ -69,8 +71,6 @@ safe do
69
71
 
70
72
 
71
73
  tar do
72
- path "/backup/archives"
73
-
74
74
  # 'archive' is a collection item, just like 'database'
75
75
  # archive "git-repositories" do
76
76
  # # files and directories to backup
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: astrails-safe
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.8
4
+ version: 0.0.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Astrails Ltd.
@@ -40,6 +40,7 @@ files:
40
40
  - lib/astrails/safe/engine.rb
41
41
  - lib/astrails/safe/archive.rb
42
42
  - lib/astrails/safe/tmp_file.rb
43
+ - lib/extensions/mktmpdir.rb
43
44
  - templates/script.rb
44
45
  - safe.gemspec
45
46
  has_rdoc: false