Automatically backup and restore Rails dev database when switching branches
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



A simple post-checkout script that will backup your Rails dev database when you switch working branches.


Create a directory where backups will be store
  $ mkdir ~/projects/backup

Define environment variable GITBACKUP_PATH (if it's not defined, it will create and use the path ~/<your project>/../backups). 
  $ export GITBACKUP_PATH = ~/projects/backups

Copy the post-commit script into your github project's .git/hooks path and make it executable
  $ cp post-checkout ~/projects/myproject/.git/hooks
  $ chmod 755 ~/projects/myproject/.git/hooks/post-checkout

Now, when you switch branches during development, the post-checkout will run and mysqldump your database to the 
backup path in the following format:

How it works

When run you git checkout, the post-checkout script will:

- if prev and new branch are the same, do nothing.

- if either branch is 'no branch', do nothing.

- if any files in /db/migrate/* are changed (modified, added, deleted) between the previous and new branch,
  it will save database to previous branch name, and restore back the last database of the new branch name.

- if any files in /db/migrate/* are added or changed between the previous and new branch, 
  it will also run a rake db:migrate