Skip to content

Instantly share code, notes, and snippets.

@trekdemo
Last active December 12, 2015 00:18
Show Gist options
  • Save trekdemo/4682772 to your computer and use it in GitHub Desktop.
Save trekdemo/4682772 to your computer and use it in GitHub Desktop.
DB dump/restore tools and aliases
# 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"
#!/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