db_backup_rails 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5043fefd4c6803e20947086c26997a9c877e3fdc
4
+ data.tar.gz: 32dbee8fcd57f1a5413da6973fc8e5bea13cba7a
5
+ SHA512:
6
+ metadata.gz: 65ed5f0c5be50a3d08ab1901d554fec5656743c8d7b400e095495f86d12e55bed79aeb615b0299b55bc0328fcc920e00ee38180ea7662a7ef8563a5b03d147d7
7
+ data.tar.gz: cc1f9e7d015ec1e78abbe60cc32b7a8a81c69f5b5c1ed6fff2c1d1e044ef6b69db1301b5c4384d800389825f94626a753e1819a71dd3c15c8eb0affd685c9e01
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in db_backup_rails.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Leon Miller-Out
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,68 @@
1
+ # DbBackupRails
2
+
3
+ This simple gem creates database dump files on the local filesystem. The
4
+ authors use it to enable consistent nightly database backups on cloud servers.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'db_backup_rails'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install db_backup_rails
19
+
20
+ If you are using Rails 2.3, add the following to your Rakefile:
21
+
22
+ require 'db_backup_rails/tasks'
23
+
24
+ ## Usage
25
+
26
+ This gem provides `backup:db` and `backup:db:prune` rake tasks.
27
+
28
+ bundle exec rake backup:db
29
+ bundle exec rake backup:db:prune
30
+
31
+ ### Custom Directory
32
+
33
+ **Please note that the default dir has changed in 1.0.0!**
34
+
35
+ This defaults to using a directory at `[Rails.root]/../../shared/backup`, which
36
+ works well with Capistrano's standard directory structure. You can override it
37
+ by setting `BACKUP_DIR`. If `BACKUP_DIR` is relative, it will be relative to
38
+ `Rails.root`. E.g.
39
+
40
+ BACKUP_DIR=tmp/backup bundle exec rake backup:db
41
+
42
+ will back up to `[Rails.root]/tmp/backup`.
43
+
44
+ `BACKUP_DIR` applies to all provided tasks.
45
+
46
+ ### Setting the number of versions to keep when pruning
47
+
48
+ `backup:db:prune` keeps 7 versions by default. You can override this with
49
+ `NUMBER_TO_KEEP`, e.g.
50
+
51
+ NUMBER_TO_KEEP=30 bundle exec rake backup:db:prune
52
+
53
+ ### Setting the Rails environment
54
+
55
+ As with most rake tasks, you can specify RAILS_ENV if you want to operate on an
56
+ environment other than development.
57
+
58
+ ## Limitations
59
+
60
+ * Only works on Unix-y systems
61
+
62
+ ## Contributing
63
+
64
+ 1. Fork it
65
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
66
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
67
+ 4. Push to the branch (`git push origin my-new-feature`)
68
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'db_backup_rails/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "db_backup_rails"
8
+ spec.version = DbBackupRails::VERSION
9
+ spec.authors = ["Leon Miller-Out"]
10
+ spec.email = ["leon@singlebrook.com"]
11
+ spec.description = %q{Simple gem to back up your database to local storage}
12
+ spec.summary = %q{Simple gem to back up your database to local storage}
13
+ spec.homepage = "https://github.com/singlebrook/db_backup_rails"
14
+ spec.license = "MIT"
15
+
16
+ spec.post_install_message = "Thanks for using db_backup_rails! If you have upgraded to 1.x.x from " +
17
+ "0.x.x, please note that THE DEFAULT BACKUP DIR HAS CHANGED! See the " +
18
+ "README.md for more info."
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency 'rails', '~> 2.3'
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.3"
28
+ spec.add_development_dependency "rake"
29
+ end
@@ -0,0 +1,89 @@
1
+ # Simple database backup rake task
2
+ # Supports mysql and postgres
3
+ # Env vars:
4
+ # BACKUP_DIR - where to store files. Relative to Rails app root. Defaults to '../../shared/backup',
5
+ # which works nicely with Capistrano's default file structure.
6
+ # NUM_TO_KEEP - how many backup files to keep. Defaults to 7.
7
+ # RAILS_ENV - which database to back up. Defaults to development like all Rake tasks.
8
+
9
+ namespace :backup do
10
+ desc "Backup database"
11
+ task :db => [:environment] do
12
+ FileUtils.mkdir_p backup_dir
13
+
14
+ adapter = settings['adapter']
15
+ host = settings['host'] || '127.0.0.1'
16
+ port = settings['port']
17
+ database = settings['database']
18
+ user = settings['username']
19
+ password = settings['password']
20
+
21
+ # Run the appropriate backup utility
22
+ case adapter
23
+ when 'mysql'
24
+ port ||= '3306'
25
+ password_arg = password.present? ? "-p#{password}" : ''
26
+ system "/usr/bin/env mysqldump -h #{host} -P #{port} -u #{user} #{password_arg} #{database} > #{output_file}"
27
+ when 'postgresql'
28
+ port ||= '5432'
29
+
30
+ # pg_dump doesn't take a password arg, so we have to write the password to the ~/.pgpass file.
31
+ pgpass_file = ENV['HOME']+'/.pgpass'
32
+ pgpass_content = "#{host}:#{port}:#{database}:#{user}:#{password}"
33
+ File.open(pgpass_file, 'a+') do |pgpass|
34
+ pgpass.write(pgpass_content) unless pgpass.grep(pgpass_content).present?
35
+ end
36
+
37
+ # Ensure that .pgpass has the correct mode (only really needed if we just)
38
+ # created it.
39
+ # Ruby 1.8/1.9 compatibility
40
+ file_util_class = File.respond_to?(:chmod) ? File : FileUtils
41
+ file_util_class.chmod(0600, pgpass_file)
42
+
43
+ system "/usr/bin/env pg_dump -Fc -h #{host} -p #{port} -U #{user} --no-password #{database} > #{output_file}"
44
+ else
45
+ raise RuntimeError, "I don't know how to back up #{settings['adapter']} databases!"
46
+ end
47
+ system "/usr/bin/env gzip -f #{output_file}"
48
+ end
49
+
50
+ namespace :db do
51
+ desc "Prune database backups"
52
+ task :prune => [:environment] do
53
+ files = Dir.glob("#{output_file_prefix}-*")
54
+
55
+ r_index = (-1 * num_to_keep) - 1
56
+
57
+ if files.length > num_to_keep
58
+ files[0..r_index].each do |file|
59
+ FileUtils.rm file
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ def backup_dir
66
+ relative_dir = ENV['BACKUP_DIR'] || '../../shared/backup'
67
+ File.expand_path(relative_dir, Rails.root)
68
+ end
69
+
70
+ def output_file
71
+ File.expand_path("#{output_file_prefix}-#{Time.now.strftime('%Y%m%d')}.dump", Rails.root)
72
+ end
73
+
74
+ def output_file_prefix
75
+ "#{backup_dir}/#{settings['database']}"
76
+ end
77
+
78
+ def num_to_keep
79
+ if ENV['NUM_TO_KEEP'].to_i > 0
80
+ ENV['NUM_TO_KEEP'].to_i
81
+ else
82
+ 7
83
+ end
84
+ end
85
+
86
+ def settings
87
+ @settings ||= YAML.load(File.read(Rails.root.join("config", "database.yml")))[Rails.env]
88
+ end
89
+ end
@@ -0,0 +1 @@
1
+ load 'db_backup_rails/tasks/backup_db.rake'
@@ -0,0 +1,3 @@
1
+ module DbBackupRails
2
+ VERSION = "1.0.3"
3
+ end
@@ -0,0 +1,5 @@
1
+ require "db_backup_rails/version"
2
+
3
+ module DbBackupRails
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db_backup_rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Leon Miller-Out
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.3'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Simple gem to back up your database to local storage
56
+ email:
57
+ - leon@singlebrook.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - db_backup_rails.gemspec
68
+ - lib/db_backup_rails.rb
69
+ - lib/db_backup_rails/tasks.rb
70
+ - lib/db_backup_rails/tasks/backup_db.rake
71
+ - lib/db_backup_rails/version.rb
72
+ homepage: https://github.com/singlebrook/db_backup_rails
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message: Thanks for using db_backup_rails! If you have upgraded to 1.x.x
77
+ from 0.x.x, please note that THE DEFAULT BACKUP DIR HAS CHANGED! See the README.md
78
+ for more info.
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.0.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Simple gem to back up your database to local storage
98
+ test_files: []