mypg 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mypg.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Felipe Bazzarella
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.
@@ -0,0 +1,73 @@
1
+ # Mypg
2
+
3
+ Easy backup for Rails applications with PostgreSQL database.
4
+
5
+ ## Installation
6
+
7
+ Add this line into your application's Gemfile:
8
+
9
+ gem 'mypg'
10
+
11
+ Install the gem with Bundler:
12
+
13
+ $ bundle
14
+
15
+ ## Configuration
16
+
17
+ And then add some settings:
18
+
19
+ $ rails g mypg:install
20
+
21
+ This will create a configuration file in `config/mypg.yml` who looks like this:
22
+
23
+ # target_directory: '/path/to/backup'
24
+ # files_to_keep: 7
25
+
26
+ Just enter a valid directory to save your backup files.
27
+
28
+ **Note**: If not specified, the amount of files that will be saved is 7.
29
+
30
+ ## How To
31
+
32
+ #### Backup
33
+
34
+ Go to your Rails root directory and run this:
35
+
36
+ $ rake db:backup RAILS_ENV=production
37
+
38
+ This command will create a `.pgsql` file in the directory with your database's structure and data. Moreover, will delete the older files wich exceed the amount to be saved.
39
+
40
+ ##### Pro Tip
41
+
42
+ You can create automatic daily backups just setting up a task on [Cron](http://crontab.org/):
43
+
44
+ $ crontab -e
45
+
46
+ Insert this and save:
47
+
48
+ # m h dom mon dow command
49
+ 0 0 * * * cd ~/my/rails/root && rake db:backup RAILS_ENV=production
50
+
51
+ With that, every day in the midnight (server's time) Cron will perform your backup task automatically.
52
+
53
+ #### Restore
54
+
55
+ If you wish to restore some saved backup, go to your Rails root directory and run this:
56
+
57
+ $ rake db:restore 'relative/target/directory/to/backup/database_x.pgsql' RAILS_ENV=production
58
+
59
+ This command will restructure the production database and restore all the data from backup file.
60
+
61
+ **Note**: The path for the backup file must be relative to the current directory in the console.
62
+
63
+ ## Thanks
64
+
65
+ [Leonardo Ávila](http://facebook.com/leuavila) for contributing in the README translation and revision.
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,11 @@
1
+ module Mypg
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_install_file
7
+ template 'mypg.yml', 'config/mypg.yml'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,2 @@
1
+ # target_directory: '/path/to/backup'
2
+ # files_to_keep: 7
@@ -0,0 +1,20 @@
1
+ require 'mypg/railtie'
2
+
3
+ module Mypg
4
+ def self.target_directory
5
+ yaml['target_directory'] if yaml.present?
6
+ end
7
+
8
+ def self.files_to_keep
9
+ (yaml['files_to_keep'] if yaml.present?) || 7
10
+ end
11
+
12
+ private
13
+ def self.yaml
14
+ begin
15
+ YAML.load_file(Rails.root.join('config/mypg.yml').to_s)
16
+ rescue Errno::ENOENT
17
+ nil
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,7 @@
1
+ module Mypg
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ Dir[File.expand_path('../../tasks/*.rake', __FILE__)].each { |f| load f }
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Mypg
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,46 @@
1
+ require 'colored'
2
+
3
+ namespace :db do
4
+ desc "Dump all database's data and structure"
5
+ task backup: :environment do
6
+ unless File.directory? Mypg.target_directory.to_s
7
+ puts %{
8
+ You should define a valid target directory in 'config/mypg.yml'.
9
+ Or run 'rails g mypg:install' to create this file with some examples.
10
+ }.yellow
11
+
12
+ exit
13
+ end
14
+
15
+ puts "Backing up your #{Rails.env} database...".green.bold
16
+
17
+ config = ActiveRecord::Base.configurations[Rails.env]
18
+ file = "#{config['database']}_#{Time.now.strftime('%Y%m%d%H%M%S')}.pgsql"
19
+
20
+ system "PGPASSWORD=#{config['password']} pg_dump --clean -h localhost -U #{config['username']} #{config['database']} > #{Mypg.target_directory}/#{file}"
21
+
22
+ files = Dir["#{Mypg.target_directory}/*.pgsql"].sort_by{ |f| File.mtime(f) }.reverse
23
+
24
+ FileUtils.rm files.last(files.size - Mypg.files_to_keep) if files.size > Mypg.files_to_keep
25
+
26
+ puts "Done!".green.bold
27
+ end
28
+
29
+ desc "Restore last database backup"
30
+ task restore: :environment do
31
+ if ARGV[1].present?
32
+ puts "Restoring database backup from '#{ARGV[1]}'...".green.bold
33
+
34
+ config = ActiveRecord::Base.configurations[Rails.env]
35
+ file = Rails.root.join(ARGV[1]).to_s
36
+
37
+ print "You're in #{Rails.env.titleize}! Are you sure? ".red.bold
38
+
39
+ system "psql -h localhost -U #{config['username']} -d #{config['database']} -W < #{file}"
40
+
41
+ puts "Done!".green.bold
42
+ else
43
+ puts "Please tell me the file that you want to restore...".yellow.bold
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../lib/mypg/version', __FILE__)
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Felipe Bazzarella"]
5
+ gem.email = ["fbazzarella@gmail.com"]
6
+ gem.description = %q{Dump and restore PostgreSQL's structure and data.}
7
+ gem.summary = %q{Dump and restore PostgreSQL's structure and data.}
8
+
9
+ gem.files = `git ls-files`.split($/)
10
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
11
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
12
+ gem.name = "mypg"
13
+ gem.require_paths = ["lib"]
14
+ gem.version = Mypg::VERSION
15
+
16
+ gem.add_runtime_dependency('colored', '~> 1.2')
17
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mypg
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Felipe Bazzarella
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ prerelease: false
16
+ type: :runtime
17
+ name: colored
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: '1.2'
24
+ requirement: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ description: Dump and restore PostgreSQL's structure and data.
31
+ email:
32
+ - fbazzarella@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - .gitignore
38
+ - Gemfile
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/generators/mypg/install_generator.rb
43
+ - lib/generators/mypg/templates/mypg.yml
44
+ - lib/mypg.rb
45
+ - lib/mypg/railtie.rb
46
+ - lib/mypg/version.rb
47
+ - lib/tasks/backup.rake
48
+ - mypg.gemspec
49
+ homepage:
50
+ licenses: []
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ required_rubygems_version: !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 1.8.25
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Dump and restore PostgreSQL's structure and data.
73
+ test_files: []