backupit 0.0.1

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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jinzhu
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,22 @@
1
+ = BackUpIt
2
+
3
+ == Configuration File Example
4
+ storage :file do
5
+ path '/your_backup_path'
6
+ end
7
+
8
+ server 'delonghi' do
9
+ command "delonghi@10.0.1.1 -p222"
10
+ rsync ['/home/www/app/shared/config', '/home/www/app/shared/attachments']
11
+
12
+ mysql 'delonghi' do
13
+ user 'root'
14
+ password 'mypassword'
15
+ options '-h 192.168.1.100'
16
+ databases ['delonghi-staging', 'delonghi-production']
17
+ tables ['users','products'] # this would overwrite databases! `man mysqldump` for more help.
18
+ end
19
+ end
20
+
21
+
22
+ == Copyright (c) 2010 Jinzhu. See LICENSE for details.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = backupit
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Jinzhu. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "backupit"
8
+ gem.summary = %Q{A tool to backup your servers}
9
+ gem.description = %Q{A tool to backup your servers}
10
+ gem.email = "wosmvp@gmail.com"
11
+ gem.homepage = "http://github.com/jinzhu/backupit"
12
+ gem.authors = ["Jinzhu"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "backupit #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
data/backupit.gemspec ADDED
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{backupit}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jinzhu"]
12
+ s.date = %q{2010-09-27}
13
+ s.default_executable = %q{backup}
14
+ s.description = %q{A tool to backup your servers}
15
+ s.email = %q{wosmvp@gmail.com}
16
+ s.executables = ["backup"]
17
+ s.extra_rdoc_files = [
18
+ "LICENSE",
19
+ "README",
20
+ "README.rdoc"
21
+ ]
22
+ s.files = [
23
+ ".document",
24
+ ".gitignore",
25
+ "LICENSE",
26
+ "README",
27
+ "README.rdoc",
28
+ "Rakefile",
29
+ "VERSION",
30
+ "backupit.gemspec",
31
+ "bin/backup",
32
+ "lib/backup.rb",
33
+ "lib/backup/attribute.rb",
34
+ "lib/backup/configuration/base.rb",
35
+ "lib/backup/configuration/mysql.rb",
36
+ "lib/backup/configuration/server.rb",
37
+ "lib/backup/configuration/storage.rb",
38
+ "lib/backup/server.rb",
39
+ "lib/backup/storage.rb",
40
+ "test/helper.rb",
41
+ "test/test_backupit.rb"
42
+ ]
43
+ s.homepage = %q{http://github.com/jinzhu/backupit}
44
+ s.rdoc_options = ["--charset=UTF-8"]
45
+ s.require_paths = ["lib"]
46
+ s.rubygems_version = %q{1.3.7}
47
+ s.summary = %q{A tool to backup your servers}
48
+ s.test_files = [
49
+ "test/test_backupit.rb",
50
+ "test/helper.rb"
51
+ ]
52
+
53
+ if s.respond_to? :specification_version then
54
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
55
+ s.specification_version = 3
56
+
57
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
58
+ else
59
+ end
60
+ else
61
+ end
62
+ end
63
+
data/bin/backup ADDED
@@ -0,0 +1,42 @@
1
+ #!/usr/bin/env ruby
2
+ begin
3
+ require 'rubygems'
4
+ require 'colored'
5
+ rescue LoadError
6
+ puts '`colored` is not available. you should: gem install colored'
7
+ exit
8
+ end
9
+
10
+ require 'optparse'
11
+ require 'lib/backup'
12
+
13
+ options = {}
14
+
15
+ OptionParser.new do |opts|
16
+ opts.banner = "Usage: backup --pretend -f config_file"
17
+
18
+ opts.on( '-f', '--file file', 'configuration file' ) do |file|
19
+ options[:config] = file
20
+ end
21
+
22
+ opts.on( '-p', '--pretend', 'Run but do not make any changes' ) do |data|
23
+ options[:pretend] = true
24
+ end
25
+
26
+ opts.on( '-h', '--help', 'Display this screen' ) do
27
+ puts opts
28
+ exit
29
+ end
30
+ opts.parse!
31
+ end
32
+
33
+ if !options[:config] && File.exist?('backup.rb')
34
+ options[:config] = 'backup.rb'
35
+ elsif !options[:config] && File.exist?('config/backup.rb')
36
+ options[:config] = 'config/backup.rb'
37
+ elsif !options[:config]
38
+ puts 'No configuration file found'.red
39
+ exit
40
+ end
41
+
42
+ Backup::Main.start(options)
@@ -0,0 +1,14 @@
1
+ module Backup
2
+ module Attribute
3
+ def generate_attributes(*attrs)
4
+ attrs.flatten.each do |attr|
5
+ class_eval <<-METHOD
6
+ def #{attr}(value=nil)
7
+ instance_variable_set("@#{attr}", value) if value
8
+ instance_variable_get("@#{attr}")
9
+ end
10
+ METHOD
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,33 @@
1
+ Dir[File.join(File.dirname(__FILE__),'*.rb')].map {|f| require f}
2
+
3
+ module Backup
4
+ module Configuration
5
+ class Base
6
+ extend Backup::Attribute
7
+
8
+ def storage(name=nil,&block)
9
+ @storages ||= {}
10
+
11
+ if block
12
+ name ||= "storage_#{@storages.keys.size}"
13
+ @storages[name] = Backup::Configuration::Storage.new
14
+ @storages[name].instance_eval &block
15
+ end
16
+
17
+ @storages
18
+ end
19
+
20
+ def server(name=nil,&block)
21
+ @servers ||= {}
22
+
23
+ if block
24
+ name ||= "server_#{@servers.keys.size}"
25
+ @servers[name] = Backup::Configuration::Server.new
26
+ @servers[name].instance_eval &block
27
+ end
28
+
29
+ @servers
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ module Backup
2
+ module Configuration
3
+ class Mysql
4
+ extend Backup::Attribute
5
+ generate_attributes :user, :password, :options, :databases, :tables
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ module Backup
2
+ module Configuration
3
+ class Server
4
+ extend Backup::Attribute
5
+
6
+ generate_attributes :command,:rsync
7
+
8
+ def mysql(name=nil,&block)
9
+ @mysqls ||= {}
10
+
11
+ if block
12
+ name ||= "mysql_#{@servers.keys.size}"
13
+ @mysqls[name] = Backup::Configuration::Mysql.new
14
+ @mysqls[name].instance_eval &block
15
+ end
16
+
17
+ @mysqls
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,8 @@
1
+ module Backup
2
+ module Configuration
3
+ class Storage
4
+ extend Backup::Attribute
5
+ generate_attributes :path
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ module Backup
2
+ class Server
3
+ attr_accessor :config, :name, :storage
4
+
5
+ def backup
6
+ storage.backup(self) if storage
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,58 @@
1
+ require 'tempfile'
2
+
3
+ module Backup
4
+ class Storage
5
+ extend Backup::Attribute
6
+ attr_accessor :name, :config
7
+
8
+ def initialize(name, config)
9
+ self.name = name
10
+ self.config = config
11
+ end
12
+
13
+ def backup(server)
14
+ @server = server
15
+ @server_config = @server.config
16
+ @backup_path = "#{config.path}/#{@server.name}"
17
+
18
+ backup_rsync
19
+ backup_mysql
20
+ commit_changes
21
+ end
22
+
23
+ def backup_rsync
24
+ target_path = File.join(@backup_path, "rsync")
25
+ FileUtils.mkdir_p target_path
26
+
27
+ @server_config.rsync.to_a.map do |path|
28
+ Backup::Main.run "rsync -rav '#{@server_config.command}:#{path}' '#{target_path}'"
29
+ end
30
+ end
31
+
32
+ def backup_mysql
33
+ target_path = File.join(@backup_path, "rsync")
34
+ FileUtils.mkdir_p target_path
35
+
36
+ @server_config.mysql.map do |key, mysql|
37
+ mysql_config = ""
38
+ mysql_config += " -u#{mysql.user}" if mysql.user
39
+ mysql_config += " -p#{mysql.user}" if mysql.password
40
+ mysql_config += " --databases #{mysql.databases.to_a.join(' ')}" if mysql.databases
41
+ mysql_config += " --tables #{mysql.tables.to_a.join(' ')}" if mysql.tables
42
+ mysql_config += " #{mysql.options}" if mysql.options
43
+
44
+ tmpfile = Tempfile.new('mysql.sql')
45
+ Backup::Main.run("ssh '#{@server_config.command}' -c '$(which mysqldump) #{mysql_config} > #{tmpfile.path}'") &&
46
+ Backup::Main.run("scp '#{@server_config.command}:#{tmpfile.path}' '#{target_path}/#{key}.sql'") &&
47
+ Backup::Main.run("ssh '#{@server_config.command}' -c 'rm #{tmpfile.path}'")
48
+ end
49
+ end
50
+
51
+ def commit_changes
52
+ Dir.chdir(@backup_path) do
53
+ Backup::Main.run("$(which git) add .")
54
+ Backup::Main.run("$(which git) commit -am '#{Time.now.strftime("%Y-%m-%d %H:%M")}'")
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/backup.rb ADDED
@@ -0,0 +1,31 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ require 'backup/attribute'
3
+ require 'backup/configuration/base'
4
+ require 'backup/server'
5
+ require 'backup/storage'
6
+
7
+ module Backup
8
+ class Main
9
+ def self.start(options)
10
+ @@options = options
11
+
12
+ @configuration = Backup::Configuration::Base.new
13
+ @configuration.instance_eval { eval File.read(options[:config]) }
14
+
15
+ @configuration.storage.map do |storage_key, storage_value|
16
+ @configuration.server.map do |name, config|
17
+ server = Backup::Server.new
18
+ server.name = name
19
+ server.config = config
20
+ server.storage = Backup::Storage.new(storage_key, storage_value)
21
+ server.backup
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.run(shell)
27
+ puts shell.red
28
+ @@options[:pretend] ? true : system(shell)
29
+ end
30
+ end
31
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'backupit'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestBackupit < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backupit
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Jinzhu
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-27 00:00:00 +08:00
19
+ default_executable: backup
20
+ dependencies: []
21
+
22
+ description: A tool to backup your servers
23
+ email: wosmvp@gmail.com
24
+ executables:
25
+ - backup
26
+ extensions: []
27
+
28
+ extra_rdoc_files:
29
+ - LICENSE
30
+ - README
31
+ - README.rdoc
32
+ files:
33
+ - .document
34
+ - .gitignore
35
+ - LICENSE
36
+ - README
37
+ - README.rdoc
38
+ - Rakefile
39
+ - VERSION
40
+ - backupit.gemspec
41
+ - bin/backup
42
+ - lib/backup.rb
43
+ - lib/backup/attribute.rb
44
+ - lib/backup/configuration/base.rb
45
+ - lib/backup/configuration/mysql.rb
46
+ - lib/backup/configuration/server.rb
47
+ - lib/backup/configuration/storage.rb
48
+ - lib/backup/server.rb
49
+ - lib/backup/storage.rb
50
+ - test/helper.rb
51
+ - test/test_backupit.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/jinzhu/backupit
54
+ licenses: []
55
+
56
+ post_install_message:
57
+ rdoc_options:
58
+ - --charset=UTF-8
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ hash: 3
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ requirements: []
80
+
81
+ rubyforge_project:
82
+ rubygems_version: 1.3.7
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: A tool to backup your servers
86
+ test_files:
87
+ - test/test_backupit.rb
88
+ - test/helper.rb