Last active
December 12, 2015 00:18
-
-
Save trekdemo/4682772 to your computer and use it in GitHub Desktop.
DB dump/restore tools and aliases
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
# PSQL commands | |
alias psql-connect-development='psql `basename $PWD`_development' | |
alias psql-connect-test='psql `basename $PWD`_test' | |
alias psql-connect='psql-connect-development' | |
# Bundler aliases | |
alias be="bundle exec" | |
alias bi="bundle install" | |
alias bl="bundle list" | |
alias bp="bundle package" | |
alias bu="bundle update" |
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
#!/usr/bin/ruby | |
# Creates and loads backups for the current project | |
# | |
# local-backup <action> <environment> <backup file path> | |
# local-backup load development ~/Desktop/backup | |
# # => loads the given db dump file | |
# local-backup dump development ~/Desktop/backup | |
# # => creates a compressed db dump to the given path | |
require 'yaml' | |
class Backup | |
def initialize(subcommands) | |
@mode, | |
@target_environment, | |
@target_uri = subcommands | |
end | |
def start | |
case | |
when load? then self.load | |
when dump? then self.dump | |
end | |
end | |
def load | |
system %{ | |
cat '#{@target_uri}' | \ | |
pg_restore --verbose --clean --no-acl --no-owner -d #{database} | |
} | |
end | |
def dump | |
system %{ | |
pg_dump -Fc #{database} > '#{@target_uri}' | |
} | |
end | |
private | |
def database | |
YAML.load(IO.read('config/database.yml'))[@target_environment]['database'] | |
end | |
def load? | |
@mode == 'load' | |
end | |
def dump? | |
@mode == 'dump' | |
end | |
end | |
backup = Backup.new(ARGV) | |
backup.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment