gitback 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Bryce Thornton
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,91 @@
1
+ Gitback
2
+ ====
3
+
4
+ Gitback allows you to version arbitrary files and/or directories in a git
5
+ repository. You just need to include the gem and write a brief ruby script
6
+ that indicates the files/directories you'd like to backup. Then, run the
7
+ script via cron. Gitback will take care of a adding/commiting/pushing whenever
8
+ your files are modified.
9
+
10
+ The typical usage for this is backing up config files.
11
+
12
+
13
+ ## Requirements ###############################################################
14
+
15
+ * git (http://git-scm.com) tested with 1.6.0.4
16
+ * grit (http://github.com/mojombo/grit) 2.0.0 or higher
17
+
18
+
19
+ ## Install ####################################################################
20
+
21
+ $ gem install gitback -s http://gemcutter.org
22
+
23
+ ## Usage ######################################################################
24
+
25
+ Here's a basic example of a script using gitback:
26
+
27
+ require 'rubygems'
28
+ require 'gitback'
29
+
30
+ Gitback::Repository.new '/var/config-backup/' do |repo|
31
+ repo.backup '/opt/nginx/conf/nginx.conf'
32
+ end
33
+
34
+ This will check /opt/nginx/conf/nginx.conf for changes. If the file has
35
+ changed, gitback will commit a new version.
36
+
37
+ This nginx config file would be saved to the following location:
38
+
39
+ /var/config-backup/opt/nginx/conf/nginx.conf
40
+
41
+ Everything starts with instantiating a new `Gitback::Repository` object. The
42
+ first parameter is the path to the git repository you'd like to backup to. The
43
+ second parameter is a block indicating the files/directories you'd like to
44
+ backup.
45
+
46
+
47
+ ### Directory support
48
+
49
+ In addition to basic files, directory paths can also be backed up:
50
+
51
+ Gitback::Repository.new '/var/config-backup/' do |repo|
52
+ repo.backup '/opt/nginx/conf/nginx.conf'
53
+ repo.backup '/etc/mysql/'
54
+ end
55
+
56
+ Notice that '/etc/mysql' is a directory. Gitback will copy everything within
57
+ that directory into the git repository.
58
+
59
+
60
+ ### Namespaces
61
+
62
+ Namespaces are also supported. If you'd like to use the same repository for
63
+ multiple servers you can specify a namespace like this:
64
+
65
+ Gitback::Repository.new '/var/config-backup/' do |repo|
66
+ repo.namespace 'server1.domain.com' do
67
+ repo.backup '/opt/nginx/conf/nginx.conf'
68
+ end
69
+ end
70
+
71
+ This will save the file to the following location:
72
+
73
+ /var/config-backup/server1.domain.com/opt/nginx/conf/nginx.conf
74
+
75
+
76
+ ### Remote Git Repositories
77
+
78
+ Gitback is intended to be used with remote git repositories. If your git
79
+ repository is tracking a remote branch, gitback will push changes to
80
+ the remote after each commit.
81
+
82
+
83
+ ### Running Via Cron
84
+
85
+ There's nothing special about a gitback script. In order for it to backup
86
+ your files you'll need to run it via the command line. I suggest setting up
87
+ a cron job to do this for you at regular intervals.
88
+
89
+ ## Copyright ###################################################################
90
+
91
+ Copyright (c) 2010 Bryce Thornton. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "gitback"
8
+ gem.summary = %Q{A simple ruby library for backing up files to git}
9
+ gem.description = %Q{Provide a list of files and/or directories and gitback will copy them to your git repo, commit and push when there are changes.}
10
+ gem.email = "brycethornton@gmail.com"
11
+ gem.homepage = "http://github.com/brycethornton/gitback"
12
+ gem.authors = ["Bryce Thornton"]
13
+ gem.add_dependency "grit", ">= 2.0.0"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "gitback #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,6 @@
1
+ require 'fileutils'
2
+ require 'ftools'
3
+ require 'grit'
4
+ include Grit
5
+
6
+ require 'gitback/repository'
@@ -0,0 +1,115 @@
1
+ module Gitback
2
+ class Repository
3
+ attr_accessor :remote
4
+ attr_accessor :branch
5
+
6
+ def initialize(repository_path, &block)
7
+ @repository_path = clean_path(repository_path)
8
+
9
+ if File.exists?(@repository_path)
10
+ prepare_git_repository
11
+
12
+ if @repo
13
+ yield self
14
+ commit_git_changes
15
+ end
16
+ else
17
+ puts "ERROR: it doesn't look like '#{@repository_path}' exists."
18
+ end
19
+ end
20
+
21
+ # Creates a namespace for files to be stored under
22
+ # Allows a single repository to be used for many different backups
23
+ def namespace(name, &block)
24
+ @namespace = clean_path(name)
25
+ yield self
26
+ end
27
+
28
+ # Sets up the paths and copies the files into the git repo
29
+ def backup(file_path)
30
+ file_path = clean_path(file_path)
31
+
32
+ begin
33
+ dest_path = file_path
34
+ dest_path = '/' + @namespace + file_path if @namespace
35
+ dest_path = @repository_path + dest_path
36
+
37
+ dirname = File.dirname(dest_path)
38
+
39
+ # Make sure the path exists in the repo
40
+ if !File.exists?(dirname)
41
+ FileUtils.mkpath(dirname)
42
+ end
43
+
44
+ # Copy the file(s) to the repo
45
+ if File.exists?(file_path)
46
+ # We pass remove_destination to avoid issues with symlinks
47
+ FileUtils.cp_r file_path, dest_path, :remove_destination => true
48
+ else
49
+ puts "ERROR: '#{file_path}' doesn't seem to exist."
50
+ end
51
+ rescue Errno::EACCES
52
+ puts "ERROR: '#{file_path}' doesn't seem to be readable and/or writable by this user."
53
+ end
54
+ end
55
+
56
+ private
57
+ # Creates the Grit::Repo object for use throughout the class
58
+ def prepare_git_repository
59
+ # Allow one minute for slow repositories
60
+ Grit::Git.git_timeout = 60.0
61
+
62
+ Dir.chdir(@repository_path) do
63
+ begin
64
+ @repo = Grit::Repo.new('.')
65
+
66
+ # Figure out the remote branch
67
+ @remote = @repo.git.list_remotes.first
68
+ @branch = @repo.head.name
69
+
70
+ # Do a git-pull to make sure we have the newest changes from the repo
71
+ if @remote
72
+ puts "Pulling any changes from the remote git repository..."
73
+ @repo.git.pull({}, @remote, @branch)
74
+ end
75
+ rescue Grit::InvalidGitRepositoryError
76
+ puts "ERROR: #{@repository_path} doesn't seem to be a git repository."
77
+ end
78
+ end
79
+ end
80
+
81
+ # Use the repo object to commit and push the changes
82
+ def commit_git_changes
83
+ Dir.chdir(@repository_path) do
84
+ status = @repo.status
85
+
86
+ # Only add if we have untracked files
87
+ if status.untracked.size > 0
88
+ puts "Adding new files to the repository..."
89
+ @repo.add(@repository_path + '/*')
90
+ end
91
+
92
+ commit_result = @repo.commit_all("Updating files")
93
+
94
+ # Attempt to push if anything was committed and we have a remote repo
95
+ if commit_result !~ /working directory clean/
96
+ if @remote
97
+ puts "Pushing repository changes..."
98
+ @repo.git.push({}, @remote, @branch)
99
+ end
100
+ else
101
+ puts "No changes committed."
102
+ end
103
+ end
104
+ end
105
+
106
+ # This just normalizes file/directory paths
107
+ def clean_path(file_path)
108
+ if File.directory?(file_path)
109
+ file_path = File.expand_path(file_path)
110
+ end
111
+
112
+ file_path
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'gitback'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestGitback < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gitback
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Bryce Thornton
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-17 00:00:00 -04:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: grit
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 2
29
+ - 0
30
+ - 0
31
+ version: 2.0.0
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: Provide a list of files and/or directories and gitback will copy them to your git repo, commit and push when there are changes.
35
+ email: brycethornton@gmail.com
36
+ executables: []
37
+
38
+ extensions: []
39
+
40
+ extra_rdoc_files:
41
+ - LICENSE
42
+ - README.md
43
+ files:
44
+ - .document
45
+ - .gitignore
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/gitback.rb
51
+ - lib/gitback/repository.rb
52
+ - test/helper.rb
53
+ - test/test_gitback.rb
54
+ has_rdoc: true
55
+ homepage: http://github.com/brycethornton/gitback
56
+ licenses: []
57
+
58
+ post_install_message:
59
+ rdoc_options:
60
+ - --charset=UTF-8
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ segments:
75
+ - 0
76
+ version: "0"
77
+ requirements: []
78
+
79
+ rubyforge_project:
80
+ rubygems_version: 1.3.6
81
+ signing_key:
82
+ specification_version: 3
83
+ summary: A simple ruby library for backing up files to git
84
+ test_files:
85
+ - test/helper.rb
86
+ - test/test_gitback.rb