Class: RBackup

Inherits:
Object
  • Object
show all
Defined in:
lib/rbackup.rb

Constant Summary collapse

@@usage =
<<-USAGE

Usage:
  rbackup [PROFILE]...
  
  PROFILE
    The name of the profile listed in your YAML configuration.

USAGE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RBackup

Returns a new instance of RBackup.



15
16
17
18
# File 'lib/rbackup.rb', line 15

def initialize(*args)
  get_yaml
  get_profiles(args, @yaml)
end

Instance Attribute Details

#profilesObject

Returns the value of attribute profiles.



13
14
15
# File 'lib/rbackup.rb', line 13

def profiles
  @profiles
end

#yamlObject

Returns the value of attribute yaml.



13
14
15
# File 'lib/rbackup.rb', line 13

def yaml
  @yaml
end

Instance Method Details

#error(e) ⇒ Object



51
52
53
54
# File 'lib/rbackup.rb', line 51

def error(e)
  puts "\n  Error:\n    #{e}\n#{@@usage}"
  exit
end

#esc(paths) ⇒ Object



56
57
58
59
60
# File 'lib/rbackup.rb', line 56

def esc(paths)
  paths = paths.to_a
  paths.collect! { |path| path.gsub('SPEC', SPEC) } if $TESTING
  paths.collect  { |path| path.gsub(' ', '\ ') }.join(' ')
end

#get_profiles(input, hash, force = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rbackup.rb', line 20

def get_profiles(input, hash, force=false)
  @profiles ||= []
  hash.each do |key, value|
    next unless value.respond_to?(:keys)
    is_profile = value['source'] && value['destination']
    if input.include?(key) || input.empty? || force
      if is_profile
        @profiles << value
      else
        get_profiles(input, value, true)
      end
    elsif !is_profile
      get_profiles(input, value, force)
    end
  end
end

#get_yamlObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rbackup.rb', line 37

def get_yaml
  if $TESTING
    config = SPEC + '/fixtures/rbackup.yml'
  else
    config = File.expand_path("~/.rbackup.yml")
  end
  if File.exists?(config)
    @yaml = File.open(config)
    @yaml = YAML::load(yaml)
  else
    error("YAML configuration not found.")
  end
end

#rsync(profile) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rbackup.rb', line 62

def rsync(profile)
  inc1ude = []
  exclude = []
  destination = profile['destination']
  source = profile['source'].to_a

  options = "--delete --numeric-ids --safe-links -axzSvL"
  # --delete                    delete extraneous files from dest dirs
  # --numeric-ids               don't map uid/gid values by user/group name
  # --safe-links                ignore symlinks that point outside the tree
  # -a, --archive               recursion and preserve almost everything (-rlptgoD)
  # -x, --one-file-system       don't cross filesystem boundaries
  # -z, --compress              compress file data during the transfer
  # -S, --sparse                handle sparse files efficiently
  # -v, --verbose               verbose
  
  if destination.include?(':') || source.include?(':')
    options += ' -e ssh'
    # -e, --rsh=COMMAND         specify the remote shell to use
  else
    options += 'E'
    # -E, --extended-attributes copy extended attributes, resource forks
    FileUtils.mkdir_p destination
  end
  
  if profile['include']
    exclude = %w(*) unless profile['exclude']
    inc1ude = profile['include'].to_a
  end

  if profile['exclude']
    exclude += profile['exclude'].to_a
  end
  
  inc1ude = inc1ude.collect { |i| "--include='#{i}'" }.join(' ')
  exclude = exclude.collect { |e| "--exclude='#{e}'" }.join(' ')
  # --exclude=PATTERN         use one of these for each file you want to exclude
  # --include-from=FILE       don't exclude patterns listed in FILE

  cmd = "rsync #{options} #{inc1ude} #{exclude} #{esc(source)} #{esc(destination)}"
  if $TESTING
    `#{cmd}`
  else
    puts "Executing: #{cmd}"
    system(cmd)
  end
end

#runObject



110
111
112
113
114
# File 'lib/rbackup.rb', line 110

def run
  @profiles.each do |profile|
    rsync(profile)
  end
end