-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Ability to specify multiple profiles * Executing without parameters runs all profiles
- Loading branch information
Showing
6 changed files
with
84 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,81 @@ | ||
class RBackup | ||
|
||
@@usage = <<-USAGE | ||
Usage: | ||
rbackup [YAML] PROFILE | ||
rbackup [PROFILE]... | ||
YAML | ||
The path to your YAML configuration file. | ||
Default: ~/.rbackup.yml | ||
Example: mate #{File.expand_path(File.dirname(__FILE__) + '/../spec/fixtures/rbackup.yml')} | ||
PROFILE | ||
The name of the profile listed in your YAML configuration. | ||
USAGE | ||
|
||
attr_accessor :config, :destination, :exclude, :profile, :source | ||
attr_accessor :profiles, :yaml | ||
|
||
def initialize(*args) | ||
if @profile = args.pop | ||
@config = args.pop || File.expand_path("~/.rbackup.yml") | ||
configure | ||
else | ||
error("You must specify a profile.") | ||
@profiles = args | ||
configure | ||
if @profiles.empty? | ||
@profiles = @yaml.keys | ||
end | ||
end | ||
|
||
def configure | ||
if File.exists?(@config) | ||
yaml = File.open(@config) | ||
yaml = YAML::load(yaml) | ||
yaml = yaml[profile] | ||
fix = lambda { |path| path.gsub ' ', '\ '} | ||
@destination = fix.call(yaml['destination']) | ||
@exclude = yaml['exclude'].to_a.collect &fix | ||
@source = yaml['source'].to_a.collect &fix | ||
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 | ||
|
||
def error(e) | ||
" Error:\n #{e}\n#{@@usage}" | ||
puts "\n Error:\n #{e}\n#{@@usage}" | ||
exit | ||
end | ||
|
||
def esc(paths) | ||
paths = paths.to_a | ||
paths.collect! { |path| path.gsub('SPEC', SPEC) } if $TESTING | ||
paths.collect { |path| path.gsub(' ', '\ ') }.join(' ') | ||
end | ||
|
||
def run | ||
options = "--numeric-ids -EaxzS" | ||
# --numeric-ids don't map uid/gid values by user/group name | ||
# -E, --extended-attributes copy extended attributes, resource forks | ||
# -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 | ||
|
||
ex = exclude.collect { |e| "--exclude='#{e}'" }.join(' ') | ||
# --exclude=PATTERN use one of these for each file you want to exclude | ||
|
||
`rsync #{options} #{ex} #{source.join(' ')} #{destination}` | ||
@profiles.each do |profile| | ||
if config = yaml[profile] | ||
destination = config['destination'] | ||
exclude = config['exclude'].to_a | ||
source = config['source'].to_a | ||
|
||
FileUtils.mkdir_p destination | ||
|
||
options = "--numeric-ids -EaxzSv" | ||
# --numeric-ids don't map uid/gid values by user/group name | ||
# -E, --extended-attributes copy extended attributes, resource forks | ||
# -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 | ||
|
||
ex = exclude.collect { |e| "--exclude='#{e}'" }.join(' ') | ||
# --exclude=PATTERN use one of these for each file you want to exclude | ||
|
||
cmd = "rsync #{options} #{ex} #{esc(source)} #{esc(destination)}" | ||
if $TESTING | ||
`#{cmd}` | ||
else | ||
puts "Executing: #{cmd}" | ||
system(cmd) | ||
end | ||
else | ||
error("Profile #{profile} not found.") | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
profile_1: | ||
source: SPEC/fixtures/source/* | ||
destination: SPEC/fixtures/destination | ||
exclude: 2.txt | ||
exclude: 2.txt | ||
profile_2: | ||
source: SPEC/fixtures/source/* | ||
destination: SPEC/fixtures/destination | ||
exclude: | ||
- 2.txt | ||
- 3.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters