backup_driver 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: a378df25915fe18d6e94d2949f6f18e8b9666d5e
4
+ data.tar.gz: aed135c8be75849d5da4eaa03daa3147dbb90e9f
5
+ SHA512:
6
+ metadata.gz: a67b68257fee90001be6f0a3dd4eb54a8935b0fa67885d90c637056b653fa4c49d6dfc0735c54fd9b52cd11b21219fa3b4ac65a5b2b774df748f2a6a52ffbbec
7
+ data.tar.gz: 4bf272c699026d6c2caccc1b1fae815de3c320102e331375b96c10153340ae40f6e1f3525e787847177376a3737070c0c34a8ab239ed6fda3294524965909458
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in backup_driver.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vinicius Teles
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,63 @@
1
+ # BackupDriver
2
+
3
+ BackupDriver is a tool created to backup files from Unix systems. It's was created to address some very specific personal needs:
4
+
5
+ * Backup log files to Amazon S3 and remove them from the server
6
+ * Backup local mysql databases to Amazon S3
7
+
8
+ The generated backup file is encrypted using [gpg][] and sent to Amazon S3. The operations are performed using tools that must be installed on the system:
9
+
10
+ * [gpg][]
11
+ * [s3cmd][]
12
+ * [mysqldump][]
13
+
14
+ [Backup][b] is another tool that you might be interested in checking out. It's scope is much broader. So it can be useful in many situations that are not covered by BackupDriver.
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ gem 'backup_driver'
21
+
22
+ And then execute:
23
+
24
+ $ bundle
25
+
26
+ Or install it yourself as:
27
+
28
+ $ gem install backup_driver
29
+
30
+ ## Usage
31
+
32
+ ### Backup nginx log files
33
+
34
+ ```shell
35
+ backup_driver backup --source /var/log/nginx --bucket "s3://mybucket" --gpg-options "--recipient 'my@email.com'"
36
+ ```
37
+
38
+ **CAUTION**: BackupDriver removes the log files after sending them to Amazon S3. If you don't want that to happen, please consider using a different tool or change the code to suite your needs.
39
+
40
+ ### Backup mysql database
41
+
42
+ ```shell
43
+ backup_driver mysql --name db_name --user db_user --password db_password --gpg-options "--recipient 'my@email.com'"
44
+ ```
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ **Copyright (c) 2013 [Vinícius Manhães Teles][v] ([@viniciusteles][t])**
55
+ Released under the [MIT License][l].
56
+
57
+ [gpg]: http://www.gnupg.org/
58
+ [s3cmd]: http://s3tools.org/s3cmd
59
+ [mysqldump]: http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html
60
+ [b]: https://github.com/meskyanichi/backup
61
+ [v]: http://viniciusteles.com
62
+ [t]: https://twitter.com/viniciusteles
63
+ [l]: https://github.com/viniciusteles/backup_driver/blob/master/LICENSE.txt
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'backup_driver/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "backup_driver"
8
+ spec.version = BackupDriver::VERSION
9
+ spec.authors = ["Vinicius Teles"]
10
+ spec.email = ["vinicius@improveit.com.br"]
11
+ spec.description = %q{BackupDriver is a tool created to backup files from Unix systems.}
12
+ spec.summary = %q{BackupDriver can be used to backup log files and mysql databases to Amazon S3.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'thor', '= 0.18.1'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require File.expand_path("../../lib/backup_driver", __FILE__)
4
+ BackupDriver::CLI.start
@@ -0,0 +1,22 @@
1
+ require "thor"
2
+
3
+ module BackupDriver
4
+
5
+ LIBRARY_PATH = File.join(File.dirname(__FILE__), 'backup_driver')
6
+
7
+ %w(
8
+ command
9
+ create_command
10
+ mysql_create_command
11
+ encrypt_command
12
+ store_command
13
+ clean_command
14
+ driver
15
+ cli
16
+ version
17
+ ).each { |lib| require File.join(LIBRARY_PATH, lib) }
18
+
19
+ def self.backup
20
+ Driver.new.backup
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module BackupDriver
2
+ class CleanCommand < Command
3
+ def do
4
+ remove_source_files
5
+ remove_temp_dir
6
+ end
7
+
8
+ def remove_source_files
9
+ driver.log << `/bin/rm #{driver.create_command.source}/*`
10
+ end
11
+
12
+ def remove_temp_dir
13
+ driver.log << `/bin/rm -r #{driver.create_command.temp_dir}`
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module BackupDriver
2
+ class CLI < Thor
3
+ desc 'files', "Backup a file or directory"
4
+ option :gpg_options
5
+ option :source, :required => true
6
+ option :bucket, :required => true
7
+ def backup
8
+ Driver.new(options).backup
9
+ end
10
+
11
+ desc 'mysql', "Backup a mysql database"
12
+ option :mysql, :default => true
13
+ option :name, :required => true
14
+ option :user, :required => true
15
+ option :bucket, :required => true
16
+ option :gpg_options
17
+ option :password
18
+ def mysql
19
+ Driver.new(options).backup
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,13 @@
1
+ module BackupDriver
2
+ class Command
3
+ attr_reader :driver
4
+
5
+ def initialize(driver)
6
+ @driver = driver
7
+ end
8
+
9
+ def options
10
+ driver.options
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,49 @@
1
+ module BackupDriver
2
+ class CreateCommand < Command
3
+ def do
4
+ create_temp_dir
5
+ prepare_source
6
+ driver.log << `/bin/tar -C #{source_parent} -cvf #{backup_file} #{source_basename}`
7
+ end
8
+
9
+ def create_temp_dir
10
+ driver.log << `/bin/mkdir -p #{temp_dir}`
11
+ end
12
+
13
+ def temp_dir
14
+ "/tmp/backup_driver"
15
+ end
16
+
17
+ def prepare_source
18
+ # Implemented in subclasses only
19
+ end
20
+
21
+ def backup_file
22
+ "#{temp_dir}/#{file_name}"
23
+ end
24
+
25
+ def file_name
26
+ "#{backup_basename}-#{formatted_time}.tar"
27
+ end
28
+
29
+ def backup_basename
30
+ source_basename
31
+ end
32
+
33
+ def source_basename
34
+ File.basename(source)
35
+ end
36
+
37
+ def source
38
+ options[:source]
39
+ end
40
+
41
+ def source_parent
42
+ File.dirname(source)
43
+ end
44
+
45
+ def formatted_time
46
+ driver.time.strftime("%Y%m%d-%H%M%S")
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,44 @@
1
+ module BackupDriver
2
+ class Driver
3
+ attr_reader :options, :time, :log
4
+
5
+ def initialize(options)
6
+ @options = options
7
+ @time = Time.now
8
+ @log = []
9
+ end
10
+
11
+ def backup
12
+ create_command.do
13
+ encrypt_command.do
14
+ store_command.do
15
+ clean_command.do
16
+ output_log
17
+ end
18
+
19
+ def output_log
20
+ puts @log.join("\n")
21
+ end
22
+
23
+ def create_command
24
+ @create_command ||= select_create_command
25
+ end
26
+
27
+ def select_create_command
28
+ return MysqlCreateCommand.new(self) if options[:mysql]
29
+ CreateCommand.new(self)
30
+ end
31
+
32
+ def encrypt_command
33
+ @encrypt_command ||= EncryptCommand.new(self)
34
+ end
35
+
36
+ def store_command
37
+ @store_command ||= StoreCommand.new(self)
38
+ end
39
+
40
+ def clean_command
41
+ @clean_command ||= CleanCommand.new(self)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,15 @@
1
+ module BackupDriver
2
+ class EncryptCommand < Command
3
+ def do
4
+ driver.log << `/usr/bin/gpg #{options[:gpg_options]} --encrypt --compress-algo bzip2 --output #{encrypted_file} #{backup_file}`
5
+ end
6
+
7
+ def encrypted_file
8
+ "#{backup_file}.gpg"
9
+ end
10
+
11
+ def backup_file
12
+ driver.create_command.backup_file
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,21 @@
1
+ module BackupDriver
2
+ class MysqlCreateCommand < CreateCommand
3
+ def prepare_source
4
+ driver.log << `/bin/mkdir -p #{temp_dir}/mysql`
5
+ driver.log << `/usr/bin/mysqldump -u #{options[:user]} #{password_params} #{options[:name]} > #{source}/#{options[:name]}.sql`
6
+ end
7
+
8
+ def backup_basename
9
+ options[:name]
10
+ end
11
+
12
+ def source
13
+ "#{temp_dir}/mysql"
14
+ end
15
+
16
+ def password_params
17
+ return "-p #{options[:password]}" if options[:password]
18
+ ""
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,36 @@
1
+ module BackupDriver
2
+ class StoreCommand < Command
3
+ def do
4
+ driver.log << `/usr/bin/s3cmd put --reduced-redundancy #{encrypted_file} #{bucket}/#{path}`
5
+ end
6
+
7
+ def encrypted_file
8
+ driver.encrypt_command.encrypted_file
9
+ end
10
+
11
+ def bucket
12
+ options[:bucket]
13
+ end
14
+
15
+ def path
16
+ "#{basedir}#{basename}/#{year}/#{month}/"
17
+ end
18
+
19
+ def basedir
20
+ return "mysql/" if options[:mysql]
21
+ ""
22
+ end
23
+
24
+ def basename
25
+ driver.create_command.backup_basename
26
+ end
27
+
28
+ def year
29
+ driver.time.strftime("%Y")
30
+ end
31
+
32
+ def month
33
+ driver.time.strftime("%m-%b")
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module BackupDriver
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backup_driver
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Vinicius Teles
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.18.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.18.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: BackupDriver is a tool created to backup files from Unix systems.
56
+ email:
57
+ - vinicius@improveit.com.br
58
+ executables:
59
+ - backup_driver
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - .gitignore
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - backup_driver.gemspec
69
+ - bin/backup_driver
70
+ - lib/backup_driver.rb
71
+ - lib/backup_driver/clean_command.rb
72
+ - lib/backup_driver/cli.rb
73
+ - lib/backup_driver/command.rb
74
+ - lib/backup_driver/create_command.rb
75
+ - lib/backup_driver/driver.rb
76
+ - lib/backup_driver/encrypt_command.rb
77
+ - lib/backup_driver/mysql_create_command.rb
78
+ - lib/backup_driver/store_command.rb
79
+ - lib/backup_driver/version.rb
80
+ homepage: ''
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.0.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: BackupDriver can be used to backup log files and mysql databases to Amazon
104
+ S3.
105
+ test_files: []