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(reverse, *args) ⇒ RBackup

Returns a new instance of RBackup.



18
19
20
21
22
# File 'lib/rbackup.rb', line 18

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

Instance Attribute Details

#profilesObject

Returns the value of attribute profiles.



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

def profiles
  @profiles
end

#yamlObject

Returns the value of attribute yaml.



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

def yaml
  @yaml
end

Instance Method Details

#error(e) ⇒ Object



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

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

#esc(paths) ⇒ Object



60
61
62
63
64
# File 'lib/rbackup.rb', line 60

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

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



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbackup.rb', line 24

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



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rbackup.rb', line 41

def get_yaml
  if $rbackup_config
    config = $rbackup_config
  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



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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/rbackup.rb', line 66

def rsync(profile)
  inc1ude = []
  exclude = []
  destination = profile['destination']
  source = profile['source']
  
  if @reverse
    last = source.split('/').last
    source = source.split('/')[0..-2].join('/')
    destination = [ destination, '/', last ].join('/')
    destination, source = source, destination
  end

  options = "--numeric-ids --safe-links -axzSvL"
  # --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 profile['delete'].nil? || profile['delete']
    options = "--delete " + options
    # --delete                  delete extraneous files from dest dirs
  end
  
  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'] ].flatten
  end

  if profile['exclude']
    exclude += [ profile['exclude'] ].flatten
  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



125
126
127
128
129
# File 'lib/rbackup.rb', line 125

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