backup-restorer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest ADDED
@@ -0,0 +1,6 @@
1
+ Manifest
2
+ README.rdoc
3
+ Rakefile
4
+ bin/backup-restore
5
+ conf/settings.yml
6
+ lib/backup-restorer.rb
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = BackupRestorer
2
+
3
+ Ruby gem to restore backups
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'echoe'
4
+ require 'fileutils'
5
+ require 'yaml'
6
+
7
+ Echoe.new('backup-restorer', '0.1.0') do |p|
8
+ p.runtime_dependencies = ['net-ssh', 'net-scp']
9
+ p.project = 'yurimello'
10
+ p.author = 'Yuri Mello'
11
+ p.summary = 'A command-line tool to restore backups'
12
+ p.description = "Tool for use with backup gem to restore your backups"
13
+ #p.url = "http://github.com/yurimello/backup-restorer"
14
+ #p.author = "Yuri Mello"
15
+ p.email = "yuri.mcr@gmail.com"
16
+ p.ignore_pattern = ["tmp/*", "script/*"]
17
+ p.development_dependencies = []
18
+ end
19
+
20
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,36 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{backup-restorer}
5
+ s.version = "0.1.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
+ s.authors = [%q{Yuri Mello}]
9
+ s.date = %q{2011-06-16}
10
+ s.description = %q{Tool for use with backup gem to restore your backups}
11
+ s.email = %q{yuri.mcr@gmail.com}
12
+ s.executables = [%q{backup-restore}]
13
+ s.extra_rdoc_files = [%q{README.rdoc}, %q{bin/backup-restore}, %q{lib/backup-restorer.rb}]
14
+ s.files = [%q{Manifest}, %q{README.rdoc}, %q{Rakefile}, %q{backup-restorer.gemspec}, %q{bin/backup-restore}, %q{conf/settings.yml}, %q{lib/backup-restorer.rb}]
15
+ s.homepage = %q{http://yurimello.github.com/yurimello/backup-restorer/}
16
+ s.rdoc_options = [%q{--line-numbers}, %q{--inline-source}, %q{--title}, %q{Backup-restorer}, %q{--main}, %q{README.rdoc}]
17
+ s.require_paths = [%q{lib}]
18
+ s.rubyforge_project = %q{yurimello}
19
+ s.rubygems_version = %q{1.8.5}
20
+ s.summary = %q{A command-line tool to restore backups}
21
+
22
+ if s.respond_to? :specification_version then
23
+ s.specification_version = 3
24
+
25
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
26
+ s.add_runtime_dependency(%q<net-ssh>, [">= 0"])
27
+ s.add_runtime_dependency(%q<net-scp>, [">= 0"])
28
+ else
29
+ s.add_dependency(%q<net-ssh>, [">= 0"])
30
+ s.add_dependency(%q<net-scp>, [">= 0"])
31
+ end
32
+ else
33
+ s.add_dependency(%q<net-ssh>, [">= 0"])
34
+ s.add_dependency(%q<net-scp>, [">= 0"])
35
+ end
36
+ end
@@ -0,0 +1,79 @@
1
+ #!/usr/bin/env ruby
2
+ require File.expand_path(File.dirname(__FILE__) + '/../lib/backup-restorer')
3
+ require 'rubygems'
4
+ require 'fileutils'
5
+ require 'net/ssh'
6
+ require 'net/scp'
7
+ require 'yaml'
8
+
9
+ def help
10
+ puts "backup-restorer [-t|--type type] < -f|--files file [files...] >"
11
+ end
12
+
13
+ def set_settings
14
+ conf_struct = YAML.load_file "#{File.dirname(__FILE__)}/../conf/settings.yml"
15
+ puts "Please insert an local source path(If not use local source, let blank)"
16
+ conf_struct["paths"]["source"]["local"] = STDIN.gets.chomp
17
+
18
+ puts "Please insert an remote source path(If not use remote source, let blank)"
19
+ conf_struct["paths"]["source"]["remote"] = STDIN.gets.chomp
20
+
21
+ puts "Please insert an destination path(Only local)"
22
+ conf_struct["paths"]["destination"] = STDIN.gets.chomp
23
+
24
+ if conf_struct["paths"]["source"]["remote"]
25
+ puts "Please insert an host address for your remote source"
26
+ conf_struct["ssh"]["host"] = STDIN.gets.chomp
27
+ puts "Please insert your username from remote host"
28
+ conf_struct["ssh"]["username"] = STDIN.gets.chomp
29
+ puts "Please insert your password from remote host(If uses you a public key, let blank)"
30
+ system "stty -echo"
31
+ conf_struct["ssh"]["password"] = STDIN.gets.chomp
32
+ system "stty echo"
33
+ end
34
+ FileUtils.mkdir_p("#{ENV["HOME"]}/.backup-restorer")
35
+ file = File.open("#{ENV["HOME"]}/.backup-restorer/config.yml", "w")
36
+ file.write(conf_struct.to_yaml)
37
+ file.close
38
+ puts "The configuration file was generated with success."
39
+ puts "If you wish edit this file, the path is\n#{ENV["HOME"]}/.backup-restorer/config.yml"
40
+ end
41
+
42
+ files_first_index = nil
43
+ type = "remote"
44
+ ARGV.each_with_index do |arg, index|
45
+ if arg == "-h" || arg == "--help"
46
+ help
47
+ exit 0
48
+ end
49
+ if arg == "-c" || arg == "--config"
50
+ set_settings
51
+ exit 0
52
+ end
53
+ if arg == "-t" || arg == "--type"
54
+ type = ARGV[ index + 1 ]
55
+ if type != "local" && type != "remote"
56
+ help
57
+ exit 1
58
+ end
59
+ end
60
+ if arg == "-f" || arg == "--files"
61
+ files_first_index = index + 1
62
+ end
63
+ end
64
+
65
+ unless File.exist?("#{ENV["HOME"]}/.backup-restorer/config.yml")
66
+ puts "You have not the configuration file..."
67
+ puts "So, answer a few questions to generate a configuration file."
68
+ set_settings
69
+ end
70
+
71
+ unless files_first_index
72
+ help
73
+ exit 1
74
+ end
75
+
76
+ CONF = YAML.load_file "#{ENV["HOME"]}/.backup-restorer/config.yml"
77
+ restorer = BackupRestorer::Restorer.new(type, ARGV.slice(files_first_index, ARGV.length))
78
+ restorer.restore
79
+
data/conf/settings.yml ADDED
@@ -0,0 +1,10 @@
1
+ paths:
2
+ source:
3
+ local: /local/backup/path
4
+ remote: /remote/backup/path
5
+ destination: /samba/path
6
+
7
+ ssh:
8
+ host: host
9
+ username: user
10
+ password: password
@@ -0,0 +1,77 @@
1
+ module BackupRestorer
2
+ class Restorer
3
+ def initialize(type, files)
4
+ @type = type
5
+ @files = files
6
+ @file_paths = get_file_paths
7
+ end
8
+
9
+ def restore
10
+ @type == "local" ? restore_local : restore_remote
11
+ end
12
+ private
13
+ def restore_local
14
+ @file_paths.each do |file_path|
15
+ remove_from_destination(file_path) if file_path.exists_on_destination?
16
+ begin
17
+ FileUtils.cp_r("#{CONF["paths"]["source"][@type]}/#{file_path.full_path}", "#{CONF["paths"]["destination"]}/#{file_path.parent_path}")
18
+ puts "#{CONF["paths"]["source"][@type]}/#{file_path.full_path}: Copied."
19
+ rescue Errno::ENOENT
20
+ puts "#{CONF["paths"]["source"][@type]}/#{file_path.full_path}: No such file or directory."
21
+ end
22
+ end
23
+ end
24
+
25
+ def restore_remote
26
+ Net::SSH.start( CONF["ssh"]["host"], CONF["ssh"]["username"], :password => CONF["ssh"]["password"] ) do |ssh|
27
+ @file_paths.each do |file_path|
28
+ remove_from_destination(file_path) if file_path.exists_on_destination?
29
+ begin
30
+ ssh.scp.download!( "#{CONF["paths"]["source"][@type]}/#{file_path.full_path}", "#{CONF["paths"]["destination"]}/#{file_path.parent_path}", :recursive => true )
31
+ puts "#{CONF["paths"]["source"][@type]}/#{file_path.full_path}: Copied."
32
+ rescue Net::SCP::Error
33
+ puts "#{CONF["paths"]["source"][@type]}/#{file_path.full_path}: No such file or directory."
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ def remove_from_destination(file_path)
40
+ puts "Already exists file: #{CONF["paths"]["destination"]}/#{file_path.full_path}. Deleting..."
41
+ FileUtils.rm_r("#{CONF["paths"]["destination"]}/#{file_path.full_path}", {:force => true})
42
+ end
43
+
44
+ def strip_absent_files(file_paths)
45
+ file_paths.select { |file_path| file_path if file_path.exists_on_source?(@type) }
46
+ end
47
+ def get_file_paths
48
+ @files = strip_types_from_files
49
+ @files.map { |file| FilePath.new(file) }
50
+ end
51
+
52
+ def strip_types_from_files
53
+ @files.select { |file| file if file != 'local' and file != 'remote' }
54
+ end
55
+ end
56
+
57
+ class FilePath
58
+ attr_accessor :full_path, :parent_path, :existance_on_source
59
+ def initialize(filepath)
60
+ @full_path = filepath
61
+ @parent_path = get_parent_path
62
+ end
63
+ def exists_on_destination?
64
+ File.exist?("#{CONF["paths"]["destination"]}/#{@full_path}")
65
+ end
66
+
67
+ def exists_on_source?(type)
68
+ return @existance_on_source if type == "remote"
69
+ File.exist?("#{CONF["paths"]["source"][@type]}/#{file_path.full_path}")
70
+ end
71
+
72
+ private
73
+ def get_parent_path
74
+ @full_path.split("/").slice(0..-2).join("/")
75
+ end
76
+ end
77
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backup-restorer
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Yuri Mello
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-06-16 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: net-ssh
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: net-scp
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: "0"
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: Tool for use with backup gem to restore your backups
38
+ email: yuri.mcr@gmail.com
39
+ executables:
40
+ - backup-restore
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - README.rdoc
45
+ - bin/backup-restore
46
+ - lib/backup-restorer.rb
47
+ files:
48
+ - Manifest
49
+ - README.rdoc
50
+ - Rakefile
51
+ - backup-restorer.gemspec
52
+ - bin/backup-restore
53
+ - conf/settings.yml
54
+ - lib/backup-restorer.rb
55
+ homepage: http://yurimello.github.com/yurimello/backup-restorer/
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --line-numbers
61
+ - --inline-source
62
+ - --title
63
+ - Backup-restorer
64
+ - --main
65
+ - README.rdoc
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "1.2"
80
+ requirements: []
81
+
82
+ rubyforge_project: yurimello
83
+ rubygems_version: 1.8.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: A command-line tool to restore backups
87
+ test_files: []
88
+