Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Version 0.1.0

  • Loading branch information...
commit 1f4eeba9de631ee7ecf781f3296f6052dc465cc8 0 parents
@winton authored
5 .gitignore
@@ -0,0 +1,5 @@
+.DS_Store
+*.gem
+coverage
+pkg
+tmp
18 MIT-LICENSE
@@ -0,0 +1,18 @@
+Copyright (c) 2009 Winton Welsh
+
+Permission is hereby granted, free of charge, to any person obtaining a copy of
+this software and associated documentation files (the "Software"), to deal in
+the Software without restriction, including without limitation the rights to
+use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software is furnished to do so,
+subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
+FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
+COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
+IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 README.markdown
@@ -0,0 +1,31 @@
+rbackup
+=======
+
+Backup your stuff with Ruby and Rsync.
+
+Setup
+-----
+
+<pre>
+gem sources -a http://gems.github.com
+sudo gem install winton-rbackup
+</pre>
+
+Create ~/.rbackup.yml
+---------------------
+
+<pre>
+documents:
+ source: ~/Documents
+ destination: /Volumes/USB Key
+ exclude:
+ - Software
+ - Virtual Machines.localized
+</pre>
+
+Backup
+------
+
+<pre>
+rbackup documents
+</pre>
54 Rakefile
@@ -0,0 +1,54 @@
+require 'rubygems'
+require 'rake'
+require 'rake/gempackagetask'
+require 'spec/rake/spectask'
+require 'gemspec'
+
+desc "Generate gemspec"
+task :gemspec do
+ File.open("#{Dir.pwd}/#{GEM_NAME}.gemspec", 'w') do |f|
+ f.write(GEM_SPEC.to_ruby)
+ end
+end
+
+desc "Install gem"
+task :install do
+ Rake::Task['gem'].invoke
+ `sudo gem uninstall #{GEM_NAME} -x`
+ `sudo gem install pkg/#{GEM_NAME}*.gem`
+ `rm -Rf pkg`
+end
+
+desc "Package gem"
+Rake::GemPackageTask.new(GEM_SPEC) do |pkg|
+ pkg.gem_spec = GEM_SPEC
+end
+
+desc "Setup project"
+task :setup do
+ name = File.basename(Dir.pwd)
+ `rm -Rf .git`
+ begin
+ dir = Dir['**/gem_template*']
+ from = dir.pop
+ if from
+ rb = from.include?('.rb')
+ to = File.dirname(from) + "/#{name}#{'.rb' if rb}"
+ FileUtils.mv(from, to)
+ end
+ end while dir.length > 0
+ Dir["**/*"].each do |path|
+ next if path.include?('Rakefile')
+ if File.file?(path)
+ `sed -i "" 's/gem_template/#{name}/g' #{path}`
+ end
+ end
+ `git init`
+end
+
+desc "Run specs"
+Spec::Rake::SpecTask.new do |t|
+ t.rcov = true
+ t.spec_opts = ["--format", "specdoc", "--colour"]
+ t.spec_files = FileList["spec/**/*_spec.rb"]
+end
6 bin/rbackup
@@ -0,0 +1,6 @@
+#!/usr/bin/env ruby
+
+$:.push File.expand_path("#{File.dirname(__FILE__)}/../lib")
+require 'rbackup'
+
+RBackup.new(*ARGV).run
18 gemspec.rb
@@ -0,0 +1,18 @@
+GEM_NAME = 'rbackup'
+GEM_FILES = FileList['**/*'] - FileList['coverage', 'coverage/**/*', 'pkg', 'pkg/**/*']
+GEM_SPEC = Gem::Specification.new do |s|
+ # == CONFIGURE ==
+ s.author = "Winton Welsh"
+ s.email = "mail@wintoni.us"
+ s.homepage = "http://github.com/winton/#{GEM_NAME}"
+ s.summary = "Backup your stuff with Ruby and Rsync"
+ # == CONFIGURE ==
+ s.executables << GEM_NAME
+ s.extra_rdoc_files = [ "README.markdown" ]
+ s.files = GEM_FILES.to_a
+ s.has_rdoc = false
+ s.name = GEM_NAME
+ s.platform = Gem::Platform::RUBY
+ s.require_path = "lib"
+ s.version = "0.1.0"
+end
61 lib/rbackup.rb
@@ -0,0 +1,61 @@
+class RBackup
+
+ @@usage = <<-USAGE
+ Usage:
+ rbackup [YAML] 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
+
+ def initialize(*args)
+ if @profile = args.pop
+ @config = args.pop || File.expand_path("~/.rbackup.yml")
+ configure
+ else
+ error("You must specify a profile.")
+ 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
+ else
+ error("YAML configuration not found.")
+ end
+ end
+
+ def error(e)
+ " Error:\n #{e}\n#{@@usage}"
+ exit
+ 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}`
+ end
+end
1  spec/fixtures/destination/.gitignore
@@ -0,0 +1 @@
+1.txt
4 spec/fixtures/rbackup.yml
@@ -0,0 +1,4 @@
+profile_1:
+ source: SPEC/fixtures/source/*
+ destination: SPEC/fixtures/destination
+ exclude: 2.txt
1  spec/fixtures/source/1.txt
@@ -0,0 +1 @@
+1
1  spec/fixtures/source/2.txt
@@ -0,0 +1 @@
+2
33 spec/rbackup_spec.rb
@@ -0,0 +1,33 @@
+require File.expand_path("#{File.dirname(__FILE__)}/spec_helper")
+
+describe RBackup do
+ before(:each) do
+ @rb = RBackup.new(SPEC + '/fixtures/rbackup.yml', 'profile_1')
+ # Replace SPEC in config paths
+ replace = lambda { |s| s.gsub('SPEC', SPEC) }
+ @rb.destination = replace.call(@rb.destination)
+ @rb.exclude = @rb.exclude.collect &replace
+ @rb.source = @rb.source.collect &replace
+ # Empty destination folder
+ Dir[SPEC + '/fixtures/destination/*'].each do |path|
+ FileUtils.rm_rf(path)
+ end
+ # Debug
+ # debug @rb.destination
+ # debug @rb.exclude
+ # debug @rb.source
+ end
+
+ it "should load the YAML configuration" do
+ @rb.destination.should == "#{SPEC}/fixtures/destination"
+ @rb.exclude.should == [ "2.txt" ]
+ @rb.source.should == [ "#{SPEC}/fixtures/source/*" ]
+ end
+
+ it "should backup" do
+ @rb.run
+ File.exists?(SPEC + '/fixtures/destination/1.txt').should == true
+ File.exists?(SPEC + '/fixtures/destination/2.txt').should == false
+ File.read(SPEC + '/fixtures/destination/1.txt').should == '1'
+ end
+end
1  spec/spec.opts
@@ -0,0 +1 @@
+--color
16 spec/spec_helper.rb
@@ -0,0 +1,16 @@
+$TESTING=true
+SPEC = File.dirname(__FILE__)
+$:.unshift File.expand_path("#{SPEC}/../lib")
+
+require 'rbackup'
+require 'pp'
+
+Spec::Runner.configure do |config|
+end
+
+# For use with rspec textmate bundle
+def debug(object)
+ puts "<pre>"
+ puts object.pretty_inspect.gsub('<', '&lt;').gsub('>', '&gt;')
+ puts "</pre>"
+end
Please sign in to comment.
Something went wrong with that request. Please try again.